:root {
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background-color: var(--viewport-surround, var(--touch-background));
  color: #f5f7f5;
  --touch-background: #080f0b;
  --accent: #dbffa8;
  --muted: #a7ada8;
  --edge: rgb(255 255 255 / 16%);
  --tool-size: 44px;
  --guide-text-shadow: 0 1px 3px rgb(0 0 0 / 96%), 0 0 8px rgb(0 0 0 / 84%);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background-color: var(--viewport-surround, var(--touch-background));
}
button, input, select { font: inherit; }
html, body { overscroll-behavior: none; }
button, select { color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: default; }
:where(button, input, select):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.workspace {
  /* Keep the viewport surface in the document for browsers with floating toolbars. */
  position: relative;
  isolation: isolate;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.stage {
  position: absolute;
  inset: 0;
  isolation: isolate;
  overflow: hidden;
  background: radial-gradient(ellipse closest-side at center, #1a2921, var(--touch-background));
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#camera-preview, #string-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#camera-feed {
  /* Keep Safari's inline decoder active without exposing its video layer. */
  position: absolute;
  inset: 0 auto auto 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.stage:has(#camera-preview:not([hidden]))::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgb(0 0 0 / 18%);
  pointer-events: none;
}

#string-canvas { z-index: 1; }
.workspace-header, .workspace-footer {
  position: absolute;
  z-index: 2;
  right: 0;
  left: 0;
  display: grid;
  gap: 8px;
  padding-inline: max(10px, env(safe-area-inset-left)) max(10px, env(safe-area-inset-right));
  pointer-events: none;
}

.workspace-header { top: max(5px, env(safe-area-inset-top)); }
.workspace-footer { bottom: max(9px, env(safe-area-inset-bottom)); justify-items: center; }
.mode-toolbar, .mode-switch, .tools { display: flex; align-items: center; }
.mode-toolbar { min-width: 0; width: 100%; gap: 8px; }
.mode-toolbar button { pointer-events: auto; }
.tools { gap: 6px; justify-content: center; max-width: 100%; pointer-events: auto; }
#library-toggle { margin-inline-start: auto; }
#library-toggle, #help-toggle { color: var(--accent); }
#pages-toggle { font-variant-numeric: tabular-nums; }
#pages-toggle[aria-expanded="true"], #library-toggle[aria-expanded="true"] { background: #25331d; }

.tool {
  display: grid;
  flex: none;
  width: var(--tool-size);
  height: var(--tool-size);
  padding: 0;
  place-items: center;
  border: 1px solid var(--edge);
  border-radius: 50%;
  background: rgb(0 0 0 / 69%);
}

.tool svg, .tool-pill svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.7;
}

.tool:disabled { opacity: 0.45; }
.tool.is-destructive { color: #ff7970; }
#draw-string[aria-pressed="true"], #audio-toggle, #done-edit, #markers[aria-pressed="true"],
#mute[aria-pressed="false"], #labels-toggle:where(:not([data-inactive])) { color: var(--accent); }
.tool[aria-expanded="true"], #draw-string[aria-pressed="true"], #help-toggle[aria-pressed="true"] { background: #25331d; }
#mute .speaker-muted, #mute[aria-pressed="true"] .speaker-waves { display: none; }
#mute[aria-pressed="true"] .speaker-muted { display: initial; }

.tool-pill {
  display: inline-flex;
  flex: none;
  min-height: var(--tool-size);
  min-width: 0;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 11px;
  border: 1px solid var(--edge);
  border-radius: 999px;
  background: rgb(0 0 0 / 72%);
  font-size: 0.8125rem;
  font-weight: 650;
  white-space: nowrap;
}
.tool-pill svg { width: 15px; height: 15px; }
#string-sound { max-width: min(32vw, 132px); }

.mode-switch {
  width: min(188px, 38vw);
  min-width: 96px;
  padding: 2px;
  border: 1px solid var(--edge);
  border-radius: 9px;
  background: rgb(0 0 0 / 64%);
}

.mode-switch button {
  flex: 1;
  min-height: calc(var(--tool-size) - 6px);
  padding: 4px 6px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  font-size: 0.8125rem;
  font-weight: 650;
}

.mode-switch button[aria-pressed="true"] { background: #343936; color: #fff; }

#workspace-status {
  justify-self: center;
  max-width: min(100%, 330px);
  margin: 0;
  padding-inline: 10px;
  color: #fff;
  font-size: 0.8125rem;
  font-weight: 550;
  line-height: 1.35;
  text-align: center;
  text-shadow: var(--guide-text-shadow);
  text-wrap: pretty;
}

#workspace-status:empty { display: none; }

.control-panel {
  display: grid;
  gap: 10px;
  width: min(295px, 100%);
  max-height: 54dvh;
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--edge);
  border-radius: 16px;
  background: #121a15;
  font-size: 0.875rem;
  pointer-events: auto;
  box-shadow: 0 10px 28px rgb(0 0 0 / 32%);
}

.pages-panel { justify-self: end; width: min(220px, 100%); }
.labels-panel { width: min(245px, 100%); }
.panel-group { display: grid; gap: 3px; }
.panel-group + .panel-group { border-top: 1px solid var(--edge); padding-top: 8px; }
.panel-group h2 { margin: 0 0 3px; color: var(--muted); font-size: 0.75rem; font-weight: 600; }

.control-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 7px 14px;
}

.control-row select, .panel-action, .keyboard-notes button {
  min-height: 35px;
  padding: 5px 9px;
  border: 1px solid var(--edge);
  border-radius: 8px;
  background: #202a22;
}

.panel-action { text-align: start; }
.panel-action:not(:disabled):hover { background-color: #293829; }
.panel-action:disabled { opacity: 0.5; }
.control-row input { width: 100%; margin: 0; accent-color: var(--accent); }
.control-row input[type="checkbox"] { justify-self: end; width: 18px; height: 18px; }
#mic-toggle { justify-self: start; }
#mic-toggle[aria-pressed="true"] { color: var(--accent); }
#mic-meter { width: 100%; accent-color: var(--accent); }
#mic-status { grid-column: 1 / -1; color: var(--muted); font-size: 0.75rem; line-height: 1.3; }

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

.keyboard-notes:focus-within {
  position: absolute;
  z-index: 3;
  right: 10px;
  bottom: max(66px, calc(env(safe-area-inset-bottom) + 57px));
  left: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  pointer-events: none;
}

.keyboard-notes button { pointer-events: auto; }

@media (hover: hover) {
  .camera-permission:hover { background: #edffcf; }
  .tool:not(:disabled):hover, .tool-pill:hover, .mode-switch button:hover { background-color: #253128; }
}

@media (max-width: 420px) {
  .mode-toolbar { gap: 5px; }
  .mode-switch { width: clamp(88px, 33vw, 134px); min-width: 88px; }
  .tool-pill { padding-inline: 8px; }
  #pages-toggle svg { display: none; }
  .tools { gap: 4px; }
}

@media (max-width: 392px) {
  .mode-toolbar { gap: 4px; --tool-size: 40px; }
  .mode-switch { min-width: 84px; }
  .mode-toolbar .tool-pill { padding-inline: 7px; }
}

@media (max-width: 345px) {
  .workspace-header { padding-inline: max(4px, env(safe-area-inset-left)) max(4px, env(safe-area-inset-right)); }
  .mode-toolbar { gap: 3px; --tool-size: 36px; }
  .mode-switch { width: 30vw; min-width: 76px; }
  .mode-switch button { padding-inline: 4px; font-size: 0.75rem; }
  .mode-toolbar .tool-pill { padding-inline: 6px; font-size: 0.75rem; }
  .workspace-footer { padding-inline: max(4px, env(safe-area-inset-left)) max(4px, env(safe-area-inset-right)); }
  .tools { gap: 2px; --tool-size: 42px; }
  .tools svg { width: 17px; height: 17px; }
}

.camera-gate {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.camera-permission {
  display: inline-flex;
  min-height: 46px;
  align-items: center;
  gap: 8px;
  padding: 11px 22px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #0b100e;
  font-size: 1rem;
  font-weight: 650;
  pointer-events: auto;
}
.camera-permission svg { width: 19px; height: 19px; fill: currentColor; }
.camera-permission:active { opacity: 0.78; }
.camera-permission:disabled { opacity: 0.6; }

.library-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  justify-items: center;
  align-items: end;
  background: rgb(0 0 0 / 45%);
}

.library-sheet {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: min(100%, 540px);
  height: min(710px, calc(100% - max(16px, env(safe-area-inset-top))));
  overflow: hidden;
  border: 1px solid var(--edge);
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  background: #111714;
  box-shadow: 0 -8px 36px rgb(0 0 0 / 35%);
}

.library-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 54px;
  border-bottom: 1px solid var(--edge);
  padding-inline: max(16px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right));
}

.library-header h2 { grid-column: 2; margin: 0; font-size: 1.0625rem; font-weight: 670; }
.library-header button {
  justify-self: end;
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--accent);
  font-size: 0.9375rem;
  font-weight: 650;
}

.library-content {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 18px max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.library-section + .library-section { margin-top: 24px; }
.library-section h3 { margin: 0 0 10px; font-size: 1rem; font-weight: 650; }
.library-tip { margin: 9px 0; color: var(--muted); font-size: 0.8125rem; line-height: 1.35; }
.library-content input {
  width: 100%;
  min-height: 42px;
  padding: 8px 11px;
  border: 1px solid var(--edge);
  border-radius: 9px;
  background: #202823;
  color: inherit;
  font-size: 1rem;
}

.library-list {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--edge);
  border-radius: 12px;
  background: #1b231e;
}
.library-list:empty { display: none; }
.library-list > p { margin: 0; padding: 12px; color: var(--muted); font-size: 0.875rem; }
.library-list > * + * { border-top: 1px solid var(--edge); }
.library-list > button, .library-list > div > button {
  min-height: 44px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  text-align: start;
}
.library-list > button {
  display: grid;
  align-content: center;
  gap: 4px;
  min-height: 53px;
  padding: 8px 12px;
}
.library-list > button + button { border-top: 1px solid var(--edge); }
.library-list > button:focus-visible, .library-list > div > button:focus-visible { outline-offset: -3px; }
.library-list > button:hover, .library-list > div > button:hover { background: #29342b; }
.library-list > div { display: flex; align-items: center; min-height: 44px; }
.library-list > div > button { padding: 8px 12px; }
.library-list > div > button:first-child { flex: 1; }
.library-list :where(small, .note-count, .preset-count) { color: var(--muted); font-size: 0.75rem; }

.save-action { min-height: 42px; margin-top: 10px; color: var(--accent); }
#save-form { display: grid; gap: 8px; margin-top: 10px; }
#save-form label { font-size: 0.875rem; }
#confirm-save { justify-self: start; margin: 0; }

.help-guide {
  position: absolute;
  z-index: 1;
  inset: calc(max(5px, env(safe-area-inset-top)) + 49px)
    max(10px, env(safe-area-inset-right))
    calc(max(9px, env(safe-area-inset-bottom)) + 49px)
    max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: #fff;
  text-align: center;
  text-shadow: var(--guide-text-shadow);
  pointer-events: none;
}

#help-gesture {
  display: block;
  width: min(100%, 440px);
  height: auto;
  max-height: max(40px, calc(100% - 84px));
  object-fit: contain;
  aspect-ratio: 22 / 9;
  pointer-events: none;
}

#help-caption {
  max-width: min(100%, 33rem);
  margin: 0;
  padding-inline: 4px;
  font-size: clamp(0.8125rem, 3.5vw, 1rem);
  font-weight: 650;
  line-height: 1.3;
  text-wrap: pretty;
  pointer-events: none;
}

#help-caption:empty { display: none; }

.help-step-switch {
  display: flex;
  justify-content: center;
  gap: 2px;
}

.help-step-switch button {
  min-height: 36px;
  padding: 4px 10px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: inherit;
  font-size: 0.8125rem;
  font-weight: 650;
  text-shadow: inherit;
  pointer-events: auto;
}

.help-step-switch button[aria-pressed="true"] {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
}
