:root {
  --bg: #f6f7f9; --panel: #ffffff; --ink: #1d2433; --muted: #6b7385; --line: #e3e6ec;
  --accent: #1f6feb; --accent-soft: #e8f0fe; --pos: #137a3a; --neg: #b42318; --row-hover: #f3f6fb;
  --side: #17202e; --side-ink: #c9d1de; --side-muted: #7d889b; --side-active: #263247;
  --mono: "Cascadia Mono", Consolas, "Courier New", monospace;
  --sans: "Segoe UI", "Microsoft JhengHei UI", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; font: 13px/1.45 var(--sans); color: var(--ink); background: var(--bg); }

/* Sidebar */
#side { width: 216px; flex: none; background: var(--side); color: var(--side-ink); display: flex; flex-direction: column;
  padding: 16px 12px; gap: 6px; overflow-y: auto; }
.brand { display: flex; gap: 10px; align-items: center; padding: 2px 4px 14px; }
.brand-mark { width: 32px; height: 32px; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 700;
  display: grid; place-items: center; font-size: 15px; }
.brand-title { font-weight: 600; color: #fff; font-size: 14px; }
.brand-sub { font-size: 11px; color: var(--side-muted); }
.co-label { font-size: 11px; color: var(--side-muted); padding: 0 4px; }
#company { width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid #2c384d; background: #1f2a3c; color: #fff; font: inherit; }
.gate-card label.gate-chk { flex-direction: row; align-items: center; gap: 6px; }
.gate-card label.gate-chk input { padding: 0; }
.co-info { font-size: 11px; color: var(--side-muted); padding: 2px 4px 10px; line-height: 1.5; }
.co-info b { font-weight: 600; color: var(--side-ink); }
.co-info .stale { display: block; color: #ffb86b; }
nav .sec { font-size: 11px; color: var(--side-muted); padding: 12px 8px 4px; letter-spacing: .04em; }
nav a { display: block; padding: 6px 10px; border-radius: 6px; color: var(--side-ink); text-decoration: none; cursor: pointer; }
nav a:hover { background: #1f2a3c; }
nav a.active { background: var(--side-active); color: #fff; font-weight: 600; }
.side-foot { margin-top: auto; font-size: 11px; color: var(--side-muted); padding: 12px 4px 0; }

/* Main layout */
main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100vh; }
#top { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 20px 8px;
  border-bottom: 1px solid var(--line); background: var(--panel); }
.top-actions { display: flex; gap: 8px; flex: none; }
#scroll { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
main.ent #scroll { overflow-y: auto; display: block; }
main.ent #filter-row, main.ent #group-row, main.ent #title, main.ent #list { display: none; }
#title { font-size: 18px; margin: 12px 20px 2px; font-weight: 600; }
#list { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* Global search */
.search { position: relative; flex: 1; max-width: 560px; }
#gsearch { width: 100%; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); font: inherit; }
#gsearch:focus { background: var(--panel); }
#gresults { position: absolute; top: 38px; left: 0; right: 0; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 8px 24px rgba(20,30,50,.12); z-index: 20; max-height: 70vh; overflow-y: auto; padding: 4px; }
.gr { display: flex; gap: 10px; align-items: baseline; padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.gr:hover, .gr.on { background: var(--accent-soft); }
.gr .k { font-size: 11px; color: var(--muted); width: 64px; flex: none; }
.gr .l { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gr .s { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.gr-empty { padding: 10px; color: var(--muted); }

button { font: inherit; border-radius: 6px; padding: 5px 12px; cursor: pointer; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); }
button:hover { border-color: #c5cbd6; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#filters { padding: 8px 20px; display: flex; flex-direction: column; gap: 8px; }
.row { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.presets { display: flex; gap: 4px; flex-wrap: wrap; }
.chip { padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); cursor: pointer;
  font-size: 12px; color: var(--muted); }
.chip:hover { color: var(--ink); }
.chip.on { background: var(--accent-soft); border-color: #b6cdf7; color: var(--accent); font-weight: 600; }
.dates { display: flex; gap: 10px; color: var(--muted); }
input[type=date], input[type=text], input[type=search] { font: inherit; padding: 4px 8px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--panel); color: var(--ink); }
.f { display: flex; flex-direction: column; gap: 2px; }
.f span { font-size: 11px; color: var(--muted); }
.f input { width: 230px; }
.f.small input { width: 130px; }
.f input.set { border-color: #9dbcf3; background: #f5f8ff; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; background: var(--panel); }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--line); padding: 4px 11px; color: var(--muted); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent); color: #fff; }
#flags { display: flex; gap: 14px; color: var(--muted); }
#flags label { display: flex; gap: 5px; align-items: center; cursor: pointer; }

#summary, .kpis { display: flex; gap: 10px; padding: 4px 20px 8px; flex-wrap: wrap; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; min-width: 120px; }
.stat .k { font-size: 11px; color: var(--muted); }
.stat .v { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat .v small { font-size: 11px; color: var(--muted); font-weight: 400; margin-left: 3px; }
.hint { padding: 0 20px 6px; color: var(--muted); font-size: 12px; }
.hint:empty { display: none; }

#table-wrap { flex: 1; min-height: 0; overflow: auto; margin: 0 20px 16px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; position: relative; }
table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
thead th { position: sticky; top: 0; background: #fafbfc; z-index: 1; text-align: left; font-weight: 600; font-size: 12px;
  color: var(--muted); padding: 7px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; user-select: none; }
#grid thead th { cursor: pointer; }
#grid thead th:hover { color: var(--ink); }
thead th.num { text-align: right; }
thead th .arrow { color: var(--accent); }
tbody td { padding: 5px 10px; border-bottom: 1px solid #f0f2f5; white-space: nowrap; max-width: 340px; overflow: hidden;
  text-overflow: ellipsis; }
tbody tr:hover td { background: var(--row-hover); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.neg { color: var(--neg); }
td.id { font-family: var(--mono); font-size: 12px; }
td.link, td.flink { cursor: pointer; }
td.link { color: var(--accent); }
td.link:hover { text-decoration: underline; }
td.flink:hover { color: var(--accent); }
tr.grp { cursor: pointer; }
tfoot td { position: sticky; bottom: 0; background: #fafbfc; border-top: 1px solid var(--line); padding: 7px 10px;
  font-weight: 600; white-space: nowrap; }
.empty { padding: 40px; text-align: center; color: var(--muted); }
#more-wrap { padding: 10px; text-align: center; }
#loading { position: fixed; right: 24px; bottom: 24px; background: var(--ink); color: #fff; padding: 6px 12px; border-radius: 6px; }
.error { margin: 0 20px 12px; padding: 10px 12px; background: #fdecea; color: var(--neg); border-radius: 6px; }

/* Profile pages */
#ent-head { padding: 14px 20px 4px; }
.crumbs { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.crumbs button { padding: 3px 10px; font-size: 12px; }
.ent-kind { font-size: 11px; color: var(--accent); font-weight: 600; letter-spacing: .04em; }
.ent-title { font-size: 20px; font-weight: 600; margin: 2px 0 0; }
.ent-sub { color: var(--muted); font-family: var(--mono); font-size: 12px; }
.ent-info { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 6px 20px; padding: 12px 0 2px; }
.ent-info div { min-width: 0; }
.ent-info .k { font-size: 11px; color: var(--muted); }
.ent-info .v { overflow-wrap: anywhere; }
.ent-info .wide { grid-column: 1 / -1; }
.ent-info .v.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; cursor: pointer; }
.ent-info .v.open { cursor: pointer; white-space: pre-wrap; }
.ent-info .v.link { color: var(--accent); cursor: pointer; }
.ent-info .v.link:hover { text-decoration: underline; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; margin: 0 20px 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 9px 12px; border-bottom: 1px solid var(--line); }
.card-head h3 { margin: 0; font-size: 14px; font-weight: 600; }
.card-head h3 .n { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 6px; }
.card-head button { font-size: 12px; padding: 3px 10px; color: var(--accent); }
.card-body { max-height: 420px; overflow: auto; }
.card-note { padding: 6px 12px; font-size: 12px; color: var(--muted); }
.chart-card .card-body { padding: 8px 12px 4px; max-height: none; overflow: visible; }
.chart svg { display: block; width: 100%; height: 170px; }
.chart .bar { fill: #8fb4f5; }
.chart .bar:hover { fill: var(--accent); }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: #eef0f4; }
.empties { padding: 0 20px 20px; color: var(--muted); font-size: 12px; }

/* Login, user box, preview banner */
.side-foot #who { color: #fff; font-size: 12px; margin-bottom: 4px; }
.who-links { display: flex; gap: 10px; flex-wrap: wrap; }
.who-links a { color: var(--side-ink); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.foot-note { margin-top: 8px; }
#as-bar { background: #fff4d6; border-bottom: 1px solid #f0d58a; color: #6b4e00; padding: 6px 20px; font-size: 12px; }
#as-bar button { margin-left: 10px; padding: 2px 10px; }
#gate { position: fixed; inset: 0; background: var(--bg); display: grid; place-items: center; z-index: 100; }
.gate-card { width: 320px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 10px 30px rgba(20,30,50,.08); }
.gate-card h2 { margin: 4px 0 0; font-size: 18px; }
.gate-card p { margin: 0; color: var(--muted); font-size: 12px; }
.gate-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.gate-card select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.gate-card input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; color: var(--ink); }
.gate-err { color: var(--neg); font-size: 12px; min-height: 16px; }
.gate-card a { text-align: center; color: var(--muted); cursor: pointer; font-size: 12px; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); padding: 8px 12px; }
.gate-card [hidden], #gate[hidden] { display: none !important; }
