:root {
  --bg: #f6f3ee; --card: #fff; --ink: #1d1f22; --ink-2: #5d6168; --line: #e4dfd6; --soft: #efebe4;
  --accent: #2f6f5e; --accent-ink: #fff; --danger: #c93a3f; --good: #2e8b57; --warn: #b7791f;
  --shadow: 0 8px 30px rgba(30, 25, 15, .12);
  --r: 12px;
  color-scheme: light dark;
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16171a; --card: #202227; --ink: #eceef1; --ink-2: #a1a6ae; --line: #33363d; --soft: #2a2d33;
    --accent: #5fb39a; --accent-ink: #0c1d18; --shadow: 0 8px 30px rgba(0, 0, 0, .45); }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); }
body { overscroll-behavior: none; }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }

.btn { border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 7px 13px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.big { padding: 11px 16px; font-size: 16px; width: 100%; }
.btn.icon { width: 38px; padding: 7px 0; }
.btn:disabled { opacity: .5; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; background: var(--card); }
.seg button { border: 0; background: none; padding: 6px 11px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
input[type=text], input:not([type]), input[type=number] {
  border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 8px 10px; width: 100%; }
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Join ---------- */
.join { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.join-card { width: min(380px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 26px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 4px; }
.brand img { border-radius: 10px; }
.brand h1 { font-size: 21px; margin: 0; }
.brand p { margin: 0; color: var(--ink-2); }
.join-card label { display: grid; gap: 5px; font-weight: 600; font-size: 14px; }
.join-card input { font-weight: 400; font-size: 16px; padding: 10px 12px; }
#join-code { text-transform: uppercase; letter-spacing: .12em; font-family: ui-monospace, Menlo, monospace; }
.join-error { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- Room layout ---------- */
.room { height: 100%; display: grid; grid-template-rows: auto 1fr; }
.top { display: flex; align-items: center; gap: 12px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--line); min-height: 54px; }
.top-title { display: flex; gap: 9px; align-items: center; min-width: 0; flex: 1; }
.top-title img { border-radius: 7px; flex: none; }
.top-title div { display: grid; min-width: 0; }
.top-title b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-title span { font-size: 12px; }
.timer { display: grid; justify-items: center; font-variant-numeric: tabular-nums; line-height: 1.1; min-width: 74px; }
.timer span { font-size: 22px; font-weight: 700; }
.timer small { font-size: 11px; color: var(--ink-2); text-transform: uppercase; letter-spacing: .06em; }
.timer.low span { color: var(--danger); }
.timer.ended span { color: var(--ink-2); }
.top-actions { display: flex; gap: 8px; align-items: center; flex: 1; justify-content: flex-end; }
#finish-btn[aria-pressed="true"] { background: var(--good); border-color: var(--good); color: #fff; }

.body { display: grid; grid-template-columns: 1fr 340px; min-height: 0; }
.stage { overflow: auto; position: relative; min-height: 0; -webkit-overflow-scrolling: touch; }
.pages { display: grid; justify-content: center; gap: 14px; padding: 18px 14px 110px; }
.page { position: relative; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.14), 0 6px 20px rgba(0,0,0,.06); }
.page canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.page .ink { touch-action: pan-x pan-y pinch-zoom; }
.room[data-tool="pen"] .page .ink, .room[data-tool="hl"] .page .ink, .room[data-tool="eraser"] .page .ink { touch-action: none; cursor: crosshair; }
.room.no-draw .page .ink { cursor: default !important; touch-action: pan-x pan-y pinch-zoom !important; }
.page .num { position: absolute; left: -2px; top: -2px; transform: translateY(-100%); font-size: 11px; color: var(--ink-2); padding: 2px; }
.page .cursor { position: absolute; pointer-events: none; transform: translate(-3px, -3px); transition: left .1s linear, top .1s linear; z-index: 2; }
.page .cursor i { display: block; width: 10px; height: 10px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.2); }
.page .cursor span { position: absolute; left: 12px; top: 6px; font-size: 11px; color: #fff; padding: 1px 6px; border-radius: 6px; white-space: nowrap; }
.empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; padding: 30px; }
.empty h2 { margin: 0 0 6px; font-size: 19px; }
.viewing { position: sticky; top: 8px; z-index: 5; margin: 8px auto 0; width: max-content; max-width: 90%;
  background: var(--ink); color: var(--bg); padding: 6px 12px; border-radius: 20px; font-size: 13px; display: flex; gap: 10px; align-items: center; }
.viewing button { background: none; border: 0; color: inherit; text-decoration: underline; cursor: pointer; padding: 0; }

/* ---------- Side panel ---------- */
.panel { border-left: 1px solid var(--line); background: var(--card); display: grid; grid-template-rows: auto 1fr; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; background: none; padding: 11px 4px; cursor: pointer; color: var(--ink-2); border-bottom: 2px solid transparent; position: relative; }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.count { font-size: 12px; color: var(--ink-2); font-weight: 400; }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--danger); vertical-align: 2px; }
.tab { overflow: auto; min-height: 0; padding: 12px; }
.tab.chat { display: grid; grid-template-rows: 1fr auto auto; padding: 0; }

.people { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.person { border: 1px solid var(--line); border-radius: var(--r); padding: 9px 11px; display: grid; gap: 6px; }
.person-top { display: flex; align-items: center; gap: 8px; }
.swatch { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.person-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person.offline { opacity: .55; }
.badge { font-size: 11px; padding: 1px 7px; border-radius: 10px; background: var(--soft); color: var(--ink-2); }
.badge.host { background: var(--accent); color: var(--accent-ink); }
.badge.done { background: var(--good); color: #fff; }
.person-where { font-size: 13px; color: var(--ink-2); }
.person-controls { display: flex; flex-wrap: wrap; gap: 6px; }
.person-controls .btn { padding: 3px 9px; font-size: 13px; }
.person-controls .btn[aria-pressed="false"] { text-decoration: line-through; color: var(--ink-2); }

.messages { list-style: none; margin: 0; padding: 12px; overflow: auto; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.msg b { font-size: 13px; }
.msg time { font-size: 11px; color: var(--ink-2); margin-left: 6px; }
.msg p { margin: 1px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.say { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.chat-off { padding: 0 12px 12px; margin: 0; }

.section { display: grid; gap: 9px; padding-bottom: 16px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.section:last-child { border-bottom: 0; }
.section h3 { margin: 0; font-size: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > label { flex: 1; min-width: 90px; }
.field { display: grid; gap: 4px; font-size: 13px; color: var(--ink-2); }
.field input { color: var(--ink); }
.file-pick { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.file-pick span { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.toggle { display: flex; align-items: center; gap: 9px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.code { font-family: ui-monospace, Menlo, monospace; letter-spacing: .1em; font-weight: 700; background: var(--soft); padding: 2px 8px; border-radius: 6px; }

.board { display: grid; gap: 6px; }
.board-row { display: grid; grid-template-columns: 22px 1fr auto; gap: 8px; align-items: center; padding: 7px 10px; border-radius: 9px; background: var(--soft); }
.board-row.first { background: color-mix(in srgb, #ffb224 30%, var(--card)); }
.board-row b { font-variant-numeric: tabular-nums; }
.qlist { display: grid; gap: 4px; }
.q { display: grid; grid-template-columns: 1fr 70px 34px; gap: 8px; align-items: center; font-size: 14px; }
.q input { padding: 5px 7px; text-align: right; }
.q button { border: 0; background: none; cursor: pointer; color: var(--accent); text-align: left; padding: 0; font: inherit; }
.grid-wrap { overflow: auto; }
table.grid { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
table.grid th, table.grid td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: center; white-space: nowrap; }
table.grid th:first-child, table.grid td:first-child { text-align: left; position: sticky; left: 0; background: var(--card); }
table.grid td.full { color: var(--good); }
table.grid td.drop { background: color-mix(in srgb, var(--danger) 18%, transparent); }
table.grid td.zero { background: color-mix(in srgb, var(--danger) 38%, transparent); }

/* ---------- Pen tools ---------- */
.tools { position: fixed; left: 50%; transform: translateX(-50%); bottom: max(14px, env(safe-area-inset-bottom)); margin-left: -170px;
  display: flex; align-items: center; gap: 3px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 5px; box-shadow: var(--shadow); z-index: 20; }
.tools button { width: 36px; height: 36px; border: 0; border-radius: 9px; background: none; cursor: pointer; font-size: 17px; display: grid; place-items: center; }
.tools button[aria-pressed="true"] { background: var(--soft); box-shadow: inset 0 0 0 1.5px var(--accent); }
.tools button:disabled { opacity: .35; cursor: default; }
.tools .sep { width: 1px; height: 24px; background: var(--line); margin: 0 3px; }
.swatches { display: flex; gap: 3px; }
.swatches button { width: 26px; height: 26px; }
.swatches button i { width: 16px; height: 16px; border-radius: 50%; display: block; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.swatches button[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--ink); background: none; }

.toast { position: fixed; left: 50%; top: 70px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 15px; border-radius: 10px; z-index: 50; max-width: 90%; box-shadow: var(--shadow); font-size: 14px; }

#panel-btn { display: none; }

/* ---------- Phones and narrow windows ---------- */
@media (max-width: 860px) {
  .body { grid-template-columns: 1fr; }
  #panel-btn { display: block; }
  .panel { position: fixed; left: 0; right: 0; bottom: 0; height: min(70vh, 560px); border-left: 0; border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0; box-shadow: var(--shadow); z-index: 30; transform: translateY(105%); transition: transform .22s ease; padding-bottom: env(safe-area-inset-bottom); }
  .room.panel-open .panel { transform: none; }
  .tools { margin-left: 0; }
  .top { gap: 8px; flex-wrap: wrap; }
  .top-title { order: 1; flex: 1 1 calc(100% - 100px); }
  .timer { order: 2; }
  .top-actions { order: 3; flex: 1 1 100%; justify-content: space-between; }
  .pages { padding: 12px 6px 100px; }
}
@media (max-width: 420px) {
  .tools button { width: 32px; height: 34px; }
  .swatches button { width: 22px; }
  #zoom-out, #zoom-in { display: none; }
}

/* ---------- Who drew what ---------- */
.page .tag { position: absolute; z-index: 3; pointer-events: none; transform: translate(6px, -120%);
  background: var(--c); color: #fff; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 8px 8px 8px 2px;
  white-space: nowrap; box-shadow: 0 1px 4px rgba(0,0,0,.25); text-shadow: 0 1px 1px rgba(0,0,0,.25); }
.own-colour { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 0 6px; color: var(--ink-2); }
.own-colour i { width: 16px; height: 16px; border-radius: 50%; display: block; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.person { border-left: 4px solid var(--c, var(--line)); }
