/* ========================================
   CSS Custom Properties — Theming
   ======================================== */

:root {
  --bg: #ffffff;
  --bg-secondary: #f5f5f7;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --text-tertiary: #aeaeb2;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --danger: #ff3b30;
  --success: #34c759;
  --warning: #ff9500;
  --surface: #f5f5f7;
  --border: #d2d2d7;
  --shadow: rgba(0, 0, 0, 0.04);
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --radius: 12px;
  --radius-sm: 8px;
  --transcript-size: 1.125rem;
  --transcript-weight: 400;
  --transcript-line-height: 1.65;
}

/* System dark mode (default behavior) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --bg-secondary: #1c1c1e;
    --text: #f5f5f7;
    --text-secondary: #98989d;
    --text-tertiary: #636366;
    --surface: #1c1c1e;
    --border: #38383a;
    --shadow: rgba(0, 0, 0, 0.5);
    --modal-bg: #2a2a2e;
    --modal-border: #48484a;
  }
}

/* Manual dark override */
[data-theme="dark"] {
  --bg: #000000;
  --bg-secondary: #1c1c1e;
  --text: #f5f5f7;
  --text-secondary: #98989d;
  --text-tertiary: #636366;
  --surface: #1c1c1e;
  --border: #38383a;
  --shadow: rgba(0, 0, 0, 0.5);
  --modal-bg: #2a2a2e;
  --modal-border: #48484a;
}

/* Light mode (default values; also re-applied when manually forced) */
:root,
[data-theme="light"] {
  --modal-bg: #ffffff;
  --modal-border: #d2d2d7;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg-secondary: #f5f5f7;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --text-tertiary: #aeaeb2;
  --surface: #f5f5f7;
  --border: #d2d2d7;
  --shadow: rgba(0, 0, 0, 0.04);
}

/* ========================================
   Reset & Base
   ======================================== */

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

html {
  font-family: var(--font);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}

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

.header-left h1 {
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.meta {
  font-size: 0.75rem;
  color: var(--text-tertiary);
}

/* ========================================
   Badges
   ======================================== */

.badge {
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-stopped {
  background: var(--surface);
  color: var(--text-secondary);
}

.badge-active {
  background: #34c75920;
  color: var(--success);
}

.badge-paused {
  background: #ff950020;
  color: var(--warning);
}

/* ========================================
   Buttons
   ======================================== */

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
  background: var(--surface);
  color: var(--text);
}

.icon-btn.active {
  background: var(--accent);
  color: #fff;
}

#lang-label {
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

#lang-label.lang-sk {
  color: #ff3b30;
}

#lang-label.lang-en {
  color: #0071e3;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  gap: 6px;
}

.btn:hover:not(:disabled) {
  background: var(--surface);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-danger {
  color: var(--danger);
  border-color: var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: #ff3b3010;
}

.btn-small {
  padding: 4px 10px;
  font-size: 0.75rem;
}

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

.btn.active:hover:not(:disabled) {
  background: var(--accent-hover);
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.pill-btn:hover {
  border-color: var(--text-secondary);
}

.pill-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ========================================
   Speaker Panel
   ======================================== */

#speaker-panel {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}

.speaker-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.speaker-row + .speaker-row {
  margin-top: 12px;
}

.speaker-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.speaker-group label {
  font-size: 0.6875rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.label-link {
  font-size: 0.6875rem;
  color: var(--accent);
  text-decoration: none;
  margin-left: 6px;
  text-transform: none;
  letter-spacing: 0;
}
.label-link:hover { text-decoration: underline; }

.speaker-group select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.8125rem;
  appearance: none;
  cursor: pointer;
  min-width: 180px;
}

.transport-buttons {
  display: flex;
  gap: 8px;
}

#mic-level {
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border);
  height: 24px;
  flex: 1;
  max-width: 300px;
}

/* Speaker input rows */
.input-row {
  display: flex;
  gap: 6px;
  align-items: center;
}

#voice-custom-row {
  margin-top: 6px;
}

.input-row input[type="text"] {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.8125rem;
  outline: none;
  transition: border-color 0.15s;
}

.input-row input[type="text"]:focus {
  border-color: var(--accent);
}

/* Persistent banner */
#banner-bar {
  padding: 10px 20px;
  background: #0071e320;
  border-bottom: 1px solid #0071e340;
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 500;
  text-align: center;
}

/* Announcement toast */
#announcement-toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 0.9375rem;
  font-weight: 500;
  z-index: 200;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
  max-width: calc(100vw - 40px);
  text-align: center;
}

#announcement-toast.visible {
  opacity: 1;
}

/* Debug Panel */
#debug-panel {
  margin-top: 12px;
}

#debug-panel summary {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}

#debug-log {
  margin-top: 8px;
  max-height: 445px;
  overflow-y: auto;
  background: #000;
  color: #e0e0e0;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  font-size: 0.6875rem;
  line-height: 1.5;
}

.debug-line {
  white-space: pre-wrap;
  word-break: break-all;
}

.debug-line .ts {
  color: #636366;
}

.debug-line .src {
  font-weight: 600;
}

.debug-line.level-info .src { color: #64d2ff; }
.debug-line.level-warn .src { color: #ffd60a; }
.debug-line.level-error .src { color: #ff453a; }
.debug-line.level-info .msg { color: #e0e0e0; }
.debug-line.level-warn .msg { color: #ffd60a; }
.debug-line.level-error .msg { color: #ff6961; }

/* End-to-end latency total — highlighted for quick spotting */
.debug-line .latency-total {
  color: #30d158;
  font-weight: 700;
  background: rgba(48, 209, 88, 0.12);
  padding: 0 4px;
  border-radius: 3px;
}

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

#listener-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.controls-left,
.controls-center,
.controls-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ========================================
   Transcript Area
   ======================================== */

#transcript-container {
  flex: 1;
  min-height: 0;
  position: relative;
}

#transcript {
  height: 100%;
  overflow-y: auto;
  padding: 20px;
}

#scroll-btn {
  position: absolute;
  bottom: 1.25rem;
  right: 1.25rem;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px var(--shadow);
  transition: opacity 0.2s, transform 0.2s;
}
#scroll-btn:hover {
  color: var(--text);
  border-color: var(--text-tertiary);
}
#scroll-btn.hidden {
  opacity: 0 !important;     /* override .hidden { display:none } */
  display: flex !important;  /* keep in layout so transition works */
  pointer-events: none;
  transform: translateY(6px);
}

.entry {
  margin-bottom: 16px;
  animation: fadeIn 0.2s ease;
}

.entry.speaking .translation {
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
}

.entry .translation {
  font-size: var(--transcript-size);
  font-weight: var(--transcript-weight);
  line-height: var(--transcript-line-height);
  color: var(--text);
}

.entry .original {
  font-size: calc(var(--transcript-size) * 0.85);
  line-height: 1.5;
  color: var(--text-tertiary);
  margin-top: 4px;
}

.entry .original.hidden {
  display: none;
}

.partial {
  font-size: var(--transcript-size);
  font-style: italic;
  color: var(--text-tertiary);
  line-height: var(--transcript-line-height);
  min-height: 1.5em;
}

.streaming {
  opacity: 0.7;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  color: var(--text-tertiary);
  font-size: 1rem;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ========================================
   Modals (dialog)
   ======================================== */

dialog {
  border: 1px solid var(--modal-border);
  border-radius: var(--radius);
  background: var(--modal-bg);
  color: var(--text);
  padding: 28px;
  max-width: 380px;
  width: calc(100% - 40px);
  box-shadow: 0 20px 60px var(--shadow);
  margin: 0;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

dialog h2 {
  font-size: 1.125rem;
  font-weight: 600;
  margin-bottom: 16px;
}

dialog input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9375rem;
  outline: none;
  transition: border-color 0.15s;
}

dialog input[type="password"]:focus {
  border-color: var(--accent);
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

.dialog-buttons-split {
  justify-content: space-between;
  align-items: center;
}

.dialog-buttons-right {
  display: flex;
  gap: 8px;
}

dialog.dialog-wide {
  max-width: 640px;
}

#manuscript-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  font-size: 0.8125rem;
  line-height: 1.5;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

#manuscript-textarea:focus {
  border-color: var(--accent);
}

.modal-content {
  text-align: center;
}

.modal-content p {
  color: var(--text-secondary);
  font-size: 0.9375rem;
  line-height: 1.5;
}

.error {
  color: var(--danger);
  font-size: 0.8125rem;
  margin-top: 8px;
}

/* ========================================
   Tuning Modal
   ======================================== */

.tuning-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.tuning-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tuning-label-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.tuning-field label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
}

.tuning-field .tuning-unit {
  font-size: 0.6875rem;
  color: var(--text-tertiary);
}

.tuning-help-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-tertiary);
  font-size: 0.625rem;
  font-weight: 700;
  cursor: help;
  position: relative;
  flex-shrink: 0;
  padding: 0;
  font-family: var(--font);
}

.tuning-help-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#tuning-modal {
  overflow: visible;
}

.tuning-tooltip {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  width: 280px;
  padding: 10px 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.6875rem;
  font-weight: 400;
  line-height: 1.5;
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  z-index: 10;
  pointer-events: none;
  text-align: left;
}

.tuning-help-btn:hover .tuning-tooltip {
  display: block;
}

footer .tuning-tooltip {
  top: auto;
  bottom: calc(100% + 6px);
}

.tuning-field input[type="number"] {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
  -moz-appearance: textfield;
}

.tuning-field input[type="number"]::-webkit-inner-spin-button,
.tuning-field input[type="number"]::-webkit-outer-spin-button {
  opacity: 1;
}

.tuning-field input[type="number"]:focus {
  border-color: var(--accent);
}

.tuning-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

.tuning-actions-right {
  display: flex;
  gap: 8px;
}

/* ========================================
   Connection dot
   ======================================== */

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
  background: var(--text-tertiary);
  transition: background 0.3s;
}

.conn-dot.conn-connected {
  background: var(--success);
}

.conn-dot.conn-connecting {
  background: var(--warning);
  animation: conn-pulse 1.4s ease-in-out infinite;
}

@keyframes conn-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ========================================
   Pause overlay
   ======================================== */

.pause-overlay {
  position: absolute;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: #ff950020;
  border: 1px solid #ff950050;
  border-radius: 20px;
  color: var(--warning);
  font-size: 0.75rem;
  font-weight: 600;
  pointer-events: none;
  z-index: 10;
}

/* ========================================
   Quota badge
   ======================================== */

.quota-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.quota-loading {
  background: var(--surface);
  color: var(--text-tertiary);
}

.quota-error {
  background: var(--surface);
  color: var(--text-tertiary);
}

.quota-normal {
  background: #34c75920;
  color: var(--success);
}

.quota-warning {
  background: #ff950020;
  color: var(--warning);
}

.quota-critical {
  background: #ff3b3020;
  color: var(--danger);
}

.quota-flash {
  background: #ff3b3020;
  color: var(--danger);
  animation: quota-flash 0.8s ease-in-out infinite;
}

@keyframes quota-flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* ========================================
   API key cards
   ======================================== */

.api-keys-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
  min-height: 40px;
}

.api-key-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.api-key-card.is-active {
  border-color: var(--accent);
  background: #0071e310;
}

.api-key-card-info {
  flex: 1;
  min-width: 0;
}

.api-key-card-name {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.api-key-active-badge {
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--accent);
  color: #fff;
  padding: 1px 6px;
  border-radius: 10px;
}

.api-key-card-key {
  font-size: 0.6875rem;
  color: var(--text-tertiary);
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  margin-top: 2px;
}

.api-key-card-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

/* Text inputs inside dialogs */
dialog input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9375rem;
  outline: none;
  transition: border-color 0.15s;
  margin-top: 4px;
}

dialog input[type="text"]:focus {
  border-color: var(--accent);
}

.new-key-quota {
  margin-top: 8px;
  font-size: 0.8125rem;
}

/* ========================================
   Manuscript backup list
   ======================================== */

.manuscript-backups {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.manuscript-backups-label {
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}

.manuscript-backup-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
}

.manuscript-backup-item:last-child {
  border-bottom: none;
}

.backup-meta {
  color: var(--text-secondary);
  flex-shrink: 0;
  min-width: 130px;
}

.backup-preview {
  flex: 1;
  color: var(--text-tertiary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: 'SF Mono', 'Menlo', 'Consolas', monospace;
  font-size: 0.6875rem;
}

/* ========================================
   Utility
   ======================================== */

.hidden {
  display: none !important;
}

/* ========================================
   Responsive
   ======================================== */

@media (max-width: 640px) {
  header {
    padding: 10px 16px;
  }

  .header-left h1 {
    font-size: 1rem;
  }

  #listener-controls {
    padding: 8px 16px;
  }

  #transcript {
    padding: 16px;
  }

  #speaker-panel {
    padding: 12px 16px;
  }

  .speaker-group select {
    min-width: 140px;
  }

  .speaker-row {
    gap: 10px;
  }

  #mic-level {
    max-width: 100%;
    order: 10;
    width: 100%;
  }

  .transport-buttons {
    flex: 1;
    justify-content: center;
  }
}

/* ========================================
   Footer
   ======================================== */

footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  font-size: 0.6875rem;
  color: var(--text-tertiary);
  text-align: center;
  line-height: 1.4;
}

footer a {
  color: var(--accent);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* ========================================
   Scrollbar (subtle, Apple-style)
   ======================================== */

#transcript::-webkit-scrollbar {
  width: 6px;
}

#transcript::-webkit-scrollbar-track {
  background: transparent;
}

#transcript::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

#debug-log::-webkit-scrollbar {
  width: 4px;
}

#debug-log::-webkit-scrollbar-track {
  background: transparent;
}

#debug-log::-webkit-scrollbar-thumb {
  background: #444;
  border-radius: 2px;
}
