:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --text-dim: #9aa3b2;
  --accent: #4f8cff;
  --accent-press: #3d74e0;
  --danger: #ff5c5c;
  --ok: #37d67a;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --panel-2: #f0f2f5;
    --border: #dce0e6;
    --text: #1b1f27;
    --text-dim: #5c6472;
    --accent: #2f6bff;
    --accent-press: #2456d6;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;   /* iOS: don't inflate text in landscape */
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Follow the visible area as mobile browser toolbars collapse/expand
     (100% above is the fallback for browsers without dvh). */
  height: 100dvh;
}

button, select, a.btn, .mode-nav a {
  touch-action: manipulation;       /* no double-tap-to-zoom delay */
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  row-gap: 8px;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  /* keep clear of the notch / rounded corners on iPhone and iPad */
  padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}
.brand span { vertical-align: middle; }

.key-area {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

#api-key,
#code-input {
  flex: 1;
  min-width: 160px;
  max-width: 520px;
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}
#api-key:focus,
#code-input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
#code-input { max-width: 240px; text-transform: uppercase; letter-spacing: .08em; }

/* Sender <-> Receiver switch */
.mode-nav { display: flex; gap: 4px; }
.mode-nav a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 13px;
  padding: 5px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
}
.mode-nav a:hover { background: var(--panel-2); }
.mode-nav a.active { color: var(--text); background: var(--panel-2); border-color: var(--border); }

/* Session ID shown on the sender */
.session-area {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}
.session-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: .14em;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  user-select: all;
}

.session-area select { max-width: 200px; font-size: 12px; padding: 6px 8px; }

.receiver-panel .scroller { border-top: 0; }

/* ---------- Home page ---------- */
.home {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 24px 16px;
}
.home h1 { margin: 0; font-size: 22px; }
.choice-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 340px));
  gap: 20px;
}
.choice {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.choice h2 { margin: 0; font-size: 18px; }
.choice p { margin: 0; color: var(--text-dim); font-size: 14px; line-height: 1.5; flex: 1; }
a.btn {
  display: inline-block;
  text-align: center;
  text-decoration: none;
  padding: 9px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-weight: 600;
}
a.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
a.btn.primary:hover { background: var(--accent-press); }
.join-form { display: flex; gap: 8px; }
.join-form input {
  flex: 1;
  min-width: 0;
  padding: 9px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-transform: uppercase;
  letter-spacing: .08em;
}
.join-form input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.choice .form-error { margin: -6px 0 0; flex: none; color: var(--danger); font-size: 13px; }
@media (max-width: 760px) { .choice-grid { grid-template-columns: minmax(0, 1fr); width: 100%; } }

.remember {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  user-select: none;
}

.status {
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  white-space: nowrap;
  border: 1px solid var(--border);
}
.status.idle      { color: var(--text-dim); }
.status.connecting{ color: #ffcf5c; border-color: #7a6420; }
.status.live      { color: var(--ok); border-color: #1f6b3f; }
.status.error     { color: var(--danger); border-color: #7a2020; }

/* ---------- Workspace ---------- */
.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr;
  padding: 12px;
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  padding-bottom: max(12px, env(safe-area-inset-bottom));   /* home indicator */
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 700; }

.lang-fixed {
  font-size: 13px;
  color: var(--text-dim);
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* ---------- Buttons / inputs ---------- */
button {
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 7px 14px;
  background: var(--panel-2);
  color: var(--text);
  transition: background .12s ease, opacity .12s ease;
}
button:hover { background: var(--border); }
button:disabled { opacity: .5; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}
button.primary:hover { background: var(--accent-press); }
button.primary.recording { background: var(--danger); }

button.ghost { background: transparent; }
button.ghost:hover { background: var(--panel-2); }

select {
  font: inherit;
  padding: 7px 8px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
}

/* ---------- Transcript ---------- */
.listening {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  font-size: 12px;
  color: var(--ok);
}
.listening .dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--ok);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }

.error-banner {
  margin: 8px 14px 0;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: rgba(255, 92, 92, .12);
  border: 1px solid rgba(255, 92, 92, .4);
  color: var(--danger);
  font-size: 13px;
}

/* passing, non-fatal message (reconnecting, microphone paused...) */
.error-banner.notice {
  background: rgba(255, 207, 92, .12);
  border-color: rgba(255, 207, 92, .45);
  color: #ffcf5c;
}
.bg-hint {
  margin: 8px 14px 0;
  padding: 6px 10px;
  border-radius: var(--radius);
  background: var(--panel-2);
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.4;
}

.transcript {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  line-height: 1.6;
  scroll-behavior: smooth;
}
/* Teleprompter (receiver): vertical, line by line, big text, green > marker */
.scroller {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-top: 2px solid var(--border);
}
.scroller-view {
  --tp-size: 64px;                 /* set from the Size slider */
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}
/* teleprompter + the full transcript side by side (stacked on narrow screens) */
.tp-body {
  flex: 1;
  min-height: 0;
  display: flex;
}
.tp-main {
  flex: 1 1 62%;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.tp-main .scroller-view { flex: 1; }
/* Pause + Jump to latest, right under the teleprompter */
.tp-quick {
  flex-shrink: 0;
  display: flex;
  gap: 8px;
  padding: 8px 14px;
  border-top: 1px solid var(--border);
}
.tp-quick button { flex: 1; max-width: 220px; }
.tp-transcript {
  flex: 0 0 38%;
  position: relative;               /* offsetTop of lines is measured from here */
  overflow-y: auto;
  padding: 12px 16px;
  border-left: 1px solid var(--border);
  font-size: 16px;
  line-height: 1.7;
  color: var(--text);
}
.tx-line { cursor: pointer; border-radius: 4px; padding: 1px 2px; }
.tx-line:hover { background: var(--panel-2); }
.tx-line.past { color: var(--text-dim); }
.tx-line.cur {                      /* where the teleprompter is right now */
  background: rgba(55, 214, 122, .22);
  color: var(--text);
  box-shadow: 0 0 0 1px rgba(55, 214, 122, .55);
}
@media (max-width: 860px) {
  .tp-body { flex-direction: column; }
  .tp-main { flex: 1 1 60%; }
  .tp-transcript { flex: 0 0 40%; border-left: 0; border-top: 1px solid var(--border); }
}

.tp-track {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 0 14px 0 calc(var(--tp-size) * 1.15);   /* room for the marker */
  will-change: transform;
}
.tp-line {
  font-size: var(--tp-size);
  /* fixed rhythm, so a line that shrinks to fit keeps the same spacing */
  line-height: calc(var(--tp-size) * 1.22);
  /* a font made for legibility, in plain bold: heavier weights blur together */
  font-family: "Atkinson Hyperlegible", var(--font);
  font-weight: 700;
  letter-spacing: 0.01em;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding: calc(var(--tp-size) * 0.1) 0;
  color: var(--text);
  white-space: nowrap;      /* one line of text = one row, never broken mid-word */
  overflow: hidden;
}
.tp-marker {
  position: absolute;
  left: 8px;
  top: 38%;                         /* set precisely from script */
  transform: translateY(-50%);
  font-size: calc(var(--tp-size) * 1.25);
  line-height: 1;
  font-weight: 900;
  color: var(--ok);                 /* green */
  pointer-events: none;
}
.scroller-ctrls {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}
.scroller-ctrls .tick-field {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 120px;
}
.scroller-ctrls input[type="range"] { flex: 1; max-width: 160px; }
.scroller-ctrls .tick-val {
  white-space: nowrap;
  min-width: 2.6em;
  text-align: right;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.transcript .hint { color: var(--text-dim); font-size: 13px; }
.transcript .line { margin: 0 0 10px; }
.transcript .line.final   { color: var(--text); }
.transcript .line.interim { color: var(--text-dim); font-style: italic; }

.transcript .session-sep {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0 14px;
  color: var(--text-dim);
  font-size: 11px;
  letter-spacing: 0.05em;
}
.transcript .session-sep::before { content: attr(data-time); white-space: nowrap; }
.transcript .session-sep::after  { content: ""; flex: 1; height: 1px; background: var(--border); }

.hidden { display: none !important; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; }
  .key-area { order: 3; flex: 1 1 100%; }   /* its own full-width row */
}

/* Phones and tablets (any touch screen, including iPad in landscape) */
@media (max-width: 860px), (pointer: coarse) {
  /* iOS Safari zooms the whole page when a field under 16px gets focus */
  input, select, textarea { font-size: 16px !important; }
  /* comfortable thumb-sized targets */
  button, select, a.btn { min-height: 44px; }
  .mode-nav a { display: inline-flex; align-items: center; min-height: 44px; padding: 0 10px; }
  input[type="range"] { min-height: 32px; }
  .session-area select { max-width: 100%; }
}

/* Phones: pack the top bar into three rows (brand+nav+status / session / key) */
@media (max-width: 600px) {
  .brand span { display: none; }                 /* the emoji is enough */
  .status { order: 1; margin-left: auto; }
  .session-area { order: 2; width: 100%; }
  .session-area > span { display: none; }
  .session-area select { flex: 1 1 110px; min-width: 0; max-width: none; }
  /* teleprompter sliders: one full-width row each (easy to drag, never clipped) */
  .scroller-ctrls .tick-field { flex: 1 1 100%; min-width: 0; }
  .scroller-ctrls input[type="range"] { max-width: none; min-width: 0; }
}

/* Scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }
::-webkit-scrollbar-track { background: transparent; }
