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

:root {
  --bg: #1a1a1a;
  --fg: #fafafa;
  --card: #262626;
  --card-fg: #fafafa;
  --muted: #404040;
  --muted-fg: #a3a3a3;
  --border: rgba(255,255,255,0.10);
  --input-border: rgba(255,255,255,0.15);
  --primary: #e5e5e5;
  --primary-fg: #262626;
  --accent: #404040;
  --accent-fg: #fafafa;
  --destructive: #ef4444;
  --ring: #737373;
  --radius: 0.625rem;
}

.light {
  --bg: #f5f5f5;
  --fg: #171717;
  --card: #ffffff;
  --card-fg: #171717;
  --muted: #e5e5e5;
  --muted-fg: #737373;
  --border: #d4d4d4;
  --input-border: #d4d4d4;
  --primary: #262626;
  --primary-fg: #fafafa;
  --accent: #e5e5e5;
  --accent-fg: #262626;
  --destructive: #dc2626;
  --ring: #737373;
}

html { scrollbar-gutter: stable; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  line-height: 1.5;
}

/* ---- Header ---- */
.header {
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 0;
}
.header-inner {
  max-width: 56rem;
  margin: 0 auto;
  padding: 0 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.header h1 { font-size: 1.5rem; font-weight: 700; }
.header-actions { display: flex; align-items: center; gap: 0.25rem; }

/* ---- Buttons ---- */
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-weight: 500;
  border-radius: var(--radius);
  transition: background 0.15s, opacity 0.15s;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
  padding: 0.75rem 1.5rem;
  font-size: 0.875rem;
  width: 100%;
}
.btn-primary:hover:not(:disabled) { opacity: 0.9; }
.btn-ghost {
  padding: 0.375rem;
  border-radius: var(--radius);
  color: var(--muted-fg);
}
.btn-ghost:hover:not(:disabled) { background: var(--accent); color: var(--accent-fg); }
.btn-outline {
  border: 1px solid var(--border);
  padding: 0.5rem;
  border-radius: var(--radius);
}
.btn-outline:hover:not(:disabled) { background: var(--accent); }
.btn-sm { font-size: 0.75rem; padding: 0.25rem 0.5rem; }
.btn-destructive { color: var(--destructive); }
.btn-destructive:hover { color: var(--destructive); background: rgba(239,68,68,0.1); }

/* ---- Main layout ---- */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 56rem;
  margin: 0 auto;
  width: 100%;
  padding: 1rem;
  gap: 0.75rem;
}

/* ---- Card ---- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}

/* ---- Audio Player ---- */
.player { transition: opacity 0.2s; }
.player.disabled { opacity: 0.5; }
.player-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.player-label {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--muted-fg);
  font-size: 0.75rem;
  font-weight: 500;
}
.player-title {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--fg);
  max-width: 60%;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.player-seek {
  width: 100%;
  height: 6px;
  background: var(--muted);
  border-radius: 3px;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
.player-seek:disabled { cursor: not-allowed; }
.player-seek::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--fg);
}
.player-times {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted-fg);
  margin-top: 0.25rem;
}
.player-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

/* ---- Collapsible ---- */
.collapsible-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: var(--card);
  padding: 0.625rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  transition: background 0.15s;
  cursor: pointer;
}
.collapsible-trigger:hover { background: var(--accent); }
.collapsible-trigger .label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--muted-fg);
}
.collapsible-trigger .count { font-size: 0.75rem; color: var(--muted-fg); opacity: 0.6; }
.collapsible-trigger svg { transition: transform 0.2s; color: var(--muted-fg); }
.collapsible-trigger.open svg { transform: rotate(180deg); }
.collapsible-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
}
.collapsible-content.open { max-height: 1000px; }

/* ---- History ---- */
.history-list { display: flex; flex-direction: column; gap: 0.375rem; }
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  transition: background 0.15s;
}
.history-item:hover { background: rgba(255,255,255,0.05); }
.light .history-item:hover { background: rgba(0,0,0,0.04); }
.history-item.active { background: rgba(255,255,255,0.08); border-left: 2px solid var(--primary); }
.light .history-item.active { background: rgba(0,0,0,0.06); }
.history-item-text {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  color: inherit;
  padding: 0;
}
.history-item-title {
  font-size: 0.875rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-item-preview {
  font-size: 0.75rem;
  color: var(--muted-fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-item-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
}
.history-item-date { font-size: 0.75rem; color: var(--muted-fg); opacity: 0.6; }
.history-item .btn-delete { opacity: 0.5; }
.history-item:hover .btn-delete { opacity: 1; }
.history-footer {
  margin-top: 0.375rem;
  padding-top: 0.375rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}
.history-footer .btn { font-size: 0.65rem; padding: 0.125rem 0.375rem; opacity: 0.5; }
.history-footer .btn:hover { opacity: 1; }

/* ---- Error ---- */
.error-box {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  border-radius: var(--radius);
  padding: 0.625rem 1rem;
  font-size: 0.875rem;
  color: var(--destructive);
}

/* ---- Textarea ---- */
.textarea-wrapper {
  position: relative;
}
.textarea-backdrop {
  position: absolute;
  top: 1px; left: 1px; right: 1px; bottom: 1px;
  padding: 0.75rem 1rem;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  overflow: hidden;
  pointer-events: none;
  color: transparent;
  background: var(--card);
  border-radius: var(--radius);
  z-index: 1;
}
.textarea-backdrop mark {
  background: #f97316;
  color: transparent;
  border-radius: 2px;
}
.textarea {
  width: 100%;
  min-height: 400px;
  padding: 0.75rem 1rem;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  line-height: 1.5;
  resize: vertical;
  transition: border-color 0.15s;
  position: relative;
  z-index: 2;
}
.textarea.highlight-active {
  background: transparent;
}
.textarea::placeholder { color: var(--muted-fg); }
.textarea::selection { background: #f97316; color: #fff; }
.textarea::-moz-selection { background: #f97316; color: #fff; }
.textarea:focus { outline: none; border-color: var(--ring); box-shadow: 0 0 0 1px var(--ring); }
.btn-focus-mode {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 3;
  padding: 0.375rem;
  border-radius: var(--radius);
  color: var(--muted-fg);
  background: var(--card);
  border: 1px solid var(--border);
  opacity: 0.6;
  transition: opacity 0.15s, background 0.15s;
}
.btn-focus-mode:hover { opacity: 1; background: var(--accent); color: var(--accent-fg); }
.btn-focus-mode.active { opacity: 1; color: var(--fg); }

/* ---- Focus Mode ---- */
main.focus-mode #history-section,
main.focus-mode #error-box,
main.focus-mode #btn-generate,
main.focus-mode .quick-actions,
main.focus-mode .text-stats { display: none; }
main.focus-mode .textarea-wrapper { flex: 1; display: flex; flex-direction: column; }
main.focus-mode .textarea { flex: 1; min-height: 0; }

/* ---- Speed Button ---- */
.btn-speed {
  font-size: 0.7rem;
  font-weight: 600;
  min-width: 2rem;
  text-align: center;
}

/* ---- Quick Actions ---- */
.quick-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.quick-btn {
  font-size: 0.75rem;
  color: var(--muted-fg);
  padding: 0.2rem 0.5rem;
  border-radius: calc(var(--radius) - 2px);
  border: 1px solid var(--border);
  background: none;
  transition: background 0.15s, color 0.15s;
  cursor: pointer;
}
.quick-btn:hover { background: var(--accent); color: var(--accent-fg); }
.quick-btn:active { opacity: 0.7; }
.quick-btn-generate {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  background: var(--primary);
  color: var(--primary-fg);
  border-color: var(--primary);
  font-weight: 500;
}
.quick-btn-generate:hover { opacity: 0.9; background: var(--primary); color: var(--primary-fg); }
.quick-btn-generate:disabled { opacity: 0.4; cursor: not-allowed; }
.quick-btn-generate.loading { pointer-events: none; }
.char-limit {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--muted-fg);
}
.char-limit.warn { color: var(--destructive); }

/* ---- Text Stats ---- */
.text-stats {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.75rem;
  color: var(--muted-fg);
  flex-wrap: wrap;
}
.stat-sep { opacity: 0.4; }

/* ---- Login Page ---- */
.login-page {
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.login-card {
  width: 100%;
  max-width: 24rem;
  background: var(--card);
  border-radius: 1rem;
  padding: 2rem;
  border: 1px solid var(--border);
  box-shadow: 0 20px 40px rgba(0,0,0,0.3);
}
.login-header { text-align: center; margin-bottom: 2rem; }
.login-header h1 { font-size: 1.5rem; font-weight: 700; }
.login-header p { font-size: 0.875rem; color: var(--muted-fg); margin-top: 0.25rem; }
.login-form { display: flex; flex-direction: column; gap: 1rem; }
.login-input {
  width: 100%;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-family: inherit;
  font-size: 1rem;
  transition: border-color 0.15s;
}
.login-input::placeholder { color: var(--muted-fg); }
.login-input:focus { outline: none; border-color: var(--ring); box-shadow: 0 0 0 1px var(--ring); }
.login-error { font-size: 0.875rem; color: #f87171; text-align: center; }

/* ---- Dropdown Menu ---- */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 0.25rem;
  min-width: 14rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.25rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  z-index: 50;
}
.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  border-radius: calc(var(--radius) - 2px);
  color: var(--fg);
  transition: background 0.15s;
}
.dropdown-item:hover { background: var(--accent); }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}
.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  width: 100%;
  max-width: 24rem;
  box-shadow: 0 20px 40px rgba(0,0,0,0.4);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-sm { max-width: 18rem; }
.modal-header h2 { font-size: 1rem; font-weight: 600; }
.modal-body { padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem; }
.modal-footer { padding: 0.75rem 1.25rem; border-top: 1px solid var(--border); }
.modal-footer .btn-primary { padding: 0.625rem 1.25rem; }

/* ---- Form Controls ---- */
.form-group { display: flex; flex-direction: column; gap: 0.375rem; }
.form-label { font-size: 0.8125rem; font-weight: 500; color: var(--muted-fg); }
.form-select, .form-input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg);
  font-family: inherit;
  font-size: 0.875rem;
  transition: border-color 0.15s;
}
.form-select:focus, .form-input:focus {
  outline: none;
  border-color: var(--ring);
  box-shadow: 0 0 0 1px var(--ring);
}
.form-hint { font-size: 0.75rem; color: var(--muted-fg); opacity: 0.7; }

/* ---- History Voice Tag ---- */
.history-item-voice {
  font-size: 0.625rem;
  color: var(--muted-fg);
  opacity: 0.6;
}

/* ---- SVG icons ---- */
svg { display: inline-block; vertical-align: middle; }
[hidden] { display: none !important; }

/* ---- Spinner ---- */
@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 1s linear infinite; }

/* ---- Responsive ---- */
@media (max-width: 640px) {
  .history-item-date { display: none; }
}
