/* Chrome components: slabs, option groups, value controls, tabs, HUD, menu, dialogs, toast. */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-weight: 500;
  font-size: var(--fs-ui);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
}

::selection {
  background: var(--sel-bg);
  color: var(--sel-fg);
}

button,
input,
textarea,
select {
  font: inherit;
  color: inherit;
}

[hidden] {
  display: none !important;
}

:focus {
  outline: none;
}

:focus-visible {
  outline: 2px solid var(--sel-bg);
  outline-offset: 2px;
}

.caps {
  text-transform: uppercase;
  letter-spacing: var(--track);
}

.num {
  font-variant-numeric: tabular-nums;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- slab button ---------- */

.slab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--touch);
  min-width: var(--touch);
  padding: 0 14px;
  border: 0;
  border-radius: var(--r-slab);
  background: var(--slab);
  color: var(--text);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 14px;
  line-height: 1.1;
  text-transform: uppercase;
  letter-spacing: var(--track);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  white-space: nowrap;
}

.slab:active,
.slab.pressed {
  background: var(--slab-hi);
}

@media (hover: hover) {
  .slab:hover {
    background: var(--slab-hi);
  }
}

.slab.sel,
.slab[aria-pressed='true'],
.slab[aria-checked='true'] {
  background: var(--sel-bg);
  color: var(--sel-fg);
}

.slab.live,
.slab.live:hover {
  background: var(--live);
  color: var(--live-fg);
}

.slab.primary,
.slab.primary:hover {
  background: var(--sel-bg);
  color: var(--sel-fg);
}

.slab:disabled,
.slab[aria-disabled='true'] {
  opacity: 0.4;
  cursor: default;
}

.slab.icon {
  padding: 0;
  width: var(--touch);
}

.slab svg {
  width: 22px;
  height: 22px;
  flex: none;
  fill: currentColor;
}

.slab.wrap {
  white-space: normal;
  text-align: center;
}

/* ---------- option group ---------- */

.opts {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

.opts .slab {
  flex: 1 1 auto;
  min-width: 64px;
}

.swatch {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 22px;
  border-radius: var(--r-slab);
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0;
  text-transform: none;
  box-shadow: inset 0 0 0 1px var(--line);
}

.slab[aria-checked='true'] .swatch,
.slab.sel .swatch {
  box-shadow: inset 0 0 0 1px var(--sel-fg);
}

/* ---------- value control ---------- */

.vc {
  display: grid;
  grid-template-columns: var(--touch) minmax(64px, 1fr) var(--touch);
  grid-template-rows: auto auto;
  gap: 2px;
  width: 100%;
  max-width: 420px;
}

.vc .vc-val {
  min-width: 0;
  padding: 0 6px;
  font-size: var(--fs-value);
  background: var(--panel);
  box-shadow: inset 0 0 0 1px var(--line);
}

.vc .vc-val .unit {
  color: var(--muted);
  font-size: 12px;
}

.vc input.vc-entry {
  width: 100%;
  min-width: 0;
  height: var(--touch);
  padding: 0 6px;
  border: 0;
  border-radius: var(--r-slab);
  background: var(--sel-bg);
  color: var(--sel-fg);
  font-weight: 600;
  font-size: var(--fs-value);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.vc .vc-slider {
  grid-column: 1 / -1;
}

.vc.roving:focus-visible {
  outline-offset: 4px;
}

/* range slider, slab idiom */
input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 32px;
  margin: 0;
  background: transparent;
  accent-color: var(--text);
  cursor: pointer;
  touch-action: none;
}

input[type='range']::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: var(--line);
}

input[type='range']::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: var(--line);
}

input[type='range']::-moz-range-progress {
  height: 4px;
  background: var(--text);
}

input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 26px;
  margin-top: -11px;
  border: 0;
  border-radius: var(--r-slab);
  background: var(--sel-bg);
}

input[type='range']::-moz-range-thumb {
  width: 14px;
  height: 26px;
  border: 0;
  border-radius: var(--r-slab);
  background: var(--sel-bg);
}

/* ---------- section heading / labels ---------- */

.section-h {
  display: flex;
  align-items: center;
  gap: var(--s2);
  margin: var(--s4) 0 var(--s2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
}

.section-h::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--line);
}

.label {
  color: var(--muted);
  font-size: var(--fs-label);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
}

/* ---------- tabs and page dots ---------- */

.tabs {
  display: flex;
  gap: var(--s1);
  min-width: 0;
}

.tab {
  min-height: var(--touch);
  padding: 0 12px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 15px;
  text-transform: uppercase;
  letter-spacing: var(--track);
  cursor: pointer;
  touch-action: manipulation;
}

.tab[aria-selected='true'] {
  color: var(--text);
  border-bottom-color: var(--text);
}

.dots {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-width: 0;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px var(--muted);
}

.dot.on {
  background: var(--text);
  box-shadow: none;
}

/* ---------- app frame ---------- */

.app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  height: 100vh;
  height: 100dvh;
}

.app > .script-view,
.app > .read-view {
  grid-row: 1;
  grid-column: 1;
  min-height: 0;
}

.app[data-mode='script'] .read-view,
.app[data-mode='read'] .script-view {
  display: none;
}

.app.immersive {
  grid-template-rows: minmax(0, 1fr) 0;
}

.app.immersive .bottom-bar {
  display: none;
}

/* bottom bar */
.bottom-bar {
  grid-row: 2;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--s2);
  padding: var(--s2) calc(var(--s2) + var(--safe-r)) calc(var(--s2) + var(--safe-b)) calc(var(--s2) + var(--safe-l));
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.bottom-bar .bb-left {
  justify-self: start;
}

.bottom-bar .bb-right {
  justify-self: end;
}

.seg {
  display: flex;
  gap: 2px;
}

.seg .slab {
  min-width: 84px;
}

.app[data-mode='script'] #bb-full {
  visibility: hidden;
}

@media (max-width: 380px) {
  .seg .slab {
    min-width: 64px;
    padding: 0 10px;
  }
  .bottom-bar .slab {
    padding: 0 10px;
  }
}

/* ---------- script view ---------- */

.script-view {
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  padding: calc(var(--s3) + var(--safe-t)) calc(var(--s4) + var(--safe-r)) var(--s3) calc(var(--s4) + var(--safe-l));
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}

.script-head {
  display: flex;
  align-items: flex-end;
  gap: var(--s3);
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--s1);
  flex: 1;
  min-width: 0;
}

.field input {
  height: var(--touch);
  width: 100%;
  min-width: 0;
  padding: 0 var(--s3);
  border: 0;
  border-radius: var(--r-slab);
  background: var(--slab);
  color: var(--text);
  font-size: var(--fs-value);
  font-weight: 500;
}

.field input::placeholder {
  color: var(--dim);
}

.word-count {
  flex: none;
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: var(--fs-label);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
  font-variant-numeric: tabular-nums;
}

.script-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
}

.script-actions .slab {
  flex: 1 1 auto;
}

.script-view.kbd-up .script-actions {
  display: none;
}

.editor-wrap {
  position: relative;
  flex: 1;
  min-height: 120px;
  display: flex;
}

.editor {
  flex: 1;
  width: 100%;
  min-height: 0;
  padding: var(--s4);
  border: 1px solid var(--line);
  border-radius: var(--r-panel);
  background: var(--panel);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 18px;
  line-height: 1.5;
  resize: none;
  caret-color: var(--text);
  overscroll-behavior: contain;
}

.editor::placeholder {
  color: var(--dim);
}

.editor:focus-visible {
  outline-offset: -2px;
}

.format-guard {
  position: absolute;
  inset: var(--s3);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  gap: var(--s4);
  padding: var(--s5);
  border-left: 3px solid var(--live);
  border-radius: var(--r-panel);
  background: var(--panel);
  box-shadow: var(--shadow-float);
}

.format-guard p {
  margin: 0;
  max-width: 52ch;
  font-size: 17px;
  line-height: 1.4;
}

/* ---------- panels: menu, sheet, dialog ---------- */

@keyframes panel-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slide-from-right {
  from {
    opacity: 0.2;
    transform: translateX(24px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes slide-from-left {
  from {
    opacity: 0.2;
    transform: translateX(-24px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: stretch;
  justify-content: center;
  background: var(--panel);
  animation: fade-in var(--t-panel) ease-out;
}

.panel {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-width: 0;
  background: var(--panel);
  animation: panel-in var(--t-panel) ease-out;
}

@media (min-width: 900px) {
  .layer {
    align-items: center;
    padding: var(--s5);
    background: rgba(10, 10, 11, 0.6);
  }
  .panel {
    max-width: 1100px;
    max-height: 720px;
    border-radius: var(--r-panel);
    box-shadow: var(--shadow-float);
  }
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s2);
  padding: calc(var(--s2) + var(--safe-t)) calc(var(--s3) + var(--safe-r)) 0 calc(var(--s3) + var(--safe-l));
  border-bottom: 1px solid var(--line);
}

@media (min-width: 900px) {
  .panel-head {
    padding-top: var(--s2);
    padding-left: var(--s3);
    padding-right: var(--s3);
  }
}

.panel-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
}

.panel-head .tabs {
  order: 3;
  flex: 1 1 100%;
}

.panel-head .tabs .tab {
  flex: 1;
}

.head-tools {
  display: flex;
  align-items: center;
  gap: var(--s2);
  margin-left: auto;
  padding-bottom: var(--s2);
}

.transport {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  margin-right: auto;
  padding-bottom: var(--s2);
  color: var(--muted);
  font-size: var(--fs-label);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
  font-variant-numeric: tabular-nums;
}

.transport .state {
  padding: 3px 6px;
  border-radius: var(--r-slab);
}

.transport .state.running {
  background: var(--live);
  color: var(--live-fg);
}

@media (min-width: 720px) {
  .panel-head {
    flex-wrap: nowrap;
  }
  .panel-head .tabs {
    order: 0;
    flex: 0 1 auto;
  }
  .panel-head .tabs .tab {
    flex: none;
  }
  .transport {
    margin-left: auto;
    margin-right: 0;
  }
  .head-tools {
    margin-left: 0;
  }
}

.panel-body {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  padding: var(--s2) calc(var(--s4) + var(--safe-r)) var(--s4) calc(var(--s4) + var(--safe-l));
  touch-action: pan-y;
}

.page.from-right {
  animation: slide-from-right var(--t-page) ease-out;
}

.page.from-left {
  animation: slide-from-left var(--t-page) ease-out;
}

.page-intro {
  margin: var(--s3) 0 var(--s2);
  max-width: 70ch;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.4;
}

.mrow {
  display: grid;
  grid-template-columns: minmax(130px, 32%) minmax(0, 1fr);
  align-items: center;
  gap: var(--s3);
  min-height: 72px;
  padding: var(--s2) 0;
  border-bottom: 1px solid var(--line);
}

.mrow > .label {
  font-size: 14px;
  color: var(--text);
}

.mrow .ctl {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s2);
  min-width: 0;
}

.mrow .ctl.focus-ring {
  outline: 2px solid var(--sel-bg);
  outline-offset: 4px;
  border-radius: var(--r-slab);
}

.mrow .info {
  color: var(--muted);
  font-size: 15px;
  line-height: 1.35;
  font-variant-numeric: tabular-nums;
}

.mrow .info strong {
  color: var(--text);
  font-weight: 600;
}

@media (max-width: 560px) {
  .mrow {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--s2);
  }
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s1);
  flex: 1 1 100%;
  min-width: 0;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 8px;
  border-radius: var(--r-slab);
  box-shadow: inset 0 0 0 1px var(--line);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.chip.none {
  color: var(--dim);
}

.learn-status {
  min-height: 1.4em;
  margin: var(--s2) 0;
  padding: var(--s2) var(--s3);
  border-left: 3px solid var(--live);
  background: var(--slab);
  font-size: 15px;
  line-height: 1.35;
}

.learn-status:empty {
  display: none;
}

.panel-foot {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--s2);
  padding: var(--s2) calc(var(--s3) + var(--safe-r)) calc(var(--s2) + var(--safe-b)) calc(var(--s3) + var(--safe-l));
  border-top: 1px solid var(--line);
}

@media (min-width: 900px) {
  .panel-foot {
    padding-bottom: var(--s2);
  }
}

/* library sheet */
.lib-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lib-row {
  display: flex;
  align-items: stretch;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  padding: var(--s1) 0;
}

.lib-open {
  flex: 1;
  min-width: 0;
  justify-content: space-between;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 500;
  font-size: 17px;
  padding: 0 var(--s3);
  background: transparent;
}

.lib-open .lib-name {
  overflow: hidden;
  text-overflow: ellipsis;
}

.lib-open .lib-words {
  flex: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
}

.lib-row.current .lib-name::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 8px;
  vertical-align: middle;
  background: var(--text);
}

.lib-empty {
  color: var(--muted);
  padding: var(--s5) 0;
}

/* dialog */
.dialog-layer {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: calc(var(--s4) + var(--safe-t)) calc(var(--s4) + var(--safe-r)) calc(var(--s4) + var(--safe-b)) calc(var(--s4) + var(--safe-l));
  background: rgba(10, 10, 11, 0.6);
  animation: fade-in var(--t-panel) ease-out;
}

.dialog {
  width: 100%;
  max-width: 460px;
  padding: var(--s5);
  border-radius: var(--r-panel);
  background: var(--panel);
  box-shadow: var(--shadow-float);
  animation: panel-in var(--t-panel) ease-out;
}

.dialog h2 {
  margin: 0 0 var(--s3);
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
}

.dialog p {
  margin: 0 0 var(--s5);
  color: var(--text);
  font-size: 17px;
  line-height: 1.4;
}

.dialog .actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--s2);
}

/* toast */
.toast {
  position: fixed;
  left: 0;
  right: 0;
  width: max-content;
  margin: 0 auto;
  bottom: calc(76px + var(--safe-b));
  z-index: 90;
  display: flex;
  align-items: center;
  gap: var(--s3);
  max-width: calc(100vw - 32px);
  min-height: var(--touch);
  padding: 4px 4px 4px 16px;
  border-radius: var(--r-slab);
  background: var(--slab);
  box-shadow: var(--shadow-float);
  color: var(--text);
  font-size: 15px;
  line-height: 1.3;
  animation: toast-in var(--t-panel) ease-out;
}

.toast:not(.has-action) {
  padding-right: 16px;
}

.toast.error {
  border-left: 3px solid var(--live);
}

.toast .slab {
  flex: none;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.app.immersive ~ .toast {
  bottom: calc(24px + var(--safe-b));
}

@media (prefers-reduced-motion: reduce) {
  .layer,
  .panel,
  .dialog-layer,
  .dialog,
  .toast,
  .page.from-right,
  .page.from-left {
    animation: none !important;
  }
  .hud,
  .pill,
  .flash {
    transition: none !important;
  }
}

/* ---------- HUD status strip (never mirrored) ---------- */

.hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: stretch;
  gap: 2px;
  height: calc(40px + var(--safe-t));
  padding: var(--safe-t) calc(var(--s2) + var(--safe-r)) 0 calc(var(--s2) + var(--safe-l));
  background: rgba(10, 10, 11, 0.85);
  color: var(--text);
  transition: opacity var(--t-panel) ease-out;
}

.hud.off {
  opacity: 0;
  pointer-events: none;
}

.hud-item {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex: none;
  min-width: 44px;
  height: 40px;
  padding: 0 8px;
  border: 0;
  border-radius: var(--r-slab);
  background: none;
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--track);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}

/* extend the hit area to 44px without growing the strip */
.hud-item::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -2px;
  bottom: -4px;
}

@media (hover: hover) {
  .hud-item:hover {
    background: var(--slab);
  }
}

.hud-item:active {
  background: var(--slab-hi);
}

.hud-name {
  flex: 1 1 auto;
  min-width: 0;
  justify-content: flex-start;
}

.hud-name span {
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-k {
  color: var(--muted);
}

.hud.running #hud-wpm,
.hud.running #hud-elapsed {
  color: var(--live);
}

.hud-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: var(--line);
  pointer-events: none;
}

.hud-progress i {
  position: absolute;
  inset: 0;
  background: var(--text);
  transform-origin: 0 50%;
  transform: scaleX(0);
}

@media (max-width: 640px) {
  .hud-opt {
    display: none;
  }
}

@media (max-width: 440px) {
  .hud-time {
    display: none;
  }
  .hud-k {
    display: none;
  }
}

/* ---------- floating control pill (never mirrored) ---------- */

.pill {
  position: absolute;
  z-index: 11;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: max-content;
  max-width: calc(100% - 16px - var(--safe-l) - var(--safe-r));
  padding: 6px;
  border-radius: var(--r-panel);
  background: var(--panel);
  box-shadow: var(--shadow-float);
  touch-action: none;
  cursor: grab;
  transition: opacity 400ms ease-out;
  visibility: hidden;
}

.pill.placed {
  visibility: visible;
}

.pill.dragging {
  cursor: grabbing;
  transition: none;
}

.pill.faded {
  opacity: 0.25;
}

.pill-line {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
}

.pill-line .slab {
  cursor: pointer;
}

.pill-vc {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 196px;
  min-width: 0;
  flex: 1 1 196px;
  max-width: 300px;
}

.pill-vc .label {
  padding-left: 2px;
  font-size: 12px;
}

.pill .vc {
  cursor: default;
}

.pill .swatch {
  width: 26px;
  height: 20px;
}

.slab.cd-on {
  box-shadow: inset 0 -3px 0 var(--live);
}

.slab .cd-n {
  font-size: 15px;
}

.pill-more {
  display: none;
}

@media (max-width: 600px), (max-height: 560px) {
  .pill-line .pill-more {
    display: inline-flex;
  }
  .pill:not(.expanded) .pill-values {
    display: none;
  }
}
