:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-soft: #eef2f7;
  --text: #17202c;
  --muted: #5d6b7e;
  --border: #d9e0ea;
  --accent: #2b6cb0;
  --accent-hover: #245a94;
  --accent-soft: #e3eefb;
  --accent-text: #ffffff;
  --ok: #2f855a;
  --warn: #b7791f;
  --bad: #c53030;
  --overlay: rgba(220, 40, 40, 0.45);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141b; --surface: #171e28; --surface-soft: #1d2633; --text: #e6ebf2; --muted: #98a6b8;
    --border: #2a3545; --accent: #5a9be0; --accent-hover: #74abe6; --accent-soft: #1f3550; --accent-text: #0b111a;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141b; --surface: #171e28; --surface-soft: #1d2633; --text: #e6ebf2; --muted: #98a6b8;
  --border: #2a3545; --accent: #5a9be0; --accent-hover: #74abe6; --accent-soft: #1f3550; --accent-text: #0b111a;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh; display: flex; flex-direction: column;
}
main { flex: 1; width: 100%; max-width: 1180px; margin: 0 auto; padding: 24px 16px 40px; }
h1 { font-size: 26px; line-height: 1.2; margin: 0 0 6px; }
h2 { font-size: 18px; margin: 0; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--accent); text-decoration: none; font-weight: 700; font-size: 17px; }
.brand span { color: var(--text); }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.status { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); padding: 4px 10px; border-radius: 99px; background: var(--surface-soft); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.status.ok .dot { background: var(--ok); }
.status.bad .dot { background: var(--bad); }

/* ---------- buttons, inputs ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.big { padding: 11px 20px; font-size: 16px; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible, textarea:focus-visible, input:focus-visible, select:focus-visible, .chip:focus-visible, .seg:focus-visible {
  outline: 3px solid var(--accent-soft); outline-offset: 1px; border-color: var(--accent);
}
textarea, input[type=text], input[type=number], select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
textarea { resize: vertical; min-height: 140px; font-size: 16px; }
select { width: auto; padding: 4px 8px; }

.label { font-weight: 700; margin-bottom: 8px; display: block; }
.label.small, .small { font-size: 13px; }
.muted { color: var(--muted); }
.hint { margin: 12px 0 6px; color: var(--muted); font-size: 13px; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

/* ---------- layout ---------- */
.intro { margin-bottom: 18px; max-width: 720px; }
.intro p { color: var(--muted); margin: 0; }
.grid-2 { display: grid; grid-template-columns: 3fr 2fr; gap: 18px; align-items: start; }
.grid-case { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 18px; align-items: start; }
.side { display: flex; flex-direction: column; gap: 18px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card.soft { background: var(--surface-soft); box-shadow: none; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 16px; flex-wrap: wrap; }

/* ---------- chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px dashed var(--border); background: var(--surface); color: var(--text);
  padding: 4px 10px; border-radius: 99px; font: inherit; font-size: 13px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); border-style: solid; color: var(--accent); }
#suggestChips { margin-top: 10px; }

/* ---------- segmented ---------- */
.segmented { display: inline-flex; background: var(--surface-soft); border-radius: 10px; padding: 3px; }
.seg { border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; padding: 7px 14px; border-radius: 8px; cursor: pointer; }
.seg.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.advanced { margin-top: 14px; color: var(--muted); }
.advanced summary { cursor: pointer; font-size: 13px; }
.advanced input { max-width: 180px; margin-top: 4px; }

/* ---------- understood panel ---------- */
.trait-group { margin: 8px 0; }
.trait-group .k { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.tag { display: inline-block; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; margin: 3px 4px 0 0; font-size: 13px; }
.tag.no { text-decoration: line-through; color: var(--muted); }
.tag.unknown { border-style: dashed; color: var(--warn); }
.look { font-size: 13px; margin: 4px 0; color: var(--muted); }
.look .tag { color: var(--text); }
.warn-line { color: var(--warn); font-size: 13px; margin-top: 8px; }
.tips { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 12px; }
.tips ul { margin: 0; padding-left: 18px; color: var(--muted); font-size: 13px; }

/* ---------- case view ---------- */
.case-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.case-title { font-size: 20px; max-width: 760px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.case-actions { display: flex; gap: 8px; }
.stage { padding: 12px; }
.frame { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--surface-soft); }
.frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.frame .overlay { mix-blend-mode: normal; pointer-events: none; }
.frame .before { clip-path: inset(0 50% 0 0); }
.slider-line { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; box-shadow: 0 0 4px rgba(0,0,0,.5); left: 50%; pointer-events: none; }
.badge { position: absolute; left: 10px; top: 10px; background: rgba(0,0,0,.6); color: #fff; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; }
.busy { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; background: color-mix(in srgb, var(--surface) 82%, transparent); text-align: center; padding: 20px; font-weight: 600; }
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 4px solid var(--border); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.stage-tools { display: flex; align-items: center; gap: 12px; margin-top: 10px; min-height: 28px; flex-wrap: wrap; }
.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; cursor: pointer; }
#compareRange { flex: 1; min-width: 140px; }
.viewing-old { margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: var(--accent-soft); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 14px; }

.area-line { margin: 12px 0 0; font-size: 14px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
#applyBtn { margin-top: 14px; }

.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 360px; overflow: auto; }
.timeline li { display: flex; gap: 10px; align-items: center; padding: 6px; border-radius: 10px; cursor: pointer; border: 1px solid transparent; }
.timeline li:hover { background: var(--surface-soft); }
.timeline li.viewing { border-color: var(--accent); background: var(--accent-soft); }
.timeline img { width: 52px; height: 52px; border-radius: 8px; object-fit: cover; flex: none; }
.timeline .t-title { font-weight: 600; font-size: 14px; }
.timeline .t-sub { font-size: 12px; color: var(--muted); }
.timeline .current-pill { font-size: 11px; background: var(--ok); color: #fff; border-radius: 99px; padding: 1px 7px; margin-left: 6px; }
.desc { margin: 0 0 6px; }

/* ---------- drawer ---------- */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100%); background: var(--surface); z-index: 20; display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0,0,0,.2); }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.cases { overflow: auto; padding: 8px; }
.case-row { display: flex; align-items: center; gap: 4px; border-radius: 10px; }
.case-row:hover { background: var(--surface-soft); }
.case-pick, .case-pick-gap { flex: none; width: 18px; height: 18px; margin: 0 2px 0 6px; accent-color: var(--accent); }
.case-row .case-item { flex: 1; min-width: 0; }
.case-row .case-item:hover { background: transparent; }
.case-del { flex: none; margin-right: 6px; color: var(--muted); }
.case-del:hover { color: var(--bad); border-color: var(--bad); }
.cases-bulk { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 16px; border-bottom: 1px solid var(--border); }
.cases-bulk label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.cases-bulk input { width: 18px; height: 18px; accent-color: var(--accent); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.danger:hover { filter: brightness(1.08); border-color: var(--bad); }
.case-item { display: flex; gap: 12px; padding: 8px; border-radius: 10px; cursor: pointer; text-decoration: none; color: inherit; }
.case-item:hover { background: var(--surface-soft); }
.case-item img { width: 64px; height: 64px; border-radius: 8px; object-fit: cover; flex: none; background: var(--surface-soft); }
.case-item .d { font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.case-item .m { font-size: 12px; color: var(--muted); }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; }

.toast { position: fixed; left: 50%; bottom: 52px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 30; max-width: calc(100% - 32px); box-shadow: var(--shadow); font-size: 14px; }
.toast.error { background: var(--bad); color: #fff; }

.disclaimer { text-align: center; font-size: 12px; color: var(--muted); padding: 10px 16px; border-top: 1px solid var(--border); background: var(--surface); }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .topbar { position: static; }
  .topbar .btn { padding: 6px 10px; font-size: 14px; }
  .grid-2, .grid-case { grid-template-columns: 1fr; }
  h1 { font-size: 22px; }
  .row { flex-direction: column; align-items: stretch; }
  .row .btn { width: 100%; }
}

/* ---------- print: one clean case sheet ---------- */
@media print {
  .topbar, .side .card:first-child, .stage-tools, .viewing-old, .case-actions, .drawer, .scrim, .toast, #describeView, .busy { display: none !important; }
  body { background: #fff; color: #000; }
  .card { box-shadow: none; border-color: #ccc; }
  .grid-case { grid-template-columns: 1fr 1fr; }
  .timeline { max-height: none; }
}

/* ---------- sign in + account ---------- */
.login { max-width: 420px; margin: 40px auto; display: flex; flex-direction: column; gap: 6px; }
.login h1 { margin-bottom: 0; }
.login input { margin-bottom: 8px; }
.login .btn { margin-top: 8px; }
.login-info { color: var(--ok); font-size: 14px; margin: 4px 0 0; }
.login-error { color: var(--bad); font-size: 14px; margin: 4px 0 0; }
input[type=email], input[type=password] {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.user { font-size: 13px; color: var(--muted); display: inline-flex; gap: 6px; align-items: center; }
.user .role { background: var(--accent-soft); color: var(--accent); border-radius: 99px; padding: 1px 8px; font-weight: 600; }
.cloud { font-size: 13px; color: var(--muted); align-self: center; padding: 4px 10px; border-radius: 99px; background: var(--surface-soft); }
.cloud.ok { color: var(--ok); }
.cloud.bad { color: var(--bad); }
