/* ==========================================================================
   Collaborative AI Canvas — styles
   1. Tokens & reset
   2. Canvas surface
   3. Top bar (48h clock / round countdown / entries / frozen banner)
   4. Info button + about popover
   5. FAB + draw hint + pill
   6. Toasts
   7. Bottom sheet
   8. Buttons & form controls
   9. Refine toolbar (stage 2 of drawing)
   10. Motion preferences / wide screens
   ========================================================================== */

/* -- 1. Tokens & reset ---------------------------------------------------- */

:root {
  --bg: #111111;
  --bg-elev: #1b1b1d;
  --bg-elev-2: #232326;
  --line: #303035;
  --ink: #f2f2f3;
  --ink-dim: #a0a0a8;
  --ink-faint: #6d6d76;
  --accent: #ffffff;
  --accent-ink: #111111;
  --danger: #ff6b5e;
  --good: #58d68d;
  --radius: 16px;
  --tap: 44px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  text-rendering: optimizeLegibility;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

a { color: var(--ink); text-underline-offset: 3px; }
a:hover { color: #fff; }

button { font: inherit; color: inherit; }

/* -- 2. Canvas surface ---------------------------------------------------- */

#canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
  background: var(--bg);
}
#canvas.is-panning { cursor: grabbing; }
body.mode-draw #canvas { cursor: crosshair; }

/* -- 3. Top bar ----------------------------------------------------------- */

#topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 10px) 0 calc(var(--safe-l) + 10px);
  pointer-events: none;
}

#bar {
  pointer-events: auto;
  margin-left: calc(var(--tap) + 4px); /* clear the fixed ⓘ button */
  background: rgba(24, 24, 26, 0.82);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: 13px;
  padding: 8px 11px 9px;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.45);
}

/* The 48-hour clock leads: it is the thing that makes this an event. The round
   countdown sits under it as a labelled secondary line. */
#eventRow {
  display: flex;
  align-items: baseline;
  gap: 7px;
  white-space: nowrap;
}

.icon-hourglass {
  width: 17px;
  height: 17px;
  flex: 0 0 auto;
  color: var(--ink-dim);
  align-self: center;
}

#eventLeft {
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.015em;
  line-height: 1.15;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
}

#eventSuffix {
  font-size: 12px;
  color: var(--ink-dim);
}

#eventRow .stat-dot { margin-left: auto; align-self: center; }

#roundRow {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-top: 3px;
  margin-bottom: 7px;
  font-size: 12.5px;
  letter-spacing: 0.01em;
  color: var(--ink-dim);
  white-space: nowrap;
}

#roundLine { overflow: hidden; text-overflow: ellipsis; }
#countdown {
  color: var(--ink);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Sits immediately after the countdown, not pushed to the far edge — the two
   numbers are meant to be read together as the round fills up. */
#entriesHint {
  flex: 0 0 auto;
  padding: 1px 8px;
  border-radius: 999px;
  background: #2c2c32;
  color: var(--ink);
  font-size: 11.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

#progress {
  height: 4px;
  border-radius: 999px;
  background: #37373d;
  overflow: hidden;
}

#progressFill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: var(--accent);
  transform-origin: left center;
  transition: width 240ms linear;
}
#progressFill.is-low { background: var(--danger); }

.stat-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ink-faint);
  flex: 0 0 auto;
}
.stat-dot.is-live { background: var(--good); }
.stat-dot.is-down { background: var(--danger); }

#frozenBanner {
  pointer-events: auto;
  margin-left: calc(var(--tap) + 4px);
  margin-top: 0;
  background: rgba(24, 24, 26, 0.9);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: 13px;
  padding: 11px 13px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.45);
}

/* -- 4. Info button + about popover --------------------------------------- */

#infoBtn {
  position: fixed;
  z-index: 40;
  top: calc(var(--safe-t) + 8px);
  left: calc(var(--safe-l) + 10px);
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(24, 24, 26, 0.82);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  color: var(--ink-dim);
  cursor: pointer;
}
#infoBtn:hover, #infoBtn[aria-expanded="true"] { color: var(--ink); }
#infoBtn svg { width: 21px; height: 21px; }

#about {
  position: fixed;
  z-index: 41;
  top: calc(var(--safe-t) + 8px + var(--tap) + 8px);
  left: calc(var(--safe-l) + 10px);
  width: min(320px, calc(100vw - var(--safe-l) - var(--safe-r) - 20px));
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 15px 12px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.6);
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-dim);
  animation: pop 160ms ease-out;
}
#about p { margin: 0 0 9px; }
#about p:last-child { margin-bottom: 0; }
.about-anon { color: var(--ink-faint); }
.about-links { display: flex; gap: 8px; align-items: center; padding-top: 4px; }
.about-links a { color: var(--ink); }

@keyframes pop {
  from { opacity: 0; transform: translateY(-6px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

/* -- 5. FAB + draw hint + pill -------------------------------------------- */

#fab {
  position: fixed;
  z-index: 35;
  right: calc(var(--safe-r) + 16px);
  bottom: calc(var(--safe-b) + 20px);
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: none;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.55);
  cursor: pointer;
  transition: transform 140ms ease, background-color 140ms ease;
}
#fab:active { transform: scale(0.94); }
#fab svg { width: 27px; height: 27px; }
#fab .icon-close { display: none; }
body.mode-draw #fab { background: var(--danger); color: #fff; }
body.mode-draw #fab .icon-pencil { display: none; }
body.mode-draw #fab .icon-close { display: block; }

#drawHint {
  position: fixed;
  z-index: 34;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 34px);
  background: rgba(24, 24, 26, 0.86);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 15px;
  font-size: 12.5px;
  color: var(--ink-dim);
  pointer-events: none;
  white-space: nowrap;
}

#pill {
  position: fixed;
  z-index: 34;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 34px);
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  padding: 10px 17px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  white-space: nowrap;
  animation: pillIn 260ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
@keyframes pillIn {
  from { opacity: 0; transform: translateX(-50%) translateY(14px) scale(0.9); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}

/* Same slot as the pill: after a lost round, one tap re-opens the sheet with
   the same shape + prompt already loaded. */
#retry {
  position: fixed;
  z-index: 34;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 30px);
  min-height: var(--tap);
  max-width: min(300px, calc(100vw - var(--safe-l) - var(--safe-r) - 100px));
  padding: 0 20px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-elev-2);
  color: var(--ink);
  font-size: 14px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.55);
  cursor: pointer;
  animation: pillIn 260ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
#retry:active { transform: translateX(-50%) scale(0.96); }
#retry:hover { background: #2e2e34; }

/* #pill, #retry and #drawHint all live in the same slot above the FAB. While
   drawing or refining, the two status chips step aside for the draw hint and
   the refine toolbar — they come back on their own when the user exits. This
   matters now that submitting doesn't end your turn: you can redraw and submit
   again to replace your entry, so the pill is up while you draw. */
body.mode-draw #retry,
body.mode-refine #retry,
body.mode-draw #pill,
body.mode-refine #pill { display: none !important; }

/* -- 6. Toasts ------------------------------------------------------------ */

#toasts {
  position: fixed;
  z-index: 60;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 96px);
  width: min(420px, calc(100vw - 32px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
/* While the sheet is up, float toasts just above it (never off-screen). */
body.sheet-open #toasts {
  bottom: min(calc(var(--sheet-h, 340px) + 12px), calc(100dvh - 76px));
}

.toast {
  background: rgba(30, 30, 33, 0.95);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 15px;
  font-size: 13.5px;
  color: var(--ink);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  text-align: center;
  max-width: 100%;
  animation: toastIn 200ms ease-out;
}
.toast.is-out { animation: toastOut 220ms ease-in forwards; }
.toast.is-win { background: #fff; color: #111; border-color: #fff; font-weight: 600; }
.toast.is-error { border-color: rgba(255, 107, 94, 0.55); }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}
@keyframes toastOut {
  from { opacity: 1; transform: none; }
  to   { opacity: 0; transform: translateY(6px); }
}

/* -- 7. Bottom sheet ------------------------------------------------------ */

#scrim {
  position: fixed;
  inset: 0;
  z-index: 45;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  transition: opacity 220ms ease;
}
#scrim.is-open { opacity: 1; }

#sheet {
  position: fixed;
  z-index: 50;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 92dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg-elev);
  border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  padding: 0 calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  box-shadow: 0 -14px 44px rgba(0, 0, 0, 0.6);
  transform: translateY(100%);
  transition: transform 280ms cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform;
}
#sheet.is-open { transform: translateY(var(--drag-y, 0px)); }
#sheet.is-dragging { transition: none; }

#sheetHandle {
  padding: 9px 0 6px;
  display: grid;
  place-items: center;
  touch-action: none;
  cursor: grab;
}
#sheetHandle span {
  display: block;
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background: #45454c;
}

.sheet-head {
  display: flex;
  gap: 13px;
  align-items: center;
  padding: 4px 0 12px;
}
#thumb {
  width: 66px;
  height: 66px;
  flex: 0 0 auto;
  border-radius: 12px;
  background: #0d0d0e;
  border: 1px solid var(--line);
}
.sheet-head-text h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.sheet-head-text p {
  margin: 3px 0 0;
  font-size: 12.5px;
  color: var(--ink-dim);
}

/* Grows with the prompt up to ~6 lines, then scrolls inside itself — long
   prompts stay comfortable without pushing the buttons under the keyboard. */
#prompt {
  display: block;
  width: 100%;
  min-height: 92px;
  max-height: 168px;
  overflow-y: auto;
  resize: none;
  padding: 12px 13px;
  font: inherit;
  font-size: 16px; /* ≥16px stops iOS zoom-on-focus */
  color: var(--ink);
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}
#prompt::placeholder { color: var(--ink-faint); }
#prompt:focus { border-color: #55555e; }

.counter {
  display: flex;
  justify-content: flex-end;
  font-size: 11.5px;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
  padding: 5px 2px 0;
}
.counter .is-max { color: var(--danger); }

#turnstile {
  min-height: 66px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0 2px;
}
#turnstile:empty::after {
  content: "Loading verification…";
  font-size: 12px;
  color: var(--ink-faint);
}

#sheetError {
  margin: 6px 0 0;
  padding: 9px 11px;
  font-size: 13px;
  color: #ffd9d5;
  background: rgba(255, 107, 94, 0.13);
  border: 1px solid rgba(255, 107, 94, 0.4);
  border-radius: 10px;
}

.sheet-actions {
  display: flex;
  gap: 10px;
  padding-top: 12px;
}

/* -- 8. Buttons ----------------------------------------------------------- */

.btn {
  flex: 1 1 0;
  min-height: var(--tap);
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid transparent;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 140ms ease, transform 120ms ease;
}
.btn:active { transform: scale(0.985); }
.btn[disabled] { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost {
  background: transparent;
  color: var(--ink-dim);
  border-color: var(--line);
  flex: 0 0 38%;
}

/* -- 9. Refine toolbar ---------------------------------------------------- */

/* Stage 2 sits where the thumb already is: bottom of the screen, above the
   FAB slot (the FAB itself is hidden while refining — Cancel replaces it). */
#refineBar {
  position: fixed;
  z-index: 36;
  left: calc(var(--safe-l) + 8px);
  right: calc(var(--safe-r) + 8px);
  bottom: calc(var(--safe-b) + 10px);
  margin: 0 auto;
  max-width: 480px;
  padding: 8px;
  background: rgba(24, 24, 26, 0.9);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
  touch-action: manipulation;
  animation: refineIn 200ms cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes refineIn {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}

#refineSize {
  padding: 2px 4px 7px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-dim);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#refineSize.is-warn { color: #ffd48a; }
#refineSize.is-bad { color: #ffb3ab; }

.refine-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
}
.refine-row + .refine-row { padding-top: 6px; }

.rfbtn {
  flex: 1 1 auto;
  min-width: 0;
  min-height: var(--tap);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--bg-elev-2);
  color: var(--ink-dim);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 130ms ease, color 130ms ease, transform 110ms ease;
}
.rfbtn:active { transform: scale(0.96); }
.rfbtn svg { width: 17px; height: 17px; flex: 0 0 auto; }
.rfbtn.is-on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.rfbtn[disabled] { opacity: 0.4; cursor: default; }

.rfbtn-icon {
  flex: 0 0 auto;
  width: var(--tap);
  padding: 0;
}
.rfbtn-icon svg { width: 19px; height: 19px; }

.rfseg {
  flex: 0 0 auto;
  display: flex;
  border: 1px solid var(--line);
  border-radius: 11px;
  overflow: hidden;
  background: var(--bg-elev-2);
}
.rfseg button {
  width: 36px;
  min-height: var(--tap);
  padding: 0;
  border: none;
  background: transparent;
  color: var(--ink-dim);
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
}
.rfseg button + button { border-left: 1px solid var(--line); }
.rfseg button.is-on { background: #3a3a42; color: var(--ink); }

.refine-spacer { flex: 1 1 auto; }

/* Protect is a mode, not a brush: it reads cyan so it can't be mistaken for
   the white "selected brush" state, and it parks the brush controls. */
#rfProtect.is-on {
  background: #17515c;
  border-color: #2aa3b8;
  color: #b9f2fb;
}
body.mode-protect .rfseg,
body.mode-protect #rfAdd,
body.mode-protect #rfErase { opacity: 0.55; }
body.mode-protect #rfAdd.is-on,
body.mode-protect #rfErase.is-on {
  background: var(--bg-elev-2);
  border-color: var(--line);
  color: var(--ink-dim);
}

body.mode-refine #fab,
body.mode-refine #drawHint { display: none !important; }
body.mode-refine #toasts { bottom: calc(var(--safe-b) + 218px); }

/* -- 10. Motion preferences / wide screens -------------------------------- */

@media (min-width: 720px) {
  #sheet {
    left: 50%;
    right: auto;
    width: 440px;
    transform: translate(-50%, 100%);
    border-radius: 20px;
    bottom: 20px;
    border: 1px solid var(--line);
  }
  #sheet.is-open { transform: translate(-50%, var(--drag-y, 0px)); }
  body.sheet-open #toasts {
    bottom: min(calc(var(--sheet-h, 340px) + 32px), calc(100dvh - 76px));
  }
  #bar { max-width: 460px; }
  #frozenBanner { max-width: 460px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
