/* styles.css — layout and components. Uses ONLY tokens from tokens.css (no raw colours or sizes here).
   Plain, clean dark default styling; visual polish comes later. */

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-ui);
  font-size: var(--text-m);
  line-height: var(--leading-normal);
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100vh; min-height: 100dvh; }

h1, h2, h3, p, ul, ol, dl, dd { margin: 0; }
ul, ol { padding: 0; list-style: none; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; }

:focus-visible { outline: var(--focus-width) solid var(--color-focus); outline-offset: 2px; }
main:focus { outline: none; }

.num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--color-text-muted); }
.tone-profit { color: var(--color-profit); }
.tone-loss { color: var(--color-loss); }

/* ---------- App frame ---------- */

.app-header, main, .tabbar-inner {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
}

.app-header {
  padding: calc(var(--safe-top) + var(--space-3)) calc(var(--safe-right) + var(--space-4)) var(--space-2)
           calc(var(--safe-left) + var(--space-4));
}
.app-title { font-size: var(--text-l); font-weight: var(--weight-bold); color: var(--color-accent); letter-spacing: 0.02em; }

main {
  padding: var(--space-2) calc(var(--safe-right) + var(--space-4))
           calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-8)) calc(var(--safe-left) + var(--space-4));
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--color-surface);
  border-top: var(--border-width) solid var(--color-border);
  padding-bottom: var(--safe-bottom);
}
.tabbar-inner { display: flex; align-items: center; justify-content: space-around; height: var(--tabbar-height); }
.tab {
  display: flex; align-items: center; justify-content: center;
  flex: 1; min-height: var(--tap-min); padding: 0 var(--space-2);
  font-size: var(--text-s); color: var(--color-text-muted);
}
.tab[aria-current="page"] { color: var(--color-accent); font-weight: var(--weight-bold); }
.tab-add {
  flex: 0 0 auto; width: var(--tap-min); height: var(--tap-min); padding: 0;
  border-radius: var(--radius-l); background: var(--color-accent); color: var(--color-on-accent);
  font-size: var(--text-xl); font-weight: var(--weight-bold); line-height: 1;
}
.tab-add[aria-current="page"] { color: var(--color-on-accent); }

.toast {
  position: fixed; left: var(--space-4); right: var(--space-4); bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-4));
  max-width: var(--content-max); margin: 0 auto; padding: var(--space-3) var(--space-4);
  background: var(--color-surface-raised); border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-m); font-weight: var(--weight-medium);
}
.toast:empty { display: none; }

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

.button {
  display: flex; align-items: center; justify-content: center; width: 100%;
  min-height: var(--tap-min); padding: var(--space-2) var(--space-4);
  border: var(--border-width) solid var(--color-accent); border-radius: var(--radius-m);
  background: var(--color-accent); color: var(--color-on-accent);
  font-size: var(--text-m); font-weight: var(--weight-bold); cursor: pointer;
}
.button-secondary { background: transparent; color: var(--color-accent); }
.button[disabled] { opacity: 0.5; cursor: default; }

/* ---------- Dashboard ---------- */

.screen > * + * { margin-top: var(--space-5); }

.banner {
  padding: var(--space-3) var(--space-4);
  border: var(--border-width) solid var(--color-warning); border-radius: var(--radius-m);
  background: var(--color-surface);
}
.banner { padding: 0; }
.banner-toggle {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); width: 100%;
  min-height: var(--tap-min); padding: var(--space-2) var(--space-4); border: 0; background: transparent; cursor: pointer; text-align: left;
}
.banner-title { font-weight: var(--weight-bold); color: var(--color-warning); }
.banner-title::before { content: "! "; }
.banner ul { padding: 0 var(--space-4) var(--space-2); border-top: var(--border-width) solid var(--color-border); }
.banner ul[hidden] { display: none; }
.banner a { display: flex; align-items: center; padding: var(--space-2) 0; min-height: var(--tap-min); }

.period { display: flex; flex-direction: column; gap: var(--space-3); }
.segmented { display: flex; gap: var(--space-1); padding: var(--space-1); background: var(--color-surface); border-radius: var(--radius-m); }
.segmented button {
  flex: 1; min-height: var(--tap-min); border: 0; border-radius: var(--radius-s);
  background: transparent; color: var(--color-text-muted); cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--color-surface-raised); color: var(--color-accent); font-weight: var(--weight-bold); }
.period-nav { display: flex; align-items: center; justify-content: space-between; }
.period-nav button {
  width: var(--tap-min); height: var(--tap-min); border: var(--border-width) solid var(--color-border);
  border-radius: var(--radius-m); background: var(--color-surface); font-size: var(--text-l); cursor: pointer;
}
.period-nav button[disabled] { opacity: 0.35; cursor: default; }
.period-label { font-weight: var(--weight-medium); text-align: center; text-transform: capitalize; }

.odometer { padding: var(--space-5) var(--space-4); background: var(--color-surface); border-radius: var(--radius-l); text-align: center; }
.odometer-label { color: var(--color-text-muted); font-size: var(--text-s); }
.odometer-value { display: block; max-width: 100%; overflow-wrap: anywhere; font-size: var(--text-odometer); font-weight: var(--weight-bold); line-height: var(--leading-tight); margin: var(--space-2) 0; }
.odometer-delta { font-size: var(--text-s); color: var(--color-text-muted); }
.odometer-after { margin-top: var(--space-3); padding-top: var(--space-3); border-top: var(--border-width) solid var(--color-border); font-size: var(--text-s); color: var(--color-text-muted); }
.odometer-after .num { color: var(--color-text); font-weight: var(--weight-bold); }

.stats { background: var(--color-surface); border-radius: var(--radius-m); }
.stats > div { display: grid; grid-template-columns: 1fr auto; gap: 0 var(--space-3); padding: var(--space-3) var(--space-4); }
.stats > div + div { border-top: var(--border-width) solid var(--color-border); }
.stats dt { color: var(--color-text-muted); }
.stats dd { text-align: right; font-weight: var(--weight-bold); }
.stats .delta { grid-column: 1 / -1; font-size: var(--text-xs); color: var(--color-text-muted); text-align: right; font-weight: var(--weight-regular); }

.section-title { font-size: var(--text-m); font-weight: var(--weight-bold); }
.section-hint { margin-top: var(--space-1); font-size: var(--text-s); color: var(--color-text-muted); }
.vehicles > section + section { margin-top: var(--space-5); }
.cards { margin-top: var(--space-3); }
.cards > li + li { margin-top: var(--space-3); }

.card { display: block; padding: var(--space-4); background: var(--color-surface); border: var(--border-width) solid var(--color-border); border-radius: var(--radius-m); }
.card-head { display: flex; align-items: flex-start; gap: var(--space-3); }
.rank { min-width: 2ch; font-size: var(--text-l); font-weight: var(--weight-bold); color: var(--color-accent); }
.card-title { flex: 1; min-width: 0; }
.card-name { display: block; font-weight: var(--weight-bold); }
.card-sub { display: block; font-size: var(--text-s); color: var(--color-text-muted); }
.verdict { font-size: var(--text-xs); font-weight: var(--weight-bold); padding: var(--space-1) var(--space-2); border: var(--border-width) solid currentColor; border-radius: var(--radius-s); white-space: nowrap; }
.card-main { margin-top: var(--space-3); display: flex; align-items: baseline; flex-wrap: wrap; gap: var(--space-2) var(--space-3); }
.per-km { font-size: var(--text-xl); font-weight: var(--weight-bold); }
.card-meta { margin-top: var(--space-3); display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2) var(--space-3); font-size: var(--text-s); }
.card-meta dt { color: var(--color-text-muted); font-size: var(--text-xs); }
.card-meta dd { font-weight: var(--weight-medium); }
.card-note { margin-top: var(--space-3); font-size: var(--text-s); color: var(--color-text-muted); }

.list { margin-top: var(--space-3); background: var(--color-surface); border-radius: var(--radius-m); }
.list a { display: flex; justify-content: space-between; gap: var(--space-3); padding: var(--space-3) var(--space-4); min-height: var(--tap-min); }
.list li + li { border-top: var(--border-width) solid var(--color-border); }
.list .total { display: flex; justify-content: space-between; padding: var(--space-3) var(--space-4); font-weight: var(--weight-bold); }
.list-title { display: block; font-weight: var(--weight-medium); }
.list-sub { display: block; font-size: var(--text-xs); color: var(--color-text-muted); }
.list-amount { white-space: nowrap; font-weight: var(--weight-medium); }
.row-link {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  min-height: var(--tap-min); padding: var(--space-3) var(--space-4);
  background: var(--color-surface); border: var(--border-width) solid var(--color-border); border-radius: var(--radius-m);
  font-weight: var(--weight-bold);
}
.row-link-end { display: flex; align-items: center; gap: var(--space-3); color: var(--color-text); }
.back-link { display: inline-flex; align-items: center; min-height: var(--tap-min); color: var(--color-accent); font-weight: var(--weight-bold); }
.screen-title { font-size: var(--text-xl); line-height: var(--leading-tight); }
.empty-text { margin-top: var(--space-3); color: var(--color-text-muted); }

/* ---------- Empty state / placeholder ---------- */

.onboarding { padding-top: var(--space-7); text-align: center; }
.onboarding h2 { font-size: var(--text-xl); line-height: var(--leading-tight); }
.onboarding p { margin: var(--space-3) 0 var(--space-6); color: var(--color-text-muted); }
.onboarding .button + .button { margin-top: var(--space-3); }

.placeholder h2 { font-size: var(--text-xl); }
.placeholder p { margin: var(--space-3) 0 var(--space-5); color: var(--color-text-muted); }
.placeholder a { display: inline-flex; align-items: center; min-height: var(--tap-min); color: var(--color-accent); font-weight: var(--weight-bold); }

/* ---------- Buttons: extra kinds ---------- */

.button-small { width: auto; flex: none; padding: var(--space-2) var(--space-4); }
.button-danger { background: transparent; color: var(--color-loss); border-color: var(--color-loss); }
a.button { text-decoration: none; }

/* ---------- Vehicle detail ---------- */

.vehicle-head > * + * { margin-top: var(--space-1); }
.tag {
  display: inline-block; margin-top: var(--space-2); padding: var(--space-1) var(--space-2);
  border: var(--border-width) solid var(--color-text-muted); border-radius: var(--radius-s);
  font-size: var(--text-xs); font-weight: var(--weight-bold); color: var(--color-text-muted);
}
.info { background: var(--color-surface); border-radius: var(--radius-m); }
.info > div { display: flex; justify-content: space-between; gap: var(--space-3); padding: var(--space-2) var(--space-4); }
.info > div + div { border-top: var(--border-width) solid var(--color-border); }
.info dt { color: var(--color-text-muted); }
.notice { padding: var(--space-3) var(--space-4); border: var(--border-width) solid var(--color-warning); border-radius: var(--radius-m); color: var(--color-warning); font-weight: var(--weight-bold); }
.notice li + li { margin-top: var(--space-2); }
.summary > * + * { margin-top: var(--space-3); }
.per-km-line { font-size: var(--text-l); font-weight: var(--weight-bold); }
.loss-reason { margin-top: var(--space-2); font-weight: var(--weight-bold); color: var(--color-loss); }
.odometer-delta { margin-top: var(--space-2); }

.breakdown-row { padding: var(--space-3) var(--space-4); }
.breakdown-line { display: flex; justify-content: space-between; gap: var(--space-3); }
.bar-line { display: flex; align-items: center; gap: var(--space-3); margin-top: var(--space-2); }
.bar { flex: 1; height: var(--bar-height); background: var(--color-surface-raised); border-radius: var(--radius-s); overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--color-accent); }
.bar-percent { min-width: 4ch; text-align: right; font-size: var(--text-s); color: var(--color-text-muted); }
.breakdown-row .list-sub { margin-top: var(--space-1); }

.tabs { display: flex; gap: var(--space-1); padding: var(--space-1); background: var(--color-surface); border-radius: var(--radius-m); }
.tabs button { flex: 1; min-height: var(--tap-min); border: 0; border-radius: var(--radius-s); background: transparent; color: var(--color-text-muted); cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--color-surface-raised); color: var(--color-accent); font-weight: var(--weight-bold); }
.tab-panel { margin-top: var(--space-3); }
.tab-panel .list { margin-top: 0; }

.manage > * + * { margin-top: var(--space-3); }

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

.form { display: flex; flex-direction: column; gap: var(--space-4); margin-top: var(--space-4); }
.field label { display: block; margin-bottom: var(--space-1); font-weight: var(--weight-medium); }
.field input {
  display: block; width: 100%; min-height: var(--tap-min); padding: var(--space-2) var(--space-3);
  font: inherit; font-size: var(--text-m); color: var(--color-text);
  background: var(--color-surface); border: var(--border-width) solid var(--color-field-border); border-radius: var(--radius-m);
}
.field input[aria-invalid="true"] { border-color: var(--color-loss); }
.field-hint { margin-top: var(--space-1); font-size: var(--text-s); color: var(--color-text-muted); }
.field-error { margin-top: var(--space-1); font-size: var(--text-s); font-weight: var(--weight-bold); color: var(--color-loss); }
.field-error:empty { display: none; }
.field-error::before { content: "! "; }

/* ---------- Confirmation dialog ---------- */

.dialog {
  width: calc(100% - 2 * var(--space-4)); max-width: var(--content-max); padding: var(--space-5);
  color: var(--color-text); background: var(--color-surface-raised);
  border: var(--border-width) solid var(--color-border); border-radius: var(--radius-l);
}
.dialog::backdrop { background: var(--color-backdrop); }
.dialog-title { font-size: var(--text-l); line-height: var(--leading-tight); }
.dialog-text { margin: var(--space-3) 0 var(--space-5); color: var(--color-text-muted); }
.dialog-actions { display: flex; flex-direction: column; gap: var(--space-3); }

/* ---------- Vehicle detail: reason mark, data section ---------- */

.breakdown-row.is-flagged { border-left: var(--space-1) solid var(--color-accent); }
.flag { margin-top: var(--space-1); font-size: var(--text-s); font-weight: var(--weight-bold); color: var(--color-accent); }

.data-details { background: var(--color-surface); border-radius: var(--radius-m); }
.data-details summary { display: flex; align-items: center; min-height: var(--tap-min); padding: var(--space-2) var(--space-4); font-weight: var(--weight-bold); cursor: pointer; }
.data-details .info { border-top: var(--border-width) solid var(--color-border); border-radius: 0; }
.notice { padding: var(--space-2) var(--space-3); font-size: var(--text-s); }

/* ---------- Record forms (trip, expense, fleet expense) ---------- */

.field-hint:empty { display: none; }
.actions-row { display: flex; gap: var(--space-3); margin-bottom: var(--space-3); }
.actions-row .button { flex: 1; text-align: center; }
.records .tabs { margin-top: 0; }

/* live calculation panel */
.live { background: var(--color-surface); border-radius: var(--radius-m); }
.live > div { display: flex; justify-content: space-between; gap: var(--space-3); padding: var(--space-3) var(--space-4); }
.live > div + div { border-top: var(--border-width) solid var(--color-border); }
.live dt { color: var(--color-text-muted); }
.live dd { font-weight: var(--weight-bold); text-align: right; }
.live-total { font-size: var(--text-l); }
.live-total dt { color: var(--color-text); }

/* "Додатково" */
.more { background: var(--color-surface); border-radius: var(--radius-m); }
.more summary { display: flex; align-items: center; min-height: var(--tap-min); padding: var(--space-2) var(--space-4); font-weight: var(--weight-bold); cursor: pointer; }
.more .field { padding: 0 var(--space-4) var(--space-4); }

/* choices: big tappable options */
.choice { margin: 0; padding: 0; border: 0; min-width: 0; }
.choice legend { padding: 0; margin-bottom: var(--space-2); font-weight: var(--weight-medium); }
.choice-options { display: grid; gap: var(--space-2); }
.choice-grid .choice-options { grid-template-columns: 1fr 1fr; }
.choice-segmented .choice-options { grid-template-columns: 1fr 1fr; }
.choice-option { position: relative; display: block; }
.choice-option input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.choice-option span {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: calc(var(--tap-min) + var(--space-3)); padding: var(--space-2) var(--space-3);
  background: var(--color-surface); border: var(--border-width) solid var(--color-field-border); border-radius: var(--radius-m);
  font-weight: var(--weight-medium);
}
.choice-segmented .choice-option span { min-height: var(--tap-min); }
.choice-option input:checked + span { background: var(--color-surface-raised); border-color: var(--color-accent); color: var(--color-accent); font-weight: var(--weight-bold); }
.choice-option input:focus-visible + span { outline: var(--focus-width) solid var(--color-focus); outline-offset: 2px; }
.salary-box > * + *, .spread > * + *, .spread-box > * + * { margin-top: var(--space-3); }

/* on/off switch */
.toggle { display: flex; align-items: center; gap: var(--space-3); min-height: var(--tap-min); cursor: pointer; font-weight: var(--weight-medium); }
.toggle input { width: var(--space-7); height: var(--space-5); margin: 0; accent-color: var(--color-accent); flex: none; }

/* warnings that do not block saving */
.warning-box { padding: var(--space-3) var(--space-4); border: var(--border-width) solid var(--color-warning); border-radius: var(--radius-m); background: var(--color-surface); }
.warning-title { font-weight: var(--weight-bold); color: var(--color-warning); }
.warning-title::before { content: "! "; }
.warning-box ul { margin-top: var(--space-2); }
.warning-box li + li { margin-top: var(--space-2); }

/* ---------- Settings ---------- */

.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
label:has(+ .visually-hidden:focus-visible) { outline: var(--focus-width) solid var(--color-focus); outline-offset: 2px; }
.settings-section { padding: var(--space-4); background: var(--color-surface); border-radius: var(--radius-m); }
.settings-section > * + * { margin-top: var(--space-3); }
.settings-section .field input { background: var(--color-bg); }
.last-backup { font-weight: var(--weight-bold); }
.restore-message { padding: var(--space-3) var(--space-4); border: var(--border-width) solid var(--color-loss); border-radius: var(--radius-m); font-weight: var(--weight-medium); }
.restore-message .field-hint { font-weight: var(--weight-regular); }
.version { text-align: center; font-size: var(--text-s); }
.reminder { display: block; padding: var(--space-3) var(--space-4); border: var(--border-width) solid var(--color-border); border-radius: var(--radius-m); background: var(--color-surface); color: var(--color-text-muted); font-size: var(--text-s); }
.dialog .field { margin-bottom: var(--space-4); }
.dialog .field input { background: var(--color-bg); }
.dialog-text { white-space: pre-line; }

/* ---------- Quick-add sheet (the "+") ---------- */

button.tab { background: transparent; border: 0; font: inherit; cursor: pointer; }
button.tab-add { background: var(--color-accent); color: var(--color-on-accent); font-size: var(--text-xl); font-weight: var(--weight-bold); line-height: 1; }
.sheet {
  width: 100%; max-width: var(--content-max); margin: auto auto 0; padding: 0;
  color: var(--color-text); background: var(--color-surface-raised);
  border: var(--border-width) solid var(--color-border); border-bottom: 0; border-radius: var(--radius-l) var(--radius-l) 0 0;
}
.sheet::backdrop { background: var(--color-backdrop); }
.sheet-body { padding: var(--space-4) calc(var(--safe-right) + var(--space-4)) calc(var(--safe-bottom) + var(--space-5)) calc(var(--safe-left) + var(--space-4)); }
.sheet-body > * + * { margin-top: var(--space-3); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); }
.sheet-title { font-size: var(--text-l); }
.sheet-close { width: var(--tap-min); height: var(--tap-min); border: 0; border-radius: var(--radius-m); background: transparent; color: var(--color-text-muted); font-size: var(--text-l); cursor: pointer; }
.sheet-option {
  display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-1); width: 100%;
  min-height: calc(var(--tap-min) + var(--space-4)); padding: var(--space-3) var(--space-4); text-align: left;
  color: var(--color-text); background: var(--color-surface); border: var(--border-width) solid var(--color-border); border-radius: var(--radius-m); cursor: pointer;
}
.sheet-option-label { font-weight: var(--weight-bold); font-size: var(--text-m); }
.sheet-option-hint { font-size: var(--text-s); color: var(--color-text-muted); }

/* ---------- iPhone details, keyboard, motion (Stage 8) ---------- */

/* When the browser scrolls a field into view, leave room for the bottom bar and the home indicator */
html { scroll-padding-top: calc(var(--safe-top) + var(--space-4)); scroll-padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-6)); }

/* No double-tap zoom delay on controls */
a, button, summary, label, input, select { touch-action: manipulation; }

/* While typing on a phone the keyboard needs the space: the bottom bar steps aside */
@media (pointer: coarse) { .is-typing .tabbar { display: none; } }

/* The "new version" bar */
.update-bar {
  position: fixed; left: var(--space-4); right: var(--space-4); z-index: 10;
  bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-4));
  max-width: var(--content-max); margin: 0 auto; padding: var(--space-3) var(--space-4);
  background: var(--color-surface-raised); border: var(--border-width) solid var(--color-accent); border-radius: var(--radius-m);
}
.update-text { display: block; font-weight: var(--weight-bold); }
.update-actions { display: flex; gap: var(--space-3); margin-top: var(--space-3); }
.update-actions .button { flex: 1; }

/* iPhone Safari draws empty date fields collapsed and centred: make them look like the other fields */
@supports (-webkit-touch-callout: none) {
  .field input[type="date"] { -webkit-appearance: none; appearance: none; text-align: left; min-height: var(--tap-min); }
}

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