:root {
  --bg: #f5f5f7;
  --card: #fff;
  --border: #d8d8dd;
  --text: #1d1d1f;
  --muted: #6e6e73;
  /* Accent matches Lo Destro logo navy so the palette feels cohesive. */
  --accent: #1f3a5f;
  --accent-text: #fff;
  --error: #c0392b;
  --warn-bg: #fff8e1;
  --warn-border: #f0c14b;
  --warn-text: #6a4f00;
  --input-bg: #fff;
  --row-hover: #eef5ff;
  --shadow-card: 0 1px 4px rgba(0, 0, 0, .06);
  --shadow-dropdown: 0 6px 18px rgba(0, 0, 0, .08);
}

:root[data-theme="dark"] {
  --bg: #14181f;
  --card: #1e242d;
  --border: #2d3540;
  --text: #e6e8eb;
  --muted: #9aa3ae;
  /* Lighter navy so the accent stays visible against the dark card */
  --accent: #6da3d8;
  --accent-text: #0e1116;
  --error: #ff7b6b;
  --warn-bg: #3a2c08;
  --warn-border: #8a6818;
  --warn-text: #f4cf72;
  --input-bg: #161a21;
  --row-hover: #232c38;
  --shadow-card: 0 1px 6px rgba(0, 0, 0, .35);
  --shadow-dropdown: 0 6px 18px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }
/* The HTML `hidden` attribute must always win over our display rules
   (e.g. .centered uses display:grid which would otherwise shadow it). */
[hidden] { display: none !important; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
}
h1 { font-size: 1.4rem; margin: 0; font-weight: 600; }
button {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  padding: .55rem 1rem;
  border-radius: 6px;
  font-size: .95rem;
  cursor: pointer;
}
button:hover { filter: brightness(1.08); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

input, textarea {
  width: 100%;
  padding: .45rem .6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: var(--input-bg);
  color: var(--text);
}
/* Native date pickers don't always invert the calendar icon in dark mode */
:root[data-theme="dark"] input[type="date"]::-webkit-calendar-picker-indicator,
:root[data-theme="dark"] input[type="time"]::-webkit-calendar-picker-indicator,
:root[data-theme="dark"] input[type="number"] {
  filter: invert(0.85);
}
input:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
input:invalid:not(:placeholder-shown) {
  border-color: var(--error);
}
label {
  display: block;
  font-size: .8rem;
  color: var(--muted);
  margin: 0 0 .2rem;
  font-weight: 500;
}
label.inline {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  margin-left: .8rem;
  font-size: .8rem;
}

/* ── Login ─────────────────────────────────────────────────────────── */
.centered {
  min-height: 100vh;
  display: grid;
  place-items: center;
}
.card {
  background: var(--card);
  padding: 2rem;
  border-radius: 12px;
  box-shadow: var(--shadow-card);
  width: min(360px, 92vw);
  display: flex;
  flex-direction: column;
  gap: .85rem;
}
.card h1 { text-align: center; margin-bottom: .4rem; font-weight: 500; color: var(--accent); }
.login-logo {
  display: block;
  width: 80%;
  max-width: 280px;
  margin: 0 auto .5rem;
  height: auto;
}
.error {
  color: var(--error);
  font-size: .85rem;
  min-height: 1.1em;
}

/* ── App shell ─────────────────────────────────────────────────────── */
header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: .8rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand {
  display: flex;
  align-items: center;
  gap: .9rem;
}
.header-logo {
  height: 32px;
  width: auto;
}
.header-actions { display: flex; gap: .5rem; }
.header-actions button { background: transparent; color: var(--accent); border: 1px solid var(--border); }

main {
  max-width: 1000px;
  margin: 1.5rem auto;
  padding: 0 1.5rem 4rem;
}

fieldset {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem 1.25rem;
  margin: 0 0 1rem;
}
legend {
  font-weight: 600;
  padding: 0 .5rem;
  color: var(--text);
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .85rem;
  margin-top: .5rem;
}
.grid .full { grid-column: 1 / -1; }
@media (max-width: 700px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

.muted { color: var(--muted); font-size: .85rem; margin: .25rem 0; }
.hint {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  padding: .5rem .75rem;
  border-radius: 6px;
  font-size: .85rem;
  margin: .75rem 0 0;
}

/* ── Typeahead ─────────────────────────────────────────────────────── */
.typeahead {
  position: relative;
  margin-bottom: 1rem;
}
.ta-results {
  list-style: none;
  margin: .25rem 0 0;
  padding: 0;
  position: absolute;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 100%;
  max-height: 280px;
  overflow-y: auto;
  z-index: 5;
  box-shadow: var(--shadow-dropdown);
  display: none;
}
.ta-results.open { display: block; }
.ta-results li {
  padding: .55rem .75rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .5rem;
}
.ta-results li:last-child { border-bottom: 0; }
.ta-results li:hover, .ta-results li.active { background: var(--row-hover); }
.ta-results .name { flex: 1; }
.ta-results .meta { color: var(--muted); font-size: .8rem; }
.ta-results .badge {
  font-size: .7rem;
  padding: .1rem .45rem;
  border-radius: 4px;
  background: var(--warn-bg);
  color: var(--warn-text);
  border: 1px solid var(--warn-border);
  white-space: nowrap;
}
.ta-clear {
  background: transparent;
  color: var(--accent);
  border: 0;
  padding: .25rem 0;
  font-size: .8rem;
  cursor: pointer;
  margin-top: .25rem;
}

/* ── Addenda table ─────────────────────────────────────────────────── */
#addenda-table {
  width: 100%;
  border-collapse: collapse;
  margin: .75rem 0;
}
#addenda-table th, #addenda-table td {
  text-align: left;
  padding: .35rem;
  border-bottom: 1px solid var(--border);
}
#addenda-table th { font-size: .75rem; color: var(--muted); font-weight: 500; }
#addenda-table input { padding: .35rem .5rem; }
#addenda-table .remove-btn {
  background: transparent;
  color: var(--error);
  padding: .25rem .5rem;
  border: 1px solid var(--border);
}

/* ── Submit ────────────────────────────────────────────────────────── */
.form-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 1.5rem 0;
}
#submit-status { font-size: .9rem; }
#submit-status.ok { color: #1f7a3a; }
#submit-status.err { color: var(--error); }
