/*
  Fuel Tracker v2 — stylesheet
  Rule: core functionality first, cosmetics last (the maths and features
  are already correct — this pass is visual polish only).

  Calm, modern, mobile-first. Plain and functional — no gratuitous
  animation, just a couple of subtle colour/shadow transitions on
  hover/focus. Everything is built on two small systems so the whole
  page shares one rhythm instead of ad-hoc numbers:
    - a spacing scale (--space-*)   → paddings, gaps, margins
    - a type scale     (--text-*)   → every font-size on the page
  Two deliberately-tuned themes (light/dark) live in the token block
  below; dark is not just an inverted light theme.
*/

/* ---------- Reset ---------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

h1,
h2,
h3,
p,
fieldset,
legend,
ul,
ol {
  margin: 0;
  padding: 0;
}

ul,
ol {
  list-style: none;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

img,
svg {
  max-width: 100%;
  display: block;
}

/* ---------- Design tokens ---------- */

:root {
  color-scheme: light dark;

  /* Spacing scale (4px base) — every gap/padding/margin below is one
     of these, so vertical rhythm stays consistent across the page. */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  /* Type scale — every font-size on the page picks one of these. */
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 1rem;
  --text-md: 1.0625rem;
  --text-lg: 1.125rem;
  --text-xl: 1.375rem;
  --text-2xl: 1.75rem;
  --text-hero: 2.5rem;

  /* Radius scale — bigger containers get more rounding. */
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;

  /* ---- Light theme palette ---- */
  /* Neutrals carry a slight green bias rather than pure grey, on-brand
     with the petrol-green accent. */
  --bg: #f4f7f4;
  --surface: #ffffff;
  --surface-inset: #eef2ef;
  --surface-hover: #e5ebe6;
  --border: #dbe3dd;
  --border-soft: #e6ebe7;
  --text: #16201a;
  --text-muted: #5c675f;

  --accent: #1e7a57;
  --accent-hover: #196a4b;
  --accent-active: #145a3f;
  --accent-contrast: #ffffff;
  --accent-soft: rgba(30, 122, 87, 0.1);
  --chart-bar: var(--accent);

  --money: #a96a10;
  --money-soft: rgba(169, 106, 16, 0.1);
  --danger: #8a3b2a;

  --focus-ring: #1e7a57;
  --focus-ring-soft: rgba(30, 122, 87, 0.18);

  --shadow-sm: 0 1px 2px rgba(18, 26, 20, 0.06);
  --shadow-md: 0 6px 18px rgba(18, 26, 20, 0.07), 0 1px 3px rgba(18, 26, 20, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    /* ---- Dark theme palette ---- */
    /* Tuned on its own terms, not an invert: neutrals stay green-black
       (not blue-black), and the accent/money colours are re-picked so
       they read clearly against a dark surface. */
    --bg: #10140f;
    --surface: #181d18;
    --surface-inset: #1f251f;
    --surface-hover: #262d26;
    --border: #333c34;
    --border-soft: #232a24;
    --text: #e8ece8;
    --text-muted: #9aa89d;

    --accent: #1e7a57;
    --accent-hover: #24935f;
    --accent-active: #17663f;
    --accent-contrast: #ffffff;
    --accent-soft: rgba(63, 181, 131, 0.16);
    /* A brighter green than the button accent, used only for the chart
       bars, so they stay clearly visible against a dark card surface. */
    --chart-bar: #2fb37e;

    --money: #d68a2e;
    --money-soft: rgba(214, 138, 46, 0.14);
    --danger: #d98366;

    --focus-ring: #3fb583;
    --focus-ring-soft: rgba(63, 181, 131, 0.22);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.35), 0 2px 4px rgba(0, 0, 0, 0.3);
  }
}

/* ---------- Base ---------- */

html {
  -webkit-text-size-adjust: 100%;
}

body {
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

a {
  color: var(--accent);
}

button,
input,
select {
  touch-action: manipulation;
}

/* Visible focus state for every interactive element. */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Text inputs/selects get a softer glow ring instead, so it reads as
   "this field is active" rather than a harsh rectangle. */
.field input:focus-visible,
.field select:focus-visible,
.tfield > input:focus-visible,
.tfield > select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--focus-ring-soft);
}

/* ---------- Cloud-pull adoption banner ---------- */
/* The "pull my week down from the cloud" prompt, prepended above
   everything else on the page (see showCloudPullBanner() in ui.js). Plain
   and functional, but visually distinct as a page-level prompt rather
   than one more card — a solid accent-soft background and a left accent
   bar, using the same theme-aware tokens as the rest of the page. */

.cloud-pull-banner {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-4);
  background: var(--accent-soft);
  border-bottom: 1px solid var(--border);
  border-left: 4px solid var(--accent);
}

.cloud-pull-text {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text);
  line-height: 1.5;
}

.cloud-pull-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.cloud-pull-actions .btn,
.cloud-pull-actions .btn-secondary {
  width: auto;
  min-width: 8rem;
  flex: 1 1 8rem;
}

@media (min-width: 640px) {
  .cloud-pull-banner {
    max-width: 640px;
    margin-inline: auto;
    border-left-width: 4px;
    border-radius: 0 0 var(--radius-md) var(--radius-md);
  }
}

/* ---------- Header ---------- */

.app-header {
  padding: var(--space-5) var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg);
}

.app-header h1 {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

/* Small muted label sitting above the hero number, e.g. "This week". */
.eyebrow {
  margin-top: var(--space-3);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* The hero: the number that matters most on the whole page. */
.headline {
  margin-top: var(--space-1);
  font-size: var(--text-hero);
  font-weight: 800;
  letter-spacing: -0.01em;
  line-height: 1.1;
  color: var(--money);
  font-variant-numeric: tabular-nums;
}

/* ---------- Page layout ---------- */

.app {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  padding: var(--space-4);
  max-width: 100%;
}

/* ---------- Cards ---------- */

.card {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: var(--space-5);
}

.card-title {
  font-size: var(--text-lg);
  font-weight: 700;
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--border-soft);
}

/* ---------- Forms ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.field label {
  font-size: var(--text-sm);
  font-weight: 600;
}

.field input,
.field select {
  width: 100%;
  min-height: 44px;
  padding: 0.6rem 0.75rem;
  font-size: var(--text-base);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="checkbox"] {
  width: 1.25rem;
  height: 1.25rem;
  accent-color: var(--accent);
  flex: none;
}

.hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
  line-height: 1.5;
}

/* ---------- Today (hero card) ---------- */

.today-headline {
  font-size: var(--text-lg);
  font-weight: 700;
  line-height: 1.4;
  color: var(--text);
}

.today-trip-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.today-trip-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding-block: var(--space-1);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.today-cheapest {
  padding: var(--space-3) var(--space-4);
  background: var(--money-soft);
  color: var(--money);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
}

/* Subtle footnote shown only when the Today card is displaying the v3
   morning agent's pre-computed briefing instead of a live calculation. */
.today-sync-note {
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-style: italic;
}

/* ---------- Days ---------- */

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

.day + .day {
  border-top: 1px solid var(--border-soft);
  padding-top: var(--space-3);
}

.day-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.day-name {
  font-weight: 600;
  font-size: var(--text-md);
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 44px;
  padding-block: var(--space-1);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
}

.day-off {
  opacity: 0.6;
}

.day-off .day-name {
  color: var(--text-muted);
  font-weight: 500;
}

/* ---------- Trips ---------- */

.trip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}

.trip-fields {
  flex: 1 1 100%;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-3) var(--space-4);
}

.trip-cost {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ---------- Buttons ---------- */

.btn {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0.7rem 1.1rem;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: var(--text-base);
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: background-color 0.15s ease, box-shadow 0.15s ease;
}

.btn:hover {
  background: var(--accent-hover);
  box-shadow: var(--shadow-sm);
}

.btn:active {
  background: var(--accent-active);
  box-shadow: none;
}

.btn-secondary {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0.7rem 1.1rem;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--text-base);
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn-secondary:hover {
  background: var(--surface-hover);
  border-color: var(--text-muted);
}

.btn-remove {
  margin-left: auto;
  min-height: 44px;
  padding: 0.5rem 0.8rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.btn-remove:hover {
  color: var(--danger);
  background: var(--surface-hover);
}

/* ---------- Totals ---------- */

.total-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding-block: var(--space-2);
}

.total-line + .total-line {
  border-top: 1px solid var(--border-soft);
}

.total-label {
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.total-value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.total-headline {
  font-size: var(--text-2xl);
  font-weight: 800;
  color: var(--money);
  font-variant-numeric: tabular-nums;
}

.per-car,
.per-person {
  padding-top: var(--space-3);
  border-top: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.per-car-row,
.per-person-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding-block: var(--space-1);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

/* ---------- Sub-headings (Cars / People / Per car / Per person) ---------- */

.subheading {
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-top: var(--space-2);
}

/* ---------- Labelled field: a caption sitting above its control ---------- */

.tfield {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.tfield-cap {
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.tfield > input,
.tfield > select {
  width: 100%;
  min-height: 44px;
  padding: 0.55rem 0.65rem;
  font-size: var(--text-base);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

/* A computed value (not typed) — styled as a small money chip so it
   visually reads as "result", never mistaken for an editable field. */
.tfield > .trip-cost {
  min-height: 44px;
  display: flex;
  align-items: center;
  padding: 0 var(--space-2);
  background: var(--money-soft);
  color: var(--money);
  border-radius: var(--radius-sm);
  font-weight: 700;
}

/* ---------- Editable lists (cars / people / stations) ---------- */

.cars-list,
.people-list,
.stations-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.list-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.list-row .tfield {
  flex: 1 1 6rem;
}

.list-row .tfield:first-child {
  flex: 2 1 9rem;
}

.effective-econ {
  flex: 1 1 100%;
  display: inline-block;
  padding: var(--space-1) var(--space-2);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.effective-econ strong {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* ---------- Insights (budget / projection / alerts) ---------- */

.insight-line {
  font-size: var(--text-sm);
  line-height: 1.55;
  color: var(--text-muted);
}

/* A plain-English alert (above-average / price-jump) — only ever shown
   when the engine actually has something worth flagging; hidden via the
   `hidden` attribute the rest of the time. */
.insight-alert {
  padding: var(--space-3) var(--space-4);
  background: var(--surface-inset);
  border-left: 3px solid var(--danger);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  line-height: 1.5;
  color: var(--text);
}

/* ---------- Station price comparison ---------- */

.station-compare-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.station-compare-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.station-compare-name {
  font-weight: 600;
}

.station-compare-cost {
  color: var(--money);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- Fill-ups ---------- */

.fillup-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--space-3) var(--space-4);
}

.fillup-list {
  display: flex;
  flex-direction: column;
}

.fillup-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-1) var(--space-3);
  padding-block: var(--space-3);
}

.fillup-row + .fillup-row {
  border-top: 1px solid var(--border-soft);
}

.fillup-car {
  font-weight: 700;
}

.fillup-eco {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.fillup-sub {
  flex: 1 1 100%;
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.fillup-row .btn-remove {
  margin-left: auto;
}

.fillup-car-blocks {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.fillup-car-block {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-4);
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.fillup-car-block-name {
  font-weight: 700;
  font-size: var(--text-md);
}

/* ---------- History & spend chart ---------- */

.chart-holder {
  overflow-x: auto;
}

.chart {
  display: block;
  width: 100%;
  height: auto;
}

.chart-empty {
  padding: var(--space-6) var(--space-2);
  text-align: center;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* SVG marks: color lives here (theme-aware), geometry lives in chart.js. */
.bar {
  fill: var(--chart-bar);
}

.chart-axis {
  stroke: var(--border);
  stroke-width: 1.5;
}

.chart-budget {
  stroke: var(--money);
  stroke-width: 1.5;
}

.chart-budget-label {
  fill: var(--money);
  font-weight: 600;
}

.chart-bar-label {
  fill: var(--text);
  font-variant-numeric: tabular-nums;
}

.chart-x-label {
  fill: var(--text-muted);
}

.trend-note {
  padding: var(--space-3) var(--space-4);
  background: var(--surface-inset);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  line-height: 1.55;
  color: var(--text);
}

.history-list {
  display: flex;
  flex-direction: column;
}

.history-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-1) var(--space-3);
  padding-block: var(--space-3);
}

.history-row + .history-row {
  border-top: 1px solid var(--border-soft);
}

.history-when {
  font-weight: 700;
  font-size: var(--text-base);
}

.history-cost {
  color: var(--money);
  font-weight: 800;
  font-size: var(--text-lg);
  font-variant-numeric: tabular-nums;
}

.history-sub {
  flex: 1 1 100%;
  font-size: var(--text-xs);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.history-row .btn-remove {
  margin-left: auto;
}

/* ---------- Larger screens ---------- */

@media (min-width: 640px) {
  .app {
    max-width: 640px;
    margin-inline: auto;
    padding: var(--space-6) var(--space-4);
  }

  .app-header {
    padding: var(--space-6) 0 var(--space-6);
    max-width: 640px;
    margin-inline: auto;
  }

  .card {
    padding: var(--space-6);
  }

  .btn,
  .btn-secondary {
    width: auto;
    min-width: 12rem;
  }
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
