:root {
  color-scheme: light;
  --bg: #f7f6f2;
  --ink: #171a24;
  --muted: #687083;
  --panel: #ffffff;
  --line: #dfe2ea;
  --accent: #2f5f8f;
  --accent-strong: #193f66;
  --accent-soft: #e3edf7;
  --tab-bg: #eef2f7;
  --tab-border: #d9e0ea;
  --tab-text: #43516b;
  --warn: #b84a37;
  --good: #32745a;
  --shadow: 0 16px 40px rgba(25, 35, 54, 0.08);
}

:root[data-theme="slate"] {
  --bg: #f5f5f4;
  --ink: #18181b;
  --muted: #71717a;
  --line: #dedbd4;
  --accent: #3f3f46;
  --accent-strong: #27272a;
  --accent-soft: #e7e5e4;
  --tab-bg: #ece9e4;
  --tab-border: #d8d3ca;
  --tab-text: #57534e;
  --warn: #b4533f;
  --good: #4d7c5c;
}

:root[data-theme="teal"] {
  --bg: #f2f8f8;
  --ink: #162522;
  --muted: #627571;
  --line: #d4e5e3;
  --accent: #1f7a75;
  --accent-strong: #145a57;
  --accent-soft: #dff3f0;
  --tab-bg: #e7f2f0;
  --tab-border: #cce2df;
  --tab-text: #365f5b;
  --warn: #b84a37;
  --good: #2f7661;
}

:root[data-theme="rose"] {
  --bg: #fbf6f5;
  --ink: #24191b;
  --muted: #78686b;
  --line: #eadbdc;
  --accent: #9d4d61;
  --accent-strong: #743346;
  --accent-soft: #f6e2e7;
  --tab-bg: #f5e8eb;
  --tab-border: #e8cbd2;
  --tab-text: #6d4651;
  --warn: #b9472d;
  --good: #417a5b;
}

:root[data-theme="mono"] {
  --bg: #f7f7f7;
  --ink: #151515;
  --muted: #666666;
  --line: #dddddd;
  --accent: #111111;
  --accent-strong: #000000;
  --accent-soft: #eeeeee;
  --tab-bg: #eeeeee;
  --tab-border: #d8d8d8;
  --tab-text: #444444;
  --warn: #a33a2a;
  --good: #3f6f52;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button,
.file-button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  min-height: 40px;
  padding: 0 14px;
}

button:hover,
.file-button:hover {
  background: var(--accent-strong);
}

button.secondary,
.file-button.secondary {
  background: #e7e2d8;
  color: var(--ink);
}

button.danger {
  background: #8f2f22;
}

.file-button {
  align-items: center;
  display: inline-flex;
}

.file-button input {
  display: none;
}

.topbar {
  align-items: center;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 20px;
  justify-content: space-between;
  padding: 22px clamp(16px, 4vw, 42px);
  position: sticky;
  top: 0;
  z-index: 2;
}

.topbar h1 {
  font-size: clamp(24px, 4vw, 40px);
  line-height: 1.05;
  margin: 0;
}

.eyebrow {
  color: #7b5d2a;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  margin: 0 0 5px;
  text-transform: uppercase;
}

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

.cycle-picker {
  align-items: center;
  display: flex;
  flex-direction: row;
  gap: 8px;
}

.cycle-picker select {
  min-height: 40px;
  min-width: 180px;
}

.theme-picker {
  align-items: center;
  display: flex;
  flex-direction: row;
  gap: 8px;
}

.theme-picker select {
  min-height: 40px;
  min-width: 105px;
}

.app-tabs {
  background: var(--tab-bg);
  border-bottom: 1px solid var(--tab-border);
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  padding: 14px clamp(14px, 3vw, 36px);
}

.tab-button {
  background: transparent;
  border: 1px solid transparent;
  color: var(--tab-text);
  font-weight: 700;
}

.tab-button:hover,
.tab-button[aria-selected="true"] {
  background: #ffffff;
  border-color: #b8d2c4;
  color: var(--accent-strong);
}

.hidden {
  display: none !important;
}

.sync-status {
  background: var(--accent-soft);
  border: 1px solid #a8ded7;
  border-radius: 999px;
  color: var(--accent-strong);
  font-size: 13px;
  font-weight: 800;
  padding: 9px 12px;
}

main {
  display: grid;
  gap: 18px;
  margin: 0 auto;
  max-width: 1180px;
  padding: 22px clamp(14px, 3vw, 36px) 42px;
}

.toolbar,
.kpi-grid,
.split-layout {
  display: grid;
  gap: 14px;
}

.toolbar {
  grid-template-columns: repeat(4, minmax(150px, 1fr));
}

label {
  color: var(--muted);
  display: grid;
  font-size: 13px;
  font-weight: 700;
  gap: 7px;
}

input,
select {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  min-height: 42px;
  padding: 8px 10px;
  width: 100%;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: 0;
}

.kpi-grid {
  grid-template-columns: repeat(5, minmax(150px, 1fr));
}

.kpi-grid article,
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.kpi-grid article {
  display: grid;
  gap: 8px;
  min-height: 102px;
  padding: 18px;
}

.kpi-grid span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.kpi-grid strong {
  font-size: clamp(21px, 2vw, 30px);
}

.split-layout {
  grid-template-columns: 1fr;
}

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

.view-section[hidden] {
  display: none;
}

.recovery-panel {
  color: var(--muted);
  font-size: 14px;
}

.recovery-panel details {
  background: transparent;
  border-top: 1px solid var(--line);
  padding: 12px 0 0;
}

.recovery-panel summary {
  cursor: pointer;
  font-weight: 800;
}

.recovery-actions {
  display: flex;
  gap: 8px;
  padding-top: 12px;
}

#diagnostics-output {
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  overflow: auto;
  padding: 12px;
  white-space: pre-wrap;
}

.panel {
  min-width: 0;
  overflow: hidden;
}

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

.panel-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.panel-heading h2 {
  font-size: 18px;
  margin: 0;
}

.panel-heading span,
.panel-heading strong {
  color: var(--muted);
  font-size: 13px;
}

.heading-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.mini-action {
  min-height: 32px;
  padding: 0 10px;
}

.category-list {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.category-watch {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.smart-warnings {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.warning-item,
.empty-note {
  background: #fff7ed;
  border: 1px solid #f2d3b6;
  border-radius: 8px;
  color: #7c3f23;
  font-weight: 700;
  margin: 0;
  padding: 10px 12px;
}

.empty-note {
  background: #eef8f5;
  border-color: #cce2df;
  color: var(--accent-strong);
}

.watch-row {
  display: grid;
  gap: 7px;
}

.watch-row header {
  align-items: center;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.watch-row strong {
  min-width: 0;
}

.watch-row em {
  color: var(--muted);
  font-style: normal;
  font-weight: 800;
  white-space: nowrap;
}

.watch-row.over em {
  color: var(--warn);
}

.watch-row.over .bar > span {
  background: var(--warn);
}

.watch-row.tracked-only .bar > span {
  opacity: 0.55;
}

.category-row {
  display: grid;
  gap: 8px;
}

.category-row header {
  align-items: center;
  display: flex;
  justify-content: space-between;
}

.category-row input {
  max-width: 130px;
  min-height: 34px;
}

.bar {
  background: #eee9df;
  border-radius: 999px;
  height: 10px;
  overflow: hidden;
}

.bar > span {
  background: var(--accent);
  display: block;
  height: 100%;
  max-width: 100%;
}

.category-meta {
  color: var(--muted);
  display: flex;
  font-size: 12px;
  justify-content: space-between;
}

.entry-form {
  align-items: end;
  display: grid;
  gap: 10px;
  grid-template-columns: 135px 1fr 120px 1fr auto;
  padding: 14px 16px;
}

.owed-form {
  grid-template-columns: 1fr 1fr 120px 145px auto;
}

.fixed-form {
  grid-template-columns: 1fr 1fr 120px 120px 90px 95px auto;
}

.table-wrap {
  overflow-x: auto;
}

.table-wrap.compact {
  max-height: none;
}

table {
  border-collapse: collapse;
  font-size: 14px;
  width: 100%;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  text-align: left;
  vertical-align: top;
}

th {
  background: #f8faf8;
  color: var(--muted);
  font-size: 12px;
  position: sticky;
  top: 0;
  z-index: 1;
}

td.amount,
th.amount {
  text-align: right;
}

.mini-button {
  background: #e7eee9;
  color: var(--ink);
  min-height: 30px;
  padding: 0 9px;
}

.status-paid {
  color: var(--good);
  font-weight: 800;
}

.status-outstanding {
  color: var(--warn);
  font-weight: 800;
}

.owed-status-select {
  background: #f8faf8;
  min-height: 34px;
  min-width: 135px;
  padding: 5px 8px;
}

.table-input {
  min-height: 34px;
  padding: 5px 8px;
}

.amount-input {
  text-align: right;
}

.due-input {
  max-width: 88px;
}

.due-controls {
  display: grid;
  gap: 6px;
  grid-template-columns: 72px 78px;
}

.due-month-input:disabled {
  color: var(--muted);
  opacity: 0.55;
}

.paid-cell {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex-direction: row;
  font-size: 12px;
  gap: 8px;
}

.paid-cell input {
  min-height: auto;
  width: auto;
}

@media (max-width: 1050px) {
  .toolbar,
  .kpi-grid,
  .split-layout,
  .lower,
  .app-tabs {
    grid-template-columns: 1fr 1fr;
  }

  .entry-form,
  .owed-form,
  .fixed-form {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 700px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
    position: static;
  }

  .toolbar,
  .kpi-grid,
  .split-layout,
  .lower,
  .app-tabs,
  .entry-form,
  .owed-form,
  .fixed-form {
    grid-template-columns: 1fr;
  }

  table {
    min-width: 680px;
  }
}
