:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --me: #0f8a6e;
  --me-soft: #e3f4ef;
  --them: #5b5fc7;
  --them-soft: #ecedfb;
  --primary: #1b1f24;
  --primary-text: #ffffff;
  --live: #d6383a;
  --warn-bg: #fff4e0;
  --warn-text: #7a4b00;
  --err-bg: #fde8e8;
  --err-text: #8a1c1c;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --surface: #181c21;
    --surface-2: #20252c;
    --border: #2c333c;
    --text: #e8ebef;
    --muted: #96a1ae;
    --me: #3cc4a1;
    --me-soft: #15302a;
    --them: #9498f0;
    --them-soft: #24254a;
    --primary: #e8ebef;
    --primary-text: #111418;
    --live: #ff6b6b;
    --warn-bg: #3a2c10;
    --warn-text: #f4cf86;
    --err-bg: #3d1a1a;
    --err-text: #ffb4b4;
    color-scheme: dark;
  }
}

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

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); font-weight: 600; }
button.primary:hover:not(:disabled) { background: var(--primary); opacity: .9; }
button.ghost { background: transparent; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--them); outline-offset: 2px; }

select, input {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 8px;
  min-width: 0;
}

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

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 15px; }
.brand-mark { width: 22px; height: 22px; }
.me-fill { fill: var(--me); }
.them-fill { fill: var(--them); }

.status {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}
.status[data-state="live"] { background: var(--err-bg); color: var(--live); }
.status[data-state="live"]::before {
  content: "";
  display: inline-block;
  width: 7px; height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--live);
  vertical-align: 1px;
  animation: pulse 1.4s ease-in-out infinite;
}
.status[data-state="connecting"] { color: var(--text); }
@keyframes pulse { 50% { opacity: .3; } }

/* Sources */
.sources {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 10px;
  padding: 12px 16px 0;
}
.source {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.source-head { display: flex; align-items: center; gap: 6px; }
.source-row { display: flex; gap: 8px; }
.source-row select { flex: 1; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.me { background: var(--me); }
.dot.them { background: var(--them); }

.meter { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 3px; transition: width 60ms linear; }
[data-ch="0"] .meter-fill { background: var(--me); }
[data-ch="1"] .meter-fill { background: var(--them); }

.hint { margin: 0; font-size: 12px; color: var(--muted); }
.hint.ok { color: var(--text); }
.hint.warn { color: var(--warn-text); }

/* Controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 16px;
}
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.field input, .field select { font-size: 14px; color: var(--text); }
.spacer { flex: 1; }

.notice {
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--warn-bg);
  color: var(--warn-text);
}
.notice[data-kind="error"] { background: var(--err-bg); color: var(--err-text); }

/* Transcript */
.transcript {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0 16px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty { margin: auto; max-width: 420px; color: var(--muted); }
.empty-title { color: var(--text); font-weight: 600; margin: 0 0 6px; }
.empty ol { margin: 0; padding-left: 20px; }
.empty li { margin: 3px 0; }

.msg { max-width: 82%; display: flex; flex-direction: column; gap: 3px; }
.msg[data-ch="1"] { align-self: flex-start; }
.msg[data-ch="0"] { align-self: flex-end; align-items: flex-end; }

.msg-meta { font-size: 11px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.msg-meta .who { font-weight: 650; }
.msg[data-ch="0"] .who { color: var(--me); }
.msg[data-ch="1"] .who { color: var(--them); }

.bubble {
  padding: 8px 12px;
  border-radius: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg[data-ch="0"] .bubble { background: var(--me-soft); border-bottom-right-radius: 4px; }
.msg[data-ch="1"] .bubble { background: var(--them-soft); border-bottom-left-radius: 4px; }
.interim { color: var(--muted); font-style: italic; }
.msg.open .bubble { box-shadow: inset 0 0 0 1px var(--border); }

.msg.echo .bubble { opacity: .55; text-decoration: line-through; text-decoration-color: var(--muted); }
.tag {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn-text);
}

/* Footer */
.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 16px 12px;
}
.stats { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.actions { display: flex; gap: 6px; }
.actions button { padding: 5px 10px; font-size: 13px; }

@media (max-width: 520px) {
  .msg { max-width: 92%; }
  .controls .spacer { display: none; }
}

/* ---- v0.2 additions ---------------------------------------------------- */

button.small { padding: 5px 10px; font-size: 13px; }
button.danger { color: var(--err-text); }
code, .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; overflow-x: auto; white-space: pre-wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.form-error { color: var(--err-text); background: var(--err-bg); padding: 8px 12px; border-radius: 8px; }
h1 { font-size: 20px; margin: 18px 0 6px; }
h2 { font-size: 15px; margin: 0; }

/* Sign-in / setup */
body.gate { align-items: center; justify-content: center; padding: 24px 16px; }
.gate-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 22px; }
.gate-card .field input { width: 100%; }

/* Top bar with tabs */
.topbar { flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: 9px; }
.tabs button { border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; font-size: 13px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / .08); }
.topbar-end { display: flex; align-items: center; gap: 8px; }

/* Views */
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* Speech engine row */
.engine-check { display: flex; align-items: center; gap: 8px; align-self: center; }
.engine-state { margin-left: auto; font-size: 11px; font-weight: 600; color: var(--muted); }
.engine-state[data-state="listening"] { color: var(--me); }
[data-ch="1"] .engine-state[data-state="listening"] { color: var(--them); }
.engine-state[data-state="error"] { color: var(--err-text); }

/* Saved */
.saved-layout { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(220px, 320px) 1fr; gap: 10px; padding: 12px 16px; }
.saved-list, .saved-view { overflow: hidden; }
.session-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.session-list button { width: 100%; text-align: left; white-space: normal; border-color: transparent; display: flex; flex-direction: column; gap: 2px; }
.session-list button[aria-current="true"] { border-color: var(--border); background: var(--surface-2); }
.session-list .when { font-weight: 600; }
.session-list .counts, .session-list .preview { font-size: 12px; color: var(--muted); }
.saved-view .transcript { margin: 0; border: 0; padding: 4px 0; }
.empty-list { color: var(--muted); font-size: 13px; padding: 6px 2px; }

/* Settings */
.settings { padding: 12px 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 10px; align-content: start; overflow-y: auto; }
.settings input { flex: 1; }
.hint.err { color: var(--err-text); }

@media (max-width: 700px) {
  .saved-layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .session-list { max-height: 30vh; }
}
