*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
[hidden] { display: none !important; }
body {
  font-family: 'SF Pro Display', 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: #f5f5f5;
  color: #1a1a1a;
  min-height: 100vh;
}

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

/* ---------- HOME ---------- */
#view-home {
  max-width: 880px;
  margin: 0 auto;
  padding: 80px 24px 40px;
  text-align: center;
}
#view-home h1 {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 12px;
}
#view-home .home-subtitle {
  margin: 0 0 40px;
  color: #666;
  font-size: 16px;
}
#role-picker {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}
.role-card {
  display: block;
  padding: 32px 24px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 12px;
  text-decoration: none;
  color: #1a1a1a;
  text-align: left;
  transition: transform 0.1s, box-shadow 0.15s, border-color 0.15s;
}
.role-card:hover {
  border-color: #1a1a1a;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  transform: translateY(-1px);
}
.role-card h2 {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px;
}
.role-card p {
  margin: 0;
  color: #555;
  font-size: 14px;
  line-height: 1.5;
}

/* ---------- ROLE SWITCH (used in control header + display corner) ---------- */
.role-switch {
  font-size: 12px;
  padding: 6px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  background: #fff;
  color: #555;
  cursor: pointer;
}
.role-switch:hover { background: #fafafa; color: #1a1a1a; }
.role-switch--corner {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 20;
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.7);
}
.role-switch--corner:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

/* ---------- LANG SWITCH ---------- */
.lang-switch {
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 6px 10px;
  min-width: 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  background: #fff;
  color: #555;
  cursor: pointer;
}
.lang-switch:hover { background: #fafafa; color: #1a1a1a; }
.lang-switch--display {
  position: absolute;
  top: 16px;
  right: 124px;
  z-index: 20;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 6px 10px;
  min-width: 40px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
}
.lang-switch--display:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

/* ---------- CONTROL ---------- */
#view-control {
  max-width: none;
  margin: 0;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100vh;
}
#view-control > header,
#view-control > #controls,
#view-control > #hotkeys-hint {
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding-left: 24px;
  padding-right: 24px;
}
#view-control > #ctrl-main {
  width: 100%;
  padding: 0 24px;
}
#view-control header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
#view-control h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  margin-right: auto;
}
#view-control header .header-meta {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

#connection-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  background: #eee;
  color: #555;
}
#connection-status::before {
  content: '';
  width: 8px; height: 8px;
  border-radius: 50%;
  background: currentColor;
}
#connection-status[data-status="waiting"],
#connection-status[data-status="waitingPeer"] { background: #fff7e6; color: #b8860b; }
#connection-status[data-status="connected"] { background: #e8f7ed; color: #1a7f37; }
#connection-status[data-status="relayOffline"] { background: #fff1d6; color: #c2570a; }
#connection-status[data-status="disconnected"] { background: #fce8e8; color: #b91c1c; }

#room-code-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 4px 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
  color: #555;
}
#room-code-pill .room-code-label {
  font-weight: 500;
  letter-spacing: 0.2px;
}
#room-code-pill code#room-code {
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 2px;
  background: #1a1a1a;
  color: #fff;
  padding: 4px 8px;
  border-radius: 5px;
}

/* ---------- SCRIPT MANAGER ---------- */
.script-manager {
  position: relative;
  display: inline-flex;
}
.script-manager-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 4px 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
  color: #555;
  cursor: pointer;
  max-width: 260px;
}
.script-manager-toggle:hover { background: #fafafa; color: #1a1a1a; }
.script-manager-toggle[aria-expanded="true"] {
  border-color: #1a1a1a;
  background: #fafafa;
}
.script-manager-label {
  font-weight: 500;
  letter-spacing: 0.2px;
  color: #777;
}
.script-manager-current {
  font-weight: 600;
  color: #1a1a1a;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.script-manager-caret {
  font-size: 10px;
  color: #999;
  margin-left: 2px;
}
.script-manager-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  min-width: 280px;
  max-width: 360px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.script-manager-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.script-manager-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  border-radius: 6px;
}
.script-manager-item:hover { background: #f5f5f5; }
.script-manager-item.active {
  background: #f0f0f0;
}
.script-manager-item-name {
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  padding: 6px 8px;
  border: none;
  background: transparent;
  font-size: 13px;
  color: #1a1a1a;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}
.script-manager-item.active .script-manager-item-name {
  color: #1a1a1a;
  font-weight: 700;
}
.script-manager-item-action {
  flex: 0 0 auto;
  padding: 4px 6px;
  border: none;
  background: transparent;
  color: #888;
  font-size: 13px;
  border-radius: 4px;
  cursor: pointer;
  line-height: 1;
}
.script-manager-item-action:hover {
  background: #e8e8e8;
  color: #1a1a1a;
}
.script-manager-item-action[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
}
.script-manager-new {
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px dashed #bbb;
  border-radius: 6px;
  background: #fff;
  color: #1a1a1a;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
}
.script-manager-new:hover {
  background: #fafafa;
  border-color: #1a1a1a;
}
.script-manager-new[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}
@media (max-width: 720px) {
  .script-manager-toggle { max-width: 200px; }
  .script-manager-current { max-width: 120px; }
  .script-manager-menu { min-width: 240px; }
}

#script-input {
  width: 100%;
  min-height: 480px;
  padding: 12px 14px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  resize: vertical;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 14px;
  line-height: 1.5;
}

#ctrl-main {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}
.ctrl-pane {
  display: flex;
  flex-direction: column;
}
.ctrl-pane.editor-pane {
  flex: 1 1 0;
  min-width: 0;
}
.ctrl-pane.preview-pane {
  flex: 0 0 auto;
}
@media (max-width: 720px) {
  #ctrl-main { flex-direction: column; }
  .ctrl-pane.preview-pane { width: 100%; align-self: stretch; }
}
.pane-label {
  font-size: 12px;
  color: #555;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}
.pane-label-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.pane-label-row .pane-label {
  margin-bottom: 0;
}
.remaining-counter {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
  color: #555;
}
.remaining-counter-label {
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #777;
}
.remaining-counter-value {
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #1a1a1a;
}

/* ---------- VERSION MARK ---------- */
.version-mark {
  position: fixed;
  right: 10px;
  bottom: 8px;
  z-index: 50;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.5px;
  color: #888;
  opacity: 0.4;
  pointer-events: none;
  user-select: none;
}
.version-mark--display {
  color: #fff;
  opacity: 0.35;
}

/* ---------- DISPLAY REMAINING COUNTER ---------- */
.display-remaining {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 20;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: rgba(255, 255, 255, 0.55);
  pointer-events: none;
  user-select: none;
  letter-spacing: 0.5px;
}

#ctrl-preview {
  position: relative;
  background: #000;
  border-radius: 8px;
  border: 1px solid #ddd;
  overflow: hidden;
  outline: none;
  cursor: ns-resize;
  height: 360px;
  aspect-ratio: var(--display-ratio, 16 / 10);
}
@media (max-width: 720px) {
  #ctrl-preview { height: auto; width: 100%; }
}
#ctrl-preview-stage {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: top left;
}
#ctrl-preview:focus-visible { box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.4); }
#ctrl-preview-eyeline {
  position: absolute;
  top: 33%;
  left: 0; right: 0;
  height: 2px;
  background: rgba(255, 0, 0, 0.6);
  pointer-events: none;
  z-index: 10;
}
#ctrl-preview-scroll {
  position: absolute;
  inset: 0;
}
#ctrl-preview-text {
  margin: 0 auto;
  max-width: var(--text-width, 90%);
  padding: var(--padding-y, 50%) 32px;
  font: 600 var(--font-size, 56px)/1.5 'SF Pro Display', 'Inter', system-ui, sans-serif;
  color: #fff;
  text-rendering: optimizeLegibility;
}
#ctrl-preview-text h1 { font-size: 1.6em; font-weight: 800; margin: 0.6em 0; }
#ctrl-preview-text h2 { font-size: 1.3em; font-weight: 800; margin: 0.6em 0; }
#ctrl-preview-text h3 { font-size: 1.1em; font-weight: 800; margin: 0.6em 0; }
#ctrl-preview-text p { margin: 0.6em 0; }
#ctrl-preview-text strong { font-weight: 800; }
#ctrl-preview-text em { font-style: italic; }
#ctrl-preview-text ul, #ctrl-preview-text ol { margin: 0.6em 0; padding-left: 1.2em; }
#ctrl-preview-text li { margin: 0.3em 0; }
#ctrl-preview-text blockquote {
  border-left: 4px solid rgba(255, 255, 255, 0.3);
  padding-left: 16px;
  margin: 0.6em 0;
  opacity: 0.85;
}
#ctrl-preview-text code {
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  background: rgba(255, 255, 255, 0.1);
  padding: 0 0.3em;
  border-radius: 4px;
}
#ctrl-preview-hint {
  position: absolute;
  bottom: 8px;
  left: 0; right: 0;
  text-align: center;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.4);
  pointer-events: none;
  z-index: 5;
}

#controls {
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  align-items: center;
}
#controls label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: #555;
}
#controls label > span:first-child {
  font-weight: 500;
}
#controls input[type="range"] {
  width: 100%;
}
#controls output {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: #1a1a1a;
}
#controls button {
  padding: 10px 14px;
  border: 1px solid #ddd;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  font-weight: 500;
}
#controls button:hover { background: #fafafa; }
#controls button:active { background: #f0f0f0; }
#controls .seek-mirror-row {
  display: flex;
  gap: 6px;
  align-items: stretch;
}
#controls .seek-mirror-row .seek-btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 8px 6px;
  font-size: 13px;
  white-space: nowrap;
}
#controls #btn-play {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}
#controls #btn-play:hover { background: #333; }
#controls #btn-mirror[aria-pressed="true"],
#controls #btn-voice[aria-pressed="true"] {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}

#hotkeys-hint {
  font-size: 12px;
  color: #777;
  text-align: center;
}

/* ---------- SECTION MARKERS ---------- */
#view-control > .section-markers {
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.section-markers.is-empty { display: none; }
.section-markers-label {
  font-size: 12px;
  color: #555;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex: 0 0 auto;
}
.section-list {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scroll-behavior: smooth;
  padding: 2px 0;
}
.section-btn {
  flex: 0 0 auto;
  padding: 6px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  background: #fff;
  color: #555;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background 0.1s, color 0.1s, border-color 0.1s;
}
.section-btn:hover {
  background: #fafafa;
  color: #1a1a1a;
  border-color: #1a1a1a;
}
.section-btn:active {
  background: #f0f0f0;
}

/* ---------- DISPLAY ---------- */
#view-display {
  background: #000;
  color: #fff;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}
body:has(#view-display:not([hidden])) { background: #000; }

#pair-prompt {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  gap: 20px;
}
#pair-prompt h2 {
  font-weight: 600;
  margin: 0;
  font-size: 28px;
}
#pair-prompt > * { width: 100%; max-width: 420px; }
#display-code-input {
  width: 100%;
  padding: 16px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 6px;
  text-align: center;
  text-transform: uppercase;
  background: #111;
  border: 1px solid #333;
  border-radius: 8px;
  color: #fff;
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
}
#display-connect-btn {
  display: inline-block;
  margin-top: 16px;
  padding: 12px 24px;
  background: #fff;
  color: #000;
  border: none;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}
#display-status {
  margin-top: 24px;
  color: #999;
  font-size: 14px;
}

#prompter {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: #000;
  filter: brightness(var(--brightness, 1));
  transition: filter 0.15s ease;
}
#eye-line {
  position: absolute;
  top: 33%;
  left: 0;
  right: 0;
  height: 2px;
  background: rgba(255, 0, 0, 0.6);
  pointer-events: none;
  z-index: 10;
}
#prompter-scroll {
  position: absolute;
  inset: 0;
  will-change: auto;
}
#prompter-text {
  margin: 0 auto;
  max-width: var(--text-width, 90%);
  padding: 0 32px;
  font: 600 56px/1.5 'SF Pro Display', 'Inter', system-ui, sans-serif;
  color: #fff;
  text-rendering: optimizeLegibility;
}
#prompter-text.mirror { transform: scaleX(-1); }

#prompter-text h1 { font-size: 1.6em; font-weight: 800; margin: 0.6em 0; }
#prompter-text h2 { font-size: 1.3em; font-weight: 800; margin: 0.6em 0; }
#prompter-text h3 { font-size: 1.1em; font-weight: 800; margin: 0.6em 0; }
#prompter-text p { margin: 0.6em 0; }
#prompter-text strong { font-weight: 800; }
#prompter-text em { font-style: italic; }
#prompter-text ul, #prompter-text ol { margin: 0.6em 0; padding-left: 1.2em; }
#prompter-text li { margin: 0.3em 0; }
#prompter-text blockquote {
  border-left: 4px solid rgba(255, 255, 255, 0.3);
  padding-left: 16px;
  margin: 0.6em 0;
  opacity: 0.85;
}
#prompter-text code {
  font-family: 'SF Mono', ui-monospace, Menlo, monospace;
  background: rgba(255, 255, 255, 0.1);
  padding: 0 0.3em;
  border-radius: 4px;
}
#prompter-text hr {
  border: none;
  border-top: 2px solid rgba(255, 255, 255, 0.2);
  margin: 1em 0;
}
