:root {
  --bg: #050608;
  --surface: #0E1216;
  --surface-2: #161C22;
  --line: rgba(255, 255, 255, 0.08);
  --text: #EDF0F3;
  --muted: #A6AFBA;
  --faint: #8A94A0;
  --accent: #8CB6E8;
  --accent-dim: rgba(140, 182, 232, 0.13);
  --c-fuel: #8CB6E8;
  --c-wear: #C4CCD4;
  --c-fixed: #A78BDB;
  --c-extra: #55606C;
  --radius: 18px;
  --danger: #E06C6C;
  --chrome: linear-gradient(180deg, #F7F9FB 0%, #D4DBE2 42%, #97A2AE 55%, #E9EDF1 100%);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; background: var(--bg); touch-action: manipulation; }

body {
  touch-action: manipulation;
  font-family: "Anuphan", -apple-system, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.55;
}

.page {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: calc(env(safe-area-inset-top) + 18px) 16px calc(env(safe-area-inset-bottom) + 150px);
  background-image: radial-gradient(120% 46% at 50% -8%, #131922 0%, rgba(5, 6, 8, 0) 60%);
  background-repeat: no-repeat;
}

/* ---------- Chrome text (silver trim) ---------- */
.chrome-text {
  background: var(--chrome);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* ---------- Header ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 16px;
}
.topbar h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 2px;
}
.car-tag {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
}
.car-tag .sep { color: var(--faint); padding: 0 4px; }
.logo-mark { width: 34px; height: 34px; color: #C4CCD4; opacity: 0.9; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-bottom: 14px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
}
.card-head.static { margin-bottom: 4px; }
.card-head h2 { font-size: 16px; font-weight: 600; flex: 1; text-align: left; }
.card-head .ic { color: var(--accent); }

.group-label {
  color: var(--faint);
  font-size: 12.5px;
  letter-spacing: 0.4px;
  padding: 6px 6px 8px;
}

.ic {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

/* ---------- Accordion ---------- */
.acc { padding: 0; }
.acc-row { display: flex; align-items: stretch; }
.acc-head {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  padding: 16px;
  cursor: pointer;
  min-height: 56px;
}
.acc-row .acc-head { padding-right: 6px; }
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--muted);
  min-width: 48px;
  padding: 0 14px 0 6px;
  cursor: pointer;
  transition: color 180ms ease;
}
.icon-btn .ic { width: 19px; height: 19px; }
.card.editing .icon-btn { color: var(--accent); }
.acc-head .chev { color: var(--faint); transition: transform 200ms ease; }
.acc.open .acc-head .chev { transform: rotate(180deg); }
.acc-body { display: none; padding: 0 16px 16px; }
.acc.open .acc-body { display: block; }

.row-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 0 10px;
}

/* ---------- Hero ---------- */
.hero {
  position: relative;
  text-align: center;
  padding: 26px 18px 20px;
  overflow: hidden;
  background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1.4px);
  background-size: 13px 13px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 0 34px rgba(140, 182, 232, 0.07);
}
.hero::before {
  content: "";
  position: absolute;
  top: 0; left: 20%; right: 20%;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(140, 182, 232, 0.8), transparent);
}
.hero-label { color: var(--muted); font-size: 14px; }
.hero-total {
  font-size: 48px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
  line-height: 1.15;
  margin: 2px 0;
}
.hero-total .unit { font-size: 20px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.hero-sub { color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; }
.dot-sep { color: var(--faint); padding: 0 2px; }

.stack-bar {
  display: flex;
  height: 10px;
  border-radius: 6px;
  overflow: hidden;
  margin: 16px 0 12px;
  background: var(--surface-2);
}
.seg { transition: width 260ms ease; width: 0; }
.seg-fuel { background: var(--c-fuel); }
.seg-wear { background: var(--c-wear); }
.seg-fixed { background: var(--c-fixed); }
.seg-extra { background: var(--c-extra); }

.legend { list-style: none; text-align: left; }
.legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
  padding: 3px 0;
  font-variant-numeric: tabular-nums;
}
.legend .swatch { width: 9px; height: 9px; border-radius: 3px; flex: none; }
.legend .lg-name { flex: 1; }
.legend .lg-amt { color: var(--text); }

/* ---------- Fields ---------- */
.field-row { display: flex; gap: 10px; align-items: flex-end; margin-top: 12px; }
.field { display: block; min-width: 0; margin-top: 12px; }
.field-row .field { margin-top: 0; }
.field.grow { flex: 1; }
.field > span {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  font: inherit;
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  padding: 12px 14px;
  min-height: 48px;
  outline: none;
  transition: border-color 180ms ease;
}
.field input:focus { border-color: var(--accent); }
.field input::placeholder { color: var(--faint); }

.input-xl {
  font-size: 28px !important;
  font-weight: 600;
  min-height: 60px !important;
  text-align: center;
}

.hint { color: var(--faint); font-size: 13px; margin-top: 8px; }

.calc-chip {
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  border-radius: 10px;
  padding: 8px 10px;
  white-space: nowrap;
}
.calc-chip.off { background: var(--surface-2); color: var(--faint); }
.field-row .calc-chip { align-self: flex-end; margin-bottom: 4px; }

/* ---------- Segmented control ---------- */
.seg-control {
  display: flex;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 3px;
  margin-top: 12px;
}
.seg-control button {
  flex: 1;
  background: none;
  border: none;
  border-radius: 9px;
  color: var(--muted);
  font: inherit;
  font-size: 15px;
  font-weight: 500;
  padding: 10px;
  min-height: 44px;
  cursor: pointer;
  transition: background 180ms ease, color 180ms ease;
}
.seg-control button[aria-pressed="true"] {
  background: var(--accent);
  color: #0A0E13;
  font-weight: 600;
}

/* ---------- Buttons ---------- */
.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  border-radius: 12px;
  padding: 12px 14px;
  min-height: 48px;
  cursor: pointer;
  transition: border-color 180ms ease, opacity 180ms ease;
}
.btn-ghost:active { border-color: var(--accent); }
.btn-ghost[disabled] { opacity: 0.5; pointer-events: none; }
.btn-ghost .ic { width: 17px; height: 17px; color: var(--accent); }
.btn-ghost.spinning .ic { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn-text {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 6px;
  cursor: pointer;
}

.btn-add {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  background: none;
  border: 1px dashed rgba(255, 255, 255, 0.16);
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  border-radius: 12px;
  padding: 11px;
  margin-top: 10px;
  cursor: pointer;
  transition: color 180ms ease, border-color 180ms ease;
}
.btn-add:active { color: var(--accent); border-color: var(--accent); }
.btn-add .ic { width: 16px; height: 16px; }

.btn-del {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--danger);
  font: inherit;
  font-size: 14px;
  padding: 10px 4px 2px;
  cursor: pointer;
}
.btn-del .ic { width: 16px; height: 16px; }

/* ---------- Switch ---------- */
.switch-wrap { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.switch-label { font-size: 14.5px; color: var(--text); }
.switch { position: relative; display: inline-block; width: 46px; height: 28px; flex: none; cursor: pointer; }
/* hit area ขยายเกินขนาดที่มองเห็นให้ครบ 44px
   ต้องใช้ width/height แบบ calc + appearance:none เพราะ iOS WebKit
   ไม่ยืด checkbox ตาม inset เหมือน Chrome (กดไม่ติดบน iPhone) */
.switch input {
  position: absolute;
  left: -8px;
  top: -8px;
  width: calc(100% + 16px);
  height: calc(100% + 16px);
  -webkit-appearance: none;
  appearance: none;
  opacity: 0;
  margin: 0;
  cursor: pointer;
}
.switch i {
  position: absolute;
  inset: 0;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  transition: background 200ms ease;
  pointer-events: none;
}
.switch i::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #AEB6C0;
  transition: transform 200ms ease, background 200ms ease;
}
.switch input:checked + i { background: var(--accent); border-color: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); background: #0A0E13; }
.switch.sm { width: 40px; height: 24px; }
.switch.sm input { left: -10px; top: -10px; width: calc(100% + 20px); height: calc(100% + 20px); }
.switch.sm i::after { width: 16px; height: 16px; }
.switch.sm input:checked + i::after { transform: translateX(16px); }

/* ---------- Item list ---------- */
.item-list { list-style: none; }
.item-row { border-top: 1px solid var(--line); }
.item-row:first-child { border-top: none; }
.item-main {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 0;
  min-height: 52px;
}
.item-text { flex: 1; min-width: 0; }
.item-name { display: block; font-size: 15px; font-weight: 500; }
.item-sub { display: block; font-size: 12.5px; color: var(--faint); font-variant-numeric: tabular-nums; }
.item-perkm { font-size: 14px; font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; white-space: nowrap; }
.item-row.off .item-name, .item-row.off .item-perkm { color: var(--faint); }

/* โหมดแก้ไข: แถวข้อมูลถูกแทนที่ด้วยฟอร์มทั้งแถว */
.item-edit {
  display: none;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px 0 16px;
}
.card.editing .item-main { display: none; }
.card.editing .item-edit { display: flex; }
.item-edit .field { flex: 1 1 40%; margin-top: 0; }
.item-edit .field.grow { flex: 1 1 100%; }
/* 16px ขึ้นไปเสมอ กัน iOS auto-zoom ตอน focus input */
.item-edit .field input { font-size: 16px; padding: 10px 12px; min-height: 44px; }
.item-edit .btn-del { flex: 0 0 auto; align-self: flex-end; min-height: 44px; }

.edit-only { display: none; }
.card.editing .btn-add.edit-only { display: flex; }
.card.editing label.edit-only { display: block; margin-top: 12px; }
.card.editing .edit-actions { display: flex; gap: 10px; margin-top: 14px; }

.btn-primary {
  flex: 1;
  background: var(--accent);
  border: none;
  border-radius: 12px;
  color: #0A0E13;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  min-height: 48px;
  cursor: pointer;
}
.btn-primary:active { opacity: 0.85; }
.edit-actions .btn-ghost { flex: 0 0 auto; justify-content: center; min-width: 96px; }

.btn-restore {
  width: 100%;
  background: none;
  border: none;
  color: var(--muted);
  font: inherit;
  font-size: 13.5px;
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 12px 0 2px;
  cursor: pointer;
}
.btn-restore:active { color: var(--accent); }
.card.editing .btn-restore.edit-only { display: block; }

.annual-view {
  color: var(--muted);
  font-size: 13.5px;
  padding: 8px 0 2px;
  border-top: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.card.editing .annual-view { display: none; }

/* ---------- Mini bar ---------- */
.mini-bar {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  transform: translate(-50%, 140%);
  width: min(calc(100% - 32px), 448px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(14, 18, 22, 0.88);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 16px;
  transition: transform 260ms ease;
  z-index: 30;
  color: inherit;
  font: inherit;
  cursor: pointer;
  text-align: left;
}
.mini-bar.show { transform: translate(-50%, 0); }
.mini-left { display: block; }
.mini-label { display: block; font-size: 11.5px; color: var(--muted); line-height: 1.2; }
.mini-total { display: block; font-size: 19px; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; line-height: 1.25; }
.mini-perkm { font-size: 14px; color: var(--accent); font-variant-numeric: tabular-nums; }

.footnote {
  color: var(--faint);
  font-size: 12.5px;
  text-align: center;
  padding: 4px 12px 0;
}

/* ---------- Focus & motion ---------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.field input:focus-visible { outline: none; }
.switch input:focus-visible { outline: none; }
.switch input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

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