:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --ink: #1f2430;
  --line: #d7dbe3;
  --accent: #2f6df6;
  --danger: #e5484d;
  --ok: #2fb36a;
  --toolbar-h: 48px;
  --strip-w: 108px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
/* The `hidden` attribute must always win, even over class rules that set a
   `display` (e.g. .gate/.toolbar) — otherwise hidden overlays render stacked. */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  touch-action: none; overscroll-behavior: none; }

/* ---- Join gate ---- */
.gate { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; background: #eef1f6; }
.gate-card { background: var(--panel); padding: 28px 32px; border-radius: 16px; width: min(360px, 90vw);
  box-shadow: 0 12px 40px rgba(0,0,0,.14); }
.gate-card h1 { margin: 0 0 18px; font-size: 22px; }
.gate-card label { display: block; font-size: 13px; color: #5a6473; margin-bottom: 12px; }
.gate-card input { display: block; width: 100%; margin-top: 5px; padding: 11px 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 9px; user-select: text; -webkit-user-select: text; }
.gate-card input#join-code { text-transform: uppercase; letter-spacing: 2px; }
.primary { background: var(--accent); color: #fff; border: 0; padding: 12px; width: 100%; border-radius: 9px;
  font-size: 16px; font-weight: 600; margin-top: 6px; cursor: pointer; }
.hint { color: #8a93a2; font-size: 12px; margin: 12px 0 0; }
.gate-card.center { text-align: center; }
.gate-card.center h1 { margin-top: 10px; }
.ghost { background: transparent; border: 0; color: var(--accent); font-size: 13px; margin-top: 16px; cursor: pointer; text-decoration: underline; }
.spinner { width: 34px; height: 34px; margin: 4px auto 0; border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Tutor waiting-room panel */
.lobby { position: fixed; top: calc(var(--toolbar-h) + 10px); right: 12px; z-index: 45; width: 250px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.14);
  padding: 10px; }
.lobby-h { font-size: 12px; font-weight: 700; color: #5a6473; text-transform: uppercase; letter-spacing: .4px; margin-bottom: 8px; }
.lobby-row { display: flex; align-items: center; gap: 6px; padding: 6px; border-radius: 8px; }
.lobby-row + .lobby-sec { margin-top: 6px; }
.lobby-name { flex: 1; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lobby-name .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: #f5a623; }
.lobby-row.active .lobby-name .dot { background: var(--ok); }
.lobby-btn { border: 0; border-radius: 7px; padding: 5px 9px; font-size: 12px; font-weight: 600; cursor: pointer; }
.lobby-admit { background: var(--ok); color: #fff; }
.lobby-deny { background: #eef1f6; color: #5a6473; }
.lobby-end { background: #fdecec; color: var(--danger); }
.lobby-sec { font-size: 11px; color: #8a93a2; margin: 8px 0 2px; }
.lobby-badge { position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff; font-size: 11px;
  min-width: 18px; height: 18px; border-radius: 9px; display: grid; place-items: center; padding: 0 4px; }

/* ---- Toolbar ---- */
/* Wraps onto multiple rows when the window is narrow so nothing is hidden. The
   stage top offset (--toolbar-h) is updated in JS from the real height. */
.toolbar { position: fixed; top: 0; left: 0; right: 0; min-height: var(--toolbar-h); z-index: 40;
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; padding: 5px 8px; background: var(--panel);
  border-bottom: 1px solid var(--line); padding-top: calc(5px + env(safe-area-inset-top)); }
.tool-group { display: flex; align-items: center; gap: 3px; padding: 0 6px; border-right: 1px solid var(--line); }
.tool-group:last-child { border-right: 0; }
/* Labeled "Student controls" box grouping the 3 student toggles. */
.student-controls { flex-direction: column; align-items: flex-start; gap: 2px; padding: 2px 8px;
  border: 1px solid var(--line); border-radius: 8px; margin: 0 4px; }
.student-controls .group-label { font-size: 9px; text-transform: uppercase; letter-spacing: .4px; color: #8a93a2; }
.student-controls .group-btns { display: flex; gap: 3px; }
.toolbar button, .toolbar select { height: 34px; min-width: 34px; padding: 0 8px; border: 1px solid transparent;
  background: transparent; border-radius: 8px; font-size: 16px; cursor: pointer; color: var(--ink);
  display: inline-flex; align-items: center; justify-content: center; }
.toolbar button:hover { background: #eef1f6; }
.toolbar button:disabled { opacity: .38; cursor: not-allowed; }
.toolbar button:disabled:hover { background: transparent; }
/* Instant hover tooltips (native title is slow/unreliable in the app WebView). */
.toolbar button[data-tip] { position: relative; }
.toolbar button[data-tip]:hover::after { content: attr(data-tip); position: absolute; top: 100%; left: 50%;
  transform: translateX(-50%); margin-top: 7px; background: #23262c; color: #fff; font-size: 12px;
  padding: 5px 9px; border-radius: 7px; white-space: nowrap; z-index: 100; pointer-events: none;
  box-shadow: 0 4px 14px rgba(0,0,0,.28); }
.toolbar button.active, .toolbar .tool.active { background: #e4ecff; border-color: #b9cdfb; color: var(--accent); }
.toolbar button.toggle.active { background: #e4ecff; border-color: #b9cdfb; }

.colors { gap: 4px; }
.swatch { width: 20px; height: 20px; min-width: 20px; border-radius: 50%; border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.swatch.active { box-shadow: 0 0 0 2px var(--accent); transform: scale(1.12); }

.widths .width span { display: block; width: 20px; background: var(--ink); border-radius: 3px; }
/* Pen-thickness / eraser-option popovers float directly under their tool button. */
#widths, #eraser-opts { position: fixed; z-index: 60; border-right: 0; padding: 5px 8px;
  background: var(--bg, #fff); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 22px rgba(0, 0, 0, .14); }
.eraser-opts .erase-mode { font-size: 11px; padding: 3px 8px; }
.eraser-opts .erase-size span { display: block; background: var(--ink); border-radius: 50%; }
.eraser-opts .erase-size[data-esize="14"] span { width: 6px; height: 6px; }
.eraser-opts .erase-size[data-esize="24"] span { width: 10px; height: 10px; }
.eraser-opts .erase-size[data-esize="40"] span { width: 15px; height: 15px; }
.pip-placeholder { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; background: #1f2430; color: #fff; border-radius: inherit; pointer-events: none; }
.pip-placeholder span { font-size: 26px; }
.pip-placeholder small { font-size: 11px; opacity: .8; }
.timer { font-variant-numeric: tabular-nums; font-size: 14px; color: #5a6473; padding: 0 6px; min-width: 46px; }

.status-dot { font-size: 12px; color: var(--danger); }
.status-dot.ok { color: var(--ok); }
.rec-notice { color: #fff; background: var(--danger); font-size: 12px; padding: 3px 8px; border-radius: 6px;
  animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .55; } }

body.role-student .tutor-only { display: none; }
body.role-tutor .student-only { display: none; }

/* ---- Page strip ---- */
.page-strip { position: fixed; top: var(--toolbar-h); left: 0; bottom: 0; width: var(--strip-w); z-index: 30;
  background: var(--panel); border-right: 1px solid var(--line); padding: 8px; overflow-y: auto;
  transition: transform .18s ease, box-shadow .18s ease; }
/* Collapsed: slide out to a thin rail so past pages don't crowd the board; the
   rail expands on hover (desktop) and the pin toggle keeps it open (touch). */
body.strip-collapsed .page-strip { transform: translateX(calc(-1 * var(--strip-w) + 15px)); }
body.strip-collapsed .page-strip:hover { transform: translateX(0); box-shadow: 6px 0 20px -10px rgba(0,0,0,.3); }
.strip-toggle { position: fixed; top: calc(var(--toolbar-h) + 8px); left: 8px; z-index: 32;
  width: 26px; height: 26px; display: grid; place-items: center; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); border-radius: 7px; cursor: pointer; font-size: 14px; line-height: 1; }
.strip-toggle:hover { border-color: var(--accent); }
.page-thumb { width: 100%; aspect-ratio: 4/3; border: 2px solid var(--line); border-radius: 8px; margin-bottom: 8px;
  background: #fff; cursor: pointer; position: relative; overflow: hidden; }
.page-thumb.active { border-color: var(--accent); }
.page-thumb canvas { width: 100%; height: 100%; }
.page-thumb .lbl { position: absolute; bottom: 2px; left: 4px; font-size: 10px; color: #8a93a2; }
.page-thumb .del { position: absolute; top: 2px; right: 2px; font-size: 11px; background: rgba(255,255,255,.85);
  border-radius: 4px; padding: 0 4px; opacity: 0; }
.page-thumb:hover .del { opacity: 1; }
.page-thumb.draggable { cursor: grab; }
.page-thumb.dragging { opacity: .4; cursor: grabbing; }
/* Drop indicator: a bar on the edge the dragged page will land against. */
.page-thumb.drop-before { box-shadow: 0 -3px 0 0 var(--accent); }
.page-thumb.drop-after { box-shadow: 0 3px 0 0 var(--accent); }
#page-add { width: 100%; padding: 8px; border: 1px dashed var(--line); border-radius: 8px; background: #fff;
  cursor: pointer; color: #5a6473; }

/* ---- Stage / canvas ---- */
.stage { position: fixed; top: var(--toolbar-h); left: 0; right: 0; bottom: 0; }
body.has-strip .stage { left: var(--strip-w); transition: left .18s ease; }
body.has-strip.strip-collapsed .stage { left: 15px; }
.board, .overlay { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.overlay { pointer-events: none; }
.text-layer { position: absolute; inset: 0; pointer-events: none; }
.text-input { position: absolute; pointer-events: auto; background: transparent; border: 1px dashed var(--accent);
  outline: none; font-family: inherit; padding: 2px 4px; color: inherit; resize: none; overflow: hidden;
  user-select: text; -webkit-user-select: text; white-space: pre; }

/* ---- Fixed video panel (top-right, in line with the top bar) ---- */
.pip { position: fixed; top: calc(var(--toolbar-h) + 8px); right: 12px; z-index: 50;
  display: flex; gap: 6px; align-items: stretch; padding: 6px; border-radius: 12px;
  background: rgba(18,20,26,.92); box-shadow: 0 8px 26px rgba(0,0,0,.3); }
.pip-tile { position: relative; width: 148px; height: 100px; border-radius: 8px; overflow: hidden; background: #000; }
.pip-tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.pip-tag { position: absolute; left: 5px; bottom: 4px; font-size: 10px; color: #fff;
  background: rgba(0,0,0,.55); padding: 1px 6px; border-radius: 5px; }
.pip-side { display: flex; flex-direction: column; justify-content: space-between; gap: 6px; width: 64px; }
.pip-btn { font-size: 11px; padding: 5px 6px; border-radius: 7px; border: 0; cursor: pointer;
  background: rgba(255,255,255,.16); color: #fff; }
.pip-btn:hover { background: rgba(255,255,255,.26); }
.pip-meter { height: 8px; border-radius: 4px; background: rgba(255,255,255,.16); overflow: hidden; }
.pip-meter i, .mc-meter i { display: block; height: 100%; width: 0%; border-radius: inherit;
  background: linear-gradient(90deg,#2fb36a,#f5a623,#e5484d); transition: width .05s linear; }

/* ---- Mic / camera check overlay ---- */
.media-check { position: fixed; inset: 0; z-index: 120; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.5); }
.media-check .mc-card { background: var(--bg,#fff); color: var(--ink,#1f2430); width: 340px; padding: 22px;
  border-radius: 16px; box-shadow: 0 16px 50px rgba(0,0,0,.3); text-align: center; }
.media-check h2 { margin: 0 0 10px; font-size: 18px; }
.media-check video { width: 100%; border-radius: 10px; background: #000; margin-bottom: 12px; }
.mc-meter { height: 12px; border-radius: 6px; background: rgba(0,0,0,.1); overflow: hidden; margin: 8px 0 14px; }

.cursor-pen { cursor: crosshair; }
.cursor-pan { cursor: grab; }
.cursor-pan:active { cursor: grabbing; }

/* ---- Shared document panel (writing subjects: English / languages / humanities) ---- */
.doc-panel { position: fixed; z-index: 60; display: flex; flex-direction: column; overflow: hidden;
  background: #fff; border: 1px solid #d7dbe2; border-radius: 12px; box-shadow: 0 20px 56px rgba(20,30,50,.28); }
.doc-titlebar { display: flex; align-items: center; justify-content: space-between; cursor: grab;
  background: #1f2733; color: #fff; padding: 6px 10px; font-size: 13px; font-weight: 600; user-select: none; }
.doc-titlebar .doc-close { background: none; border: 0; color: #cdd6e2; font-size: 15px; cursor: pointer; padding: 0 4px; }
.doc-titlebar .doc-close:hover { color: #fff; }
.doc-toolbar { display: flex; flex-wrap: wrap; gap: 3px; align-items: center; padding: 5px 7px; border-bottom: 1px solid #e7e9ee; background: #f7f8fa; }
.doc-toolbar button { min-width: 28px; height: 28px; padding: 0 7px; border: 1px solid transparent; border-radius: 7px;
  background: none; color: #2a3340; font-size: 14px; cursor: pointer; line-height: 1; }
.doc-toolbar button:hover { background: #e9edf3; }
.doc-toolbar .doc-hl { background: #fff3a3; }
.doc-toolbar .doc-sep { width: 1px; align-self: stretch; margin: 4px 3px; background: #e0e3e9; }
.doc-toolbar .doc-spacer { flex: 1; }
.doc-accents { display: flex; flex-wrap: wrap; gap: 2px; padding: 6px 8px; border-bottom: 1px solid #e7e9ee; background: #fbfcfd; }
.doc-accents button { min-width: 26px; height: 26px; border: 1px solid #e2e5ea; border-radius: 6px; background: #fff; cursor: pointer; font-size: 14px; }
.doc-accents button:hover { background: #eef2f7; border-color: #c7cdd6; }
.doc-editor { flex: 1; overflow: auto; padding: 16px 20px; outline: none; color: #16202e;
  font-family: Georgia, "Times New Roman", serif; font-size: 16px; line-height: 1.7; background: #fff; }
.doc-editor:empty::before { content: "Type or paste text here — write essays together, mark up passages, build sentences…"; color: #9aa4b2; }
.doc-resize { position: absolute; right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize;
  background: linear-gradient(135deg, transparent 50%, #c2c8d2 50%); border-radius: 0 0 12px 0; }

/* Non-Cloudflare fallback-route banner (shown only if the deployment configures a
   fallback host and the connection can't hold — e.g. a student behind a block). */
.fb-banner { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 120;
  display: flex; align-items: center; gap: 10px; background: #23262c; color: #fff;
  padding: 10px 12px 10px 16px; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.3); font-size: 14px; }
.fb-banner button { border: 0; border-radius: 8px; padding: 7px 12px; font-size: 13px; font-weight: 600; cursor: pointer; }
.fb-banner #fb-go { background: var(--accent); color: #fff; }
.fb-banner #fb-x { background: transparent; color: #aab3c0; padding: 6px 8px; }

/* ---- Vocabulary builder (languages / English) ---- */
.vocab-panel { position: fixed; z-index: 60; display: flex; flex-direction: column; overflow: hidden;
  background: #fff; border: 1px solid #d7dbe2; border-radius: 12px; box-shadow: 0 20px 56px rgba(20,30,50,.28); }
.vocab-titlebar { display: flex; align-items: center; justify-content: space-between; cursor: grab;
  background: #1f2733; color: #fff; padding: 6px 8px 6px 12px; font-size: 13px; font-weight: 600; user-select: none; }
.vocab-btns { display: flex; gap: 4px; }
.vocab-titlebar button { border: 0; border-radius: 7px; padding: 4px 9px; font-size: 12px; cursor: pointer; }
.vocab-titlebar .vocab-copy { background: #3a4653; color: #fff; }
.vocab-titlebar .vocab-close { background: none; color: #cdd6e2; font-size: 15px; }
.vocab-head, .vocab-row { display: grid; grid-template-columns: 1fr 1fr 1.4fr 28px; gap: 6px; padding: 4px 10px; }
.vocab-head { font-size: 11px; text-transform: uppercase; letter-spacing: .3px; color: #8a93a2; padding-top: 8px; border-bottom: 1px solid #eef0f3; }
.vocab-rows { flex: 1; overflow: auto; padding: 4px 0; }
.vocab-row input { width: 100%; border: 1px solid #e4e7ec; border-radius: 7px; padding: 6px 8px; font-size: 14px; color: #16202e; }
.vocab-row input:focus { outline: none; border-color: var(--accent); }
.vocab-row .vocab-del { border: 0; background: none; color: #b7bfca; cursor: pointer; font-size: 13px; }
.vocab-row .vocab-del:hover { color: var(--danger); }
.vocab-foot { padding: 6px 10px; border-top: 1px solid #eef0f3; }
.vocab-foot .vocab-add { border: 1px dashed #c3cbd6; background: #f7f9fb; color: #3a4653; border-radius: 8px; padding: 6px 12px; font-size: 13px; cursor: pointer; }
.vocab-resize { position: absolute; right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize; background: linear-gradient(135deg, transparent 50%, #c2c8d2 50%); border-radius: 0 0 12px 0; }

/* Document ruled-line spacing picker (shown only in Doc mode). */
.doc-spacing { display: inline-flex; align-items: center; gap: 2px; }
.doc-spacing button { min-width: 28px; height: 30px; border: 1px solid transparent; border-radius: 7px; background: transparent; cursor: pointer; color: var(--ink); font-size: 14px; }
.doc-spacing button:hover { background: #eef1f6; }
.doc-spacing button.active { background: #e4ecff; border-color: #b9cdfb; color: var(--accent); }
.doc-spacing button[data-sp="document-narrow"] { font-size: 11px; }
.doc-spacing button[data-sp="document-wide"] { font-size: 18px; }

/* ---- Sentence builder (languages / English grammar) ---- */
.sent-panel { position: fixed; z-index: 60; display: flex; flex-direction: column; overflow: hidden;
  background: #fff; border: 1px solid #d7dbe2; border-radius: 12px; box-shadow: 0 20px 56px rgba(20,30,50,.28); }
.sent-titlebar { display: flex; align-items: center; justify-content: space-between; cursor: grab;
  background: #1f2733; color: #fff; padding: 6px 8px 6px 12px; font-size: 13px; font-weight: 600; user-select: none; }
.sent-titlebar .sent-close { background: none; border: 0; color: #cdd6e2; font-size: 15px; cursor: pointer; }
.sent-setup { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid #eef0f3; }
.sent-setup .sent-input { flex: 1; border: 1px solid #e4e7ec; border-radius: 8px; padding: 7px 10px; font-size: 14px; }
.sent-setup button { border: 1px solid #d7dbe2; background: #f5f7fa; border-radius: 8px; padding: 7px 11px; font-size: 13px; cursor: pointer; }
.sent-setup .sent-set { background: var(--accent); color: #fff; border-color: var(--accent); }
.sent-answer { min-height: 46px; margin: 12px; padding: 8px; display: flex; flex-wrap: wrap; gap: 6px; align-content: flex-start;
  border: 2px dashed #cfd6e0; border-radius: 10px; }
.sent-answer.correct { border-color: var(--ok); background: #edfbf3; }
.sent-answer .sent-hint { color: #9aa4b2; font-size: 13px; align-self: center; }
.sent-bank-label { font-size: 11px; text-transform: uppercase; letter-spacing: .3px; color: #8a93a2; padding: 0 12px; }
.sent-bank { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px 14px; min-height: 40px; }
.sent-chip { border: 1px solid #cdd6e2; background: #fff; border-radius: 9px; padding: 7px 12px; font-size: 15px; cursor: pointer; color: #16202e; }
.sent-chip:hover { background: #eef2f7; }
.sent-answer .sent-chip { background: #eaf1ff; border-color: #b9cdfb; }
