:root {
  --bg: #0a0a0a;
  --bar: rgba(20, 20, 22, 0.86);
  --bar-border: rgba(255, 255, 255, 0.08);
  --fg: #f4f4f5;
  --muted: #a1a1aa;
  --btn: rgba(255, 255, 255, 0.06);
  --btn-hover: rgba(255, 255, 255, 0.13);
  --accent: #ffb020;
  --accent-ink: #1a1206;
  --guide: rgba(255, 176, 32, 0.55);
  --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);
  --bar-h: 56px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { overflow: hidden; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ===== Toolbar ===== */
#toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
  row-gap: 6px;
  padding: 8px calc(10px + var(--safe-r)) 8px calc(10px + var(--safe-l));
  padding-top: calc(8px + var(--safe-t));
  flex: 0 0 auto;
  min-height: 0;
  height: auto;
  background: var(--bar);
  border-bottom: 1px solid var(--bar-border);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  overflow: visible;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.28s ease;
  z-index: 30;
}
#toolbar .spacer { display: none; }

body.bar-hidden #toolbar {
  transform: translateY(-105%);
  opacity: 0;
  pointer-events: none;
}

.spacer { flex: 1 0 8px; }

/* Buttons */
.btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 40px;
  min-width: 40px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 11px;
  background: var(--btn);
  color: var(--fg);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.05s ease, color 0.15s ease;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: scale(0.95); }
.btn-round { padding: 0; width: 40px; }

.btn .ico {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* play/pause use filled glyphs */
.btn .ico-play, .btn .ico-pause { fill: currentColor; stroke: none; }

.btn-label { font-size: 13px; }
.btn small { font-size: 0.7em; }

/* Primary (play) */
.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}
.btn-primary:hover { background: #ffbe45; }

/* play/pause icon swap */
#play .ico-pause { display: none; }
body.playing #play .ico-play { display: none; }
body.playing #play .ico-pause { display: inline; }
body.playing #play .play-label { display: none; }

/* Grouped stepper controls */
.group {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
}
.group-label {
  font-size: 11px;
  color: var(--muted);
  margin-right: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.group .btn-round { height: 34px; width: 34px; }
.readout {
  min-width: 30px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 14px;
}

/* Toggle buttons active state */
.btn-toggle[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.timer {
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 15px;
  color: var(--muted);
  padding: 0 6px;
}
body.playing .timer { color: var(--accent); }

/* ===== Reading area ===== */
#viewport {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  position: relative;
}
#viewport::-webkit-scrollbar { width: 0; height: 0; }

.script {
  padding: 45vh 6vw;
  margin: 0 auto;
  max-width: 1100px;
  font-size: var(--font, 48px);
  line-height: 1.42;
  font-weight: 600;
  text-align: center;
  white-space: pre-wrap;
  word-wrap: break-word;
  letter-spacing: 0.005em;
  transform-origin: center center;
  transition: transform 0.2s ease;
}
.script.empty { color: var(--muted); font-weight: 500; }

body.mirror .script { --sx: -1; }
body.flip .script { --sy: -1; }
.script { transform: scaleX(var(--sx, 1)) scaleY(var(--sy, 1)); }

/* ===== Reading guide + fades ===== */
.guide {
  position: absolute;
  top: 42%;
  left: 0;
  right: 0;
  height: 0;
  pointer-events: none;
  z-index: 10;
}
.guide .line {
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--guide) 20%, var(--guide) 80%, transparent);
  opacity: 0.5;
}
.guide .marker {
  position: absolute;
  top: -7px;
  width: 0; height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
}
.guide .marker.left { left: 0; border-left: 12px solid var(--guide); }
.guide .marker.right { right: 0; border-right: 12px solid var(--guide); }

.fade {
  position: absolute;
  left: 0; right: 0;
  height: 16%;
  pointer-events: none;
  z-index: 9;
}
.fade-top { top: 0; background: linear-gradient(to bottom, var(--bg), transparent); }
.fade-bottom { bottom: 0; background: linear-gradient(to top, var(--bg), transparent); }

/* ===== Reveal button (bar hidden) ===== */
.reveal {
  position: absolute;
  top: calc(8px + var(--safe-t));
  right: calc(10px + var(--safe-r));
  width: 44px;
  height: 44px;
  display: none;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--bar-border);
  border-radius: 50%;
  background: var(--bar);
  color: var(--fg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  cursor: pointer;
  z-index: 40;
  -webkit-tap-highlight-color: transparent;
  animation: fadeIn 0.25s ease;
}
.reveal .ico { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
body.bar-hidden .reveal { display: inline-flex; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ===== Hint ===== */
.hint {
  position: absolute;
  bottom: calc(22px + var(--safe-b));
  left: 50%;
  transform: translateX(-50%);
  max-width: 82vw;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.72);
  border: 1px solid var(--bar-border);
  color: var(--fg);
  font-size: 13.5px;
  text-align: center;
  z-index: 45;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
}
.hint.show { opacity: 1; }

/* ===== Library + Editor overlay ===== */
.editor {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: row;
  background: #0e0e10;
  overflow: hidden;
}
.editor[hidden] { display: none; }

/* sidebar (script library) */
.lib {
  flex: 0 0 300px;
  width: 300px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #0b0b0d;
  border-right: 1px solid var(--bar-border);
}
.lib-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(12px + var(--safe-t)) 12px 12px calc(12px + var(--safe-l));
  border-bottom: 1px solid var(--bar-border);
}
.lib-head strong { font-size: 15px; }
.lib-new { height: 34px; padding: 0 13px; font-size: 13px; }
.lib-list { list-style: none; margin: 0; padding: 6px; flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.lib-item { display: flex; align-items: stretch; border-radius: 10px; margin-bottom: 2px; }
.lib-item:hover { background: rgba(255, 255, 255, 0.05); }
.lib-item.active { background: rgba(255, 176, 32, 0.14); }
.lib-item-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  text-align: left;
  background: none;
  border: 0;
  color: var(--fg);
  padding: 9px 4px 9px 11px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.lib-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-item.empty-title .lib-title { color: var(--muted); font-weight: 500; font-style: italic; }
.lib-meta { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-del {
  flex: 0 0 auto;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  padding: 0 12px;
  border-radius: 10px;
  opacity: 0;
  -webkit-tap-highlight-color: transparent;
}
.lib-del:hover { color: #ff6b6b; }
.lib-del .ico { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.lib-item:hover .lib-del, .lib-item.active .lib-del { opacity: 1; }

/* edit pane */
.edit-pane { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.editor-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 10px 12px;
  border-bottom: 1px solid var(--bar-border);
  background: var(--bar);
}
.editor-hint { color: var(--muted); font-size: 12.5px; }
.lib-toggle { display: none; }
.lib-toggle .ico { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#scriptInput {
  flex: 1 1 auto;
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  padding: 18px calc(18px + var(--safe-r)) calc(18px + var(--safe-b)) 18px;
  background: transparent;
  color: var(--fg);
  font-size: 18px;
  line-height: 1.6;
  font-family: inherit;
}
.lib-scrim { display: none; }

/* mobile: the sidebar becomes a slide-in drawer */
@media (max-width: 720px) {
  .lib {
    position: absolute;
    top: 0; bottom: 0; left: 0;
    z-index: 6;
    width: 84%;
    max-width: 320px;
    flex-basis: auto;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
  }
  .editor.lib-open .lib { transform: translateX(0); }
  .lib-scrim {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 5;
    background: rgba(0, 0, 0, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }
  .editor.lib-open .lib-scrim { opacity: 1; pointer-events: auto; }
  .lib-toggle { display: inline-flex; }
}
@media (prefers-reduced-motion: reduce) {
  .lib, .lib-scrim { transition: none; }
}

/* ===== Sync ===== */
.lib-head-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sync-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-top: 10px;
  padding: 8px 10px;
  border: 1px solid var(--bar-border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  text-align: left;
}
.sync-chip:hover { background: rgba(255, 255, 255, 0.08); }
.sync-dot {
  flex: 0 0 auto;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: #6b7280; /* off = grey */
}
.sync-dot.ok { background: #4ade80; }
.sync-dot.syncing { background: var(--accent); animation: micpulse 1.2s infinite; }
.sync-dot.error { background: #ff5d5d; }
body.synced .sync-chip { color: var(--fg); }

/* Sync modal */
.sync-panel {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.6);
}
.sync-panel[hidden] { display: none; }
.sync-card {
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  background: #141416;
  border: 1px solid var(--bar-border);
  border-radius: 16px;
  padding: 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.sync-card h3 { margin: 0 0 6px; font-size: 18px; }
.sync-card p { margin: 0 0 14px; color: var(--muted); font-size: 13.5px; line-height: 1.5; }
.sync-card label { display: block; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 14px 0 6px; }
.sync-card hr { border: 0; border-top: 1px solid var(--bar-border); margin: 18px 0; }
.code-row { display: flex; align-items: center; gap: 8px; }
.code-big {
  flex: 1 1 auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--accent);
  background: rgba(255, 176, 32, 0.08);
  border: 1px dashed rgba(255, 176, 32, 0.4);
  border-radius: 10px;
  padding: 10px 12px;
  text-align: center;
  user-select: all;
}
.sync-card input {
  flex: 1 1 auto;
  min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 16px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--fg);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--bar-border);
  border-radius: 10px;
  padding: 11px 12px;
  outline: none;
}
.sync-card input:focus { border-color: var(--accent); }
.sync-card .btn { height: 42px; }
.sync-card .full { width: 100%; margin-top: 12px; justify-content: center; }
.sync-state { font-size: 13.5px; font-weight: 600; }
.sync-state.ok { color: #4ade80; }
.sync-state.syncing { color: var(--accent); }
.sync-state.error { color: #ff6b6b; }
.btn.danger { color: #ff6b6b; }
.btn.danger:hover { background: rgba(255, 107, 107, 0.12); }
.sync-close { position: absolute; }
.sync-card .row-actions { display: flex; gap: 8px; margin-top: 16px; }
.sync-card .row-actions .btn { flex: 1 1 auto; justify-content: center; }
.sync-hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ===== Wider screens ===== */
@media (min-width: 700px) {
  .group-label { display: inline; }
  .script { padding-left: 8vw; padding-right: 8vw; }
}
@media (max-width: 480px) {
  .group-label { display: none; }
  .btn-toggle .btn-label, .btn[data-act="edit"] .btn-label { display: none; }
}

/* ===== Voice mode ===== */
.btn-voice { gap: 7px; }
.mic-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff4d4d;
  display: none;
  flex: 0 0 auto;
}
#voiceBtn.listening { background: var(--accent); color: var(--accent-ink); }
#voiceBtn.listening .mic-dot { display: inline-block; animation: micpulse 1.3s infinite; }
@keyframes micpulse {
  0%   { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.6); }
  70%  { box-shadow: 0 0 0 8px rgba(255, 77, 77, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0); }
}

/* word spans (only meaningful in voice mode) */
.script .w.spoken {
  color: var(--accent);
  text-shadow: 0 0 22px rgba(255, 176, 32, 0.35);
}
body.voice .timer { color: var(--accent); }

/* ===== Landscape / short viewports (primary mobile use) ===== */
@media (max-height: 560px) and (orientation: landscape) {
  :root { --bar-h: 46px; }
  #toolbar {
    min-height: 46px;
    gap: 6px;
    padding-top: calc(6px + var(--safe-t));
    padding-bottom: 6px;
  }
  .btn { height: 34px; border-radius: 9px; }
  .btn-round { width: 34px; }
  .group { padding: 2px 6px; }
  .group .btn-round { height: 30px; width: 30px; }
  .group-label { display: none; }
  .btn-toggle .btn-label,
  .btn[data-act="edit"] .btn-label,
  .play-label { display: none; }
  .btn-primary { padding: 0 14px; }
  /* less dead space so more lines are visible while reading */
  .script { padding-top: 40vh; padding-bottom: 45vh; padding-left: 5vw; padding-right: 5vw; }
  .guide { top: 40%; }
  .fade { height: 22%; }
  .reveal { width: 40px; height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  #toolbar, .script, .reveal, .hint { transition: none; animation: none; }
}
