:root {
  --bg: #0b1015;
  --panel: #121a22;
  --panel-2: #18222c;
  --line: #233140;
  --text: #dce6ee;
  --muted: #8aa0b2;
  --accent: #3fb5ff;
  --good: #33c27f;
  --warn: #f2b134;
  --bad: #ff5d5d;
  --tl-night: #000000;
  --tl-activity: #4fa3d9;
  --tl-hot: #ffb547;
  --tl-have: #2f8f63;
  --tl-pending: #8a6d2a;
  --tl-ruler: #0e151c;
  --tl-text: #8aa0b2;
  --tl-grid: rgba(255, 255, 255, 0.08);
  --tl-head: #ff4d4d;
  --tl-outside: rgba(255, 255, 255, 0.035);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
button, .button {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
}
button:hover, .button:hover { border-color: var(--accent); }
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); color: #04121d; border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--bad); }
button.icon { padding: 6px; width: 40px; height: 40px; justify-content: center; background: transparent; border-color: transparent; }
button.icon:hover { background: var(--panel-2); border-color: var(--line); }
button.icon svg { width: 24px; height: 24px; fill: currentColor; }
button.icon.small { width: 32px; height: 32px; }
button.icon.small svg { width: 20px; height: 20px; }
button.icon.big svg { width: 32px; height: 32px; }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: 13px; }
.error { color: var(--bad); }

/* ---------- login */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
#login form { width: min(340px, 100%); display: grid; gap: 12px; text-align: center; }
#login img { justify-self: center; border-radius: 16px; }
#login h1 { margin: 0 0 8px; font-size: 20px; }
#login input { font: inherit; padding: 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); }

/* ---------- app layout */
#app {
  height: 100dvh; display: grid; grid-template-rows: auto 1fr auto auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
header { display: flex; align-items: center; gap: 4px; padding: 4px 8px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 48px; }
header h1 { font-size: 15px; margin: 0 0 0 6px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status { background: transparent; border-color: transparent; padding: 4px 8px; color: var(--muted); }
.status .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.status.good .dot { background: var(--good); }
.status.warn .dot { background: var(--warn); }
.status.bad .dot { background: var(--bad); }
.pill { border-radius: 999px; font-weight: 700; font-size: 12px; letter-spacing: 0.05em; padding: 5px 12px; }
.pill.on { background: var(--bad); border-color: var(--bad); color: #fff; }

#stage { position: relative; min-height: 0; background: #000; overflow: hidden; touch-action: none; user-select: none; }
#stage.zoomed { cursor: grab; }
#stage.zoomed:active { cursor: grabbing; }
#screen { width: 100%; height: 100%; object-fit: contain; display: block; }
#osd { position: absolute; left: 8px; right: 8px; top: 8px; display: flex; justify-content: space-between; pointer-events: none; gap: 8px; }
#osd > span, #osd-badges > span:not(:empty) { background: rgba(0, 0, 0, 0.55); padding: 3px 8px; border-radius: 6px; font-variant-numeric: tabular-nums; }
#osd-time { font-size: 15px; font-weight: 600; }
#osd-badges { display: flex; gap: 6px; background: none !important; padding: 0 !important; }
#osd-state:empty, #osd-speed:empty, #osd-zoom:empty { display: none; }
#nodata { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 18px; pointer-events: none; }

#controls { display: flex; align-items: center; justify-content: center; gap: 2px; padding: 4px 8px; background: var(--panel); border-top: 1px solid var(--line); flex-wrap: wrap; }
.speed { display: inline-flex; align-items: center; gap: 4px; margin: 0 8px; }
.speed button { width: 32px; height: 32px; padding: 0; justify-content: center; font-size: 18px; }
#speed-label { min-width: 64px; text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }

#tl-bar { background: var(--panel); border-top: 1px solid var(--line); padding: 2px 8px 6px; }
.tl-head { display: flex; align-items: center; gap: 4px; }
.day { position: relative; cursor: pointer; padding: 4px 8px; border-radius: 8px; font-weight: 600; }
.day:hover { background: var(--panel-2); }
.day input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.zoom { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.zoom button { border: 0; border-radius: 0; padding: 4px 10px; background: transparent; font-size: 13px; }
.zoom button.on { background: var(--accent); color: #04121d; }
#timeline { position: relative; height: 92px; margin-top: 2px; }
#timeline canvas { width: 100%; height: 100%; display: block; touch-action: none; border-radius: 6px; background: #0f171f; }
.tl-tip { position: absolute; bottom: 100%; margin-bottom: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 4px; pointer-events: none; z-index: 5; box-shadow: 0 6px 20px rgba(0,0,0,.5); }
.tl-thumb { width: 160px; height: 90px; border-radius: 4px; background-color: #000; background-repeat: no-repeat; }
.tl-time { text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; padding-top: 2px; }

/* ---------- dialogs */
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 16px; width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
dialog.wide { width: min(1100px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
dialog h2 small { color: var(--muted); font-weight: 400; }
dialog form[method=dialog] { display: inline; }
dialog > form[method=dialog] { display: flex; justify-content: flex-end; margin-top: 12px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row h2 { margin: 0; }
.menu { display: grid; gap: 8px; }
.menu button { justify-content: center; }
.keys td { padding: 2px 8px 2px 0; color: var(--muted); }
.keys td:first-child { color: var(--text); white-space: nowrap; }

.snap-box { position: relative; height: min(70dvh, 62vw); background: #000; border-radius: 8px; overflow: hidden; touch-action: none; user-select: none; cursor: zoom-in; }
.snap-box img { width: 100%; height: 100%; object-fit: contain; display: block; }
.snap-box img:not([src]) { visibility: hidden; }
#snap-msg { margin: 8px 0; }

#hour-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 12px; }
.hour-cell { position: relative; aspect-ratio: 16 / 9; border-radius: 6px; background-color: #05080b; background-repeat: no-repeat; cursor: pointer; border: 1px solid transparent; }
.hour-cell:hover { border-color: var(--accent); }
.hour-cell.empty { cursor: default; opacity: 0.35; }
.hour-cell span { position: absolute; left: 4px; bottom: 4px; font-size: 11px; background: rgba(0,0,0,.6); padding: 1px 5px; border-radius: 4px; }
.hour-cell.hot { box-shadow: inset 0 0 0 2px var(--tl-hot); }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.kv h3 { grid-column: 1 / -1; margin: 10px 0 2px; font-size: 13px; color: var(--accent); text-transform: uppercase; letter-spacing: 0.06em; }

#toast { position: fixed; left: 50%; bottom: calc(150px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--line); padding: 8px 14px; border-radius: 8px; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.5); max-width: calc(100vw - 32px); }

/* ---------- small screens */
@media (max-width: 640px) {
  header h1 { display: none; }
  #controls { gap: 0; padding: 2px 2px; flex-wrap: nowrap; }
  button.icon { width: 34px; height: 36px; padding: 4px; }
  button.icon svg { width: 22px; height: 22px; }
  button.icon.big svg { width: 28px; height: 28px; }
  .speed { margin: 0 2px; gap: 2px; }
  .speed button { width: 28px; height: 30px; }
  #speed-label { min-width: 44px; font-size: 13px; }
  #hour-grid { grid-template-columns: repeat(3, 1fr); }
  .zoom button { padding: 4px 7px; }
}
@media (max-height: 500px) and (orientation: landscape) {
  #timeline { height: 64px; }
  header { min-height: 40px; }
}
:fullscreen #app, #app:fullscreen { padding: 0; }
