:root {
  color-scheme: dark;
  --bg: #0b2340;
  --panel: rgba(6, 20, 40, 0.92);
  --line: #234a7a;
  --btn: #17375f;
  --btn-hi: #1f4777;
  --btn-on: #2f6db3;
  --text: #e6efff;
  --muted: #9fb4d6;
  --accent: #34d399;
}
html, body { margin: 0; height: 100%; background: var(--bg); }
body { font: 14px/1.35 system-ui, -apple-system, 'Segoe UI', sans-serif; color: var(--text); }
body[data-mode='main'], body[data-mode='turntable'], body[data-mode='studio'] { overflow: hidden; }
#app { position: relative; }
body[data-mode='main'] #app, body[data-mode='turntable'] #app { position: fixed; inset: 0; }
.fatal { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; color: #ffb4b4; text-align: center; }

button, select {
  font: 600 13px/1 system-ui, -apple-system, sans-serif; color: var(--text);
  background: var(--btn); border: 1px solid #2c5488; border-radius: 999px;
  padding: 8px 12px; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
button:hover { background: var(--btn-hi); }
button:active, button.flash, button.on { background: var(--btn-on); border-color: #5d98de; }
button.speech { background: #3a2a63; border-color: #6550a6; }
button.mic { background: #124a3c; border-color: #2b8a6c; }
button.mic.on { background: #1d8a68; }
button:disabled { opacity: 0.45; cursor: default; }
select { border-radius: 8px; padding: 6px 8px; }
kbd { font: 10px ui-monospace, Menlo, monospace; opacity: 0.55; margin-left: 5px; }

/* main demo */
.stage { position: absolute; inset: 0 0 var(--bar-h, 0px) 0; }
.bar {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(5, 16, 34, 0.9), rgba(5, 16, 34, 0));
}
.hud {
  position: fixed; top: calc(8px + env(safe-area-inset-top)); padding: 3px 8px; border-radius: 6px;
  background: rgba(0, 0, 0, 0.35); pointer-events: none; font: 12px/1.4 ui-monospace, Menlo, monospace;
}
.hud.left { left: calc(8px + env(safe-area-inset-left)); }
.hud.right { right: calc(8px + env(safe-area-inset-right)); font-family: system-ui, sans-serif; }
.meter { position: fixed; top: 40px; right: 10px; width: 140px; height: 6px; border-radius: 3px; background: rgba(255,255,255,.1); overflow: hidden; pointer-events: none; display: none; }
.meter > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 60ms linear; }
.meter.on { display: block; }

/* studio */
body[data-mode='studio'] #app { position: fixed; inset: 0; display: grid; grid-template-columns: 1fr 400px; }
.studio-stage { position: relative; }
.panel { overflow-y: auto; background: var(--panel); border-left: 1px solid var(--line); padding: 12px 14px 40px; }
.panel h2 { font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); margin: 16px 0 6px; }
.panel h2:first-child { margin-top: 0; }
.row { display: flex; flex-wrap: wrap; gap: 5px; }
.row button { padding: 6px 9px; font-size: 12px; }
.slider { display: grid; grid-template-columns: 92px 1fr 44px; align-items: center; gap: 8px; margin: 3px 0; font-size: 12px; }
.slider input { width: 100%; accent-color: var(--accent); }
.slider output { font: 11px ui-monospace, Menlo, monospace; color: var(--muted); text-align: right; }
.slider.active label { color: var(--accent); }
.glove { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin: 6px 0; }
.glove header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; font-weight: 600; }
details summary { cursor: pointer; color: var(--muted); font-size: 12px; margin: 8px 0 4px; }
@media (max-width: 800px) {
  body[data-mode='studio'] #app { grid-template-columns: 1fr; grid-template-rows: 45vh 1fr; }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
}

/* lesson */
body[data-mode='lesson'] { overflow-x: hidden; background: radial-gradient(ellipse 90% 70% at 30% 40%, #143a66 0%, #0b2340 60%, #06162b 100%) fixed; }
.lesson-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 18px 0; }
.lesson-head h1 { font-size: 18px; margin: 0; }
.lesson-head h1 span { color: var(--muted); font-weight: 500; }
#board { position: relative; height: min(84vh, 820px); height: min(84dvh, 820px); min-height: 480px; --R: 110px; }
#stage, .tests-toggle, .hint { display: none; }
#cards { display: contents; } /* desktop: the slots are placed around Chati by app/lesson.js */
#chati-layer { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
.slot { position: absolute; transform: translate(-50%, -50%); }
.scroller { height: calc(var(--R) * 1.62); overflow-y: auto; padding: 4px 8px; display: flex; flex-direction: column; gap: 12px; border-radius: 14px; background: rgba(255,255,255,.04); outline: 1px dashed rgba(159,180,214,.35); }
.scroller .card { flex: 0 0 auto; }
.card {
  width: calc(var(--R) * 1.3); padding: 6px 6px 8px; border-radius: 14px; display: grid; justify-items: center; gap: 1px;
  background: linear-gradient(#fdfbf6, #efe9dc); color: #1b2a44; border: 0; box-shadow: 0 10px 24px rgba(0,0,0,.35);
}
.card:hover { background: linear-gradient(#ffffff, #f4efe4); }
.card.small { width: calc(var(--R) * 1.05); padding: 4px 5px 6px; }
.card.small .pic svg { max-height: calc(var(--R) * 0.42); }
.card .pic svg { width: 100%; height: auto; max-height: calc(var(--R) * 0.7); display: block; }
.card b { font-size: max(13px, calc(var(--R) * 0.17)); }
.card small { color: #5a6b86; font-size: max(10px, calc(var(--R) * 0.12)); }
.strip { display: flex; align-items: center; gap: calc(var(--R) * 0.1); padding: 6px 12px; border-radius: 16px; background: rgba(0,0,0,.28); }
.strip .stem { font: 700 calc(var(--R) * 0.24)/1 ui-rounded, system-ui, sans-serif; letter-spacing: .02em; }
.ending { font: 700 calc(var(--R) * 0.22)/1 ui-rounded, system-ui, sans-serif; padding: calc(var(--R) * 0.09) calc(var(--R) * 0.14); border-radius: 12px; background: #f7d774; color: #3a2a00; border: 0; }
.ending:hover { background: #ffe38c; }
.side { position: absolute; top: 18%; width: 220px; color: var(--muted); }
#summary { padding: 10px 18px; color: var(--muted); }
#summary .note { display: inline-block; color: var(--text); background: rgba(255,255,255,.06); border: 1px solid var(--line); padding: 12px 16px; border-radius: 12px; font-size: 17px; }
.cross { position: absolute; width: 22px; height: 22px; margin: -13px 0 0 -13px; pointer-events: none; z-index: 4; display: none;
  border: 2px solid #0ff; border-radius: 50%; } /* hollow, under the canvas: the fingertip bead stays visible */
#dbg { position: fixed; left: 8px; bottom: 8px; font: 12px ui-monospace, Menlo, monospace; color: #0ff; }


/* lesson on phones: portrait (under 600 px wide, or portrait) and short landscape.
   dvh follows Safari's toolbars; the safe-area insets keep clear of the notch and home bar. */
body[data-layout='portrait'] .lesson-head, body[data-layout='landscape'] .lesson-head {
  gap: 8px; padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}
body[data-layout='portrait'] .lesson-head h1, body[data-layout='landscape'] .lesson-head h1 { font-size: 16px; flex: 1 1 auto; min-width: 0; }
body[data-layout='portrait'] .lesson-head h1 span { display: block; font-size: 13px; }
body[data-layout='portrait'] .tests-toggle, body[data-layout='landscape'] .tests-toggle { display: inline-block; min-height: 40px; padding: 8px 14px; }
body[data-layout='portrait'] #controls, body[data-layout='landscape'] #controls { display: none; width: 100%; }
body[data-layout='portrait'] .lesson-head.open #controls, body[data-layout='landscape'] .lesson-head.open #controls { display: flex; }
body[data-layout='portrait'] #controls button, body[data-layout='landscape'] #controls button { min-height: 36px; }
body[data-layout='portrait'] .side, body[data-layout='landscape'] .side { display: none !important; }
body[data-layout='portrait'] #summary, body[data-layout='landscape'] #summary {
  padding: 10px max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
/* the grid cells: cards fill their cell, nothing depends on Chati's size */
body[data-layout='portrait'] #cards .slot, body[data-layout='landscape'] #cards .slot, body[data-layout='portrait'] #strip, body[data-layout='landscape'] #strip {
  position: relative; left: auto; top: auto; transform: none;
}
body[data-layout='portrait'] #cards, body[data-layout='landscape'] #cards { display: grid; gap: 12px; align-content: start; }
body[data-layout='portrait'] #cards .card, body[data-layout='landscape'] #cards .card { width: 100%; padding: 8px 6px 10px; }
body[data-layout='portrait'] #cards .card .pic svg, body[data-layout='landscape'] #cards .card .pic svg { max-height: 58px; }
body[data-layout='portrait'] #cards .card b, body[data-layout='landscape'] #cards .card b { font-size: 16px; }
body[data-layout='portrait'] #cards .card small, body[data-layout='landscape'] #cards .card small { font-size: 12px; }
body[data-layout='portrait'] .scroller, body[data-layout='landscape'] .scroller { box-sizing: border-box; width: 100%; height: 142px; padding: 4px; gap: 8px; }
body[data-layout='portrait'] .hint, body[data-layout='landscape'] .hint { display: block; margin: 0; align-self: center; color: var(--muted); font-size: 13px; }
/* the endings: full width, big chips */
body[data-layout='portrait'] #strip, body[data-layout='landscape'] #strip { display: flex; gap: 10px; padding: 8px 12px; box-sizing: border-box; width: 100%; }
body[data-layout='portrait'] #strip .stem, body[data-layout='landscape'] #strip .stem { font-size: 22px; }
body[data-layout='portrait'] #strip .ending, body[data-layout='landscape'] #strip .ending { flex: 1 1 0; min-height: 48px; font-size: 22px; padding: 8px 0; }

/* portrait: top to bottom */
body[data-layout='portrait'] #board {
  height: auto; min-height: 0; display: flex; flex-direction: column; gap: 12px;
  padding: 0 max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
}
body[data-layout='portrait'] #stage { display: block; flex: none; height: 38vh; height: 38dvh; min-height: 200px; }
/* two across on a phone, more on a wider portrait screen (a tablet) */
body[data-layout='portrait'] #cards { grid-template-columns: repeat(auto-fill, minmax(min(150px, calc(50% - 6px)), 1fr)); }

/* short landscape: Chati at his desk on the left, the cards and the endings on the right */
body[data-layout='landscape'] #board {
  height: calc(100vh - var(--head-h, 56px)); height: calc(100dvh - var(--head-h, 56px)); min-height: 0;
  display: grid; grid-template-columns: minmax(0, 46%) minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; gap: 8px 12px;
  padding: 0 max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  box-sizing: border-box;
}
body[data-layout='landscape'] #stage { display: block; grid-column: 1; grid-row: 1 / 3; min-height: 0; }
body[data-layout='landscape'] #cards { grid-column: 2; grid-row: 1; grid-template-columns: repeat(3, minmax(0, 1fr)); align-content: center; min-height: 0; }
body[data-layout='landscape'] #cards .card .pic svg { max-height: 46px; }
body[data-layout='landscape'] .scroller { height: 118px; }
body[data-layout='landscape'] .hint { display: none; }
body[data-layout='landscape'] #strip { grid-column: 2; grid-row: 2; }
body[data-layout='landscape'] #strip .ending { min-height: 44px; }
