:root {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #6b6b6b;
  --border: #e0e0e0;
  --accent: #2563eb;
  --row-hover: #f5f7fb;

  /* Categorical palette (dataviz skill default theme, fixed order) */
  --cat-series-1: #2a78d6; /* blue */
  --cat-series-2: #008300; /* green */
  --cat-series-3: #e87ba4; /* magenta */
  --cat-series-4: #eda100; /* yellow */
  --cat-series-5: #1baf7a; /* aqua */
  --cat-series-6: #eb6834; /* orange */
  --cat-series-7: #4a3aa7; /* violet */
  --cat-series-8: #e34948; /* red */
  /* Low-salience / catch-all categories: neutral ink ramp, not hues */
  --cat-muted-a: #52514e;
  --cat-muted-b: #898781;
  --cat-muted-c: #c3c2b7;

  --status-good: #0ca30c;
  --status-critical: #d03b3b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171c;
    --fg: #e8e8e8;
    --muted: #9a9a9a;
    --border: #2d2f36;
    --accent: #5b8def;
    --row-hover: #1d2027;

    --cat-series-1: #3987e5;
    --cat-series-2: #008300;
    --cat-series-3: #d55181;
    --cat-series-4: #c98500;
    --cat-series-5: #199e70;
    --cat-series-6: #d95926;
    --cat-series-7: #9085e9;
    --cat-series-8: #e66767;
    --cat-muted-a: #c3c2b7;
    --cat-muted-b: #898781;
    --cat-muted-c: #383835;

    --status-good: #0ca30c;
    --status-critical: #e66767;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  font-size: 1.25rem;
  margin: 0;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.sync-status {
  color: var(--muted);
  font-size: 0.85rem;
}

.sync-button {
  color: var(--fg);
  text-decoration: none;
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 4px;
  padding: 0.3rem 0.7rem;
  font-size: 0.8rem;
  white-space: nowrap;
}

.sync-button:hover {
  background: var(--row-hover);
}

.logout-form {
  margin: 0;
}

.logout-button {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
}

.login-main {
  width: 100%;
  max-width: 24rem;
  padding: 1.5rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.75rem;
}

.login-form h1 {
  font-size: 1.15rem;
  margin: 0;
}

.login-subtitle {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.login-form input {
  padding: 0.5rem 0.6rem;
  font-size: 0.95rem;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
}

.login-form button {
  margin-top: 0.4rem;
  padding: 0.55rem;
  font-size: 0.95rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.login-error {
  margin: 0;
}

nav.tabs {
  position: relative;
  display: flex;
  gap: 1.25rem;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--border);
}

.nav-toggle-checkbox {
  display: none;
}

.hamburger-btn {
  display: none;
}

.nav-links {
  display: flex;
  gap: 1.25rem;
}

.nav-links a {
  display: inline-block;
  padding: 0.6rem 0.1rem;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  font-size: 0.9rem;
  white-space: nowrap;
}

.nav-links a.active {
  color: var(--fg);
  border-bottom-color: var(--accent);
}

@media (max-width: 680px) {
  .hamburger-btn {
    display: block;
    cursor: pointer;
    font-size: 1.3rem;
    padding: 0.5rem 0;
    color: var(--fg);
  }

  .nav-links {
    display: none;
    position: absolute;
    top: 100%;
    left: 1.5rem;
    flex-direction: column;
    gap: 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.4rem;
    min-width: 10rem;
    z-index: 20;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  .nav-toggle-checkbox:checked ~ .nav-links {
    display: flex;
  }

  .nav-links a {
    padding: 0.55rem 0.6rem;
    border-bottom: none;
    border-radius: 4px;
  }

  .nav-links a.active {
    background: var(--row-hover);
  }
}

.cat-dot {
  display: inline-block;
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  margin-right: 0.4rem;
  flex: none;
}

.cat-label {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.week-section {
  margin-bottom: 1.75rem;
}

.week-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.week-nav-btn {
  color: var(--fg);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  white-space: nowrap;
}

.week-nav-btn:hover {
  background: var(--row-hover);
}

.week-nav-btn.today {
  margin: 0 auto;
  color: var(--accent);
  border-color: var(--accent);
}

.add-expense {
  margin-bottom: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 0.75rem;
}

.add-expense summary {
  cursor: pointer;
  padding: 0.5rem 0;
  font-size: 0.85rem;
  color: var(--muted);
  list-style: none;
}

.add-expense summary::-webkit-details-marker {
  display: none;
}

.add-expense-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  padding-bottom: 0.75rem;
}

.add-expense-form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.add-expense-form input,
.add-expense-form select {
  padding: 0.35rem 0.5rem;
  font-size: 0.9rem;
}

.add-expense-form input[name="merchant"] {
  min-width: 10rem;
}

.tally {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 0.75rem;
}

.view-toggle {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.view-toggle a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  border: 1px solid transparent;
}

.view-toggle a.active {
  color: var(--fg);
  border-color: var(--border);
  background: var(--row-hover);
}

.category-groups {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.category-groups details {
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.category-groups summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem;
  cursor: pointer;
  list-style: none;
  font-size: 0.9rem;
}

.category-groups summary::-webkit-details-marker {
  display: none;
}

.category-groups summary:hover {
  background: var(--row-hover);
}

.category-groups details[open] summary {
  border-bottom: 1px solid var(--border);
}

.category-groups table.transactions {
  margin: 0;
}

.week-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.4rem;
}

.week-header h2 {
  font-size: 1rem;
  margin: 0;
}

.week-total {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.week-hero {
  display: none;
}

@media (max-width: 680px) {
  .tx-content {
    display: flex;
    flex-direction: column;
  }

  .tx-content > .sync-banner {
    order: 0;
  }

  .tx-content > .week-hero {
    order: 1;
  }

  .tx-content > .week-section {
    order: 2;
  }

  .tx-content > .filters {
    order: 3;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
  }

  .week-hero {
    display: block;
    text-align: center;
    padding: 0.5rem 0 1.25rem;
  }

  .week-hero-amount {
    font-size: 2.75rem;
    font-weight: 700;
    color: var(--fg);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
  }

  .week-hero-status {
    font-size: 1rem;
    font-weight: 600;
    margin-top: 0.4rem;
  }

  .week-hero-status.good {
    color: var(--status-good);
  }

  .week-hero-status.critical {
    color: var(--status-critical);
  }

  /* The hero above replaces this inline (count + total) line on mobile. */
  .week-header .week-total {
    display: none;
  }
}

.status-badge {
  font-size: 0.8rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
}

.status-badge.good {
  color: var(--status-good);
  background: color-mix(in srgb, var(--status-good) 15%, transparent);
}

.sync-banner {
  display: inline-block;
  margin-bottom: 0.75rem;
}

.status-badge.critical {
  color: var(--status-critical);
  background: color-mix(in srgb, var(--status-critical) 15%, transparent);
}

.config-form,
.category-manager {
  max-width: 40rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.config-form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.9rem;
}

.config-form input,
.config-form select {
  padding: 0.4rem 0.5rem;
  font-size: 0.9rem;
}

.export-section {
  margin-top: 2rem;
  max-width: 40rem;
}

.export-section h2 {
  font-size: 1.05rem;
  margin-bottom: 0.6rem;
}

.category-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.category-list li {
  display: grid;
  grid-template-columns: 1.1rem minmax(0, 1fr) auto auto auto;
  align-items: center;
  gap: 0.5rem 0.75rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.category-list li form {
  display: contents;
}

.category-list select {
  min-width: 9rem;
}

.category-list .cat-name-input {
  width: 100%;
  text-align: left;
  font-weight: 600;
}

.unassigned .category-list li {
  grid-template-columns: minmax(0, 1fr) auto auto auto;
}

.unassigned .category-list strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 680px) {
  .category-list li,
  .unassigned .category-list li {
    display: flex;
    flex-wrap: wrap;
  }

  .category-list li form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    width: 100%;
  }

  .category-list .cat-name-input,
  .unassigned .category-list strong {
    flex: 1 1 100%;
    width: 100%;
    white-space: normal;
  }

  .category-list select,
  .category-list button {
    flex: 1 1 auto;
  }
}

.new-category-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.new-category-form input[type="text"],
.new-category-form select {
  padding: 0.3rem 0.5rem;
  font-size: 0.85rem;
}

.new-category-form input[name="icon"] {
  width: 3.5rem;
  text-align: center;
}

section.rules h2,
section.categories h2 {
  font-size: 1.05rem;
}

table.rules {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

table.rules th,
table.rules td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

table.rules input[type="text"],
table.rules input[type="number"],
table.rules select {
  width: 100%;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem 0.35rem;
  font-size: 0.85rem;
}

.rules .actions,
.rules .actions form {
  display: inline;
}

main {
  padding: 1rem 1.5rem 2rem;
}

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  margin-bottom: 1rem;
}

.filters label {
  display: flex;
  flex-direction: column;
  font-size: 0.8rem;
  color: var(--muted);
  gap: 0.25rem;
}

.filters input,
.filters select,
.filters button {
  font-size: 0.9rem;
  padding: 0.35rem 0.5rem;
}

.filters a {
  color: var(--muted);
  font-size: 0.85rem;
}

.total {
  color: var(--muted);
  margin: 0 0 0.75rem;
}

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

table.transactions {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

table.transactions th,
table.transactions td {
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

table.transactions th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

table.transactions tbody tr:hover {
  background: var(--row-hover);
}

.amount {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.transactions select,
table.transactions input[type="text"] {
  width: 100%;
  min-width: 8rem;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem 0.35rem;
  font-size: 0.85rem;
}

table.transactions td.actions {
  text-align: center;
  white-space: nowrap;
}

.delete-row-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  opacity: 0.6;
}

.delete-row-btn:hover {
  opacity: 1;
  background: var(--row-hover);
}

/* --- Historique charts --- */

figure.chart {
  margin: 0 0 2rem;
}

figure.chart figcaption {
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.6rem;
}

.bar-chart-plot,
.diverging-chart-plot {
  position: relative;
  height: 200px;
  border-bottom: 1px solid var(--border);
}

.bars-row {
  position: absolute;
  left: 3rem;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  gap: 2px;
}

.diverging-chart-plot .bars-row {
  align-items: stretch;
}

.bar-col {
  position: relative;
  flex: 1 1 0;
  min-width: 4px;
  height: 100%;
  display: flex;
  align-items: flex-end;
}

.bar {
  width: min(24px, 80%);
  margin-inline: auto;
  border-radius: 4px 4px 0 0;
  background: var(--accent);
}

.bar.good {
  background: var(--status-good);
}

.bar.critical {
  background: var(--status-critical);
}

.div-bar {
  position: absolute;
  left: 50%;
  width: min(24px, 80%);
  transform: translateX(-50%);
}

.div-bar.good {
  background: var(--status-good);
  border-radius: 4px 4px 0 0;
}

.div-bar.critical {
  background: var(--status-critical);
  border-radius: 0 0 4px 4px;
}

.reference-line {
  position: absolute;
  left: 3rem;
  right: 0;
  height: 0;
  border-top: 2px solid var(--muted);
  z-index: 2;
}

.reference-line-label {
  position: absolute;
  right: 0;
  top: -1.2em;
  font-size: 0.7rem;
  color: var(--muted);
  background: var(--bg);
  padding: 0 0.25rem;
  white-space: nowrap;
}

.gridline {
  position: absolute;
  left: 3rem;
  right: 0;
  height: 0;
  border-top: 1px solid var(--border);
}

.gridline.zero-line {
  border-top-color: var(--muted);
}

.gridline-label {
  position: absolute;
  left: -3rem;
  width: 2.7rem;
  text-align: right;
  transform: translateY(-50%);
  font-size: 0.7rem;
  color: var(--muted);
  padding-right: 0.4rem;
  white-space: nowrap;
}

.x-axis {
  display: flex;
  gap: 2px;
  margin-top: 0.3rem;
}

.x-axis-gutter {
  flex: 0 0 3rem;
}

.x-tick {
  flex: 1 1 0;
  min-width: 4px;
  text-align: center;
  font-size: 0.65rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  margin-top: 0.6rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.legend-swatch {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 2px;
  display: inline-block;
}

.legend-swatch.good {
  background: var(--status-good);
}

.legend-swatch.critical {
  background: var(--status-critical);
}

.legend-line {
  width: 1rem;
  height: 0;
  border-top: 2px solid var(--muted);
  display: inline-block;
}

.history-table {
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0 0.75rem;
}

.history-table summary {
  cursor: pointer;
  padding: 0.5rem 0;
  font-size: 0.85rem;
  color: var(--muted);
  list-style: none;
}

.history-table summary::-webkit-details-marker {
  display: none;
}

.history-table table {
  margin-bottom: 0.75rem;
}
