:root {
  --surface-1: #fcfcfb;
  --page-plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);

  --series-blue: #2a78d6;
  --series-orange: #eb6834;
  --series-violet: #4a3aa7;

  --status-good: #0ca30c;
  --status-critical: #d03b3b;
  --status-good-bg: #e7f6e7;
  --status-critical-bg: #fbe9e9;
  --status-warning: #b8860b;
  --status-warning-bg: rgba(250, 178, 25, 0.18);

  --accent-top: #3f8ae0;
  --accent-bottom: #2a78d6;
  --accent-press: #1f63b8;
  --ring: rgba(42, 120, 214, 0.30);
  --shadow-sm: 0 1px 2px rgba(11, 11, 11, 0.05);
  --shadow-md: 0 10px 28px -10px rgba(11, 11, 11, 0.18), 0 2px 6px rgba(11, 11, 11, 0.05);
  --radius-lg: 16px;
  --radius-md: 12px;
}

:root[data-theme="dark"] {
  --surface-1: #1a1a19;
  --page-plane: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);

  --series-blue: #3987e5;
  --series-orange: #d95926;
  --series-violet: #9085e9;

  --status-good: #0ca30c;
  --status-critical: #e66767;
  --status-good-bg: rgba(12, 163, 12, 0.15);
  --status-critical-bg: rgba(230, 103, 103, 0.15);
  --status-warning: #fab219;
  --status-warning-bg: rgba(250, 178, 25, 0.15);

  --accent-top: #4a92ea;
  --accent-bottom: #3987e5;
  --accent-press: #2f76cf;
  --ring: rgba(57, 135, 229, 0.38);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 10px 28px -10px rgba(0, 0, 0, 0.55), 0 2px 6px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page-plane);
  background-image:
    radial-gradient(1200px 600px at 50% -200px, color-mix(in srgb, var(--series-blue) 8%, transparent), transparent 70%);
  background-attachment: fixed;
  transition: background-image 0.2s ease;
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
}

body.env-test {
  background-image:
    radial-gradient(1200px 600px at 50% -200px, color-mix(in srgb, var(--status-warning) 10%, transparent), transparent 70%);
}

::selection {
  background: color-mix(in srgb, var(--series-blue) 25%, transparent);
}

:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--ring);
  border-radius: 6px;
}

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 32px 20px 64px;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-badge {
  flex-shrink: 0;
  width: 46px;
  height: 46px;
  border-radius: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  background: linear-gradient(160deg, color-mix(in srgb, var(--series-blue) 18%, var(--surface-1)), var(--surface-1));
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.page-header h1 {
  margin: 0 0 2px;
  font-size: 27px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  margin: 0;
  color: var(--text-secondary);
  font-size: 14px;
}

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

.theme-switcher select {
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  background: var(--surface-1);
  color: var(--text-primary);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.theme-switcher select:hover {
  border-color: var(--baseline);
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 28px;
}

@media (max-width: 1000px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr; }
}

.stat-tile {
  --tile-accent: var(--series-blue);
  background: var(--surface-1);
  background-image: linear-gradient(180deg, color-mix(in srgb, var(--tile-accent) 7%, transparent), transparent 60%);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}

.stat-tile::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 3px;
  background: var(--tile-accent);
}

.stat-tile:nth-child(2) { --tile-accent: var(--series-orange); }
#stat-remaining-tile { --tile-accent: var(--status-good); }

/* The reserve tile is grey until a floor is set, then takes the colour of
   how close the available balance is to it. */
#stat-reserve-tile { --tile-accent: var(--baseline); }
#stat-reserve-tile.status-ok { --tile-accent: var(--status-good); }
#stat-reserve-tile.status-warning { --tile-accent: var(--status-warning); }
#stat-reserve-tile.status-critical { --tile-accent: var(--status-critical); }

.stat-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 18px;
}

.stat-sub {
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.stat-value {
  font-size: 23px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}

/* Below the 2-column breakpoint the tiles are wide again. */
@media (max-width: 1000px) {
  .stat-value { font-size: 26px; }
}

.stat-value.blue { color: var(--series-blue); }
.stat-value.orange { color: var(--series-orange); }
.stat-value.good { color: var(--status-good); }
.stat-value.critical { color: var(--status-critical); }
.stat-value.warning { color: var(--status-warning); }
.stat-value.muted { color: var(--text-muted); font-size: 20px; }

.alert-banner {
  background: var(--status-critical-bg);
  border: 1px solid color-mix(in srgb, var(--status-critical) 45%, transparent);
  color: var(--status-critical);
  border-radius: var(--radius-md);
  padding: 13px 16px;
  margin-bottom: 24px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

/* Amber variant: informative, not an error. */
.alert-banner.warning {
  background: var(--status-warning-bg);
  border-color: color-mix(in srgb, var(--status-warning) 50%, transparent);
  color: var(--status-warning);
}

.alert-banner.hidden {
  display: none;
}

.field-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0;
}

.env-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.env-switch {
  display: inline-flex;
  background: var(--gridline);
  border-radius: 9px;
  padding: 3px;
  gap: 2px;
  flex-shrink: 0;
}

.env-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.env-btn.active {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
}

.env-btn[data-env="test"].active {
  color: var(--status-warning);
}

.env-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.env-actions.hidden {
  display: none;
}

.backup-status {
  font-size: 12px;
  color: var(--text-muted);
}

.backup-status.error {
  color: var(--status-critical);
}

.test-banner {
  background: var(--status-warning-bg);
  border: 1px solid color-mix(in srgb, var(--status-warning) 45%, transparent);
  color: color-mix(in srgb, var(--status-warning) 65%, var(--text-primary));
  border-radius: var(--radius-md);
  padding: 10px 14px;
  margin-bottom: 20px;
  font-size: 13px;
  font-weight: 600;
}

.test-banner.hidden {
  display: none;
}

.columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  align-items: start;
}

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

@media (max-width: 560px) {
  .allocation-row {
    flex-wrap: wrap;
  }
  .allocation-row select.alloc-fund {
    flex: 1 1 100%;
  }
  .allocation-row input.alloc-amount {
    flex: 1 1 auto;
  }
  .field-row {
    flex-wrap: wrap;
  }
  .field-row .field {
    flex: 1 1 140px;
  }
}

.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

:root[data-theme="dark"] .panel,
:root[data-theme="dark"] .stat-tile {
  box-shadow: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.panel-header h2 {
  margin: 0;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.btn {
  border: none;
  border-radius: 9px;
  padding: 8px 15px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: filter 0.15s ease, background 0.15s ease, border-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}

.btn:active { transform: translateY(1px); }

.btn-primary {
  background: linear-gradient(180deg, var(--accent-top), var(--accent-bottom));
  color: #ffffff;
  box-shadow: 0 1px 2px rgba(11, 11, 11, 0.18), inset 0 1px 0 rgba(255, 255, 255, 0.18);
}

.btn-primary:hover { filter: brightness(1.05); }
.btn-primary:active { background: var(--accent-press); }

.btn-ghost {
  background: var(--surface-1);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.btn-ghost:hover {
  background: var(--gridline);
  border-color: var(--baseline);
}

.btn-small {
  background: var(--gridline);
  color: var(--text-primary);
  padding: 5px 11px;
  font-size: 12px;
  border-radius: 8px;
}

.btn-small:hover { background: var(--baseline); }

.btn-danger {
  color: var(--status-critical);
}

.btn-icon {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 15px;
  padding: 2px 6px;
}
.btn-icon:hover { color: var(--status-critical); }
.btn-icon.btn-edit:hover { color: var(--series-blue); }

.card-edit-form {
  margin-bottom: 0;
  border-color: var(--series-blue);
}

.edit-hint {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin: 0;
}

.inline-form {
  background: var(--page-plane);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.inline-form.hidden {
  display: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.field-row {
  display: flex;
  gap: 12px;
}

.field label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 600;
}

.field input, .field select {
  border: 1px solid var(--baseline);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface-1);
  color: var(--text-primary);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus, .field select:focus,
.sub-expense-row-header input:focus,
.allocation-row select:focus, .allocation-row input:focus {
  outline: none;
  border-color: var(--series-blue);
  box-shadow: 0 0 0 3px var(--ring);
}

.form-actions {
  display: flex;
  gap: 8px;
}

.form-error {
  color: var(--status-critical);
  font-size: 13px;
  margin: 0;
  display: none;
}

.form-error.visible {
  display: block;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
}

.checkbox-field input {
  width: 15px;
  height: 15px;
  cursor: pointer;
}

.allocations-block {
  border-top: 1px solid var(--gridline);
  padding-top: 12px;
}

#simple-expense-fields.hidden,
#decomposed-fields.hidden {
  display: none;
}

.sub-expense-row {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 10px;
  background: var(--surface-1);
}

.sub-expense-row-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.sub-expense-row-header input {
  flex: 1;
  border: 1px solid var(--baseline);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface-1);
  color: var(--text-primary);
}

.sub-expense-row .allocations-block {
  border-top: 1px solid var(--gridline);
  padding-top: 10px;
  margin-top: 10px;
}

.allocations-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.allocation-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  align-items: center;
}

.allocation-row select {
  flex: 1.4;
}

.allocation-row input {
  flex: 1;
}

.allocation-summary {
  font-size: 13px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  margin: 4px 0 0;
}

.allocation-summary.balanced { color: var(--status-good); }
.allocation-summary.unbalanced { color: var(--status-critical); }

.card-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.5;
  text-align: center;
  padding: 36px 16px;
}

.empty-icon {
  font-size: 34px;
  line-height: 1;
  opacity: 0.85;
}

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 13px 15px;
  background: var(--surface-1);
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.card:hover {
  border-color: var(--baseline);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

:root[data-theme="dark"] .card {
  box-shadow: none;
}

:root[data-theme="dark"] .card:hover {
  box-shadow: var(--shadow-md);
}

.child-card:hover {
  transform: none;
  box-shadow: var(--shadow-sm);
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.caret {
  flex-shrink: 0;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform 0.15s ease;
  display: inline-block;
}

.card.expanded > .card-top .caret {
  transform: rotate(90deg);
}

.card-title-block {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
  text-align: left;
}

.card-title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.card-amounts {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.card-amount-main {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 14px;
  text-align: right;
  white-space: nowrap;
}

.card-amount-sub {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--text-muted);
}

/* Fund bars read like a fuel gauge: the consumed part is red, what is still
   available stays green. The amounts are spelled out on the card too, so the
   colour is never the only signal. Mixing against the surface keeps both
   tones correct in light and dark mode. */
.progress-track {
  height: 7px;
  border-radius: 4px;
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--status-good) 62%, var(--surface-1)),
    color-mix(in srgb, var(--status-good) 88%, var(--surface-1)));
  margin-top: 10px;
  overflow: hidden;
}

/* A fund with no amount has nothing to show either way. */
.progress-track.empty {
  background: var(--gridline);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--status-critical) 62%, var(--surface-1)),
    color-mix(in srgb, var(--status-critical) 92%, var(--surface-1)));
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Used beyond its amount: solid alarm red across the whole bar. */
.progress-fill.over {
  background: var(--status-critical);
}

.card-details {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--gridline);
  display: none;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.card-details .detail-row {
  align-self: stretch;
}

.card.expanded .card-details {
  display: flex;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-secondary);
}

.detail-row .detail-amount {
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}

.detail-empty {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

.child-list {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
  padding-left: 14px;
  border-left: 2px solid var(--gridline);
}

.child-card {
  background: var(--page-plane);
  padding: 10px 12px;
}

.decompose-btn {
  margin-top: 4px;
}

.child-form {
  align-self: stretch;
  margin-top: 8px;
  margin-bottom: 0;
  padding: 12px;
}

.page-footer {
  margin-top: 32px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.hidden-file-input {
  display: none;
}

.attachment-field {
  border-top: 1px solid var(--gridline);
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.attachment-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.attachment-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: 5px 10px;
  border-radius: 8px;
  background: var(--gridline);
  color: var(--text-primary);
  font-size: 12px;
  word-break: break-all;
}

.attachment-error {
  margin: 0;
}

.attachment-detail {
  gap: 10px;
}

.attachment-link {
  color: var(--series-blue);
  font-weight: 600;
  text-decoration: none;
  word-break: break-all;
}

.attachment-link:hover {
  text-decoration: underline;
}

.attachment-link.missing {
  color: var(--status-critical);
  pointer-events: none;
}
