:root {
  color-scheme: light dark;
  --bg: #eef1ed;
  --surface: #ffffff;
  --ink: #26231f;
  --muted: #65716b;
  --line: #d4ddd7;
  --line-strong: #b7c6bd;
  --accent: #1f7a68;
  --accent-2: #d6523c;
  --gold: #c9972c;
  --blue: #406fa7;
  --essentials: #1f7a68;
  --car: #406fa7;
  --leisure: #8a5a9e;
  --unexpected: #c77a35;
  --savings: #c9972c;
  --investment: #5868b2;
  --surface-sheet: rgba(255, 255, 255, 0.96);
  --surface-topbar: rgba(255, 255, 255, 0.9);
  --surface-card: rgba(255, 255, 255, 0.62);
  --surface-control: #fbfdfb;
  --surface-table: #ffffff;
  --surface-head: #edf3ef;
  --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,
.table-head,
.rule-grid span {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 720;
  text-transform: uppercase;
  letter-spacing: 0;
}

h1,
h2 {
  margin: 1px 0 0;
  line-height: 1;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(1.45rem, 2.5vw, 2.35rem);
}

h2 {
  font-size: clamp(1.15rem, 1.6vw, 1.5rem);
}

.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-grid {
  display: grid;
  grid-template-columns: minmax(210px, 1.25fr) repeat(4, minmax(145px, 1fr));
  gap: 10px;
  padding-block: 16px;
  border-bottom: 1px solid var(--line);
}

.summary-grid.compact-summary {
  grid-template-columns: minmax(220px, 1fr) minmax(170px, 0.7fr) minmax(170px, 0.7fr);
  align-items: stretch;
  padding-block: 12px;
}

.money-input,
.metric {
  min-height: 114px;
  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);
}

.compact-summary .money-input,
.compact-summary .metric {
  min-height: 84px;
}

.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;
}

.assigned-card strong {
  font-size: clamp(1.05rem, 1.4vw, 1.45rem);
}

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

.metric em {
  width: fit-content;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-style: normal;
  font-weight: 820;
}

.metric em[data-state="ok"] {
  color: var(--accent);
  background: rgba(31, 122, 104, 0.1);
}

.metric em[data-state="warning"] {
  color: var(--accent-2);
  background: rgba(214, 82, 60, 0.1);
}

.category-card.essentials {
  box-shadow: inset 4px 0 0 var(--essentials);
}

.category-card.car {
  box-shadow: inset 4px 0 0 var(--car);
}

.category-card.leisure {
  box-shadow: inset 4px 0 0 var(--leisure);
}

.category-card.unexpected {
  box-shadow: inset 4px 0 0 var(--unexpected);
}

.category-card.savings {
  box-shadow: inset 4px 0 0 var(--savings);
}

.category-card.investment {
  box-shadow: inset 4px 0 0 var(--investment);
}

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

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

.rule-panel {
  padding: 16px clamp(14px, 2.6vw, 28px);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.38);
}

.rule-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

#ruleStatus {
  flex: 0 0 auto;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 0.84rem;
  font-weight: 850;
}

.metric #ruleStatus {
  width: fit-content;
  padding: 0;
  border-radius: 0;
  background: transparent;
}

.metric #ruleStatus[data-state="balanced"],
.metric #ruleStatus[data-state="unassigned"] {
  color: var(--accent);
  background: transparent;
}

.metric #ruleStatus[data-state="over"] {
  color: var(--accent-2);
  background: transparent;
}

.objective-strip {
  grid-column: 1 / -1;
  min-width: 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-card);
}

.objective-strip-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.objective-strip-head span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 720;
  text-transform: uppercase;
}

.objective-strip-head strong {
  color: var(--ink);
  font-size: 0.92rem;
}

.target-chips {
  display: grid;
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.target-chip {
  min-width: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto auto;
  align-items: center;
  column-gap: 8px;
  row-gap: 1px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-control);
}

.target-chip span {
  grid-row: span 3;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 850;
}

.target-chip strong {
  min-width: 0;
  color: var(--ink);
  font-size: 0.86rem;
  line-height: 1.05;
  white-space: normal;
}

.target-chip small {
  justify-self: end;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 850;
}

.target-chip output,
.target-chip em {
  display: none;
}

.target-chip em[data-state] {
  display: block;
  grid-column: 2 / -1;
  font-style: normal;
  font-size: 0.74rem;
  font-weight: 720;
  color: var(--muted);
}

.target-chip em[data-state="warning"] {
  color: var(--accent-2);
}

.target-chips.editing {
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}

.target-chip.editing {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
}

.cat-input {
  min-width: 0;
  height: 30px;
  padding: 0 6px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  outline: 0;
  color: var(--ink);
  background: var(--surface-control);
  font-size: 0.82rem;
  font-weight: 720;
}

.cat-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 104, 0.12);
}

.cat-target {
  width: 44px;
  flex: 0 0 44px;
  text-align: right;
}

.cat-label {
  flex: 1;
}

.cat-delete {
  flex: 0 0 auto;
}

.cat-add-btn {
  min-height: auto;
  height: 100%;
  font-size: 0.82rem;
  align-self: stretch;
}

.cat-edit-toggle {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  border-radius: 6px;
  font-size: 0.9rem;
}

.target-chip.essentials {
  border-color: color-mix(in srgb, var(--essentials), var(--line) 62%);
}

.target-chip.car {
  border-color: color-mix(in srgb, var(--car), var(--line) 62%);
}

.target-chip.leisure {
  border-color: color-mix(in srgb, var(--leisure), var(--line) 62%);
}

.target-chip.unexpected {
  border-color: color-mix(in srgb, var(--unexpected), var(--line) 62%);
}

.target-chip.savings {
  border-color: color-mix(in srgb, var(--savings), var(--line) 62%);
}

.target-chip.investment {
  border-color: color-mix(in srgb, var(--investment), var(--line) 62%);
}

#ruleStatus[data-state="balanced"],
#ruleStatus[data-state="unassigned"] {
  color: var(--accent);
  background: rgba(31, 122, 104, 0.1);
}

#ruleStatus[data-state="over"] {
  color: var(--accent-2);
  background: rgba(214, 82, 60, 0.1);
}

.stacked-track {
  height: 14px;
  display: flex;
  overflow: hidden;
  margin-top: 14px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface-action);
}

.stacked-track span {
  min-width: 0;
  transition: width 180ms ease;
}

.stacked-track .essentials {
  background-color: var(--essentials);
}

.stacked-track .car {
  background-color: var(--car);
}

.stacked-track .leisure {
  background-color: var(--leisure);
}

.stacked-track .unexpected {
  background-color: var(--unexpected);
}

.stacked-track .savings {
  background-color: var(--savings);
}

.stacked-track .investment {
  background-color: var(--investment);
}

.rule-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.rule-grid div {
  min-height: 74px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-card);
}

.rule-grid strong {
  display: block;
  margin-top: 4px;
}

.rule-grid small {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-weight: 650;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-block: 12px;
  border-bottom: 1px solid var(--line);
}

.destination-summary {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.destination-summary > span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 720;
  text-transform: uppercase;
}

.destination-total-list {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: thin;
}

.destination-total-pill {
  min-height: 30px;
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-control);
  font-size: 0.82rem;
}

.destination-total-pill b {
  color: var(--ink);
  font-weight: 760;
}

.destination-total-pill strong {
  color: var(--muted);
  font-weight: 820;
}

.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;
}

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

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

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

.table-row {
  display: grid;
  grid-template-columns: minmax(190px, 0.86fr) minmax(320px, 1.75fr) minmax(130px, 0.58fr) minmax(110px, 0.45fr) 86px;
  min-height: 54px;
  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 {
  justify-content: center;
  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);
}

.cell-input {
  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;
}

.destination-input {
  font-weight: 720;
}

.cell-unit-entry {
  width: 100%;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
}

.cell-unit-entry span {
  flex: 0 0 auto;
  color: var(--muted);
  font-weight: 820;
}

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

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

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

.category-select {
  width: 168px;
  min-height: 30px;
  padding: 0 30px 0 12px;
  border: 1px solid currentColor;
  border-radius: 999px;
  outline: 0;
  font-size: 0.82rem;
  font-weight: 850;
  appearance: none;
  background-color: transparent;
  background-image:
    linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position:
    calc(100% - 17px) 50%,
    calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

.category-select:hover,
.category-select:focus {
  box-shadow: 0 0 0 3px rgba(31, 122, 104, 0.08);
}

.category-select.essentials {
  color: var(--essentials);
  background: rgba(31, 122, 104, 0.1);
}

.category-select.car {
  color: var(--car);
  background: rgba(64, 111, 167, 0.1);
}

.category-select.leisure {
  color: var(--leisure);
  background: rgba(138, 90, 158, 0.1);
}

.category-select.unexpected {
  color: var(--unexpected);
  background: rgba(199, 122, 53, 0.12);
}

.category-select.savings {
  color: #9b7218;
  background: rgba(201, 151, 44, 0.14);
}

.category-select.investment {
  color: var(--investment);
  background: rgba(88, 104, 178, 0.11);
}

.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);
}

.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);
  padding-block: 9px;
}

.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);
}

.restore-original-link {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  color: var(--muted);
  background: var(--surface-btn);
  font-size: 0.78rem;
  font-weight: 820;
  text-decoration: none;
  text-transform: uppercase;
  transition: border-color 160ms ease, color 160ms ease, background 160ms ease;
}

.restore-original-link:hover {
  border-color: var(--ink);
  color: var(--ink);
  background: var(--surface-action);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101312;
    --surface: #171c1a;
    --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;
    --essentials: #57b89f;
    --car: #7fa4d3;
    --leisure: #b58dca;
    --unexpected: #e39a55;
    --savings: #d2a34a;
    --investment: #9aa5ec;
    --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-control: #171e1b;
    --surface-table: #141917;
    --surface-head: #111614;
    --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;
  }

  .rule-panel {
    background: rgba(18, 23, 21, 0.74);
  }

  .money-input,
  .metric,
  .rule-grid div,
  .icon-button,
  .secondary-button,
  .row-action,
  .restore-original-link {
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.025);
  }

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

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

  .category-select.essentials {
    color: #63c2aa;
    background: rgba(87, 184, 159, 0.13);
  }

  .category-select.car {
    color: #9ebcea;
    background: rgba(127, 164, 211, 0.13);
  }

  .category-select.leisure {
    color: #cfaae0;
    background: rgba(181, 141, 202, 0.13);
  }

  .category-select.unexpected {
    color: #f0b276;
    background: rgba(227, 154, 85, 0.14);
  }

  .category-select.savings {
    color: #dfb45d;
    background: rgba(210, 163, 74, 0.14);
  }

  .category-select.investment {
    color: #adb6ff;
    background: rgba(154, 165, 236, 0.13);
  }
}

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

  .summary-grid.compact-summary {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .objective-strip {
    grid-column: 1 / -1;
  }

  .rule-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .target-chips {
    grid-template-columns: repeat(3, 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,
  .rule-grid {
    grid-template-columns: 1fr;
  }

  .summary-grid.compact-summary,
  .target-chips {
    grid-template-columns: 1fr;
  }

  .target-chips {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .rule-header {
    align-items: flex-start;
    flex-direction: column;
  }

  #ruleStatus,
  .secondary-button,
  .restore-original-link {
    width: 100%;
  }

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

  .destination-summary {
    align-items: flex-start;
    flex-direction: column;
  }

  .destination-total-list {
    width: 100%;
  }

  .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;
  }
}

@media (max-width: 430px) {
  .target-chips {
    grid-template-columns: 1fr;
  }
}
