:root {
  --bg: #f5f6f8; --panel: #ffffff; --ink: #1d2330; --muted: #6b7385; --line: #e3e6ec;
  --accent: #1f6feb; --accent-soft: #e8f0fe;
  --in: #1a7f37; --out: #c62828;
  --forecast: #8a5a00; --forecast-bg: #fff4dc;
  --actual: #1f4e99; --actual-bg: #e5eefc;
  --matched: #1a7f37; --matched-bg: #e3f5e8;
  --overdue: #b42318; --overdue-bg: #fde7e5;
  --cancel: #8b8f99; --cancel-bg: #eeeff2;
  --store: #0f766e; --wholesale: #7c3aed; --shared: #64748b;
  --radius: 10px; --shadow: 0 1px 3px rgba(20,30,50,.08);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b; --panel: #1b2029; --ink: #e6e9ef; --muted: #98a0b0; --line: #2c3340;
    --accent: #5b9bff; --accent-soft: #1d2b44;
    --in: #4cc777; --out: #ff7a70;
    --forecast: #f0b44c; --forecast-bg: #3a2d12; --actual: #8db8ff; --actual-bg: #1d2b44;
    --matched: #4cc777; --matched-bg: #173322; --overdue: #ff8a80; --overdue-bg: #3d1a18;
    --cancel: #7d8494; --cancel-bg: #262b35; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink);
  font: 14px/1.45 "Segoe UI", "Rubik", "Arial Hebrew", Arial, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

header { position: sticky; top: 0; z-index: 20; background: var(--panel); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 12px; padding: 8px 16px; flex-wrap: wrap; }
header h1 { font-size: 17px; margin: 0; white-space: nowrap; }
header h1 small { color: var(--muted); font-weight: 400; }
nav.tabs { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
nav.tabs button { border: 0; background: none; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
nav.tabs button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.actor { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.actor input { width: 90px; }

main { padding: 14px 16px 60px; max-width: 1500px; margin: 0 auto; }
section.screen { display: none; } section.screen.on { display: block; }

.pills { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: thin; }
.pill { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 5px 12px;
  cursor: pointer; white-space: nowrap; font-size: 13px; }
.pill.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.pill .b { opacity: .8; margin-inline-start: 6px; font-size: 12px; }
.pill.inactive { opacity: .55; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; margin-bottom: 14px; }
.card h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.card h2 .sp { flex: 1; }
.grid { display: grid; gap: 14px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.two { grid-template-columns: 1fr 1fr; }
@media (max-width: 900px) { .two { grid-template-columns: 1fr; } }
.kpi .t { color: var(--muted); font-size: 13px; }
.kpi .v { font-size: 22px; font-weight: 700; margin: 2px 0; }
.kpi .s { font-size: 12px; color: var(--muted); }
.kpi.store { border-top: 3px solid var(--store); } .kpi.wholesale { border-top: 3px solid var(--wholesale); }
.kpi.shared { border-top: 3px solid var(--shared); } .kpi.total { border-top: 3px solid var(--accent); }
.kpi .low { color: var(--overdue); }

.num { direction: ltr; unicode-bidi: isolate; display: inline-block; font-variant-numeric: tabular-nums; }
.pos { color: var(--in); } .neg { color: var(--out); }
.muted { color: var(--muted); } .small { font-size: 12px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.toolbar .sp { flex: 1; }
input[type=text], input[type=search], input[type=number], input[type=date], select, textarea {
  border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 6px 8px; min-height: 32px; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }
button.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 6px 12px; cursor: pointer; min-height: 32px; }
button.btn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.danger { color: var(--out); border-color: var(--out); }
button.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 2px 4px; }
button:disabled { opacity: .5; cursor: default; }

.tablewrap { overflow: auto; max-height: calc(100vh - 230px); border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: start; vertical-align: middle; white-space: nowrap; }
th { position: sticky; top: 0; background: var(--panel); z-index: 2; font-weight: 600; color: var(--muted); font-size: 12.5px; }
td.wrap { white-space: normal; min-width: 180px; }
tr.sel td { background: var(--accent-soft); }
tr.cancelled td { color: var(--cancel); text-decoration: line-through; }
tr.overdue td:first-child { box-shadow: inset -3px 0 0 var(--overdue); }
tr.forecast td { background-image: linear-gradient(transparent, transparent); }
td input.cell, td select.cell { border: 1px solid transparent; background: transparent; padding: 3px 5px; min-height: 26px; width: 100%; }
td input.cell:hover, td select.cell:hover { border-color: var(--line); }
td input.cell:focus, td select.cell:focus { border-color: var(--accent); background: var(--panel); }
td input.amt { width: 110px; direction: ltr; text-align: end; }
td input.dt { width: 132px; }
td select.cell { min-width: 120px; max-width: 200px; }
tfoot td { font-weight: 700; position: sticky; bottom: 0; background: var(--panel); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid transparent; }
.badge.forecast { color: var(--forecast); background: var(--forecast-bg); border-style: dashed; border-color: var(--forecast); }
.badge.actual { color: var(--actual); background: var(--actual-bg); }
.badge.matched { color: var(--matched); background: var(--matched-bg); }
.badge.cancelled { color: var(--cancel); background: var(--cancel-bg); }
.badge.overdue { color: var(--overdue); background: var(--overdue-bg); border-color: var(--overdue); }
.badge.partial { color: var(--forecast); background: transparent; border-color: var(--forecast); font-weight: 500; }
.badge.link { color: var(--accent); background: var(--accent-soft); font-weight: 500; }
.div-store { color: var(--store); } .div-wholesale { color: var(--wholesale); } .div-shared { color: var(--shared); }

.bulkbar { display: none; position: sticky; bottom: 10px; z-index: 5; background: var(--ink); color: var(--panel);
  border-radius: var(--radius); padding: 8px 12px; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
.bulkbar.on { display: flex; }
.bulkbar select, .bulkbar button { color: var(--ink); }

/* matching */
.match { display: grid; grid-template-columns: minmax(320px, 1fr) minmax(360px, 1.2fr); gap: 14px; }
@media (max-width: 900px) { .match { grid-template-columns: 1fr; } }
.list { max-height: calc(100vh - 250px); overflow: auto; }
.item { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 6px; cursor: pointer; background: var(--panel);
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; }
.item:hover { border-color: var(--accent); }
.item.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.item .d { color: var(--muted); font-size: 12px; }
.item .a { font-weight: 700; text-align: end; }
.sugg { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; }
.sugg .k { width: 26px; height: 26px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; flex: none; }
.sugg .body { flex: 1; min-width: 0; }
.sugg .score { font-size: 12px; color: var(--muted); }
.kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-size: 11px; color: var(--muted); }

/* forms / drawer */
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.form label { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--muted); }
.form label.wide { grid-column: 1 / -1; }
.form .actions { grid-column: 1 / -1; display: flex; gap: 8px; justify-content: flex-start; }
dialog { border: 1px solid var(--line); border-radius: 12px; padding: 0; width: min(760px, calc(100vw - 32px)); background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(10,15,25,.45); }
dialog .hd { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
dialog .hd h3 { margin: 0; font-size: 16px; flex: 1; }
dialog .bd { padding: 14px 16px; max-height: 70vh; overflow: auto; }

.toast { position: fixed; bottom: 16px; inset-inline-start: 16px; z-index: 100; background: var(--ink); color: var(--panel);
  padding: 9px 14px; border-radius: 8px; opacity: 0; transform: translateY(10px); transition: .2s; pointer-events: none; max-width: 80vw; }
.toast.on { opacity: 1; transform: none; }
.toast.err { background: var(--out); color: #fff; }

.subtabs { display: flex; gap: 4px; margin-bottom: 10px; flex-wrap: wrap; }
.subtabs button { border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 5px 10px; cursor: pointer; }
.subtabs button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chartbox { position: relative; height: 300px; }
.empty { color: var(--muted); padding: 18px; text-align: center; }
.ok { color: var(--in); } .bad { color: var(--out); font-weight: 600; }

@media (max-width: 640px) {
  main { padding: 10px 16px 60px; }
  header { padding: 8px 12px; }
  nav.tabs button { padding: 6px 8px; }
  .kpi .v { font-size: 19px; }
  .tablewrap { max-height: none; }
  .hide-sm { display: none; }
}
#setBody td input.cell[type=text] { min-width: 120px; }
#setBody td input.cell[type=number] { min-width: 90px; }

/* v0.2: auth warning, connectors */
.warnbar { background: var(--forecast-bg); color: var(--forecast); padding: 6px 16px; font-size: 13px; border-bottom: 1px solid var(--line); }
.warnbar code { direction: ltr; unicode-bidi: embed; }
.conn-summary { margin: 4px 0; }
.conn-path { direction: ltr; text-align: right; word-break: break-all; }
pre.json { direction: ltr; text-align: left; white-space: pre-wrap; font-size: 12px; max-height: 60vh; overflow: auto; }

button.lazysel { border: 1px solid transparent; background: transparent; padding: 3px 5px; min-height: 26px; width: 100%; min-width: 120px; max-width: 200px; text-align: start; font: inherit; color: inherit; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
button.lazysel:hover:not([disabled]) { border-color: var(--line); }
