* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #fafafa;
  -webkit-user-select: none;
  user-select: none;
}

/* The drawing surface is constrained to a 16:9 (landscape) or 9:16 (portrait)
   frame — sized by layoutStage() in JS and capped at 1920×1080 in the backing
   store — so exports have a consistent aspect ratio. Everything outside is the
   letterbox. */
#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #FCFBF7; /* letterbox — paper (light) */
  z-index: 0;
}

@media (prefers-color-scheme: dark) {
  #stage {
    background: #1A1A1A; /* letterbox — dark */
  }
}

#canvas {
  touch-action: none;
  background: #fff; /* drawing paper */
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06), 0 6px 24px rgba(0, 0, 0, 0.10);
  /* width/height set in JS (layoutStage) to the fitted 16:9 / 9:16 box */
}

/* --- Full-screen overlay (idle / mic-denied states) --- */
#overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 2rem;
  text-align: center;
  background: rgba(250, 250, 250, 0.96);
  z-index: 20;
}

#overlay-heading {
  margin: 0;
  line-height: 0; /* the heading is just the logo image */
}

#overlay-logo {
  display: block;
  width: min(280px, 68vw);
  height: auto;
}

#overlay-msg {
  max-width: 26rem;
  color: #444;
  line-height: 1.5;
}

#privacy-note {
  margin-top: -0.75rem; /* snug it directly under the mic button */
  font-size: 0.72rem;
  color: #aaa;
}

#overlay-msg.error {
  color: #b3261e;
}

#version {
  position: absolute;
  bottom: calc(1rem + env(safe-area-inset-bottom));
  color: #aaa;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

/* --- Top bars: tool bar (armed/recording) and playback bar (stopped/replaying) --- */
.top-bar {
  position: fixed;
  top: max(0.5rem, env(safe-area-inset-top));
  /* left:50% + translateX would cap auto-width layout at half the viewport
     and make the flex row wrap; inset + auto margins centers without that. */
  left: 0;
  right: 0;
  margin-inline: auto;
  width: fit-content;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  max-width: calc(100vw - 1rem);
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  z-index: 10;
}

/* Nine buttons must fit narrow viewports on one row — size them fluidly. */
#toolbar {
  gap: 0.25rem;
}

#toolbar > .swatch,
#toolbar > .tool-btn {
  width: clamp(30px, 8.2vw, 38px);
  height: clamp(30px, 8.2vw, 38px);
}

#undo-btn {
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.15);
  background: #fff;
  color: #444;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}

#undo-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

#clear-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.15);
  background: #fff;
  color: #444;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}

#clear-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

/* --- Modals (task card, clear confirm) --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 20dvh 1rem 1rem; /* dialogs sit 20% down from the top */
  background: rgba(0, 0, 0, 0.35);
}

.modal-card {
  position: relative;
  width: min(24rem, calc(100vw - 2rem)); /* uniform size across all dialogs */
  padding: 1.25rem 1.5rem;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  text-align: center; /* center all dialog copy */
}

/* Header toolbar: app name + close, spanning the card's top. */
.modal-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr; /* logo centered, close pinned right */
  align-items: center;
  margin: -0.6rem -0.9rem 0.75rem;
  padding: 0 0 0.35rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.modal-logo {
  grid-column: 2;
  /* Width-driven, not height:auto — Safari sizes an SVG <img> in a grid track
     by its full intrinsic width when only height is set, overflowing the card.
     806:121 aspect → ~20px tall. */
  width: 133px;
  height: auto;
  display: block;
}

.modal-header .modal-close {
  position: static;
  grid-column: 3;
  justify-self: end;
  width: 34px;
  height: 34px;
  font-size: 1.35rem;
  line-height: 1;
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #111;
}

.modal-note {
  margin-top: 0.5rem;
  color: #666;
  font-size: 0.9rem;
  line-height: 1.4;
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1.25rem;
}

.modal-close {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  width: 44px;
  height: 44px;
  border: none;
  background: none;
  font-size: 1.5rem;
  color: #999;
  cursor: pointer;
  touch-action: manipulation;
}

.task-category {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #888;
}

.task-text {
  margin-top: 0.5rem;
  font-size: 1.15rem;
  line-height: 1.4;
  color: #111;
}

/* --- Accepted-task banner (armed/recording) --- */
#task-banner {
  position: fixed;
  top: calc(max(0.5rem, env(safe-area-inset-top)) + 64px);
  left: 0;
  right: 0;
  margin-inline: auto;
  max-width: calc(100vw - 2rem);
  text-align: center;
  color: #1a1a1a; /* black, no pill around it */
  font-size: 1.17rem; /* 30% larger than before */
  font-weight: 600;
  line-height: 1.3;
  pointer-events: none;
  z-index: 9;
}

/* --- Brush settings popover (long-press a swatch) --- */
#brush-popover {
  position: fixed;
  left: 0;
  right: 0;
  margin-inline: auto;
  width: fit-content;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.75rem; /* width is synced to the toolbar in openPopover() */
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  z-index: 15;
}

.popover-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

/* The 16 colors sit next to the preview as an 8-column grid. */
#popover-swatches {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.3rem;
  align-content: center;
}

.swatch.mini {
  width: 100%;
  height: auto;
  aspect-ratio: 1; /* fills its grid cell, stays square (>=24px on phones) */
}

.swatch.mini.selected {
  outline-offset: 1px;
}

.popover-row label {
  width: 4em;
  color: #444;
  font-size: 0.9rem;
}

.popover-row input[type="range"] {
  flex: 1;
  min-width: 0; /* range inputs have a ~129px intrinsic width; allow shrink */
  touch-action: manipulation;
}

.popover-value {
  font-variant-numeric: tabular-nums;
  min-width: 3em;
  text-align: right;
  color: #111;
}

/* The dialog's X sits inline at the end of the preview/sliders row (not
   absolute like the shared .modal-close) to keep the dialog short. */
#popover-close {
  position: static;
  width: 32px;
  height: 32px;
  align-self: flex-start;
  margin: -6px -6px 0 0;
  flex-shrink: 0;
  font-size: 1.3rem;
  line-height: 1;
}

#popover-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

/* Size + opacity live BELOW the colors now (full width). */
#popover-sliders {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

#brush-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  overflow: hidden;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 8px;
  cursor: pointer;
}

#brush-preview-dot {
  border-radius: 50%;
}

.swatch {
  -webkit-touch-callout: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.15);
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Slot buttons show a nib-preview dot (size + opacity of that slot). */
.swatch-dot {
  border-radius: 50%;
  pointer-events: none;
}

.swatch.selected {
  outline: 2px solid #111;
  outline-offset: 2px;
}

.swatch.eraser {
  background:
    repeating-linear-gradient(135deg, #fff 0 5px, #e4e4e4 5px 10px);
}

/* --- Save hint under the playback bar (stopped, nothing saveable yet) --- */
#save-hint {
  position: fixed;
  top: calc(max(0.5rem, env(safe-area-inset-top)) + 64px);
  left: 0;
  right: 0;
  text-align: center;
  color: #999;
  font-size: 0.9rem;
  pointer-events: none;
  z-index: 9;
}

/* --- Hint shown while armed (below the task banner when both show) --- */
#hint {
  position: fixed;
  top: calc(max(0.5rem, env(safe-area-inset-top)) + 104px);
  left: 0;
  right: 0;
  text-align: center;
  color: #999;
  pointer-events: none;
  z-index: 10;
}

/* --- "Talk out loud" reminder (first 3s of recording), small, near bottom --- */
#talk-reminder {
  position: fixed;
  left: 50%;
  bottom: calc(7rem + env(safe-area-inset-bottom)); /* above the countdown bar + Stop */
  transform: translateX(-50%);
  z-index: 14;
  max-width: min(92vw, 18rem);
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 999px;
  background: rgba(17, 17, 17, 0.88);
  color: #fff;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

/* --- Bottom controls bar --- */
#controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid rgba(0, 0, 0, 0.08);
  z-index: 10;
}

.btn {
  min-height: 44px;
  padding: 0 1.25rem;
  border: 1px solid #ccc;
  border-radius: 22px;
  background: #fff;
  color: #111;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: #111;
  border-color: #111;
  color: #fff;
}

.btn-danger {
  background: #d5292f;
  border-color: #d5292f;
  color: #fff;
}

/* --- Countdown bar (shrinks over the recording limit AND during replay) --- */
#rec-progress {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(4.9rem + env(safe-area-inset-bottom)); /* just above the Stop button */
  display: flex;
  justify-content: center;
  z-index: 11;
  pointer-events: none;
}

#rec-progress-track {
  width: min(280px, 70vw);
  height: 8px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

#rec-progress-bar {
  display: block;
  height: 100%;
  width: 100%; /* driven down to 0% by rAF while recording */
  border-radius: 999px;
  background: #d5292f;
}

/* --- Zoom reset (shown while the canvas is zoomed in) --- */
#zoom-reset {
  position: fixed;
  right: max(0.75rem, env(safe-area-inset-right));
  bottom: calc(5.25rem + env(safe-area-inset-bottom));
  z-index: 11;
  padding: 0 0.9rem;
  font-size: 0.85rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
}

/* --- Playback speed --- */
#speed-control {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

#speed-slider {
  width: 110px;
  touch-action: manipulation;
}

#speed-label {
  font-variant-numeric: tabular-nums;
  min-width: 3.2em; /* fits "3.75×" without bar jitter */
  color: #111;
}

/* --- Recording indicator --- */
#rec-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 600;
  color: #d5292f;
  font-variant-numeric: tabular-nums;
}

#capture-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: #0a58c2;
}

#capture-indicator .rec-dot {
  width: 8px;
  height: 8px;
  background: #0a58c2;
}

.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #d5292f;
  animation: rec-pulse 1.2s ease-in-out infinite;
}

@keyframes rec-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

[hidden] {
  display: none !important;
}

/* Visible keyboard focus for the custom round controls (mouse/touch use
   :focus-visible so the ring only shows for keyboard users). */
:where(button, a, input, [tabindex]):focus-visible {
  outline: 3px solid #0a58c2;
  outline-offset: 2px;
}

/* Respect reduced-motion: drop the pulses and the button press-scale. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .btn:active {
    transform: none;
  }
}
