/* ===== Tokens ===== */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #16191d;
  --text-muted: #666e7a;
  --accent: #1f5f4b;
  --accent-text: #ffffff;
  --accent-soft: #e8f2ee;
  --danger: #a32b28;
  --danger-soft: #fbecec;
  --positive: #1f6b3f;
  --caution: #8a5800;
  --negative: #a32b28;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 20, 26, .06), 0 1px 8px rgba(16, 20, 26, .04);
  --font: ui-sans-serif, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --surface: #1c2025;
    --border: #2c3138;
    --border-strong: #3a4048;
    --text: #e9ecef;
    --text-muted: #9aa3ae;
    --accent: #4aa588;
    --accent-text: #07100d;
    --accent-soft: #1b2b26;
    --danger: #e07a76;
    --danger-soft: #2c1e1e;
    --positive: #63c187;
    --caution: #d9a441;
    --negative: #e07a76;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

/* Author `display` rules (.login-wrap is a grid, .field is a flex column) outrank
   the UA stylesheet's [hidden] { display: none }, so toggling el.hidden silently
   does nothing without this. Every view toggle in app.js depends on it. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.25; margin: 0; font-weight: 640; letter-spacing: -.01em; }
h2 { font-size: 1.4rem; }
h3 { font-size: 1rem; }

.muted { color: var(--text-muted); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ===== Cards & fields ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
fieldset.card { margin: 0; }
legend {
  font-weight: 640;
  font-size: .95rem;
  padding: 0 6px;
  margin-left: -6px;
  display: flex;
  align-items: baseline;
  gap: 10px;
}
.legend-total {
  font-family: var(--mono);
  font-size: .85rem;
  font-weight: 500;
  color: var(--text-muted);
}

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; flex: 1 1 180px; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field > span { font-size: .82rem; font-weight: 550; color: var(--text-muted); display: flex; gap: 6px; align-items: baseline; }
.field-narrow { flex: 0 1 150px; }
.req, .hint { font-style: normal; font-weight: 450; font-size: .76rem; opacity: .75; }

input[type="text"], input[type="email"], input[type="password"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 9px 11px;
  width: 100%;
  min-width: 0;
}
textarea { resize: vertical; font-family: inherit; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Numeric inputs read better right-aligned in monospace */
input[inputmode="decimal"] { font-family: var(--mono); text-align: right; }

.input-affix { display: flex; align-items: stretch; }
.input-affix i {
  font-style: normal;
  display: flex;
  align-items: center;
  padding: 0 9px;
  font-size: .85rem;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-right: none;
  border-radius: 8px 0 0 8px;
}
.input-affix input { border-radius: 0 8px 8px 0; }
.input-affix.affix-end i { border: 1px solid var(--border-strong); border-left: none; border-radius: 0 8px 8px 0; }
.input-affix.affix-end input { border-radius: 8px 0 0 8px; }

/* Fields in a row share label and control tracks via subgrid, so a label that
   wraps to two lines (a long name, or one carrying a hint) doesn't sit its
   input lower than the field beside it. auto-fit keeps rows of two, three and
   six fields all laying out sensibly. */
.row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  column-gap: 14px;
  row-gap: 4px;
}
.row .field {
  display: grid;
  grid-template-rows: subgrid;
  grid-row: span 2;
  gap: 0;
  margin-bottom: 14px;
  min-width: 0;
}
.field > span { flex-wrap: wrap; align-content: start; }

/* Without subgrid, reserve two label lines so pairs still line up. */
@supports not (grid-template-rows: subgrid) {
  .row .field > span { min-height: 2.9em; }
}

/* ===== Buttons ===== */
.btn {
  font: inherit;
  font-weight: 550;
  border-radius: 8px;
  padding: 9px 15px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--text-muted); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--text-muted); padding: 6px 10px; }
.btn-quiet:hover { color: var(--text); background: var(--bg); }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .55; cursor: progress; }

.linkbtn {
  font: inherit; color: var(--accent); background: none; border: none;
  padding: 0; cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.linkbtn.back { font-size: .85rem; margin-bottom: 4px; text-decoration: none; }
.linkbtn.back:hover { text-decoration: underline; }

/* ===== Messages ===== */
.error, .success, .note, .empty {
  font-size: .87rem;
  border-radius: 8px;
  padding: 9px 12px;
  margin: 0 0 14px;
}
.error { color: var(--danger); background: var(--danger-soft); }
.success { color: var(--positive); background: var(--accent-soft); }
.note, .empty { color: var(--text-muted); background: var(--surface); border: 1px solid var(--border); }
.note-inline { margin: 2px 0 0; font-size: .8rem; }
.empty { text-align: center; padding: 34px; }
.meta { font-size: .78rem; color: var(--text-muted); margin: 0; }

/* ===== Login ===== */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 22px; }
.login-card { width: 100%; max-width: 370px; padding: 26px; }
.login-title { font-size: 1.25rem; }
.login-sub { color: var(--text-muted); font-size: .88rem; margin: 5px 0 20px; }
.turnstile { margin-bottom: 14px; min-height: 65px; }

/* ===== Topbar ===== */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner {
  max-width: 1180px; margin: 0 auto; padding: 10px 20px;
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
}
.brand {
  font: inherit; font-weight: 640; color: var(--text);
  background: none; border: none; cursor: pointer; padding: 0;
  display: flex; align-items: center; gap: 8px;
}
.topnav { display: flex; gap: 2px; margin-right: auto; }
.navlink {
  font: inherit; font-size: .9rem; font-weight: 550;
  color: var(--text-muted); background: none; border: none;
  padding: 6px 11px; border-radius: 7px; cursor: pointer;
}
.navlink:hover { color: var(--text); background: var(--bg); }
.navlink[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: .87rem; }

.main { max-width: 1180px; margin: 0 auto; padding: 22px 20px 60px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head-actions { display: flex; gap: 8px; }

/* ===== Tables ===== */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); }
thead th { font-size: .76rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); font-weight: 600; white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg); }
.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
thead .num { font-family: var(--font); }
.deal-name { font-weight: 600; }
.deal-addr { color: var(--text-muted); font-size: .82rem; }
/* Red / amber / green rating. Thresholds live in calc.js; see RATING_THRESHOLDS. */
[data-rating="good"] { color: var(--positive); }
[data-rating="fair"] { color: var(--caution); }
[data-rating="poor"] { color: var(--negative); }

/* In the deals list the rating reads better as a tinted chip than as colored
   text, because the captions sit right above the values. */
.deals-table td[data-rating] { font-weight: 600; }
.rowbtn { white-space: nowrap; width: 1%; }

/* ===== Deal form + results ===== */
.deal-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.deal-inputs { display: flex; flex-direction: column; gap: 16px; }
.results { position: sticky; top: 74px; }
.results-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.results-card h3 { margin-bottom: 2px; }
.results-toggle {
  display: none;
  font: inherit; font-size: .78rem; font-weight: 600;
  color: var(--accent); background: var(--accent-soft);
  border: none; border-radius: 99px; padding: 4px 12px; cursor: pointer;
}
.live-hint { font-size: .78rem; color: var(--text-muted); margin: 0 0 14px; }

.results-list { margin: 0; display: flex; flex-direction: column; gap: 1px; }
.result { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 6px 0; }
.result dt { font-size: .85rem; color: var(--text-muted); }
.result dt em { font-style: normal; font-size: .75rem; opacity: .7; }
.result dd { margin: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: .92rem; }
.result.sub { padding: 2px 0 6px; }
.result.sub dt, .result.sub dd { font-size: .78rem; color: var(--text-muted); }
.result.headline {
  border-top: 1px solid var(--border);
  margin-top: 5px; padding-top: 10px;
}
.result.headline dt { color: var(--text); font-weight: 600; font-size: .88rem; }
.result.headline dd { font-size: 1.12rem; font-weight: 640; }

/* ===== Settings ===== */
.settings-layout { display: flex; flex-direction: column; gap: 16px; max-width: 620px; }
.field-help { font-size: .84rem; color: var(--text-muted); margin: 2px 0 12px; }
.settings-actions { display: flex; gap: 8px; justify-content: flex-end; }

@media (max-width: 620px) {
  .settings-actions .btn { flex: 1; }
}

/* ===== Users ===== */
.users-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.users-layout .table-wrap { border: none; }
.users-layout .card > h3 { margin-bottom: 14px; }

/* ===== Glossary ===== */

/* Tap target sits inline with the label text it explains. */
.term-info {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 16px;
  height: 16px;
  margin-left: 5px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-family: var(--font);
  font-size: 10px;
  font-weight: 700;
  font-style: normal;
  line-height: 1;
  cursor: pointer;
  vertical-align: middle;
}
.term-info:hover { background: var(--accent); color: var(--accent-text); }

/* The results panel packs a lot in, so its markers are quieter. */
.result dt .term-info { background: transparent; color: var(--text-muted); }
.result dt .term-info:hover { background: var(--accent-soft); color: var(--accent); }

.term-dialog {
  width: min(560px, calc(100vw - 28px));
  max-height: min(80vh, 760px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 48px rgba(16, 20, 26, .28);
  overflow: hidden;
}
.term-dialog::backdrop { background: rgba(16, 20, 26, .5); }

.term-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}
.term-dialog-head h2 { font-size: 1.05rem; }
.term-close {
  flex: none;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}
.term-close:hover { background: var(--bg); color: var(--text); }

.term-body {
  padding: 4px 18px 18px;
  overflow-y: auto;
  max-height: calc(min(80vh, 760px) - 58px);
}

.term-entry + .term-entry {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.term-entry-title { font-size: .98rem; margin-top: 14px; }

.term-section { margin-top: 12px; }
.term-section h3 {
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  margin-bottom: 3px;
}
.term-section p { margin: 0; font-size: .9rem; }

.term-worked {
  font-family: var(--mono);
  font-size: .85rem !important;
  white-space: pre-wrap;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
}
.term-empty { color: var(--text-muted); font-style: italic; }

.term-bands { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
.term-band {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: .85rem;
  padding: 4px 8px;
  border-radius: 7px;
  border: 1px solid transparent;
}
.term-band .term-band-dot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}
.term-band[data-rating] { color: var(--text-muted); }
.term-band[data-rating="good"] .term-band-dot { color: var(--positive); }
.term-band[data-rating="fair"] .term-band-dot { color: var(--caution); }
.term-band[data-rating="poor"] .term-band-dot { color: var(--negative); }
.term-band.is-current {
  background: var(--bg);
  border-color: var(--border-strong);
  color: var(--text);
  font-weight: 600;
}
.term-band-you { margin-left: auto; font-size: .74rem; font-weight: 600; }
.term-caveat { font-size: .82rem !important; color: var(--text-muted); margin-top: 8px !important; }
.term-watch h3 { color: var(--accent); }

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 99px; font-size: .87rem; font-weight: 550;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .22); z-index: 50;
}

/* ===== Mobile ===== */
@media (max-width: 860px) {
  .deal-layout, .users-layout { grid-template-columns: minmax(0, 1fr); }
  .results { position: static; order: -1; }
}

@media (max-width: 620px) {
  /* Two fields per row instead of one. Money inputs are narrow, so stacking
     every one of them was what made the form five screens tall. */
  /* Exactly two per row on a phone, rather than whatever auto-fit decides. */
  .row { grid-template-columns: 1fr 1fr; column-gap: 10px; }
  .row .field { margin-bottom: 8px; }

  /* Half-width columns are too narrow to carry a hint beside the label, so
     drop it underneath. Desktop keeps it inline — subgrid aligns either way.
     order:1 sends the hint past the info button, which would otherwise be
     pushed onto a third line of its own. */
  .field > span .hint, .field > span .req { flex-basis: 100%; order: 1; }

  /* Fit Deals / Users / Settings / Glossary plus sign-out on a single row:
     drop who you are signed in as, and tighten the links themselves. */
  #current-user { display: none; }
  .topbar-inner { gap: 6px; flex-wrap: nowrap; }
  .topnav { gap: 0; }
  .navlink { padding: 5px 7px; font-size: .82rem; }
  .btn-quiet { padding: 5px 6px; font-size: .82rem; }
  .topbar-user { gap: 4px; }

  /* Tighten every axis of spacing. */
  .card { padding: 12px; }
  .deal-layout { gap: 12px; }
  .deal-inputs { gap: 10px; }
  .field { margin-bottom: 10px; gap: 3px; }
  .field > span { font-size: .76rem; }
  input[type="text"], input[type="email"], input[type="password"], select, textarea { padding: 8px 10px; }
  .input-affix i { padding: 0 7px; }
  legend { font-size: .88rem; }
  h2 { font-size: 1.2rem; }
  .page-head { align-items: stretch; margin-bottom: 12px; }
  .page-head-actions { width: 100%; }
  .page-head-actions .btn { flex: 1; }
  .topbar-inner { padding: 9px 14px; gap: 10px; }
  .brand span { display: none; }
  .main { padding: 14px 14px 0; }
  th, td { padding: 9px 11px; }

  /* A seven-column table can't fit a phone, and sideways scrolling hid the
     very numbers the list exists to show. Each deal becomes a card instead. */
  .deals-table, .deals-table tbody, .deals-table tr, .deals-table td { display: block; }
  .deals-table thead { display: none; }
  #deals-table-wrap { overflow-x: visible; border: none; background: none; }
  .deals-table tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 9px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 13px;
    margin-bottom: 10px;
  }
  .deals-table tbody tr:hover { background: var(--surface); }
  .deals-table td { border: none; padding: 0; }
  .deals-table td:first-child { grid-column: 1 / -1; }
  .deals-table td.num { text-align: left; }
  .deals-table td.num::before {
    content: attr(data-label);
    display: block;
    font-family: var(--font);
    font-size: .66rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
    margin-bottom: 1px;
  }
  .deals-table td.rowbtn { grid-column: 1 / -1; width: auto; }
  .deals-table td.rowbtn .btn {
    width: 100%;
    border: 1px solid var(--border-strong);
    color: var(--text);
  }

  .term-dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 88vh;
    margin: 0 0 auto;
    position: fixed;
    bottom: 0;
    border-radius: 14px 14px 0 0;
    border-bottom: none;
  }
  .term-body { max-height: calc(88vh - 58px); padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px)); }
  .term-info { width: 18px; height: 18px; font-size: 11px; }

  /* Email addresses are longer than the handles this table used to hold, so
     let them wrap instead of shoving the Added column off screen. */
  .users-table { font-size: .84rem; }
  .users-table th, .users-table td { padding: 8px 8px; }
  .users-table td:nth-child(2) { overflow-wrap: anywhere; }

  /* Results ride along at the bottom of the screen rather than sitting above
     the form, so the live numbers stay visible while you type instead of
     being a screen and a half of scrolling away. */
  .results {
    order: 0;
    position: sticky;
    bottom: 0;
    z-index: 20;
    margin: 2px -14px 0;
  }
  .results-card {
    border-radius: 14px 14px 0 0;
    border-bottom: none;
    padding: 9px 14px calc(9px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -3px 16px rgba(16, 20, 26, .12);
  }
  .results-card h3 { font-size: .88rem; }
  .results-toggle { display: block; }
  .live-hint { display: none; }

  /* Collapsed: just the four numbers that matter, two across. */
  .results-card:not(.expanded) .results-list {
    display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; margin-top: 4px;
  }
  .results-card:not(.expanded) .result:not(.headline) { display: none; }
  .results-card:not(.expanded) .result.headline {
    flex-direction: column; align-items: flex-start; gap: 0;
    border-top: none; margin-top: 0; padding: 2px 0;
  }
  .results-card:not(.expanded) .result.headline dt { font-size: .68rem; font-weight: 550; }
  .results-card:not(.expanded) .result.headline dt em { display: none; }
  .results-card:not(.expanded) .result.headline dd { font-size: 1rem; }

  /* Expanded: the full waterfall, capped so it can't swallow the screen. */
  .results-card.expanded .results-list {
    max-height: 52vh; overflow-y: auto; margin-top: 4px;
  }
}
