:root {
  --bg: #F4F6F2;
  --surface: #FFFFFF;
  --surface-2: #EDF1EA;
  --ink: #1B2520;
  --muted: #64716A;
  --line: #D6DDD5;
  --line-strong: #B8C3B9;
  --accent: #2F7A4E;
  --accent-soft: #DDEFE3;
  --accent-ink: #FFFFFF;
  --warn: #B7791F;
  --warn-soft: #FBF1DC;
  --danger: #B0413E;
  --danger-soft: #F9E3E1;
  --head: #E3EBE2;
  --ro: #F3F5F1;
  --focus: #2F7A4E;
  --shadow: 0 8px 24px rgba(20, 40, 28, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131714; --surface: #1B211D; --surface-2: #222A25; --ink: #E6EBE6; --muted: #97A49B;
    --line: #2E3731; --line-strong: #46524A; --accent: #5FBB82; --accent-soft: #1F3A2B; --accent-ink: #0F1A13;
    --warn: #E0A745; --warn-soft: #3A2E15; --danger: #E27470; --danger-soft: #3D1F1E; --head: #202823; --ro: #1F2622;
    --focus: #5FBB82; --shadow: 0 8px 24px rgba(0,0,0,.45); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #131714; --surface: #1B211D; --surface-2: #222A25; --ink: #E6EBE6; --muted: #97A49B;
  --line: #2E3731; --line-strong: #46524A; --accent: #5FBB82; --accent-soft: #1F3A2B; --accent-ink: #0F1A13;
  --warn: #E0A745; --warn-soft: #3A2E15; --danger: #E27470; --danger-soft: #3D1F1E; --head: #202823; --ro: #1F2622;
  --focus: #5FBB82; --shadow: 0 8px 24px rgba(0,0,0,.45); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column; min-height: 100%;
}
button, input { font: inherit; color: inherit; }
code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .95em; background: var(--surface-2); padding: 0 .35em; border-radius: 4px; }

/* ---------------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); position: relative; flex: none; }
.brand-mark::after { content: ""; position: absolute; inset: 8px 7px; border: 2px solid var(--accent-ink); border-radius: 2px; border-top-width: 5px; opacity: .9; }
.brand-name { font-weight: 650; letter-spacing: .01em; }
.brand-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 42vw; }
.cuma-chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px;
  background: var(--accent-soft); color: var(--ink); font-size: 13px; font-variant-numeric: tabular-nums;
}
.chip-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.chip-sep { width: 1px; height: 14px; background: var(--line-strong); }
.actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.btn {
  appearance: none; border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; line-height: 1.2; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); }
.btn.icon { width: 36px; padding: 7px 0; text-align: center; font-weight: 700; }
.btn:focus-visible, input:focus-visible, .menu-item:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

.menu { position: relative; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 280px; z-index: 30;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px;
  display: flex; flex-direction: column;
}
.menu-item { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 7px; background: none; border: 0; text-align: left; cursor: pointer; width: 100%; }
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }
.menu-item input { margin: 0; accent-color: var(--accent); }

/* ---------------- main */
main { flex: 1; display: flex; flex-direction: column; padding: 16px; padding-block: 16px calc(16px + env(safe-area-inset-bottom, 0px)); gap: 16px; }

.dropzone {
  flex: 1; display: grid; place-items: center; min-height: 60vh;
  border: 2px dashed var(--line-strong); border-radius: 16px; background: var(--surface); transition: background .15s, border-color .15s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.drop-inner { max-width: 560px; text-align: center; padding: 32px 20px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.drop-icon { color: var(--accent); }
.drop-inner h1 { font-size: 20px; margin: 0; font-weight: 650; text-wrap: balance; }
.drop-inner p { margin: 0; color: var(--muted); max-width: 60ch; }
.hint { font-size: 12px; }

.sheet { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.sheet-tools { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.sheet-stats { color: var(--muted); font-size: 13px; display: flex; gap: 14px; flex-wrap: wrap; }
.sheet-stats b { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.sheet-stats .warn { color: var(--warn); }
.sheet-tools-right { margin-left: auto; }
.sheet-foot { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.foot-hint { color: var(--muted); font-size: 12px; }

/* ---------------- grid */
.grid-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); max-height: calc(100vh - 210px); }
.grid { border-collapse: separate; border-spacing: 0; min-width: max-content; font-variant-numeric: tabular-nums; }
.grid th, .grid td { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 0; vertical-align: middle; }
.grid th {
  position: sticky; top: 0; z-index: 5; background: var(--head); color: var(--ink); font-weight: 600; font-size: 12px; letter-spacing: .01em;
  text-align: left; padding: 8px 8px; white-space: nowrap;
}
.grid th.num, .grid td.num input { text-align: right; }
.grid th.hid { color: var(--muted); font-style: italic; }
.grid td.idx, .grid th.idx { box-shadow: inset -1px 0 0 var(--line-strong); position: sticky; left: 0; z-index: 4; background: var(--head); color: var(--muted); font-size: 11px; text-align: right; padding: 0 8px; min-width: 42px; }
.grid th.idx { z-index: 6; }
.grid td input { width: 100%; border: 0; background: transparent; padding: 7px 8px; min-height: 34px; border-radius: 0; }
.grid td input:focus { outline: 2px solid var(--focus); outline-offset: -2px; background: var(--surface); }
.grid td.ro { background: var(--ro); color: var(--muted); padding: 7px 8px; font-size: 12px; white-space: nowrap; }
.grid td input:disabled { background: var(--ro); color: var(--muted); }
.grid td.invalid input { background: var(--danger-soft); }
.grid td.invalid input:focus { background: var(--surface); }
.grid tr.incomplete td.idx { box-shadow: inset 3px 0 0 var(--warn), inset -1px 0 0 var(--line-strong); }
.grid tr.bad td.idx { box-shadow: inset 3px 0 0 var(--danger), inset -1px 0 0 var(--line-strong); }
.grid td.act { white-space: nowrap; padding: 0 4px; }
.rowbtn { border: 0; background: none; cursor: pointer; padding: 6px; color: var(--muted); border-radius: 6px; line-height: 1; }
.rowbtn:hover { background: var(--surface-2); color: var(--ink); }
.rowbtn.del:hover { color: var(--danger); background: var(--danger-soft); }

.w-adh { min-width: 260px; } .w-date { min-width: 130px; } .w-act { min-width: 240px; }
.w-qte { width: 84px; } .w-unit { width: 64px; } .w-com { min-width: 220px; } .w-bon { width: 110px; }
.w-sal { min-width: 180px; } .w-mat { min-width: 200px; } .w-h { width: 92px; }

/* ---------------- combobox popup */
.combo-pop {
  position: fixed; z-index: 50; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  max-height: 280px; overflow: auto; min-width: 240px; padding: 4px;
}
.combo-opt { padding: 7px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.combo-opt.active, .combo-opt:hover { background: var(--accent-soft); }
.combo-opt small { color: var(--muted); margin-left: 6px; }
.combo-empty { padding: 8px 10px; color: var(--muted); font-size: 12px; }

/* ---------------- toast */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); max-width: min(92vw, 520px);
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 720px) {
  .brand-sub { max-width: 60vw; }
  .cuma-chip { order: 3; width: 100%; justify-content: center; }
  .actions { margin-left: 0; width: 100%; }
  .actions .btn:not(.icon) { flex: 1; }
  .grid-wrap { max-height: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
