:root {
  color-scheme: light;
  --plane: #f9f9f7; --surface: #fcfcfb; --raised: #ffffff;
  --ink: #0b0b0b; --ink2: #52514e; --muted: #6f6d68;
  --grid: #e1e0d9; --axis: #c3c2b7; --ring: rgba(11,11,11,.10); --wash: rgba(11,11,11,.05);
  --accent: #18b07f; --accent-ink: #ffffff; --sel: #d8f5e9;
  --good: #006300; --critical: #d03b3b;
  --s1:#2a78d6; --s2:#eb6834; --s3:#1baf7a; --s4:#eda100; --s5:#e87ba4; --s6:#008300; --s7:#6250d6; --s8:#e34948;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --plane: #0d0d0d; --surface: #1a1a19; --raised: #232322;
    --ink: #f0efec; --ink2: #c3c2b7; --muted: #a09e97;
    --grid: #2c2c2a; --axis: #383835; --ring: rgba(255,255,255,.10); --wash: rgba(255,255,255,.06);
    --accent: #3ddc97; --accent-ink: #06291b; --sel: #154c38;
    --good: #0ca30c; --critical: #e66767;
    --s1:#3987e5; --s2:#d95926; --s3:#199e70; --s4:#c98500; --s5:#d55181; --s6:#008300; --s7:#9085e9; --s8:#e66767;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; display: flex; flex-direction: column; background: var(--plane); color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
#top { display: flex; align-items: center; gap: 8px; padding: 8px 16px; background: var(--surface); flex-wrap: wrap; }
#top .brand { font-weight: 600; }
#top .file { color: var(--ink2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
#top .spacer { flex: 1; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--wash); color: var(--ink2); }
#tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--grid); overflow-x: auto; flex: none; }
#tabs button { border: 0; background: none; padding: 8px 12px; color: var(--ink2); cursor: pointer; font: inherit; white-space: nowrap; border-bottom: 2px solid transparent; }
#tabs button:hover { color: var(--ink); }
#tabs button[aria-selected=true] { color: var(--ink); font-weight: 600; border-bottom-color: var(--accent); }
main { flex: 1; min-height: 0; overflow: auto; padding: 16px; }
main.fill { display: flex; flex-direction: column; overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
button.btn, a.btn { border: 1px solid var(--axis); background: var(--raised); color: var(--ink); border-radius: 6px; padding: 4px 12px; cursor: pointer; text-decoration: none; display: inline-block; white-space: nowrap; }
button.btn:hover:not(:disabled), a.btn:hover { background: var(--wash); }
button.btn:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
button.danger { color: var(--critical); }
button.link { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible, tr:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input, select, textarea { background: var(--raised); border: 1px solid var(--axis); border-radius: 6px; padding: 4px 8px; min-width: 0; max-width: 100%; }
textarea { width: 100%; resize: vertical; }
input[type=checkbox], input[type=radio] { accent-color: var(--accent); }

.bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; flex: none; }
.bar label { display: inline-flex; gap: 6px; align-items: center; color: var(--ink2); }
.bar .grow { flex: 1; }
h2 { font-size: 16px; margin: 20px 0 8px; font-weight: 600; }
h2:first-child { margin-top: 0; }

.panel { background: var(--surface); border: 1px solid var(--ring); border-radius: 8px; }
.scroll { flex: 1; min-height: 0; overflow: auto; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.panel { overflow-x: auto; }
th { position: sticky; top: 0; background: var(--surface); text-align: left; font-weight: 600; color: var(--ink2); border-bottom: 1px solid var(--axis); z-index: 1; }
th, td { padding: 4px 10px; white-space: nowrap; }
td { border-bottom: 1px solid var(--grid); }
td.wrap { white-space: normal; min-width: 200px; }
.num { text-align: right; }
.neg { color: var(--critical); }
.pos { color: var(--good); }
tbody tr:hover { background: var(--wash); }
tr.selected, tr.selected:hover { background: var(--sel); }
tr.clickable { cursor: pointer; }
tr.group td { font-weight: 600; background: var(--wash); }
tr.muted td { color: var(--muted); font-style: italic; }
.tree-toggle { display: inline-block; width: 16px; cursor: pointer; color: var(--muted); user-select: none; }
.status { flex: none; display: flex; flex-wrap: wrap; gap: 4px 20px; padding: 8px 2px 0; color: var(--ink2); font-variant-numeric: tabular-nums; }
.status b { color: var(--ink); font-weight: 600; }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.tiles { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 12px; }
.tile { background: var(--surface); border: 1px solid var(--ring); border-radius: 8px; padding: 10px 16px; min-width: 150px; }
.tile .k { color: var(--ink2); font-size: 13px; }
.tile .v { font-size: 22px; font-weight: 600; }

dialog { border: 1px solid var(--ring); border-radius: 10px; background: var(--surface); color: var(--ink); padding: 0; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); box-shadow: 0 8px 32px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 34px); }
dialog h3 { margin: 0; padding: 12px 16px; font-size: 16px; border-bottom: 1px solid var(--grid); }
dialog .body { padding: 12px 16px; overflow: auto; display: grid; grid-template-columns: minmax(120px, auto) 1fr; gap: 8px 12px; align-items: center; }
dialog .body > label { color: var(--ink2); }
dialog .body .full { grid-column: 1 / -1; }
dialog .body input:not([type=checkbox]):not([type=radio]), dialog .body select { width: 100%; }
dialog .ts-pop input.ts-search { width: auto; }
dialog .foot { display: flex; gap: 8px; justify-content: flex-end; align-items: center; padding: 12px 16px; border-top: 1px solid var(--grid); }
dialog .err { color: var(--critical); flex: 1; white-space: pre-line; }
.seg { display: flex; flex-wrap: wrap; gap: 12px; }
.seg label { display: inline-flex; align-items: center; gap: 4px; color: var(--ink); }

.ts { min-width: 0; width: 240px; max-width: 100%; }
dialog .body .ts { width: 100%; }
.ts-btn { display: flex; align-items: center; gap: 8px; width: 100%; background: var(--raised); border: 1px solid var(--axis); border-radius: 6px; padding: 4px 8px; cursor: pointer; text-align: left; }
.ts-btn:hover { background: var(--wash); }
.ts-btn[aria-expanded=true] { border-color: var(--accent); }
.ts-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ts-placeholder { color: var(--ink2); }
.ts-caret { color: var(--muted); flex: none; }
.ts-pop { position: fixed; z-index: 40; display: flex; flex-direction: column; margin: 0; padding: 8px; background: var(--raised); color: var(--ink); border: 1px solid var(--ring); border-radius: 8px; box-shadow: 0 8px 28px rgba(0,0,0,.3); }
.ts-search { flex: none; margin-bottom: 6px; }
.ts-list { flex: 1; min-height: 0; overflow: auto; }
.ts-row { display: flex; align-items: center; gap: 4px; padding: 3px 8px 3px 6px; border-radius: 5px; cursor: pointer; }
.ts-row.ts-active { background: var(--wash); }
.ts-row.ts-chosen { background: var(--sel); }
.ts-row.ts-group .ts-label { color: var(--ink2); font-weight: 600; }
.ts-toggle { flex: none; width: 20px; height: 20px; line-height: 18px; text-align: center; border-radius: 4px; color: var(--ink2); font-weight: 600; user-select: none; }
.ts-toggle:not(:empty) { border: 1px solid var(--axis); }
.ts-toggle:not(:empty):hover { background: var(--wash); color: var(--ink); }
.ts-label { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.ts-label mark { background: #eda100; color: #0b0b0b; border-radius: 2px; }
.ts-hint { flex: none; color: var(--muted); font-size: 12px; }
.ts-none { padding: 16px; text-align: center; color: var(--muted); }

#toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: var(--plane); padding: 8px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px); white-space: pre-line; z-index: 20; }
#toast.show { opacity: 1; }
#tip { position: fixed; z-index: 30; pointer-events: none; background: var(--raised); color: var(--ink); border: 1px solid var(--ring); border-radius: 8px; padding: 8px 10px; box-shadow: 0 4px 16px rgba(0,0,0,.2); font-size: 13px; font-variant-numeric: tabular-nums; max-width: 320px; }
#tip .t { font-weight: 600; margin-bottom: 4px; }
#tip .r { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; flex: none; }
.chart { padding: 12px; margin-bottom: 12px; }
.chart svg { display: block; width: 100%; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; color: var(--ink2); font-size: 13px; padding: 0 0 8px; }
.legend span { display: inline-flex; align-items: center; }
svg text { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
svg text.direct { fill: var(--ink2); }
svg .grid { stroke: var(--grid); stroke-width: 1; }
svg .base { stroke: var(--axis); stroke-width: 1; }
svg .cross { stroke: var(--axis); stroke-width: 1; }
@media (max-width: 640px) {
  main { padding: 8px; }
  dialog .body { grid-template-columns: 1fr; }
  #top, #tabs { padding-left: 16px; padding-right: 16px; }
  #top { gap: 6px; }
  .panel table { min-width: max-content; }
  .book-list table { width: 100%; min-width: 680px; }
  .actions { display: flex; gap: 5px; }
  .auth { width: min(100%, 460px); margin: 5vh auto; padding: 20px; }
  .auth h1 { font-size: 21px; margin: 0 0 18px; }
  .auth form { display: grid; gap: 10px; margin-bottom: 16px; }
  .auth label { display: grid; gap: 5px; }
  .auth details { padding: 10px 0; border-top: 1px solid var(--grid); }
  .auth summary { cursor: pointer; color: var(--accent); }
}
.auth { max-width: 460px; margin: 6vh auto; padding: 24px; }
.online { background: #d9f6e9; color: #08734f; }
.notice { padding: 8px 12px; background: var(--sel); border-radius: 6px; }
@media print {
  #top, #tabs, .bar, .status button { display: none; }
  main { overflow: visible; }
}

/* Sign-in screen (cloud mode), on every screen size. */
.auth h1 { font-size: 22px; margin: 0 0 18px; }
.auth form { display: grid; gap: 12px; margin-bottom: 16px; }
.auth label { display: grid; gap: 6px; font-size: 13px; color: var(--muted, inherit); }
.auth input { width: 100%; box-sizing: border-box; padding: 9px 10px; font-size: 15px; }
.auth form .btn { justify-self: start; padding: 8px 18px; }
.auth details { padding: 10px 0; border-top: 1px solid var(--grid); }
.auth details form { margin: 12px 0 0; }
.auth summary { cursor: pointer; color: var(--accent); }
.auth .err { color: #d4473b; min-height: 1em; }
@media (prefers-color-scheme: dark) { .online { background: #173328; color: #3ddc97; } }
/* Book list (cloud mode). */
.books { max-width: 980px; margin: 4vh auto; padding: 24px; }
.books h1 { font-size: 22px; margin: 0 0 14px; }
.books .bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 14px; }
.books .book-list { overflow-x: auto; }
.books .book-list table { width: 100%; min-width: 640px; }
.books .actions { justify-content: flex-end; }
.sessions-section { margin-top: 20px; }
.sessions-section h2 { margin-top: 0; }
.sessions-table { max-height: min(48vh, 460px); overflow: auto; }
.sessions-table table { min-width: 820px; }
.current-session { display: inline-block; margin-left: 6px; }
.book-presence { color: var(--accent); }
.book-presence[hidden] { display: none; }
