:root {
  color-scheme: light dark;
  --bg: #eef1ed;
  --surface: #ffffff;
  --surface-2: #e7ece7;
  --ink: #26231f;
  --muted: #65716b;
  --line: #d4ddd7;
  --line-strong: #b7c6bd;
  --accent: #1f7a68;
  --accent-2: #d6523c;
  --gold: #c9972c;
  --blue: #406fa7;
  --green-soft: #dceee7;
  --red-soft: #f5ded8;

  /* Surface tokens */
  --surface-sheet: rgba(255, 255, 255, 0.96);
  --surface-topbar: rgba(255, 255, 255, 0.9);
  --surface-card: rgba(255, 255, 255, 0.58);
  --surface-progress: rgba(255, 255, 255, 0.72);
  --surface-track: #ffffff;
  --surface-control: #fbfdfb;
  --surface-table: #ffffff;
  --surface-head: #edf3ef;
  --surface-bank: #fbfdfb;
  --surface-bank-selected: #edf7f3;
  --surface-row-focus: #f4faf7;
  --surface-btn: #f8fbf9;
  --surface-action: #ffffff;
  --surface-footer: #edf3ef;
  --border-sheet: rgba(183, 198, 189, 0.86);
  --shadow-sheet: 0 24px 70px rgba(36, 50, 43, 0.12);

  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    linear-gradient(120deg, rgba(31, 122, 104, 0.12), transparent 34%),
    linear-gradient(210deg, rgba(64, 111, 167, 0.12), transparent 38%),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  padding: clamp(14px, 3vw, 34px);
}

.sheet-surface {
  width: min(1360px, 100%);
  margin: 0 auto;
  min-height: calc(100vh - clamp(28px, 6vw, 68px));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface-sheet);
  border: 1px solid var(--border-sheet);
  box-shadow: var(--shadow-sheet);
}

.topbar,
.summary-grid,
.toolbar,
.footer-strip {
  padding-inline: clamp(14px, 2.6vw, 28px);
}

.topbar {
  min-height: 84px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-topbar);
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 13px;
  min-width: 0;
}

.brand-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  color: white;
  background: var(--ink);
  font-weight: 850;
}

.eyebrow,
.metric span,
.money-input span,
.footer-strip span,
.progress-labels,
.table-head {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 720;
  text-transform: uppercase;
  letter-spacing: 0;
}

h1 {
  margin: 1px 0 0;
  font-size: clamp(1.6rem, 2.5vw, 2.35rem);
  line-height: 1;
  letter-spacing: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  color: var(--ink);
  background: var(--surface-btn);
}

.icon-button:hover,
.primary-button:hover,
.secondary-button:hover,
.row-action:hover {
  border-color: var(--ink);
}

.icon-button.danger {
  color: var(--accent-2);
}

/* ── Summary ─────────────────────────── */
.summary-grid {
  display: grid;
  grid-template-columns: minmax(220px, 1.35fr) repeat(3, minmax(150px, 1fr));
  gap: 10px;
  padding-block: 16px;
  border-bottom: 1px solid var(--line);
}

.summary-grid-compact {
  grid-template-columns: minmax(210px, 1.2fr) repeat(4, minmax(136px, 1fr));
}

.money-input,
.metric {
  min-height: 84px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-card);
}

.amount-entry {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.money-input input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  color: var(--ink);
  background: transparent;
  font-size: clamp(1.7rem, 3.6vw, 3.2rem);
  font-weight: 820;
  line-height: 1;
}

.amount-entry strong {
  flex: 0 0 auto;
  color: var(--ink);
  font-size: clamp(1.45rem, 2.7vw, 2.45rem);
  font-weight: 820;
  line-height: 1;
}

.metric strong {
  overflow-wrap: anywhere;
  font-size: clamp(1.18rem, 1.75vw, 1.75rem);
  line-height: 1.05;
}

.metric small {
  color: var(--muted);
  font-weight: 780;
}

.metric.positive strong {
  color: var(--accent);
}

.metric.negative strong {
  color: var(--accent-2);
}

/* ── Progress ────────────────────────── */
.progress-wrap {
  padding: 14px clamp(14px, 2.6vw, 28px) 8px;
  background: var(--surface-progress);
}

.progress-track {
  height: 12px;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface-track);
}

.progress-bar {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--gold));
  transition: width 160ms ease, background-color 160ms ease;
}

.progress-bar.over {
  background: var(--accent-2);
}

.progress-labels {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-top: 7px;
}

/* ── Remaining badge ─────────────────── */
#statusText {
  text-transform: none;
  padding: 3px 10px;
  border-radius: 99px;
  font-weight: 780;
  transition: background 200ms, color 200ms;
}

#statusText[data-state="unassigned"] {
  background: var(--gold);
  color: #ffffff;
}

#statusText[data-state="over"] {
  background: var(--accent-2);
  color: #ffffff;
}

#statusText[data-state="balanced"] {
  color: var(--accent);
}

/* ── Toolbar ─────────────────────────── */
.toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 12px;
  padding-block: 12px;
  border-bottom: 1px solid var(--line);
}

.target-bank-control {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-control);
}

.target-bank-control span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 780;
  text-transform: uppercase;
}

.target-bank-control select {
  min-width: 170px;
  height: 32px;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--ink);
  background: transparent;
  font-weight: 820;
}

.target-bank-control select:focus,
.target-bank-control select:hover {
  border-color: var(--line-strong);
  background: var(--surface-action);
  outline: 0;
}

.primary-button,
.secondary-button {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 15px;
  border-radius: 8px;
  font-weight: 820;
}

.primary-button {
  border: 1px solid #165c4d;
  color: white;
  background: var(--accent);
}

.secondary-button {
  border: 1px solid var(--line-strong);
  color: var(--ink);
  background: var(--surface-btn);
}

/* ── Table ───────────────────────────── */
.table-shell {
  flex: 1;
  min-height: 360px;
  overflow: auto;
  background: var(--surface-table);
}

.budget-table {
  min-width: 760px;
}

.table-row {
  display: grid;
  grid-template-columns: minmax(300px, 1.8fr) minmax(130px, 0.62fr) minmax(110px, 0.5fr) minmax(210px, 0.92fr) 140px;
  min-height: 44px;
  border-bottom: 1px solid var(--line);
}

.table-row > div {
  min-width: 0;
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border-right: 1px solid var(--line);
}

.table-row > div:last-child {
  border-right: 0;
}

.table-head {
  position: sticky;
  top: 0;
  z-index: 2;
  min-height: 42px;
  background: var(--surface-head);
}

.table-row:not(.table-head):focus-within {
  background: var(--surface-row-focus);
}

.bank-row {
  min-height: 52px;
  background: var(--surface-bank);
  border-bottom-color: var(--line-strong);
}

.bank-row.selected {
  background: var(--surface-bank-selected);
  box-shadow: inset 4px 0 0 var(--accent);
}

.bank-row > div {
  font-weight: 850;
}

.item-row > div:first-child {
  padding-left: 28px;
  background: linear-gradient(90deg, transparent 0, transparent 14px, var(--line) 14px, var(--line) 15px, transparent 15px);
}

.cell-input,
.cell-select {
  width: 100%;
  min-width: 0;
  height: 38px;
  border: 1px solid transparent;
  border-radius: 6px;
  outline: 0;
  color: var(--ink);
  background: transparent;
}

.cell-input {
  padding: 0 8px;
}

.cell-select {
  padding: 0 6px;
}

.bank-name {
  font-weight: 850;
}

.bank-row > div:nth-child(4) {
  gap: 8px;
}

.money-cell,
.percent-cell,
.amount,
.percent {
  text-align: right;
}

.concept-input {
  font-weight: 620;
}

.bank-total-label {
  color: var(--muted);
  font-weight: 850;
  text-transform: uppercase;
}

.bank-blank {
  display: block;
  width: 100%;
  min-height: 1px;
}

.compact-select {
  max-width: 112px;
  color: var(--muted);
  background: var(--surface-action);
}

.tag-pill {
  width: auto;
  min-width: 122px;
  max-width: 148px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding-inline: 10px;
  border: 1px solid currentColor;
  border-radius: 999px;
  font-size: 0.84rem;
  font-weight: 820;
  background: transparent;
}

.tag-pill.variable {
  color: var(--accent);
  background: rgba(31, 122, 104, 0.11);
}

.tag-pill.fixed {
  color: var(--accent-2);
  background: rgba(214, 82, 60, 0.11);
}

.cell-input:hover,
.cell-input:focus,
.cell-select:hover,
.cell-select:focus {
  border-color: var(--line-strong);
  background: var(--surface-action);
}

.computed {
  width: 100%;
  overflow-wrap: anywhere;
  font-weight: 830;
}

.computed.muted {
  color: var(--muted);
  font-weight: 760;
}

.computed.positive {
  color: var(--accent);
}

.computed.negative {
  color: var(--accent-2);
}

.row-action {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--accent-2);
  background: var(--surface-action);
}

.row-action.add {
  color: var(--accent);
}

.bank-row > div:last-child {
  justify-content: center;
  gap: 4px;
  padding-inline: 5px;
}

.bank-row .row-action {
  width: 28px;
  height: 28px;
}

.row-action.move {
  color: var(--muted);
}

.row-action.move:disabled {
  opacity: 0.25;
  cursor: default;
  pointer-events: none;
}

/* ── Footer ──────────────────────────── */
.footer-strip {
  min-height: 54px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  border-top: 1px solid var(--line);
  background: var(--surface-footer);
}

.footer-strip > div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.footer-sync {
  min-width: 0;
}

.footer-sync span {
  white-space: nowrap;
}

#syncStatus {
  color: var(--muted);
  transition: color 180ms ease;
}

#syncStatus[data-state="idle"] {
  color: var(--muted);
}

#syncStatus[data-state="ok"] {
  color: var(--accent);
}

#syncStatus[data-state="syncing"] {
  color: var(--gold);
}

#syncStatus[data-state="error"] {
  color: var(--accent-2);
}

/* ── Dark mode ───────────────────────── */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101312;
    --surface: #171c1a;
    --surface-2: #202724;
    --ink: #e3ebe6;
    --muted: #92a199;
    --line: rgba(139, 157, 148, 0.17);
    --line-strong: rgba(159, 181, 170, 0.3);
    --accent: #57b89f;
    --accent-2: #e0705f;
    --gold: #d2a34a;
    --blue: #7fa4d3;
    --green-soft: #13231e;
    --red-soft: #2c1714;
    --surface-sheet: rgba(21, 26, 24, 0.985);
    --surface-topbar: rgba(18, 23, 21, 0.96);
    --surface-card: rgba(25, 31, 28, 0.82);
    --surface-progress: rgba(18, 23, 21, 0.9);
    --surface-track: #0d1110;
    --surface-control: #171e1b;
    --surface-table: #141917;
    --surface-head: #111614;
    --surface-bank: #18201c;
    --surface-bank-selected: #152821;
    --surface-row-focus: #1b2823;
    --surface-btn: #1b221f;
    --surface-action: #19211d;
    --surface-footer: #111614;
    --border-sheet: rgba(103, 126, 116, 0.42);
    --shadow-sheet: 0 24px 70px rgba(0, 0, 0, 0.48);
  }

  body {
    background:
      linear-gradient(120deg, rgba(87, 184, 159, 0.07), transparent 32%),
      linear-gradient(210deg, rgba(127, 164, 211, 0.05), transparent 38%),
      var(--bg);
  }

  .brand-mark {
    color: var(--bg);
    background: #dbe6df;
  }

  .money-input,
  .metric,
  .target-bank-control,
  .icon-button,
  .secondary-button,
  .row-action {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.025);
  }

  .table-head {
    box-shadow: 0 1px 0 var(--line-strong);
  }

  .bank-row.selected {
    box-shadow:
      inset 4px 0 0 var(--accent),
      inset 0 1px 0 rgba(255, 255, 255, 0.025);
  }

  .cell-input:hover,
  .cell-input:focus,
  .cell-select:hover,
  .cell-select:focus,
  .target-bank-control select:focus,
  .target-bank-control select:hover {
    border-color: rgba(160, 186, 174, 0.42);
    box-shadow: 0 0 0 3px rgba(87, 184, 159, 0.08);
  }

  #statusText[data-state="unassigned"] {
    color: #241b08;
    background: var(--gold);
  }

  #statusText[data-state="over"] {
    color: #fff3ef;
    background: #b84d3f;
  }

  #syncStatus[data-state="ok"],
  .metric.positive strong,
  .computed.positive {
    color: #63c2aa;
  }

  .tag-pill.variable {
    color: #63c2aa;
    background: rgba(87, 184, 159, 0.13);
  }

  .tag-pill.fixed {
    color: #f08a78;
    background: rgba(224, 112, 95, 0.13);
  }

}

/* ── Responsive ──────────────────────── */
@media (max-width: 900px) {
  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 680px) {
  .app-shell {
    padding: 0;
  }

  .sheet-surface {
    min-height: 100vh;
    border: 0;
  }

  .topbar {
    min-height: auto;
    align-items: flex-start;
    flex-direction: column;
    padding-block: 14px;
  }

  .topbar-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
  }

  .icon-button {
    width: 100%;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .footer-strip {
    align-items: flex-start;
    flex-direction: column;
    padding-block: 12px;
  }

  .footer-strip > div {
    width: 100%;
    justify-content: space-between;
  }

  .footer-sync {
    align-items: flex-start;
    flex-direction: column;
  }

  .footer-sync span {
    white-space: normal;
  }

  .target-bank-control {
    width: 100%;
    justify-content: space-between;
  }

  .target-bank-control select {
    min-width: 0;
    flex: 1;
  }

  .primary-button,
  .secondary-button {
    width: 100%;
  }

}
