:root {
  --bg-app: #FAFAF7;
  --bg-panel: #FFFFFF;
  --text-primary: #0A0A0A;
  --text-secondary: #6B6B66;
  --divider: #E8E8E2;
  --accent: #D64545;
  --accent-hover: #C13838;

  /* Message origin colors. self = my voice (neutral primary), other = the
     interlocutor's voice (distinct warm tone). Aliases of --text-primary
     keep self consistent with body text. */
  --color-text-self: var(--text-primary);
  --color-text-other: #b91c1c;

  /* Session indicator dot (pulses while a session is connected). */
  --session-dot-active: #dc2626;

  /* Warning text (mic blocked indicator). */
  --warning-text: #b45309;

  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-app: #0E0E0C;
    --bg-panel: #1A1A18;
    --text-primary: #F4F4EF;
    --text-secondary: #8A8A82;
    --divider: #2A2A26;
    --accent: #E8625F;
    --accent-hover: #F47471;

    --color-text-self: var(--text-primary);
    --color-text-other: #fca5a5;
    --session-dot-active: #ef4444;
    --warning-text: #f59e0b;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; height: 100%; }

body {
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.3;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  cursor: pointer;
  color: inherit;
}

:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ============ Layout ============ */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

.app-panels {
  flex: 1;
  display: flex;
  min-height: 0;
}

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg-panel);
}

.panel + .panel {
  border-left: 1px solid var(--divider);
}

/* ============ Header ============ */

.app-header {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  height: 40px;
  border-bottom: 1px solid var(--divider);
  background: var(--bg-panel);
}

.brand {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.header-meta {
  grid-column: 3;
  display: flex;
  align-items: center;
  gap: 12px;
}

.meta-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  line-height: 1.1;
}

.timer-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.timer-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--divider);
  flex-shrink: 0;
  transition: background 120ms ease;
}

body.session-active .timer-dot {
  background: var(--session-dot-active);
  animation: timer-dot-pulse 1.5s ease-in-out infinite;
}

@keyframes timer-dot-pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

.timer {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.01em;
}

.cost-estimate {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 400;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.header-actions {
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.finalize {
  background: transparent;
  border: none;
  padding: 4px 8px;
  font-size: 14px;
  font-weight: 400;
  color: var(--text-secondary);
  transition: color 120ms ease;
}

body.has-content .finalize {
  color: var(--accent);
  font-weight: 500;
}

body.has-content .finalize:hover {
  text-decoration: underline;
}

.save-transcript {
  display: none;
  background: var(--text-primary);
  color: var(--bg-app);
  border: 1px solid var(--text-primary);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 500;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}

body.has-content .save-transcript {
  display: inline-flex;
}

.save-transcript:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ============ Language selector ============ */

.lang-select-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  transition: opacity 120ms ease;
}

.lang-select-wrap select {
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  border: none;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-primary);
  padding: 4px 20px 4px 8px;
  cursor: pointer;
  border-radius: 4px;
}

.lang-select-wrap select:hover {
  background: var(--divider);
}

.lang-chevron {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

/* ============ Panel headers ============ */

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 32px;
  border-bottom: 1px solid var(--divider);
  background: var(--bg-panel);
  flex: 0 0 auto;
}

.panel-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

.panel-badge {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

/* ============ Panel body ============ */

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px;
  font-size: 16px;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}

.panel-body[data-empty="true"] {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.empty-state {
  margin: 0;
  font-size: 14px;
  font-weight: 400;
  color: var(--text-secondary);
}

.panel-body:not([data-empty="true"]) .empty-state {
  display: none;
}

.entry {
  margin: 0 0 16px;
}

.entry time {
  display: block;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 400;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.entry p {
  margin: 0;
  color: var(--color-text-self);
}

/* When the original voice was the interlocutor's, the whole entry takes the
   "other" color tint. Color applies to BOTH panels for visual coherence.
   The "↩" prefix, however, applies ONLY to panel "Yo": that panel can mix
   self + other entries between consecutive sessions, so the prefix is
   informative. Panel "El otro" is transient and within a single session
   carries entries of a single origin, so the prefix would be redundant. */
.entry[data-origin="other"] p {
  color: var(--color-text-other);
}

.entry[data-origin="other"] time {
  color: var(--color-text-other);
  opacity: 0.85;
}

.panel-self .entry[data-origin="other"] p::before {
  content: "↩ ";
}

/* ============ Controls ============ */

.app-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 20px 24px 28px;
  border-top: 1px solid var(--divider);
  background: var(--bg-panel);
  flex: 0 0 auto;
}

/* ============ Direction toggle ============ */

.direction-toggle {
  display: inline-flex;
  border: 1px solid var(--divider);
  border-radius: 8px;
  padding: 2px;
  width: 360px;
  max-width: 100%;
  background: var(--bg-app);
  transition: opacity 120ms ease;
}

.direction-toggle button {
  flex: 1;
  background: transparent;
  color: var(--text-primary);
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  transition: background 120ms ease, color 120ms ease;
}

.direction-toggle button[aria-checked="true"] {
  background: var(--text-primary);
  color: var(--bg-app);
}

.direction-toggle button[aria-checked="false"]:hover {
  background: var(--divider);
}

/* ============ Push-to-talk button ============ */

.ptt-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.push-to-talk {
  position: relative;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: transparent;
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease, opacity 120ms ease;
}

.push-to-talk:hover {
  border-color: var(--accent-hover);
  color: var(--accent-hover);
}

.ptt-icon { display: none; line-height: 0; }
.ptt-wrap[data-state="idle"] .ptt-icon-mic { display: inline-flex; }
.ptt-wrap[data-state="recording"] .ptt-icon-stop { display: inline-flex; }
.ptt-wrap[data-state="switching"] .ptt-icon-spinner { display: inline-flex; }

.ptt-icon-spinner svg { animation: spin 1s linear infinite; }

@keyframes spin {
  to { transform: rotate(360deg); }
}

.ptt-text { display: none; }

.ptt-wrap[data-state="recording"] .push-to-talk {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.ptt-wrap[data-state="recording"] .push-to-talk::after {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: inherit;
  border: 2px solid var(--accent);
  pointer-events: none;
  animation: ptt-pulse 1.5s ease-out infinite;
}

@keyframes ptt-pulse {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(1.15); opacity: 0; }
}

.ptt-wrap[data-state="switching"] .push-to-talk {
  background: color-mix(in srgb, var(--accent) 50%, transparent);
  color: #fff;
  border-color: transparent;
  pointer-events: none;
  cursor: not-allowed;
}

.ptt-caption {
  margin: 0;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  text-align: center;
  min-height: 1.3em;
}

.mic-blocked-warning {
  margin: 0;
  font-size: 12px;
  font-weight: 500;
  color: var(--warning-text);
  text-align: center;
  max-width: 320px;
  line-height: 1.3;
}

.mic-blocked-warning[hidden] { display: none; }

/* ============ Locked controls during recording ============ */

body.recording .direction-toggle,
body.recording .lang-select-wrap {
  opacity: 0.4;
  pointer-events: none;
  cursor: not-allowed;
}

/* ============ Mobile (<768px) ============ */

@media (max-width: 767px) {
  .app-header {
    height: auto;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    column-gap: 12px;
    row-gap: 4px;
    padding: 8px 16px;
    min-height: 44px;
  }
  .brand { grid-column: 1; grid-row: 1; }
  .header-meta {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
  }
  .header-actions {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: end;
  }
  .finalize {
    padding-right: 0;
  }

  .app-panels {
    flex-direction: column;
  }
  .panel + .panel {
    border-left: none;
    border-top: 1px solid var(--divider);
  }
  .panel { flex: 1 1 50%; }
  .panel-header { height: 28px; }

  .app-controls {
    padding: 0;
    gap: 0;
    border-top: none;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .direction-toggle {
    width: 100%;
    max-width: none;
    border-left: none;
    border-right: none;
    border-radius: 0;
    padding: 4px;
    height: 44px;
    background: var(--bg-panel);
  }

  .ptt-wrap {
    width: 100%;
    gap: 0;
  }

  .push-to-talk {
    width: 100%;
    height: 88px;
    border-radius: 0;
    gap: 12px;
    border-left: none;
    border-right: none;
  }

  .ptt-wrap[data-state="idle"] .push-to-talk {
    border-top: 2px solid var(--accent);
    border-bottom: none;
  }

  .push-to-talk .ptt-text {
    display: inline;
    font-size: 15px;
    font-weight: 500;
  }

  .ptt-caption { display: none; }
}

/* ============ Toast (transient feedback) ============ */

#toast-container {
  position: fixed;
  z-index: 9000;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: calc(100% - 32px);
}

.toast {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--text-primary);
  color: var(--bg-app);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
  max-width: 100%;
}

.toast-text { flex: 1; min-width: 0; }

.toast-success {
  background: var(--text-primary);
}

.toast-persistent {
  background: var(--accent);
  color: #fff;
}

.toast-close {
  background: transparent;
  border: none;
  color: inherit;
  font-size: 18px;
  line-height: 1;
  padding: 0 4px;
  cursor: pointer;
}

/* ============ Modal (Phase 4 fallback + Finalizar confirm) ============ */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.45);
}

.modal-overlay[hidden] { display: none; }

.modal-card {
  background: var(--bg-panel);
  color: var(--text-primary);
  border: 1px solid var(--divider);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
  padding: 20px;
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.modal-description {
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

#fallback-modal-textarea {
  width: 100%;
  min-height: 180px;
  max-height: 40vh;
  resize: vertical;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
  padding: 8px;
  border: 1px solid var(--divider);
  border-radius: 6px;
  background: var(--bg-app);
  color: var(--text-primary);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.modal-actions-stack {
  flex-direction: column-reverse;
  align-items: stretch;
}

.modal-actions-stack .modal-btn {
  width: 100%;
  text-align: center;
}

@media (min-width: 480px) {
  .modal-actions-stack {
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
  }
  .modal-actions-stack .modal-btn {
    width: auto;
  }
}

.modal-btn {
  background: transparent;
  border: 1px solid var(--divider);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}

.modal-btn:hover {
  background: var(--divider);
}

.modal-btn-primary {
  background: var(--text-primary);
  color: var(--bg-app);
  border-color: var(--text-primary);
}

.modal-btn-primary:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.modal-btn-danger {
  background: transparent;
  color: var(--accent);
  border-color: var(--accent);
}

.modal-btn-danger:hover {
  background: var(--accent);
  color: #fff;
}

/* ============ Recovery banner (Phase 4) ============ */

.recovery-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--divider);
  font-size: 13px;
  color: var(--text-primary);
}

.recovery-banner[hidden] { display: none; }

.recovery-banner-text {
  flex: 1;
  min-width: 0;
}

.recovery-banner-actions {
  display: inline-flex;
  gap: 8px;
  flex-shrink: 0;
}

.recovery-btn {
  background: transparent;
  border: 1px solid var(--divider);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
}

.recovery-btn:hover {
  background: var(--divider);
}

.recovery-btn-primary {
  background: var(--text-primary);
  color: var(--bg-app);
  border-color: var(--text-primary);
}

.recovery-btn-primary:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

@media (max-width: 767px) {
  .recovery-banner {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 10px 12px;
  }
  .recovery-banner-actions {
    justify-content: flex-end;
  }
}

/* Phase 3a debug panel. Only visible with ?debug=1. Removed in Phase 3b. */
#debug-panel {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--bg-elevated, #fff);
  color: var(--text-primary, #111);
  border: 1px solid var(--border, rgba(0, 0, 0, 0.15));
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  font-size: 13px;
  max-width: 260px;
}

#debug-panel[hidden] { display: none; }

#debug-panel label {
  display: flex;
  align-items: center;
  gap: 6px;
}

#debug-panel select {
  font: inherit;
  padding: 2px 4px;
}

#debug-panel .debug-buttons {
  display: flex;
  gap: 8px;
}

#debug-panel button {
  font: inherit;
  padding: 4px 8px;
  cursor: pointer;
}

#debug-status {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  opacity: 0.8;
}
