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

:root {
  --bg: #0f1419;
  --bg-elevated: #1a2332;
  --bg-hover: #243044;
  --border: #2d3a4f;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-soft: rgba(99, 102, 241, 0.15);
  --active-bg: rgba(99, 102, 241, 0.25);
  --success: #34d399;
  --danger: #f87171;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 56px;
  --controls-h: 140px;
  --vocab-w: 320px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.1rem;
  white-space: nowrap;
}

.logo-icon {
  width: 28px;
  height: 28px;
  background: var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

.file-inputs {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.file-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-muted);
  transition: all 0.2s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.file-label:hover { border-color: var(--accent); color: var(--text); }
.file-label.loaded { border-color: var(--success); color: var(--success); }
.file-label input { display: none; }

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

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  transition: all 0.2s;
  flex-shrink: 0;
}

.icon-btn:hover { border-color: var(--accent); background: var(--accent-soft); }
.icon-btn.active { background: var(--accent); border-color: var(--accent); }

/* Main layout */
.main {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
}

.subtitle-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: calc(var(--controls-h) + 16px);
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  text-align: center;
  padding: 32px;
}

.empty-state-icon { font-size: 3rem; margin-bottom: 16px; opacity: 0.5; }
.empty-state h3 { margin: 0 0 8px; color: var(--text); font-weight: 600; }
.empty-state p { margin: 0; font-size: 0.9rem; line-height: 1.6; }

.subtitle {
  padding: 14px 16px;
  margin-bottom: 8px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.2s;
  line-height: 1.7;
  font-size: 1rem;
}

.subtitle:hover { background: var(--bg-hover); border-color: var(--border); }

.subtitle.active {
  background: var(--active-bg);
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent-soft);
}

.subtitle .time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-variant-numeric: tabular-nums;
}

.subtitle.active .time { color: var(--accent-hover); }

.word {
  display: inline;
  padding: 1px 2px;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.15s;
}

.word:hover { background: var(--accent-soft); }
.word.saved { color: var(--success); text-decoration: underline dotted; }

/* Vocabulary panel */
.vocab-panel {
  width: var(--vocab-w);
  background: var(--bg-elevated);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.3s ease;
}

.vocab-panel.hidden { display: none; }

.vocab-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.vocab-count {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 400;
}

.vocab-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}

.vocab-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 16px;
  font-size: 0.9rem;
  line-height: 1.6;
}

.vocab-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  margin-bottom: 4px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  gap: 8px;
}

.vocab-item-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vocab-item-context {
  font-size: 0.75rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vocab-item-main { font-weight: 500; }

.vocab-delete {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 6px;
  font-size: 1rem;
  flex-shrink: 0;
  transition: all 0.15s;
}

.vocab-delete:hover { background: rgba(248, 113, 113, 0.15); color: var(--danger); }

.vocab-actions {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.vocab-actions button {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
}

.vocab-actions button:hover { border-color: var(--danger); color: var(--danger); }

/* Controls */
.controls {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 12px 16px;
  padding-bottom: max(12px, var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 20;
}

.controls audio {
  width: 100%;
  height: 40px;
  margin-bottom: 10px;
  border-radius: var(--radius-sm);
}

.controls audio::-webkit-media-controls-panel {
  background: var(--bg-hover);
}

.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-hover);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: all 0.2s;
  min-height: 40px;
  white-space: nowrap;
}

.toggle-btn:hover { border-color: var(--accent); color: var(--text); }

.toggle-btn.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.toggle-btn .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background 0.2s;
}

.toggle-btn.on .dot { background: var(--accent); }

.progress-info {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* Toast */
.toast {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s;
  z-index: 100;
  box-shadow: var(--shadow);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Overlay for mobile vocab */
.vocab-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 30;
}

/* Tablet */
@media (max-width: 1024px) {
  :root { --vocab-w: 280px; }
  .file-label { max-width: 140px; font-size: 0.8rem; padding: 8px 10px; }
}

/* Mobile */
@media (max-width: 768px) {
  :root {
    --controls-h: 160px;
  }

  .header {
    flex-wrap: wrap;
    gap: 8px;
  }

  .logo { font-size: 1rem; }

  .file-inputs {
    order: 3;
    width: 100%;
  }

  .file-label {
    flex: 1;
    max-width: none;
    justify-content: center;
  }

  .vocab-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(90vw, 360px);
    z-index: 40;
    transform: translateX(100%);
    box-shadow: var(--shadow);
  }

  .vocab-panel.open {
    transform: translateX(0);
  }

  .vocab-panel.hidden { display: flex; }

  .vocab-overlay.show { display: block; }

  .subtitle {
    padding: 12px 14px;
    font-size: 0.95rem;
  }

  .control-row { justify-content: center; }

  .progress-info {
    width: 100%;
    text-align: center;
    margin-left: 0;
    margin-top: 4px;
  }

  .toggle-btn {
    flex: 1;
    justify-content: center;
    min-width: 0;
    padding: 8px 10px;
    font-size: 0.8rem;
  }
}

@media (max-width: 380px) {
  .toggle-btn {
    padding: 8px 10px;
    font-size: 0.75rem;
    gap: 4px;
  }
}
