:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #17191c;
  --muted: #646b75;
  --line: #dde0e5;
  --accent: #1f3a5f;
  --accent-ink: #ffffff;
  --accent-soft: #e6ecf4;
  --warn-bg: #fff6db;
  --warn-line: #f0c94a;
  --danger: #b42318;
  --ok: #18794e;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --card: #1b1e24;
    --ink: #e8eaed;
    --muted: #9aa1ab;
    --line: #2e333b;
    --accent: #8fb3e6;
    --accent-ink: #0d1726;
    --accent-soft: #1f2a3a;
    --warn-bg: #3a3114;
    --warn-line: #8a6d14;
    --danger: #ff8a80;
    --ok: #6fd3a4;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.topbar .brand { font-weight: 700; text-decoration: none; color: var(--ink); }
.topbar nav { margin-left: auto; display: flex; gap: 14px; font-size: 14px; }
.topbar nav a { text-decoration: none; color: var(--muted); }
.topbar nav a:hover { color: var(--ink); }

main { max-width: 1180px; margin: 0 auto; padding: 16px; }
h1 { font-size: 22px; margin: 4px 0 14px; }
h2 { font-size: 16px; margin: 0 0 12px; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.hidden { display: none !important; }

button, .btn {
  font: inherit; font-weight: 600; border-radius: 8px; padding: 9px 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  text-decoration: none; display: inline-flex; align-items: center; gap: 6px; min-height: 40px;
}
button:hover, .btn:hover { border-color: var(--muted); }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button:disabled { opacity: .55; cursor: default; }
button.icon { padding: 4px 9px; min-height: 32px; font-weight: 500; }

label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px; font-weight: 600; }
input, textarea, select {
  font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
.field { margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 12px; }
@media (max-width: 640px) { .grid2, .grid3 { grid-template-columns: 1fr; } }

/* Upload */
.dropzone {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 28px 16px;
  text-align: center; cursor: pointer; display: block;
}
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone img { max-width: 100%; max-height: 320px; border-radius: 6px; margin-top: 10px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg input { display: none; }
.seg label { margin: 0; padding: 8px 14px; cursor: pointer; color: var(--ink); font-weight: 600; }
.seg input:checked + label { background: var(--accent); color: var(--accent-ink); }

/* Lists */
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.list td { padding: 10px 8px; border-bottom: 1px solid var(--line); }
table.list tr.clickable { cursor: pointer; }
table.list tr.clickable:hover td { background: var(--accent-soft); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.pill { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); text-transform: uppercase; letter-spacing: .03em; }
.pill.sent { background: transparent; color: var(--ok); border: 1px solid var(--ok); }
@media (max-width: 640px) { .hide-sm { display: none; } }

/* Review editor */
.review { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .review { grid-template-columns: 1fr; } }
.photo-pane { position: sticky; top: 72px; }
.photo-pane img { width: 100%; border-radius: 6px; cursor: zoom-in; display: block; }
.photo-pane img.zoomed { cursor: zoom-out; width: 200%; max-width: none; }
.photo-scroll { overflow: auto; max-height: calc(100vh - 150px); border-radius: 6px; }
@media (max-width: 900px) { .photo-pane { position: static; } .photo-scroll { max-height: 50vh; } }

.warnings { background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; }
.warnings ul { margin: 4px 0 0; padding-left: 18px; }

.items { width: 100%; border-collapse: collapse; }
.items th { font-size: 12px; color: var(--muted); text-align: left; padding: 4px; font-weight: 600; }
.items td { padding: 4px; vertical-align: top; }
.items td textarea { min-height: 40px; }
.items .c-qty { width: 70px; }
.items .c-price, .items .c-amt { width: 104px; }
.items .c-act { width: 40px; }
.items.simple .c-qty, .items.simple .c-price { display: none; }
@media (max-width: 640px) { .items.simple tr { grid-template-columns: 1fr auto; } }
.items tr.uncertain td:first-child textarea { background: var(--warn-bg); border-color: var(--warn-line); }
.items input { text-align: right; }
@media (max-width: 640px) {
  .items thead { display: none; }
  .items tr { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
  .items td { padding: 0; }
  .items td:first-child { grid-column: 1 / -1; }
  .items .c-qty, .items .c-price, .items .c-amt, .items .c-act { width: auto; }
  .items td[data-label]::before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); }
}

.totals { margin-left: auto; width: 260px; max-width: 100%; }
.totals div { display: flex; justify-content: space-between; padding: 4px 0; }
.totals .grand { font-weight: 800; font-size: 18px; border-top: 2px solid var(--ink); margin-top: 4px; padding-top: 8px; }

.actionbar {
  position: sticky; bottom: 0; background: var(--card); border-top: 1px solid var(--line);
  padding: 10px 16px; display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; z-index: 10;
}
.dirty-dot { color: var(--danger); font-size: 13px; font-weight: 600; margin-right: auto; align-self: center; }

/* Overlay, dialog, toast */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.dialog { background: var(--card); border-radius: 12px; padding: 20px; width: 520px; max-width: 100%; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); }
.spinner { width: 42px; height: 42px; border: 4px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; margin: 0 auto 14px; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; z-index: 60; font-weight: 600; max-width: calc(100% - 32px);
}
.toast.error { background: var(--danger); color: #fff; }

.login { max-width: 340px; margin: 12vh auto; }
.pin-input { font-size: 28px; letter-spacing: .3em; text-align: center; }
.status-line { display: flex; gap: 8px; align-items: center; font-size: 14px; margin-bottom: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); display: inline-block; }
.dot.ok { background: var(--ok); }
.logo-preview { max-height: 70px; max-width: 220px; display: block; margin-bottom: 8px; background: #fff; padding: 4px; border-radius: 4px; }
