:root {
  --bg: #f6f2ed;
  --surface: #ffffff;
  --surface-2: #eee8e0;
  --ink: #221a18;
  --ink-2: #6d605a;
  --line: #e4dcd2;
  --accent: #8c1d2c;
  --accent-ink: #ffffff;
  --accent-soft: #f3dcdf;
  --good: #1f7a4a;
  --bad: #b3261e;
  --warn: #8a5a00;
  --warn-bg: #fff1d0;
  --bar: #b4465a;
  --bar-2: #ecd5d9;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(40, 15, 15, .07), 0 6px 18px rgba(40, 15, 15, .05);
  --nav-h: 56px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #130f0e; --surface: #1e1817; --surface-2: #2a2220; --ink: #f4ece7; --ink-2: #b4a59d;
    --line: #342a27; --accent: #e0667a; --accent-ink: #1a0d10; --accent-soft: #3a1b21;
    --good: #5fcf91; --bad: #ff8c82; --warn: #f2c35b; --warn-bg: #3a2d10;
    --bar: #e0667a; --bar-2: #4a2a30; --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 -apple-system, system-ui, "SF Pro Text", "Segoe UI", Roboto, "Noto Sans Thai", sans-serif;
  -webkit-tap-highlight-color: transparent; overscroll-behavior-y: none;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; }
h3 { font-size: .78rem; color: var(--ink-2); font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.hide { display: none !important; }
.muted { color: var(--ink-2); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.row { display: flex; align-items: center; gap: 10px; }
.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.wrap { flex-wrap: wrap; }

/* ---- shell: iPhone first (safe areas, bottom tab bar) ---- */
.shell { min-height: 100dvh; padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); }
.topbar {
  position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px); backdrop-filter: saturate(1.4) blur(14px);
  padding: calc(8px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px;
}
.brand { font-weight: 800; letter-spacing: .02em; color: var(--accent); white-space: nowrap; }
.main { padding: 16px max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left)); max-width: 760px; margin: 0 auto; }
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(16px); backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: var(--nav-h); text-decoration: none; color: var(--ink-2); font-size: .68rem; font-weight: 600;
}
.nav a svg { width: 25px; height: 25px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.nav a.active { color: var(--accent); }
@media (min-width: 900px) {
  .shell { padding-bottom: 0; padding-left: 220px; }
  .nav { top: 0; right: auto; width: 220px; flex-direction: column; border-top: 0; border-right: 1px solid var(--line); padding: 20px 10px; gap: 4px; }
  .nav::before { content: "NTRR Sale"; font-weight: 800; color: var(--accent); font-size: 1.2rem; padding: 0 12px 16px; }
  .nav a { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; height: 44px; padding: 0 12px; border-radius: 10px; font-size: .95rem; }
  .nav a.active { background: var(--accent-soft); }
  .topbar .brand { display: none; }
  .main { padding: 24px; }
}

/* ---- components ---- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.hero { background: linear-gradient(150deg, var(--accent), color-mix(in srgb, var(--accent) 70%, #000)); color: var(--accent-ink); border: 0; }
.hero .muted { color: inherit; opacity: .8; }
.hero .big { font-size: 2.3rem; font-weight: 800; line-height: 1.1; letter-spacing: -.02em; }
.delta { font-size: .8rem; font-weight: 700; white-space: nowrap; }
.delta.up { color: var(--good); } .delta.down { color: var(--bad); }
.hero .delta.up, .hero .delta.down { color: inherit; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.stat .v { font-size: 1.35rem; font-weight: 750; letter-spacing: -.01em; }
.stat .l { font-size: .74rem; font-weight: 700; color: var(--ink-2); text-transform: uppercase; letter-spacing: .05em; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .74rem; font-weight: 700; background: var(--surface-2); color: var(--ink-2); }
.pill.good { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.notice { background: var(--warn-bg); color: var(--warn); border-radius: 12px; padding: 12px 14px; font-size: .92rem; }
.notice.bad { background: color-mix(in srgb, var(--bad) 14%, var(--surface)); color: var(--bad); }
.notice ul { margin: 4px 0 0; padding-left: 18px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 0 18px;
  border-radius: 14px; border: 1px solid var(--line); background: var(--surface); font-weight: 700; cursor: pointer; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.primary.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.sm { min-height: 40px; padding: 0 14px; font-size: .9rem; border-radius: 12px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:focus-visible, input:focus-visible, select:focus-visible, .nav a:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

label.field { display: flex; flex-direction: column; gap: 5px; font-size: .85rem; font-weight: 600; color: var(--ink-2); }
/* 16px+ inputs so iOS Safari never zooms the page on focus */
input[type=text], input[type=password], input[type=number], input[type=search], select {
  min-height: 48px; padding: 0 12px; border-radius: 14px; border: 1px solid var(--line); background: var(--surface); width: 100%; font-size: 16px;
}
select.sm { min-height: 40px; border-radius: 12px; }
input.cell { min-height: 40px; padding: 0 8px; text-align: right; width: 130px; border-radius: 10px; }

.seg { display: flex; background: var(--surface-2); border-radius: 13px; padding: 3px; gap: 3px; }
.seg button { flex: 1; min-height: 40px; border: 0; background: transparent; border-radius: 10px; font-weight: 700; color: var(--ink-2); cursor: pointer; padding: 0 10px; white-space: nowrap; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 16px 4px; margin: 0 -16px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; min-height: 38px; padding: 0 14px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); font-weight: 650; font-size: .9rem; cursor: pointer; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.list { display: flex; flex-direction: column; }
.list > .li { padding: 12px 0; border-top: 1px solid var(--line); }
.list > .li:first-child { border-top: 0; padding-top: 0; }
.list > .li:last-child { padding-bottom: 0; }
.name { font-weight: 650; overflow-wrap: anywhere; }
.bar { height: 8px; border-radius: 99px; background: var(--bar-2); overflow: hidden; margin-top: 7px; }
.bar > i { display: block; height: 100%; background: var(--bar); border-radius: 99px; }
.split { display: flex; height: 14px; border-radius: 99px; overflow: hidden; background: var(--bar-2); }
.split > i { display: block; height: 100%; background: var(--bar); }
.split > i + i { background: var(--ink-2); opacity: .55; }
details.li > summary { list-style: none; cursor: pointer; }
details.li > summary::-webkit-details-marker { display: none; }
.sub { margin: 10px 0 0 4px; padding-left: 12px; border-left: 3px solid var(--bar-2); display: flex; flex-direction: column; gap: 10px; }
.chev { display: inline-block; transition: transform .15s; color: var(--ink-2); margin-right: 4px; }
details[open] > summary .chev { transform: rotate(90deg); }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-2); padding: 6px 8px; border-bottom: 1px solid var(--line); }
td { padding: 9px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
td.r, th.r { text-align: right; }
tr:last-child td { border-bottom: 0; }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--ink-2); font-size: 11px; }
.chart .bar-rect { fill: var(--bar); opacity: .55; }
.chart .bar-rect.last { fill: var(--accent); opacity: 1; }
.chart .grid { stroke: var(--line); stroke-width: 1; }

.drop { border: 2px dashed var(--line); border-radius: var(--radius); padding: 22px 16px; text-align: center; background: var(--surface); }
.drop input[type=file] { display: none; }
.filechip { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; background: var(--surface-2); border-radius: 12px; font-size: .9rem; }
.filechip > span { overflow: hidden; text-overflow: ellipsis; }

.modal-bg { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--surface); width: 100%; max-width: 520px; border-radius: 22px 22px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); max-height: 92dvh; overflow: auto; animation: up .22s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 900px) { .modal-bg { align-items: center; } .modal { border-radius: 22px; } }

.login { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login .card { width: 100%; max-width: 380px; }
.logo { width: 56px; height: 56px; border-radius: 14px; }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 14px; opacity: 0; pointer-events: none;
  transition: .2s; z-index: 80; font-weight: 600; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
@media (min-width: 900px) { #toast { bottom: 24px; } }
.sticky-actions { position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 10px); z-index: 10; }
@media (min-width: 900px) { .sticky-actions { bottom: 8px; } }
.spinner { padding: 40px; text-align: center; color: var(--ink-2); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

#acct { max-width: 112px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: none; }
#monthSel { min-width: 0; font-weight: 700; }
@media (max-width: 480px) { .topbar .brand { display: none; } }

/* compact 5-column tables on phones */
table.tight { font-size: .8rem; }
table.tight th, table.tight td { padding: 8px 3px; white-space: nowrap; }
table.tight th { font-size: .66rem; letter-spacing: .02em; }
table.tight .delta { font-size: .72rem; }
table.tight th { white-space: normal; line-height: 1.15; vertical-align: bottom; }
