:root {
  color-scheme: light;
  --navy: #10213f;
  --teal: #0f766e;
  --teal-dark: #115e59;
  --teal-soft: #e6f5f2;
  --blue-soft: #eef5ff;
  --paper: #ffffff;
  --canvas: #f1f5f7;
  --ink: #14213d;
  --muted: #657186;
  --line: #d9e1e8;
  --danger: #b42318;
  --shadow: 0 16px 40px rgba(16, 33, 63, .08);
  --focus: rgba(15, 118, 110, .3);
}

[data-theme="dark"] {
  color-scheme: dark;
  --navy: #e6edf7;
  --teal: #2dd4bf;
  --teal-dark: #5eead4;
  --teal-soft: #123936;
  --blue-soft: #17243a;
  --paper: #151a22;
  --canvas: #0c1016;
  --ink: #edf2f7;
  --muted: #a9b4c4;
  --line: #303946;
  --danger: #fb7185;
  --shadow: 0 16px 40px rgba(0, 0, 0, .24);
  --focus: rgba(45, 212, 191, .4);
}
[data-theme="dark"] .dashboard-panel { background: linear-gradient(135deg, #151a22, #122421); }
[data-theme="dark"] .donut::after { background: var(--paper); }
[data-theme="dark"] .item-row + .item-row { border-top-color: var(--line); }
[data-theme="dark"] dialog { background: var(--paper); color: var(--ink); }

* { box-sizing: border-box; }
html { font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: var(--canvas); color: var(--ink); }
body { margin: 0; min-width: 320px; }
button, input { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.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; }

.skip-link { position: fixed; left: 1rem; top: -5rem; z-index: 10; background: var(--navy); color: white; padding: .75rem 1rem; border-radius: .5rem; }
.skip-link:focus { top: 1rem; }
.site-header { background: radial-gradient(circle at 75% 10%, #1b6470 0, var(--navy) 48%, #09152a 100%); color: white; padding: 3.2rem max(1.25rem, calc((100vw - 1120px) / 2)) 4.2rem; display: flex; align-items: end; justify-content: space-between; gap: 2rem; }
.eyebrow, .section-kicker { margin: 0 0 .35rem; color: #5eead4; font-size: .75rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
h1 { margin: 0; font-family: Georgia, serif; font-size: clamp(2.2rem, 5vw, 4.2rem); font-weight: 500; line-height: 1; }
.subtitle { margin: .8rem 0 0; max-width: 660px; color: #c8d5e6; }
.header-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .5rem; }
button { border: 0; border-radius: .65rem; padding: .7rem 1rem; font-weight: 750; }
.primary { color: white; background: var(--teal); }
.primary:hover { background: var(--teal-dark); }
.secondary { color: var(--navy); background: white; border: 1px solid var(--line); }
.site-header .secondary { background: transparent; color: white; border-color: rgba(255,255,255,.32); }
.danger { color: var(--danger); background: #fff0ee; }
.site-header .danger { background: rgba(255,255,255,.1); color: #ffd5d1; border: 1px solid rgba(255,255,255,.18); }

.app-shell { width: min(1120px, calc(100% - 2rem)); margin: -2rem auto 4rem; position: relative; }
.period-panel, .income-panel, .privacy-note { background: var(--paper); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem 1.4rem; box-shadow: var(--shadow); display: flex; align-items: center; gap: 1rem; }
.period-panel { display: grid; grid-template-columns: 1fr auto auto auto; }
h2 { margin: 0; font-family: Georgia, serif; font-size: 1.45rem; font-weight: 600; }
.section-kicker { color: var(--teal); }
.month-field, .money-field { display: grid; gap: .35rem; color: var(--muted); font-size: .82rem; font-weight: 700; }
input, select { min-width: 0; border: 1px solid var(--line); border-radius: .55rem; padding: .68rem .72rem; background: var(--paper); color: var(--ink); }
.save-status { margin: 0; color: var(--teal-dark); font-size: .85rem; }

.dashboard-panel { display: grid; grid-template-columns: 230px 1fr; align-items: center; gap: 2rem; background: linear-gradient(135deg, #fff, #f5fbfa); border: 1px solid var(--line); border-radius: 1.2rem; padding: 1.5rem 2rem; margin: 1rem 0; box-shadow: var(--shadow); }
.balance-visual { display: grid; place-items: center; }
.donut { --spent: 0deg; width: 180px; aspect-ratio: 1; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var(--teal) var(--spent), #dce8e8 0); position: relative; }
.donut::after { content: ""; position: absolute; inset: 17px; border-radius: 50%; background: white; }
.donut > div { position: relative; z-index: 1; display: grid; text-align: center; }
.donut strong { font-family: Georgia, serif; font-size: 2.25rem; color: var(--navy); }
.donut span { max-width: 90px; color: var(--muted); font-size: .72rem; font-weight: 700; }
.overview-copy h2 { font-size: 1.9rem; }
.budget-insight { color: var(--muted); font-size: 1.05rem; line-height: 1.55; max-width: 650px; }
.overall-progress, .category-progress { height: 8px; border-radius: 20px; background: #dce8e8; overflow: hidden; }
.overall-progress span, .category-progress span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--teal); transition: width .25s ease; }
.overview-legend { display: flex; gap: 1.25rem; margin-top: .75rem; color: var(--muted); font-size: .78rem; font-weight: 700; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: .35rem; }
.actual-dot { background: var(--teal); }
.remaining-dot { background: #dce8e8; border: 1px solid #bccccc; }
.summary-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: .85rem; margin: 1rem 0; }
.summary-card { background: var(--paper); border: 1px solid var(--line); border-radius: .9rem; padding: 1rem; min-height: 112px; display: flex; flex-direction: column; justify-content: space-between; box-shadow: 0 8px 22px rgba(16,33,63,.05); transition: transform .2s ease, box-shadow .2s ease; }
.summary-card:hover { transform: translateY(-2px); box-shadow: 0 12px 30px rgba(16,33,63,.09); }
.summary-card span { color: var(--muted); font-size: .78rem; font-weight: 700; }
.summary-card strong { font-size: clamp(1.3rem, 2.4vw, 1.8rem); color: var(--navy); }
.summary-card strong, .intelligence-grid strong, .category-totals, .compact-money input { font-variant-numeric: tabular-nums; }
.income-card { background: var(--blue-soft); }
.summary-card.negative { background: #fff0ee; border-color: #f7c4be; }
.summary-card.negative strong { color: var(--danger); }

.intelligence-panel { background: var(--paper); border: 1px solid var(--line); border-radius: 1rem; padding: 1.25rem 1.4rem; margin: 1rem 0 1.5rem; box-shadow: var(--shadow); }
.intelligence-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; }
.pace-status { margin: 0; color: var(--muted); text-align: right; max-width: 460px; }
.intelligence-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; margin: 1.2rem 0; background: var(--line); border: 1px solid var(--line); }
.intelligence-grid article { display: grid; gap: .35rem; background: var(--paper); padding: 1rem; }
.intelligence-grid span { color: var(--muted); font-size: .75rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; }
.intelligence-grid strong { color: var(--navy); font-size: 1.35rem; }
.intelligence-grid article.negative strong { color: var(--danger); }
.pace-track { position: relative; height: 10px; background: var(--teal-soft); border: 1px solid var(--line); border-radius: 999px; }
.pace-fill { display: block; width: 0; height: 100%; background: var(--teal); border-radius: inherit; transition: width .25s ease; }
.pace-marker { position: absolute; top: -5px; bottom: -5px; left: 0; width: 3px; border-radius: 3px; background: var(--navy); }
.pace-legend { display: flex; gap: 1.25rem; margin-top: .75rem; color: var(--muted); font-size: .78rem; font-weight: 700; }
.pace-marker-key { display: inline-block; width: 3px; height: 12px; margin: 0 .55rem -2px .2rem; background: var(--navy); }

.income-panel { justify-content: space-between; margin-bottom: 1.5rem; }
.money-input, .compact-money { display: flex; align-items: center; border: 1px solid var(--line); border-radius: .55rem; background: var(--paper); color: var(--muted); padding-left: .65rem; }
.money-input input, .compact-money input { border: 0; width: 130px; padding-left: .3rem; }
.section-heading { display: flex; justify-content: space-between; align-items: end; gap: 1rem; margin: 1.25rem 0 .75rem; }
.category-list { display: grid; gap: 1rem; }
.category-card { background: var(--paper); border: 1px solid var(--line); border-radius: 1rem; overflow: hidden; box-shadow: var(--shadow); }
.category-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: 1rem 1.2rem; background: linear-gradient(135deg, var(--teal-soft), white); border-bottom: 1px solid var(--line); }
.category-name { border: 0; background: transparent; padding: .2rem 0; border-radius: .2rem; font-family: Georgia, serif; font-size: 1.25rem; font-weight: 700; width: min(360px, 62vw); }
.category-title-row { display: flex; align-items: center; gap: .65rem; }
.category-type { padding: .35rem .55rem; font-size: .72rem; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; }
.category-totals { margin: .25rem 0 0; color: var(--muted); font-size: .82rem; }
.category-variance.over { color: var(--danger); font-weight: 800; }
.category-progress { width: min(420px, 60vw); height: 5px; margin-top: .55rem; }
.category-progress span.over { background: var(--danger); }
.category-actions { display: flex; align-items: center; gap: .35rem; }
.text-button { color: var(--teal-dark); background: var(--paper); border: 1px solid var(--line); padding: .5rem .7rem; }
.icon-button { width: 2.2rem; height: 2.2rem; padding: 0; border-radius: 50%; background: transparent; color: var(--muted); font-size: 1.3rem; }
.icon-button:hover { color: var(--danger); background: #fff0ee; }
.item-table { padding: .5rem 1.2rem 1rem; }
.item-row { display: grid; grid-template-columns: minmax(180px, 1fr) 150px 150px 40px; gap: .75rem; align-items: center; padding: .45rem 0; }
.item-row + .item-row { border-top: 1px solid #edf1f4; }
.item-head { color: var(--muted); font-size: .75rem; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; }
.compact-money input { width: 100%; }
.privacy-note { margin-top: 1.25rem; box-shadow: none; background: var(--teal-soft); }
.privacy-icon { flex: 0 0 2.2rem; display: grid; place-items: center; height: 2.2rem; border-radius: 50%; color: white; background: var(--teal); font-weight: 900; }
.privacy-note p { margin: .35rem 0 0; color: var(--muted); }
dialog { border: 0; border-radius: 1rem; max-width: 440px; box-shadow: 0 24px 80px rgba(16,33,63,.25); }
dialog::backdrop { background: rgba(16,33,63,.55); }
dialog p { color: var(--muted); line-height: 1.55; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.25rem; }

@media (max-width: 900px) {
  .site-header { align-items: flex-start; flex-direction: column; }
  .header-actions { justify-content: flex-start; }
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .intelligence-grid { grid-template-columns: repeat(2, 1fr); }
  .summary-card:last-child { grid-column: span 2; }
  .dashboard-panel { grid-template-columns: 180px 1fr; }
  .donut { width: 150px; }
}

@media (max-width: 650px) {
  .site-header { padding-top: 2rem; }
  .period-panel { grid-template-columns: 1fr; }
  .dashboard-panel { grid-template-columns: 1fr; text-align: center; }
  .overview-legend { justify-content: center; }
  .copy-button { width: 100%; }
  .save-status { justify-self: start; }
  .income-panel, .section-heading, .category-header { align-items: stretch; flex-direction: column; }
  .intelligence-heading { align-items: flex-start; flex-direction: column; }
  .pace-status { text-align: left; }
  .category-title-row { align-items: stretch; flex-direction: column; }
  .category-type { width: 100%; }
  .item-head { display: none; }
  .item-row { grid-template-columns: 1fr 1fr 36px; padding: .75rem 0; }
  .item-name { grid-column: 1 / -1; }
  .compact-money { position: relative; padding-top: .95rem; }
  .compact-money::before { position: absolute; top: .15rem; left: .65rem; font-size: .58rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
  .compact-money:has(.item-budget)::before { content: "Budget"; }
  .compact-money:has(.item-actual)::before { content: "Actual"; }
}

@media print {
  .site-header { background: white; color: var(--ink); padding: 0 0 1rem; }
  .subtitle { color: var(--muted); }
  .header-actions, .add-item, .remove-item, .remove-category, #addCategoryButton, .privacy-note, .save-status { display: none !important; }
  .app-shell { width: 100%; margin: 0; }
  .summary-card, .category-card, .period-panel, .income-panel { box-shadow: none; break-inside: avoid; }
}
