:root {
  color-scheme: light dark;
  --ink: #2a2622;
  --paper: #faf8f3;
  --line: #d8d2c4;
  --status-alone: #b9b2a0;
  --status-a: #6fae8f;
  --status-b: #5b9bd5;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: system-ui, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  padding-top: max(0.6rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.room {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

#roomCode {
  font-weight: 600;
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Presence indicator: a row of 4 dots, filled left-to-right one per
   connected peer (capped at 4) — a glance-readable signal-strength shape,
   no numbers, no text. Filled dots get a drifting gradient; while alone
   ([data-state="searching"], the default until the first peer connects)
   they chase in sequence instead of sitting flat — public-tracker
   discovery can take anywhere from seconds to over a minute, and a static
   dim row looks identical whether it's still working or has given up. */
#status {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  flex-shrink: 0;
}

#status i {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: var(--status-alone);
  opacity: 0.35;
  transition: opacity 0.3s ease;
}

#status[data-state="searching"] i {
  animation: dot-chase 1.4s ease-in-out infinite;
}

#status[data-state="searching"] i:nth-child(1) { animation-delay: 0s; }
#status[data-state="searching"] i:nth-child(2) { animation-delay: 0.15s; }
#status[data-state="searching"] i:nth-child(3) { animation-delay: 0.3s; }
#status[data-state="searching"] i:nth-child(4) { animation-delay: 0.45s; }

@keyframes dot-chase {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}

#status i.filled {
  opacity: 1;
  background: linear-gradient(120deg, var(--status-a), var(--status-b), var(--status-a));
  background-size: 200% 200%;
  animation: status-drift 3s ease-in-out infinite;
}

@keyframes status-drift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

@media (prefers-reduced-motion: reduce) {
  #status i.filled { animation: none; background: var(--status-b); }
  #status[data-state="searching"] i { animation: none; }
}

button {
  font: inherit;
  padding: 0.6rem 1rem;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 0.6rem;
  background: var(--paper);
  color: var(--ink);
}

#log {
  list-style: none;
  margin: 0;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  flex: 1;
  min-height: 0; /* required for overflow to work on a flex child — without it
                    some mobile browsers grow #log to fit its content instead
                    of scrolling internally */
  overflow-y: auto;
  scroll-behavior: smooth;
}

#log .entry {
  display: flex;
}

#log .entry.them { justify-content: flex-start; }
#log .entry.me { justify-content: flex-end; }

#log .entry canvas {
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  background: var(--paper);
}

/* Sent while alone — nobody to reach live yet. Same gray as the "alone"
   status dot, so the color itself carries the meaning; clears once a peer
   connects and backfill takes over. */
#log .entry.pending canvas {
  animation: pending-pulse 1.6s ease-in-out infinite;
}

@keyframes pending-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--status-alone) 55%, transparent); }
  50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--status-alone) 0%, transparent); }
}

@media (prefers-reduced-motion: reduce) {
  #log .entry.pending canvas { animation: none; box-shadow: 0 0 0 2px var(--status-alone); }
}

#log .empty {
  margin: auto;
  color: #8a8375;
  text-align: center;
}

#log .entry canvas {
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 480px;
}

#compose {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

#composeDetails {
  width: 100%;
  max-width: 480px;
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  overflow: hidden;
}

#composeDetails summary {
  padding: 0.6rem 1rem;
  cursor: pointer;
  font-weight: 600;
}

/* Collapsed by default (peek), grows to reveal the full square when opened
   — tapping the summary is a separate gesture from touching the canvas
   itself, so opening never overlaps with an active drawing touch. Height
   is JS-set in px (app.js, driven by the native `toggle` event) rather
   than CSS grid-rows: the wrap clips the view, but composeCanvas's own
   box (and therefore normalization math) never changes size — proven
   necessary the hard way. `display: block` overrides the browser's
   default of unrendering this when <details> is closed, so our own
   height transition can actually play instead of an instant snap. */
#composeCanvasWrap {
  display: block;
  overflow: hidden;
  transition: height 0.25s ease;
}

/* Same 1:1 aspect as every log entry — sender and receiver boxes must
   match shape, since normalized points are only resolution-independent,
   not aspect-independent (see geometry.js). */
#composeCanvas {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #fff;
}

@media (prefers-reduced-motion: reduce) {
  #composeCanvasWrap { transition: none; }
}

/* Both drawing surfaces need the same touch/selection guards — no page
   scroll or long-press callout hijacking a stroke, on any device. */
#composeCanvas,
#log .entry canvas {
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

#composeActions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

#composeActions button {
  flex: 0 0 auto;
}

#sendBtn {
  font-weight: 600;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #ece7dd;
    --paper: #1c1a17;
    --line: #3a352c;
  }
  #composeCanvas { background: #24211d; }
}
