:root {
  color-scheme: light;
  --bg: #f2f2f0;
  --surface: #fcfcfb;
  --surface-2: #ecebe8;
  --border: #e1e0dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;       /* sequential blue 450 */
  --accent-soft: #dbe9fb;
  --bar-muted: #9ec5f4;    /* "อื่นๆ" */
  --good: #0ca30c;
  --warn: #e39a00;
  --crit: #d03b3b;
  --flash: #fff4d6;
  --glass: rgb(252 252 251 / .78);
  --glass-border: rgb(0 0 0 / .08);
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 4px 14px rgb(0 0 0 / .05);
  --shadow-float: 0 8px 30px rgb(0 0 0 / .12), 0 1px 3px rgb(0 0 0 / .08);
  --radius: 18px;
  --tabbar-h: 64px;
  --tabbar-gap: max(10px, env(safe-area-inset-bottom));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e0e0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: #2e2e2b;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --accent-soft: #16304f;
    --bar-muted: #1c5cab;
    --flash: #3a3016;
    --glass: rgb(34 34 32 / .72);
    --glass-border: rgb(255 255 255 / .1);
    --shadow: none;
    --shadow-float: 0 8px 30px rgb(0 0 0 / .5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e0e0d;
  --surface: #1a1a19;
  --surface-2: #262624;
  --border: #2e2e2b;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --text-3: #8f8e86;
  --accent: #3987e5;
  --accent-soft: #16304f;
  --bar-muted: #1c5cab;
  --flash: #3a3016;
  --glass: rgb(34 34 32 / .72);
  --glass-border: rgb(255 255 255 / .1);
  --shadow: none;
  --shadow-float: 0 8px 30px rgb(0 0 0 / .5);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, 'Sukhumvit Set', 'Thonburi', 'Noto Sans Thai', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }
.muted, .meta { color: var(--text-3); }
.meta { font-size: 12.5px; }
.strong { font-weight: 600; }
.grow { flex: 1; min-width: 0; }
.hidden { display: none !important; }
.empty { color: var(--text-3); font-size: 14px; padding: 10px 0; }
.err { color: var(--crit); font-size: 13px; margin-top: 10px; min-height: 1em; }

/* ------------------------------------------------------------ app shell */
.app { min-height: 100dvh; }
.appbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(18px) saturate(180%);
  backdrop-filter: blur(18px) saturate(180%);
}
.appbar-titles { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.appbar h1 { font-size: 28px; font-weight: 700; letter-spacing: -.02em; margin: 0; line-height: 1.15; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow); cursor: pointer; flex: none;
}
.icon-btn:active { transform: scale(.94); }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-2);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px 3px 8px; white-space: nowrap;
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); }
.pill[data-state="ok"] .dot { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent); }
.pill[data-state="late"] .dot { background: var(--warn); }
.pill[data-state="down"] { color: var(--crit); border-color: color-mix(in srgb, var(--crit) 40%, var(--border)); }
.pill[data-state="down"] .dot { background: var(--crit); }

.banner {
  margin: 0 16px 10px; background: color-mix(in srgb, var(--crit) 12%, var(--surface)); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--crit) 35%, var(--border)); border-radius: 12px; padding: 9px 12px; font-size: 13.5px;
}

.views {
  padding: 4px max(16px, env(safe-area-inset-right)) calc(var(--tabbar-h) + var(--tabbar-gap) + 18px) max(16px, env(safe-area-inset-left));
  max-width: 1180px; margin: 0 auto;
}
.view { display: flex; flex-direction: column; gap: 10px; }

/* ------------------------------------------------------------ floating tab bar */
.tabbar {
  position: fixed; z-index: 10;
  left: 50%; transform: translateX(-50%);
  bottom: var(--tabbar-gap);
  width: min(calc(100% - 24px), 440px); height: var(--tabbar-h);
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 2px; padding: 6px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  border: 1px solid var(--glass-border); border-radius: 32px;
  box-shadow: var(--shadow-float);
}
.tab {
  border: 0; background: none; cursor: pointer; border-radius: 26px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  color: var(--text-2); touch-action: manipulation; transition: background .2s, color .2s;
}
.tab[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); }
.tab:active { transform: scale(.95); }
.tab-icon { position: relative; display: grid; place-items: center; }
.tab-label { font-size: 11px; font-weight: 600; letter-spacing: .01em; }
.tab-badge {
  position: absolute; top: -5px; right: -11px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--warn); color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; border: 2px solid var(--surface);
}

/* ------------------------------------------------------------ segmented control */
.seg { display: inline-grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg.full { display: grid; width: 100%; }
.seg button {
  border: 0; background: transparent; color: var(--text-2); cursor: pointer;
  font-size: 14px; padding: 7px 10px; border-radius: 9px; touch-action: manipulation;
}
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.seg.small button { font-size: 13px; padding: 5px 10px; }

/* ------------------------------------------------------------ cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px; min-width: 0;
}
.card h2 { font-size: 15px; font-weight: 650; margin: 0 0 10px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.card h2 small { font-weight: 400; font-size: 12.5px; color: var(--text-3); }

/* KPI tiles: 2×2 on phone, 4 across on iPad */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile {
  text-align: left; border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
  border-radius: var(--radius); padding: 12px 14px; min-width: 0; display: flex; flex-direction: column; gap: 2px;
}
button.tile { cursor: pointer; }
button.tile:active { transform: scale(.98); }
.tile-label { font-size: 13px; color: var(--text-2); display: flex; align-items: center; justify-content: space-between; }
.tile-value { font-size: 27px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-value .unit { font-size: 13px; font-weight: 500; color: var(--text-3); margin-left: 4px; letter-spacing: 0; }
.tile-sub { font-size: 12px; color: var(--text-3); }
.tile.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.tile.accent .tile-label, .tile.accent .unit { color: rgb(255 255 255 / .8); }

.tile-delta { font-size: 12.5px; font-weight: 600; color: var(--text-3); }
.tile-delta.up { color: var(--good); }
.tile-delta.down { color: var(--crit); }
.tile.accent .tile-delta { color: rgb(255 255 255 / .92); }

/* สรุป: totals strip */
.sum-strip { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.sum-strip > div {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  padding: 9px 12px; display: flex; flex-direction: column; min-width: 0;
}
.sum-strip b { font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* daily sales: single-series columns, hairline grid, tap to pick a day */
.day-pick { display: flex; flex-direction: column; margin-bottom: 6px; }
.day-pick-date { font-size: 13px; color: var(--text-2); }
.day-pick-main { font-size: 26px; font-weight: 700; letter-spacing: -.02em; line-height: 1.2; }
.dchart { position: relative; height: 150px; margin-top: 8px; }
.dgrid { position: absolute; inset: 0 0 20px 0; }
.gline { position: absolute; left: 38px; right: 0; border-top: 1px solid var(--border); }
.glabel { position: absolute; left: -38px; width: 32px; transform: translateY(-50%); text-align: right; font-size: 10.5px; color: var(--text-3); }
.dcols { position: absolute; inset: 0 0 0 38px; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; }
.dcol {
  position: relative; border: 0; background: none; padding: 0 0 20px; cursor: pointer; height: 100%;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center; touch-action: manipulation;
}
.dcol .fill { width: min(24px, 72%); background: var(--accent); opacity: .42; border-radius: 4px 4px 0 0; transition: height .35s ease, opacity .15s; }
.dcol[aria-selected="true"] .fill, .dcol:hover .fill { opacity: 1; }
.dlabel { position: absolute; bottom: 2px; left: 0; right: 0; text-align: center; font-size: 10.5px; color: var(--text-3); }
.dcol[aria-selected="true"] .dlabel { color: var(--text); font-weight: 700; }
.chart-foot { margin-top: 6px; }

/* stone types / customers: ตัน + บาท table */
.btable { display: flex; flex-direction: column; }
.brow { display: grid; grid-template-columns: minmax(0, 1fr) 74px 96px; gap: 8px; align-items: start; padding: 8px 0; border-bottom: 1px solid var(--border); }
.brow.head { font-size: 12px; color: var(--text-3); padding-top: 0; }
.brow.total { border-bottom: 0; border-top: 2px solid var(--border); margin-top: -1px; font-weight: 700; }
.bname { display: flex; flex-direction: column; min-width: 0; }
.bname .n { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bsub { display: flex; align-items: center; gap: 8px; margin-top: 3px; white-space: nowrap; }
.share { flex: 0 0 40px; height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.share-fill { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
.bnum { text-align: right; display: flex; flex-direction: column; align-items: flex-end; font-size: 15px; font-weight: 600; }
.bnum .meta { font-weight: 400; }

/* horizontal bars */
.hbars { display: flex; flex-direction: column; gap: 11px; }
.hbar-top { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; }
.hbar-top .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-top .val { font-weight: 600; white-space: nowrap; }
.hbar .track { height: 8px; border-radius: 4px; background: var(--surface-2); margin: 4px 0 2px; overflow: hidden; }
.hbar .fill { height: 100%; background: var(--accent); border-radius: 4px; transition: width .4s ease; }
.hbar.other .fill { background: var(--bar-muted); }
.hbar-sub { font-size: 12px; color: var(--text-3); }

.home-grid { display: grid; gap: 12px; }

.last-strip {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
  border-radius: 14px; padding: 11px 14px; font-size: 14px; cursor: pointer; color: var(--text-2);
}
.last-strip b { color: var(--text); }
.dot-live { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: none; animation: pulse 2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--good) 50%, transparent); } 70% { box-shadow: 0 0 0 8px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

/* trucks */
.trucks-grid { display: grid; gap: 12px; }
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.list > li:last-child { border-bottom: 0; padding-bottom: 2px; }
.truck { justify-content: space-between; }
.truck .plate { font-size: 16.5px; font-weight: 650; letter-spacing: .01em; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.truck-main { min-width: 0; }
.truck-main .meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.truck-side { text-align: right; flex: none; }
.truck-side .strong { font-size: 15px; }
.truck.flash { animation: flash 3s ease-out; }
@keyframes flash { from { background: var(--flash); } to { background: transparent; } }
.badge { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); }
.truck .badge { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--text); }
.truck .badge.late { background: color-mix(in srgb, var(--crit) 18%, transparent); color: var(--crit); }

/* reports */
.report-grid { display: grid; gap: 12px; }
.view > div > .seg { margin-bottom: 12px; }
.month-pick { font-size: 14px; color: var(--text-2); margin-bottom: 10px; }
.month-pick b { color: var(--text); }
.vbars { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; height: 110px; align-items: end; }
.vbar {
  border: 0; background: none; padding: 0; height: 100%; cursor: pointer;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: stretch; gap: 4px;
}
.vbar { align-items: center; }
.vbar .fill { width: min(24px, 80%); background: var(--bar-muted); border-radius: 4px 4px 0 0; min-height: 2px; transition: height .4s ease, background .2s; }
.vbar[aria-selected="true"] .fill { background: var(--accent); }
.vlabel { font-size: 10.5px; color: var(--text-3); text-align: center; white-space: nowrap; }
.vbar[aria-selected="true"] .vlabel { color: var(--text); font-weight: 600; }

/* ฉัน */
.me-grid { display: grid; gap: 12px; }
.stack { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.profile { display: flex; align-items: center; gap: 14px; }
.profile .avatar { width: 52px; height: 52px; font-size: 22px; }
.profile-name { font-size: 20px; font-weight: 700; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: var(--surface-2); color: var(--text-2); font-weight: 700; font-size: 17px;
}
.avatar.me { background: var(--accent); color: #fff; }
.avatar.dev { color: var(--text-3); }
.person .strong { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.person.pending { opacity: .65; }
.row-actions { display: flex; gap: 4px; flex: none; }
.ghost { border: 0; background: var(--surface-2); color: var(--text); font-size: 13px; padding: 6px 10px; border-radius: 9px; cursor: pointer; }
.ghost.danger { color: var(--crit); }
.action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.chip-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
  border-radius: 14px; padding: 12px 10px; font-size: 14px; font-weight: 600;
}
.chip-btn:active { transform: scale(.97); }
.invite { display: flex; gap: 8px; margin-top: 12px; }
.invite .input { flex: 1; }
.log li { padding: 8px 0; font-size: 14px; }
.log li.warn { color: var(--crit); }

/* forms */
.input {
  width: 100%; font: inherit; font-size: 16px; /* ≥16px: iOS does not zoom */
  padding: 11px 13px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.code-input { font-size: 20px; letter-spacing: .12em; text-transform: uppercase; text-align: center; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; margin-top: 16px; font-weight: 600; padding: 12px 14px;
  border: 0; border-radius: 13px; background: var(--text); color: var(--bg); cursor: pointer;
}
.btn.small { width: auto; margin: 0; padding: 0 14px; }
.btn.secondary { background: var(--surface-2); color: var(--text); margin-top: 8px; }
.btn:disabled { opacity: .5; cursor: progress; }
.linkbtn { border: 0; background: none; color: var(--text-3); font-size: 14px; cursor: pointer; padding: 8px; display: block; margin: 12px auto 0; }
.hint { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; color: var(--text-2); background: var(--surface-2); border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; }
.hint svg { flex: none; margin-top: 1px; }

/* gate / lock screens */
.screen {
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.login { max-width: 400px; margin: 10vh auto 0; }
.login h1 { font-size: 24px; margin: 0 0 4px; }
.login p { color: var(--text-2); margin: 0 0 16px; font-size: 14px; }
.login label { display: block; font-size: 13px; color: var(--text-2); margin: 12px 0 6px; }

/* PIN pad */
.pinpad { max-width: 320px; margin: 9vh auto 0; text-align: center; }
.pinpad h1 { font-size: 24px; font-weight: 700; margin: 0 0 6px; }
.pin-msg { min-height: 2.8em; margin: 0 0 16px; font-size: 14px; color: var(--text-2); }
.pin-msg.bad { color: var(--crit); }
.dots { display: flex; justify-content: center; gap: 18px; margin-bottom: 30px; }
.dot-pin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--text-3); transition: background .1s; }
.dot-pin.on { background: var(--text); border-color: var(--text); }
.dots.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.keys { display: grid; grid-template-columns: repeat(3, 76px); gap: 16px 24px; justify-content: center; }
.key {
  width: 76px; height: 76px; border-radius: 50%; border: 0;
  background: var(--surface-2); color: var(--text);
  font-size: 30px; font-weight: 400; cursor: pointer; touch-action: manipulation; user-select: none;
}
.key:active { background: var(--border); }
.key.del { background: transparent; font-size: 24px; color: var(--text-2); }
.pinpad.busy .keys { opacity: .4; pointer-events: none; }
.pin-foot { margin-top: 20px; }

/* bottom sheet (one-time codes) */
.sheet-backdrop { position: fixed; inset: 0; z-index: 20; background: rgb(0 0 0 / .4); display: flex; align-items: flex-end; justify-content: center; animation: fade .2s; }
.sheet {
  width: min(100%, 520px); background: var(--surface); border-radius: 24px 24px 0 0; text-align: center;
  padding: 10px 20px max(20px, env(safe-area-inset-bottom)); animation: up .25s ease-out;
}
.sheet h2 { font-size: 17px; margin: 8px 0 0; }
.sheet .code { font-size: 38px; font-weight: 700; letter-spacing: .14em; margin: 14px 0 6px; user-select: all; -webkit-user-select: all; }
.sheet p { font-size: 13.5px; margin: 0 0 4px; }
.grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

/* ------------------------------------------------------------ iPad and up */
@media (min-width: 768px) {
  .appbar h1 { font-size: 32px; }
  .tiles { grid-template-columns: repeat(4, 1fr); gap: 12px; }
  .tile { padding: 16px 18px; }
  .tile-value { font-size: 34px; }
  .home-grid, .report-grid, .me-grid { grid-template-columns: 1fr 1fr; align-items: start; }
  .trucks-grid { grid-template-columns: 1fr 1.5fr; align-items: start; }
  .phone-only { display: none !important; }
  .vbars { height: 190px; }
  .dchart { height: 220px; }
  .brow { grid-template-columns: minmax(0, 1fr) 76px 100px; }
  .tabbar { width: 440px; }
}
@media (min-width: 1100px) {
  .views { padding-left: 24px; padding-right: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
