:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --border: #e7e2da;
  --text: #1f2421;
  --muted: #6b716c;
  --faint: #9aa09b;
  --accent: #1f6f5c;
  --accent-soft: #e3f0ec;
  --pos: #1f8a4c;
  --pos-soft: #e5f4ea;
  --neg: #c2412d;
  --neg-soft: #fbe9e5;
  --warn: #b7791f;
  --warn-soft: #fbf1df;
  --shadow: 0 1px 2px rgba(31, 36, 33, .06), 0 4px 16px rgba(31, 36, 33, .04);
  --radius: 14px;
  --radius-sm: 9px;
  --nav-w: 232px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121513;
    --surface: #1a1e1c;
    --surface-2: #202522;
    --border: #2c322f;
    --text: #e8ebe9;
    --muted: #a0a7a2;
    --faint: #6f7772;
    --accent: #4fb897;
    --accent-soft: #1d3a31;
    --pos: #4cc27f;
    --pos-soft: #18311f;
    --neg: #f0735c;
    --neg-soft: #3a1f1a;
    --warn: #e2a64a;
    --warn-soft: #382c17;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121513;
  --surface: #1a1e1c;
  --surface-2: #202522;
  --border: #2c322f;
  --text: #e8ebe9;
  --muted: #a0a7a2;
  --faint: #6f7772;
  --accent: #4fb897;
  --accent-soft: #1d3a31;
  --pos: #4cc27f;
  --pos-soft: #18311f;
  --neg: #f0735c;
  --neg-soft: #3a1f1a;
  --warn: #e2a64a;
  --warn-soft: #382c17;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.num { text-align: right; white-space: nowrap; }

/* ---------- login ---------- */
.login {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 32px 28px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: -.01em; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 10px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 17px;
}
.login-card h1 { font-size: 15px; font-weight: 500; color: var(--muted); margin: 10px 0 24px; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 12px; color: var(--muted); }
.input, select.input {
  width: 100%; border: 1px solid var(--border); background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 10px 12px; outline: none; min-height: 40px;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius-sm);
  padding: 8px 14px; cursor: pointer; min-height: 38px; display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent); filter: brightness(1.06); }
.btn-block { width: 100%; justify-content: center; min-height: 44px; }
.btn-sm { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.form-error { color: var(--neg); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: var(--nav-w) 1fr; min-height: 100dvh; }
.sidenav {
  position: sticky; top: 0; height: 100dvh; padding: 20px 14px; border-right: 1px solid var(--border);
  background: var(--surface); display: flex; flex-direction: column; gap: 4px;
}
.sidenav .brand { padding: 4px 8px 18px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none; cursor: pointer; border: 0; background: none; width: 100%; text-align: left;
}
.nav-item svg { width: 18px; height: 18px; flex: none; }
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-spacer { flex: 1; }
.nav-user { font-size: 12px; color: var(--faint); padding: 8px 10px; }

main { min-width: 0; padding: 20px 28px 48px; }
.topbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.topbar h2 { margin: 0; font-size: 22px; letter-spacing: -.02em; margin-right: auto; }
.periods { display: flex; gap: 4px; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 3px; }
.chip {
  border: 0; background: none; padding: 6px 11px; border-radius: 9px; cursor: pointer; color: var(--muted);
  white-space: nowrap; min-height: 32px;
}
.chip:hover { color: var(--text); }
.chip.active { background: var(--accent); color: #fff; font-weight: 600; }
.custom-range { display: flex; gap: 6px; align-items: center; }
.custom-range .input { width: 140px; min-height: 34px; padding: 5px 8px; }
.updated { font-size: 12px; color: var(--faint); white-space: nowrap; }
.banner {
  border-radius: var(--radius-sm); padding: 10px 14px; margin-bottom: 14px; font-size: 13px;
  display: flex; gap: 8px; align-items: center;
}
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.err { background: var(--neg-soft); color: var(--neg); }

/* ---------- tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.tiles.small { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); min-width: 0; cursor: default; text-align: left; position: relative;
}
.tile.clickable { cursor: pointer; }
.tile.clickable:hover { border-color: var(--accent); }
.tile-label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.tile-value { font-size: 24px; font-weight: 700; letter-spacing: -.02em; margin-top: 6px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.tiles.small .tile-value { font-size: 18px; }
.tile-sub { font-size: 12px; color: var(--faint); margin-top: 2px; }
.delta { font-size: 12px; font-weight: 600; white-space: nowrap; }
.delta.up { color: var(--pos); }
.delta.down { color: var(--neg); }
.delta.flat { color: var(--faint); }
.tile-flag { position: absolute; top: 10px; right: 12px; width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.breakdown {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px;
  margin-bottom: 12px; box-shadow: var(--shadow);
}
.breakdown h4 { margin: 0 0 8px; font-size: 13px; }
.breakdown-row { display: flex; justify-content: space-between; padding: 5px 0; border-top: 1px dashed var(--border); font-size: 13px; }
.breakdown-row:first-of-type { border-top: 0; }

/* ---------- cards / panels ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 18px; margin-bottom: 12px; min-width: 0;
}
.panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.panel-head h3 { margin: 0; font-size: 15px; margin-right: auto; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 5px 10px; cursor: pointer; color: var(--muted); font-size: 13px; }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chart-box { position: relative; height: 260px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ---------- tables ---------- */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar .input { max-width: 320px; min-height: 36px; }
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); position: sticky; top: 0;
  white-space: nowrap; user-select: none; }
th.sortable { cursor: pointer; }
th.sortable:hover { color: var(--text); }
th .arrow { font-size: 10px; margin-left: 3px; }
tbody tr { cursor: pointer; }
tbody tr:hover td { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.prod-cell { display: flex; gap: 10px; align-items: center; min-width: 260px; max-width: 420px; }
.thumb { width: 40px; height: 40px; border-radius: 8px; object-fit: contain; background: #fff; border: 1px solid var(--border); flex: none; }
.thumb.ph { display: grid; place-items: center; color: var(--faint); font-size: 11px; }
.prod-title { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.3; }
.prod-ids { font-size: 11px; color: var(--faint); margin-top: 2px; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; margin-left: 4px; vertical-align: 1px; }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.neg { background: var(--neg-soft); color: var(--neg); }
.tag.pos { background: var(--pos-soft); color: var(--pos); }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 13px; color: var(--muted); }
.cards { display: none; }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); }

/* ---------- product detail ---------- */
.drawer-back { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(760px, 100%); background: var(--bg); z-index: 41;
  overflow-y: auto; padding: 20px 24px 40px; box-shadow: -8px 0 32px rgba(0,0,0,.15);
}
.drawer-head { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 16px; }
.drawer-head img, .drawer-head .thumb { width: 72px; height: 72px; }
.drawer-head h3 { margin: 0 0 4px; font-size: 16px; line-height: 1.3; }
.close-btn { margin-left: auto; flex: none; }

/* ---------- mobile ---------- */
.bottomnav { display: none; }
.sheet { display: none; }

@media (max-width: 1180px) {
  .tiles.small { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidenav { display: none; }
  main { padding: 14px 16px calc(84px + env(safe-area-inset-bottom)); }
  .topbar h2 { font-size: 20px; width: 100%; }
  .periods { width: 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .periods::-webkit-scrollbar { display: none; }
  .tiles, .tiles.small { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile { padding: 12px 13px; }
  .tile-value { font-size: 20px; }
  .tiles.small .tile-value { font-size: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .chart-box { height: 220px; }
  .table-wrap.responsive { display: none; }
  .cards { display: grid; gap: 8px; }
  .card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px;
    display: grid; grid-template-columns: 44px 1fr; gap: 10px; cursor: pointer;
  }
  .card .thumb { width: 44px; height: 44px; }
  .card-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px 10px; margin-top: 8px; font-size: 12px; }
  .card-stats b { display: block; font-size: 14px; }
  .toolbar .input { max-width: none; flex: 1 1 100%; }
  .toolbar .seg { max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  .toolbar .seg::-webkit-scrollbar { display: none; }
  .toolbar .seg button { white-space: nowrap; padding: 7px 11px; }
  .drawer { padding: 16px 16px 40px; }
  .bottomnav {
    display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottomnav button {
    border: 0; background: none; display: grid; justify-items: center; gap: 2px; font-size: 11px; color: var(--muted);
    padding: 4px 0; cursor: pointer; min-height: 48px;
  }
  .bottomnav button svg { width: 22px; height: 22px; }
  .bottomnav button.active { color: var(--accent); font-weight: 600; }
  .sheet.open {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 42; background: var(--surface);
    border-radius: 18px 18px 0 0; padding: 12px 12px calc(16px + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 32px rgba(0,0,0,.18);
  }
  .sheet .nav-item { padding: 13px 12px; font-size: 15px; }
}

/* ---------- all costs: drill-down ---------- */
.cost-row { cursor: pointer; }
.drill > td { background: var(--surface-2); padding: 0 8px 10px; }
.drill-box { max-height: 420px; overflow: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); }
.drill-box th { position: sticky; top: 0; }
.drill-box tbody tr { cursor: default; }

/* ---------- returns & claims ---------- */
.claim-grid { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: start; }
.claim-text { margin-top: 10px; display: grid; gap: 8px; }
.claim-text textarea { font-size: 12px; line-height: 1.4; resize: vertical; }

/* install-on-phone instructions */
.install-dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 20px; width: min(420px, calc(100vw - 32px)); }
.install-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.install-dialog h3 { margin: 0 0 8px; }
.install-dialog ol { padding-left: 20px; line-height: 1.6; }
.install-dialog .muted { font-size: 13px; }

/* removal parcels: what to watch goes first */
.parcels, .parcels-done { margin-top: 10px; }
.parcel { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 8px; background: var(--surface-2); }
.parcel.late { border-color: var(--warn); background: var(--warn-soft); }
.parcel.done { opacity: .7; }
.parcel-main { min-width: 0; flex: 1; }
.parcel-track { font-weight: 600; font-size: 15px; word-break: break-all; }
.parcel details, .parcel > .parcel-main > div { font-size: 12px; }
.parcel summary, .parcels-done > summary, .removal-items > summary { cursor: pointer; font-size: 13px; }
.parcels-done > summary, .removal-items > summary { margin: 8px 0; color: var(--muted, inherit); }
