:root {
  --bg: #f6f5f2; --card: #ffffff; --ink: #1d1d1b; --ink2: #52514e; --muted: #8a8984; --line: #e4e3de;
  --accent: #1f3a5f; --accent-ink: #ffffff; --accent-soft: #e6edf6;
  --good: #1a7f4b; --good-soft: #e3f4ea; --warn: #9a6400; --warn-soft: #fdf1d8; --bad: #b42318; --bad-soft: #fde8e6;
  --info: #2a62a8; --info-soft: #e5eefa;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4;
  --radius: 14px; --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413; --card: #1e1e1c; --ink: #f0efec; --ink2: #c3c2b7; --muted: #8f8e88; --line: #33332f;
    --accent: #7fa6d6; --accent-ink: #10213a; --accent-soft: #1f2c3d;
    --good: #5cc98d; --good-soft: #17301f; --warn: #e5b04a; --warn-soft: #33280f; --bad: #f2827a; --bad-soft: #3a1a17;
    --info: #86b6ef; --info-soft: #182a40;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; -webkit-text-size-adjust: 100%; }
body { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.boot { padding: 40vh 0; text-align: center; color: var(--muted); }

/* top bar */
.top { position: sticky; top: 0; z-index: 20; background: var(--bg); padding: calc(10px + env(safe-area-inset-top)) 16px 8px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid transparent; }
.top.scrolled { border-bottom-color: var(--line); }
.top h1 { font-size: 20px; margin: 0; flex: 1; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .back { border: 0; background: none; font-size: 24px; line-height: 1; padding: 4px 8px 4px 0; color: var(--accent); cursor: pointer; }
.iconbtn { border: 1px solid var(--line); background: var(--card); border-radius: 999px; height: 36px; min-width: 36px; padding: 0 12px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer; font-size: 14px; }
.wrap { padding: 4px 16px 24px; max-width: 860px; margin: 0 auto; }

/* bottom nav */
.nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.nav a { flex: 1; height: var(--nav-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px; position: relative; }
.nav a.on { color: var(--accent); font-weight: 600; }
.nav svg { width: 24px; height: 24px; }
.nav .dot { position: absolute; top: 8px; left: calc(50% + 6px); background: var(--bad); color: #fff; font-size: 10px; border-radius: 999px; min-width: 17px; height: 17px; display: flex; align-items: center; justify-content: center; padding: 0 4px; font-weight: 700; }

/* cards & lists */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 10px 0; }
.card h3 { margin: 0 0 8px; font-size: 15px; }
.list { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.row { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: block; text-decoration: none; color: inherit; cursor: pointer; }
.row:active { background: var(--accent-soft); }
.row .l1 { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.row .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.row .s { color: var(--ink2); font-size: 13px; margin-top: 2px; }
.row .amt { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row.overdue { border-left: 4px solid var(--bad); }
.row.soon { border-left: 4px solid var(--warn); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 36px 10px; }
.sec-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 20px 2px 6px; font-weight: 600; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 4px 10px; font-size: 14px; }
.kv div:nth-child(odd) { color: var(--muted); }
.flex { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--accent-soft); color: var(--accent); }
.b-pending { background: var(--accent-soft); color: var(--accent); }
.b-in_production { background: var(--info-soft); color: var(--info); }
.b-ready { background: var(--good-soft); color: var(--good); }
.b-in_transit { background: var(--warn-soft); color: var(--warn); }
.b-delivered { background: var(--good-soft); color: var(--good); }
.b-cancelled, .b-declined, .b-expired { background: var(--line); color: var(--ink2); }
.b-bad { background: var(--bad-soft); color: var(--bad); }
.b-warn { background: var(--warn-soft); color: var(--warn); }
.b-good { background: var(--good-soft); color: var(--good); }
.b-draft { background: var(--line); color: var(--ink2); }
.b-sent { background: var(--info-soft); color: var(--info); }
.b-accepted { background: var(--good-soft); color: var(--good); }

/* chips */
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* forms */
label.f { display: block; margin: 10px 0; }
label.f > span { display: block; font-size: 13px; color: var(--ink2); margin-bottom: 4px; }
input, select, textarea { width: 100%; border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 10px 12px; font-size: 16px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { min-height: 72px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: auto; }
input[type=date] { min-width: 0; -webkit-appearance: none; appearance: none; min-height: 44px; }
.grid2 > *, .grid3 > * { min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 10px; }
@media (max-width: 380px) { .grid3 { grid-template-columns: 1fr 1fr; } }
.search { position: relative; margin: 6px 0; }
.search input { padding-left: 36px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 18px; color: var(--muted); }

/* buttons */
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 11px 16px; font-weight: 600; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; color: var(--ink); font-size: 15px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.good { background: var(--good); border-color: var(--good); color: #fff; }
.btn.warn { background: var(--warn); border-color: var(--warn); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 7px 12px; font-size: 13px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.btns .btn { flex: 1; min-width: 120px; }
.fab { position: fixed; right: 18px; bottom: calc(var(--nav-h) + 18px + env(safe-area-inset-bottom)); z-index: 25; background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 999px; height: 54px; padding: 0 20px; font-weight: 700; font-size: 15px; box-shadow: 0 6px 18px rgba(0,0,0,.18); display: flex; align-items: center; gap: 6px; cursor: pointer; }
.savebar { position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); background: var(--bg); padding: 10px 0; border-top: 1px solid var(--line); margin-top: 16px; display: flex; gap: 8px; z-index: 5; }
.savebar .btn { flex: 1; }

/* line editor */
.line { border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin: 8px 0; background: var(--card); }
.line .lh { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.line .lt { font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
.line .x { border: 0; background: none; color: var(--bad); font-size: 13px; cursor: pointer; padding: 2px 4px; }
.line label.f { margin: 6px 0; }
.link-tag { font-size: 12px; color: var(--good); margin-top: 3px; }
.totals { margin-top: 10px; }
.totals div { display: flex; justify-content: space-between; padding: 3px 0; font-variant-numeric: tabular-nums; }
.totals .grand { font-size: 18px; font-weight: 700; border-top: 1px solid var(--line); padding-top: 8px; margin-top: 4px; }

/* table */
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 8px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl .r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.scroll-x { overflow-x: auto; }

/* sheet (modal) */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.42); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { background: var(--bg); width: 100%; max-width: 620px; max-height: 92vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 16px 16px calc(18px + env(safe-area-inset-bottom)); animation: up .2s; }
@media (min-width: 700px) { .sheet-bg { align-items: center; } .sheet { border-radius: 18px; } }
.sheet h2 { margin: 2px 0 10px; font-size: 18px; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }

#toast { position: fixed; left: 50%; bottom: calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 80; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }
#toast.err { background: var(--bad); color: #fff; }

/* login */
.login { max-width: 360px; margin: 0 auto; padding: calc(40px + env(safe-area-inset-top)) 20px 20px; text-align: center; }
.login img.logo { max-width: 160px; max-height: 80px; margin-bottom: 10px; }
.login h1 { font-size: 22px; margin: 6px 0 2px; }
.pin-dots { display: flex; justify-content: center; gap: 12px; margin: 18px 0; }
.pin-dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--accent); }
.pin-dots i.on { background: var(--accent); }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 280px; margin: 0 auto; }
.keypad button { height: 60px; border-radius: 16px; border: 1px solid var(--line); background: var(--card); font-size: 22px; font-weight: 600; cursor: pointer; }
.keypad button:active { background: var(--accent-soft); }

/* attention */
.att-item { background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--bad); border-radius: 12px; padding: 12px; margin: 8px 0; }
.att-item.today { border-left-color: var(--warn); }
.att-item .btns { margin-top: 8px; }
.att-item .btns .btn { min-width: 0; }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 10px 0; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.tile .k { font-size: 12px; color: var(--muted); }
.tile .v { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; letter-spacing: -0.01em; }
.tile .d { font-size: 12px; color: var(--ink2); margin-top: 2px; }
.tile.hero { grid-column: span 2; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.tile.hero .k, .tile.hero .d { color: inherit; opacity: .85; }
.tile.hero .v { font-size: 28px; }
.neg { color: var(--bad); }

/* charts */
.chart { position: relative; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--bg); font-size: 12px; padding: 7px 9px; border-radius: 8px; white-space: nowrap; opacity: 0; transition: opacity .1s; z-index: 3; }
.chart .tip b { font-weight: 700; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink2); margin: 6px 0; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.share { display: flex; height: 18px; border-radius: 6px; overflow: hidden; gap: 2px; margin: 8px 0; background: var(--card); }
.share div { height: 100%; }
.share div:first-child { border-radius: 4px 0 0 4px; }
.share div:last-child { border-radius: 0 4px 4px 0; }

.banner { background: var(--info-soft); color: var(--info); border-radius: 12px; padding: 10px 12px; font-size: 14px; margin: 8px 0; display: flex; gap: 10px; align-items: center; }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.banner .btn { margin-left: auto; }
.img-prev { max-width: 180px; max-height: 90px; display: block; margin: 6px 0; border: 1px dashed var(--line); border-radius: 8px; padding: 4px; background: #fff; }
.menu a { display: flex; align-items: center; gap: 12px; padding: 14px; text-decoration: none; color: var(--ink); border-bottom: 1px solid var(--line); }
.menu a:last-child { border-bottom: 0; }
.menu a span.d { margin-left: auto; color: var(--muted); font-size: 13px; }
.menu { padding: 0; overflow: hidden; }
