/* Self-hosted fonts */
@font-face {
  font-family: 'Inter Variable';
  src: url('../fonts/inter/inter-latin-wght-normal.woff2') format('woff2');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Inter Variable';
  src: url('../fonts/inter/inter-latin-wght-italic.woff2') format('woff2');
  font-weight: 100 900;
  font-style: italic;
  font-display: swap;
}

@font-face {
  font-family: 'Space Grotesk Variable';
  src: url('../fonts/space-grotesk/space-grotesk-latin-wght-normal.woff2') format('woff2');
  font-weight: 300 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --surface-2: var(--bg-subtle);
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --accent-text: var(--accent-11);
}

/* Base reset and body */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: var(--step-0);
  font-family: var(--font-body);
  line-height: 1.5;
  color: var(--text);
  background-color: var(--bg);
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Container */
.container {
  width: 100%;
  max-width: 1160px;
  margin-inline: auto;
  padding-inline: var(--space-5);
}

/* Header */
header {
  padding-block: var(--space-6);
  background-color: var(--bg-subtle);
  border-bottom: 1px solid var(--border);
}

header .container {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

header h1 {
  font-family: var(--font-display);
  font-size: var(--step-3);
  font-weight: 600;
  color: var(--text);
  margin-bottom: var(--space-2);
}

header p {
  font-size: var(--step-0);
  color: var(--text-muted);
}

/* Main content */
main {
  flex: 1;
}

/* Section */
section {
  padding-block: var(--space-7);
}



h2 {
  font-family: var(--font-display);
  font-size: var(--step-2);
  font-weight: 600;
  color: var(--text);
  margin-bottom: var(--space-4);
}

/* h2 to paragraph */
h2 + p {
  margin-top: var(--space-3);
}

/* Form section */
#form {
  background-color: var(--bg-subtle);
}

#form form {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

/* Buttons */
button {
  padding: var(--space-4) var(--space-4);
  min-height: var(--target-min);
  cursor: pointer;
  border: none;
  border-radius: var(--radius);
  background-color: var(--brand-11);
  color: var(--on-brand);
  font-size: var(--step-0);
  transition: background-color var(--duration-fast) var(--ease), transform var(--duration-fast) var(--ease);
}

button:hover,
button:focus {
  background-color: var(--brand-10);
  transform: translateY(-2px);
}

/* Dark mode: use darker brand shade for better contrast with white text */
@media (prefers-color-scheme: dark) {
  button {
    background-color: #1e40af;
    color: #ffffff;
  }

  button:hover,
  button:focus {
    background-color: #1d4ed8;
  }
}

/* Delete button in expense list */
#expense-list .delete-btn {
  padding: var(--space-4) var(--space-4);
  min-height: 44px;
  font-size: var(--step-0);
}

/* Expense list */
#expense-list ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

#expense-list li {
  padding: var(--space-4);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
}

.expense-amount,
.expense-category,
.expense-date {
  flex: 1;
}

/* Footer */
footer {
  padding-block: var(--space-6);
  background-color: var(--bg-subtle);
  border-top: 1px solid var(--border);
  text-align: center;
  font-size: var(--step-n1);
  color: var(--text-muted);
}

/* Responsive: header layout */
@media (min-width: 640px) {
  header .container {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  header p {
    max-width: 400px;
  }
}

/* wrapper: the table never scrolls the page */
.table-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow-x: auto;
  overflow-y: visible;
}

/* --- base: 390px, stacked cards --- */
.totals-table,
.totals-table tbody,
.totals-table tr,
.totals-table td {
  display: block;
  width: 100%;
}

/* The header row is removed from the layout AND from the
   accessibility tree. Under 640px the table semantics are already
   gone (display: block above), so a hidden thead announces nothing
   a screen reader can use. The label lives on the cell instead,
   via data-label, in the reading order right before its value.
   display: none contributes zero to the document's scroll area.
   This replaces the off-screen absolute box. */
.totals-table thead {
  display: none;
}

.totals-table tbody {
  display: grid;
  gap: var(--space-3);
  padding: var(--space-3);
}

.totals-table tr {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-4);
}

/* The two key fields first: the label and the figure.
   Every cell is a label/value pair, stacked. */
.totals-table td {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-2) 0;
  border: 0;
  font-size: var(--step-0);
  color: var(--text);
}

.totals-table td + td {
  border-top: 1px solid var(--border);
}

/* The label, from the attribute. Present at every width;
   only this ::before is scoped to the phone. */
.totals-table td::before {
  content: attr(data-label);
  flex: 0 0 auto;
  font-size: var(--step-n1);
  font-weight: 500;
  letter-spacing: 0.01em;
  color: var(--text-muted);
}

/* The figure: right-aligned, tabular, accent-coloured. */
.totals-table td[data-label="Total"],
.totals-table td[data-label="Percent of total"] {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--accent-text);
}

/* The first cell of a card is the row's title: bigger, no label
   above it, because the card is about this value. */
.totals-table td:first-child {
  padding-top: 0;
  font-family: var(--font-display);
  font-size: var(--step-1);
  font-weight: 600;
  color: var(--text);
}

.totals-table td:first-child::before {
  content: none;                 /* the card's own title needs no label */
}

.totals-table td:last-child {
  padding-bottom: 0;
}

/* --- 640px and up: a real table --- */
@media (min-width: 640px) {
  .totals-table {
    display: table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--step-0);
  }

  .totals-table tbody {
    display: table-row-group;
    padding: 0;
  }

  .totals-table tr {
    padding: 0;
  }

  .totals-table thead {
    display: table-header-group;
    position: sticky;
    top: 0;
    z-index: 1;
  }

  .totals-table th {
    display: table-cell;
    position: sticky;
    top: 0;
    z-index: 1;
    padding-block: var(--space-3);
    padding-inline: var(--space-4);
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: var(--step-n1);
    font-weight: 500;
    letter-spacing: 0.06em;
    text-align: left;
    white-space: nowrap;
  }

  .totals-table td {
    display: table-cell;
    padding-block: var(--space-3);
    padding-inline: var(--space-4);
    border-bottom: 1px solid var(--border);
    color: var(--text);
    vertical-align: middle;
  }

  /* The phone layout's data-label pseudo-content is off above 640px. */
  .totals-table td::before {
    content: none;
  }

  .totals-table tbody tr:last-child td {
    border-bottom: 0;
  }

  .totals-table tbody tr:hover td {
    background: var(--surface-2);
  }

  /* Numeric cells: right-aligned, mono, tabular figures so the decimal
     points line up down the column. */
  .totals-table th[data-numeric],
  .totals-table td[data-numeric] {
    text-align: right;
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1;
  }

  .totals-table td[data-numeric] {
    color: var(--accent-text);
  }

  .totals-table th[data-numeric] {
    color: var(--text-muted);
  }
}

/* Form inputs */
input[type="text"],
input[type="number"] {
  width: 100%;
  height: var(--target-min);
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: var(--step-0);
  font-family: var(--font-body);
}

input[type="text"]:focus,
input[type="number"]:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 2px var(--brand-8);
}

/* Error state for inputs */
input[type="text"][aria-invalid="true"],
input[type="number"][aria-invalid="true"] {
  border-color: var(--accent-11);
}

/* Heading to table gap */
section h2 + .table-wrap {
  margin-top: var(--space-5);
}

/* ============================================================
   Delete confirmation dialog
   ============================================================ */

#confirm-delete {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: var(--space-5);
  max-width: min(90vw, 32rem);
}

#confirm-delete::backdrop {
  background: color-mix(in srgb, var(--text) 50%, transparent);
}

#confirm-delete form {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  justify-content: flex-end;
  margin-top: var(--space-5);
}

/* Secondary action button (Cancel) */
.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover,
.btn-secondary:focus-visible {
  background: var(--surface);
}

@media (prefers-reduced-motion: no-preference) {
  .btn-secondary:hover,
  .btn-secondary:focus-visible {
    transform: translateY(-2px);
  }
}
