/* ============ Tokens ============ */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --surface-3: #e8ebf0;
  --border: #e2e5ea;
  --border-faint: #eceff3;
  --border-strong: #cdd2da;
  --text: #19212e;
  --muted: #5d6879;
  --faint: #99a2b0;
  --accent: #2459d6;
  --accent-ink: #1b46ae;
  --accent-soft: #e7eefc;
  --row-hover: #f4f7fd;
  --row-sel: #e9f0fd;
  --mark: #fde68a;
  --warn: #9a5b00;
  --warn-bg: #fff1d6;
  --bad: #b3261e;
  --bad-bg: #fde7e5;
  --ok: #17663a;
  --shadow-pop: 0 14px 36px rgba(16, 24, 40, .20), 0 2px 8px rgba(16, 24, 40, .10);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
  --radius: 8px;
  --hdr-h: 38px;
  --flt-h: 36px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a;
    --surface: #161b24;
    --surface-2: #1c222d;
    --surface-3: #252c39;
    --border: #272f3c;
    --border-faint: #20262f;
    --border-strong: #38414f;
    --text: #e6eaf1;
    --muted: #9aa5b6;
    --faint: #667184;
    --accent: #6c97ff;
    --accent-ink: #9bb8ff;
    --accent-soft: #1c2a4a;
    --row-hover: #1a2130;
    --row-sel: #1d2a47;
    --mark: #7a5c00;
    --warn: #f1b95a;
    --warn-bg: #3a2c0d;
    --bad: #ff8f86;
    --bad-bg: #3d1a18;
    --ok: #6fd39a;
    --shadow-pop: 0 14px 36px rgba(0, 0, 0, .55), 0 2px 8px rgba(0, 0, 0, .35);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

/* ============ Base ============ */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }

#app { height: 100%; display: flex; flex-direction: column; min-width: 0; }

/* ============ Top bar ============ */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand .logo { color: var(--accent); flex: none; }
.brand h1 { margin: 0; font-size: 15px; line-height: 1.2; font-weight: 650; letter-spacing: -.01em; }
.data-info { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 250px; }

.search {
  position: relative; flex: 1 1 220px; max-width: 380px; min-width: 200px;
  display: flex; align-items: center;
}
.search svg { position: absolute; left: 10px; color: var(--faint); pointer-events: none; }
.search input {
  width: 100%; height: 34px; padding: 0 30px 0 32px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface-2); outline: none;
}
.search input:focus { background: var(--surface); border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search .icon-btn { position: absolute; right: 4px; }

.toolbar { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }

.btn {
  height: 34px; padding: 0 12px; display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn[aria-pressed="true"], .btn[aria-expanded="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-ink); }
.btn:focus-visible, .icon-btn:focus-visible, .seg button:focus-visible, .link:focus-visible,
.cust:focus-visible, td[data-pop]:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.caret { font-size: 10px; opacity: .7; }
.badge {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn {
  width: 26px; height: 26px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); display: inline-flex; align-items: center; justify-content: center; font-size: 16px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.link { border: 0; background: none; padding: 2px 4px; color: var(--accent-ink); font-size: 12px; border-radius: 4px; }
.link:hover { text-decoration: underline; }

.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; height: 34px; }
.seg button { border: 0; background: var(--surface); padding: 0 11px; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button:hover { background: var(--surface-2); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

/* ============ Menus ============ */
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); right: 0; z-index: 60;
  width: 260px; max-height: min(70vh, 560px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 10px;
  box-shadow: var(--shadow-pop); padding: 6px;
}
.menu-views button.item {
  width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px; border-radius: 6px; display: block;
}
.menu-views button.item:hover { background: var(--surface-2); }
.menu-views .item small { display: block; color: var(--muted); font-size: 11.5px; }
.menu-views .sep { height: 1px; background: var(--border); margin: 6px 4px; }
.menu-cols { width: 340px; padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.menu-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 6px; }
.menu-search {
  margin: 0 12px 8px; height: 30px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: 6px; background: var(--surface-2); outline: none;
}
.menu-search:focus { border-color: var(--accent); background: var(--surface); }
.col-lists { flex: 1; min-height: 0; overflow: auto; padding: 0 6px 8px; }
.sec-t { font-size: 11px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); padding: 8px 8px 4px; }
.sec-t.grp { text-transform: none; letter-spacing: 0; font-size: 12px; padding-top: 10px; color: var(--accent-ink); }
.col-list { list-style: none; margin: 0; padding: 0; }
.col-list li {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; position: relative;
}
.col-list li:hover { background: var(--surface-2); }
.col-list .grip { color: var(--faint); cursor: grab; letter-spacing: -2px; user-select: none; }
.col-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.col-list .tag { font-size: 11px; color: var(--faint); }
.col-list li.hidden-col { cursor: pointer; }
.col-list li.dragging { opacity: .4; }
.col-list li.drop-before::before, .col-list li.drop-after::after {
  content: ""; position: absolute; left: 6px; right: 6px; height: 2px; background: var(--accent); border-radius: 1px;
}
.col-list li.drop-before::before { top: -1px; }
.col-list li.drop-after::after { bottom: -1px; }
.col-note { color: var(--faint); font-size: 12px; padding: 4px 8px; }

/* ============ Stage chips ============ */
.stagebar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.stagebar .lbl { color: var(--muted); font-size: 12px; margin-right: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 13px; background: var(--surface); color: var(--text); font-size: 12px;
}
.chip:hover { background: var(--surface-2); }
.chip .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.chip[aria-pressed="true"] .n { color: inherit; }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: hsl(var(--h) 65% 48%); }
.stagebar .spacer { flex: 1; }
.stagebar .clear-all { color: var(--accent-ink); }

/* ============ Stage / empty state ============ */
.stage { flex: 1; min-height: 0; position: relative; }

.empty { height: 100%; overflow: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; padding: 32px 16px; }
.drop-card {
  width: min(560px, 100%); text-align: center; padding: 40px 24px;
  border: 2px dashed var(--border-strong); border-radius: 14px; background: var(--surface); color: var(--muted);
}
.drop-card:hover, .drop-card.over { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); }
.drop-card h2 { margin: 10px 0 4px; font-size: 18px; color: var(--text); font-weight: 650; }
.drop-card p { margin: 0 0 18px; }
.empty-actions { display: flex; gap: 8px; justify-content: center; }
.shape { width: min(560px, 100%); color: var(--muted); }
.shape summary { cursor: pointer; padding: 4px 0; }
.shape pre {
  margin: 8px 0 0; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: auto; font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; color: var(--text);
}

/* ============ Table ============ */
.table-wrap { position: absolute; inset: 0; overflow: auto; background: var(--surface); }
.grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; }

.grid th, .grid td { padding: 0; text-align: left; }

.grid thead th {
  position: sticky; z-index: 3; background: var(--surface-2);
  border-bottom: 1px solid var(--border-strong); border-right: 1px solid var(--border);
  height: var(--hdr-h); font-size: 12px; font-weight: 650; color: var(--muted);
}
.grid thead tr.hrow th { top: 0; }
.grid thead tr.frow th { top: var(--hdr-h); height: var(--flt-h); background: var(--surface); padding: 4px 6px; z-index: 3; }
.grid thead th.sticky { left: 0; z-index: 6; }
.grid thead tr.frow th.sticky { z-index: 6; }

.th-in {
  display: flex; align-items: center; gap: 6px; height: 100%; padding: 0 14px 0 12px;
  cursor: pointer; user-select: none; position: relative;
}
.th-in:hover { color: var(--text); background: var(--surface-3); }
.th-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sort-ind { color: var(--accent-ink); font-size: 10px; display: inline-flex; align-items: center; gap: 2px; flex: none; }
.sort-ind sup { font-size: 9px; }
th.sorted .th-in { color: var(--text); }
th.dragging .th-in { opacity: .45; }
th.drop-l { box-shadow: inset 3px 0 0 var(--accent); }
th.drop-r { box-shadow: inset -3px 0 0 var(--accent); }
.resizer {
  position: absolute; top: 0; right: -3px; width: 7px; height: 100%; cursor: col-resize; z-index: 2;
}
.resizer::after { content: ""; position: absolute; left: 3px; top: 8px; bottom: 8px; width: 1px; background: transparent; }
.resizer:hover::after, .resizer.active::after { background: var(--accent); width: 2px; left: 2px; }

.fin {
  width: 100%; height: 26px; border: 1px solid transparent; border-radius: 5px; padding: 0 8px;
  background: var(--surface-2); outline: none; font-size: 12px; font-weight: 400; color: var(--text);
}
.fin::placeholder { color: var(--faint); }
.fin:focus { border-color: var(--accent); background: var(--surface); }
.fin.on { background: var(--accent-soft); border-color: var(--accent); }

.grid tbody td {
  vertical-align: top; padding: 8px 12px; border-bottom: 1px solid var(--border-faint);
  border-right: 1px solid var(--border-faint); background: var(--surface); overflow: hidden;
  position: relative;
}
.grid tbody tr:hover td { background: var(--row-hover); }
.grid tbody tr.sel td { background: var(--row-sel); }
.grid td.sticky { position: sticky; left: 0; z-index: 2; border-right: 1px solid var(--border-strong); }
.grid tbody td[data-pop] { cursor: pointer; }
.grid tbody td.pop-open { box-shadow: inset 0 0 0 2px var(--accent); }
.grid tbody td.pop-open.sticky { z-index: 3; }
.none-row td { text-align: center; padding: 48px 16px !important; color: var(--muted); cursor: default; }
.none-row:hover td { background: var(--surface) !important; }

body[data-density="1"] .grid tbody td { padding-top: 5px; padding-bottom: 5px; }
body[data-density="1"] .dg-t { --lines: 1; }
body[data-density="2"] .dg-t { --lines: 2; }
body[data-density="3"] .dg-t { --lines: 4; }

/* Cell digest: first couple of lines of text, items joined with a dot, plus an item-count pill */
.dg { display: flex; gap: 8px; align-items: flex-start; }
.dg-t {
  flex: 1; min-width: 0; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: var(--lines, 2);
  overflow: hidden; overflow-wrap: anywhere; color: var(--text);
}
.dg-t .sep { color: var(--faint); font-style: normal; margin: 0 5px; font-weight: 700; }
.dg-t b { font-weight: 650; }
.cnt {
  flex: none; margin-top: 1px; min-width: 20px; height: 18px; padding: 0 6px; border-radius: 9px;
  background: var(--surface-3); color: var(--muted); font-size: 11px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; font-variant-numeric: tabular-nums;
}
tr:hover .cnt, td.pop-open .cnt { background: var(--accent-soft); color: var(--accent-ink); }
.nil { color: var(--faint); }

.cust {
  border: 0; background: none; padding: 0; text-align: left; font-weight: 650; color: var(--text);
  overflow-wrap: anywhere; line-height: 1.35;
}
.cust:hover { color: var(--accent-ink); text-decoration: underline; }
.num { font-variant-numeric: tabular-nums; }
.date-main { font-variant-numeric: tabular-nums; }
.rel { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 9px; font-size: 11px; color: var(--muted); background: var(--surface-3); }
.rel.warn { color: var(--warn); background: var(--warn-bg); }
.rel.bad { color: var(--bad); background: var(--bad-bg); }

.stage-badge {
  display: inline-block; max-width: 100%; padding: 1px 9px; border-radius: 11px; font-size: 12px; font-weight: 600;
  background: hsl(var(--h) 80% 93%); color: hsl(var(--h) 65% 26%); border: 1px solid hsl(var(--h) 55% 80%);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: top;
}
@media (prefers-color-scheme: dark) {
  .stage-badge { background: hsl(var(--h) 35% 20%); color: hsl(var(--h) 80% 82%); border-color: hsl(var(--h) 35% 32%); }
  .chip .dot { background: hsl(var(--h) 70% 62%); }
}

/* ============ Hover / pinned popover ============ */
.pop {
  position: fixed; z-index: 80; left: 0; top: 0; width: min(540px, calc(100vw - 16px));
  display: flex; flex-direction: column; max-height: 70vh;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 10px; box-shadow: var(--shadow-pop);
}
.pop.pinned { border-color: var(--accent); }
.pop-h {
  display: flex; align-items: center; gap: 8px; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--border);
  background: var(--surface-2); border-radius: 10px 10px 0 0; flex: none;
}
.pop-field { font-weight: 650; }
.pop-cust { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.pop-cust::before { content: "· "; }
.pop-title { display: flex; align-items: baseline; gap: 6px; min-width: 0; flex: 1; }
.pop-actions { display: inline-flex; gap: 2px; flex: none; }
.pop-b { padding: 10px 14px 12px; overflow: auto; }
.pop:not(.pinned) .pop-actions .pop-close { display: none; }
.pop-foot { padding: 5px 14px; border-top: 1px solid var(--border); color: var(--faint); font-size: 11.5px; flex: none; }
.pop.pinned .pop-foot { display: none; }

/* Formatted bullets (used in the popover and the detail drawer) */
.bl { list-style: none; margin: 0; padding: 0; }
.bl li { position: relative; padding: 2px 0 2px 16px; line-height: 1.5; overflow-wrap: anywhere; }
.bl li::before {
  content: ""; position: absolute; left: 3px; top: .72em; width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
}
.bl li.l1 { margin-left: 18px; }
.bl li.l1::before { background: none; border: 1.5px solid var(--accent); width: 5px; height: 5px; }
.bl li.l2 { margin-left: 36px; }
.bl li.l2::before { background: var(--faint); border: 0; border-radius: 1px; }
.bl li.hd { padding-left: 0; margin-top: 6px; font-weight: 650; color: var(--text); }
.bl li.hd::before { display: none; }
.bl li.hd:first-child { margin-top: 0; }
.bl .lb { font-weight: 650; }
.prose { margin: 0; line-height: 1.55; overflow-wrap: anywhere; }

/* ============ Detail drawer ============ */
.drawer {
  position: fixed; z-index: 70; top: 0; right: 0; bottom: 0; width: min(620px, 100vw);
  background: var(--bg); border-left: 1px solid var(--border-strong); box-shadow: -12px 0 40px rgba(16, 24, 40, .16);
  display: flex; flex-direction: column; transform: translateX(24px); opacity: 0;
  transition: transform .16s ease-out, opacity .16s ease-out;
}
.drawer.open { transform: none; opacity: 1; }
.dr-head { background: var(--surface); border-bottom: 1px solid var(--border); padding: 14px 16px 12px; flex: none; }
.dr-nav { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; color: var(--muted); font-size: 12px; }
.dr-nav .grow { flex: 1; }
.dr-title { margin: 0 0 8px; font-size: 20px; font-weight: 700; letter-spacing: -.015em; line-height: 1.2; }
.dr-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--muted); }
.dr-meta .dot { color: var(--faint); }
.dr-body { flex: 1; overflow: auto; padding: 14px 16px 28px; display: flex; flex-direction: column; gap: 14px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; box-shadow: var(--shadow-sm); }
.card.next { border-left: 4px solid var(--accent); }
.card h3, .dr-sec > h3 {
  margin: 0 0 8px; font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
}
.dr-sec { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px 8px; box-shadow: var(--shadow-sm); }
.kv { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 18px; margin: 0 0 12px; }
.kv > div { min-width: 0; }
.kv dt, .fld > .fl { font-size: 11.5px; color: var(--muted); font-weight: 600; margin-bottom: 1px; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.fld { padding: 8px 0; border-top: 1px solid var(--border-faint); }
.fld:first-of-type { border-top: 0; padding-top: 2px; }
.gaps { margin: 4px 0 6px; color: var(--faint); font-size: 12px; }
.gaps span { display: inline-block; margin: 2px 4px 2px 0; padding: 0 7px; border-radius: 9px; background: var(--surface-2); color: var(--muted); }

/* ============ Status bar, toast, drop overlay ============ */
.statusbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 6px 16px;
  background: var(--surface); border-top: 1px solid var(--border); color: var(--muted); font-size: 12px;
}
.statusbar .hints { color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }

.toast {
  position: fixed; left: 50%; bottom: 44px; transform: translateX(-50%); z-index: 100; max-width: min(560px, 92vw);
  padding: 10px 16px; border-radius: 10px; background: #1b2230; color: #fff; box-shadow: var(--shadow-pop);
}
.toast.err { background: #8c1d18; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 120; display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--accent) 18%, transparent); backdrop-filter: blur(2px);
}
.drop-overlay > div {
  padding: 28px 44px; border: 2px dashed var(--accent); border-radius: 16px; background: var(--surface);
  color: var(--accent-ink); font-size: 18px; font-weight: 650;
}

@media (max-width: 760px) {
  .hints { display: none; }
  .toolbar { margin-left: 0; width: 100%; }
  .data-info { max-width: 200px; }
  .kv { grid-template-columns: 1fr; }
}
