/* ── Reset & Base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #0e0e14;
  --surface:    #17171f;
  --card:       #1f1f2b;
  --card-hover: #262633;
  --border:     #2a2a3a;
  --accent:     #7c4dff;
  --accent-dim: #5c35c0;
  --accent-glow: rgba(124, 77, 255, 0.25);
  --text:       #e0e0ee;
  --muted:      #6b6b88;
  --danger:     #ef5350;
  --radius:     12px;
  --radius-sm:  8px;
  --transition: 0.15s ease;
}

html { font-size: 16px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  line-height: 1.5;
  min-height: 100dvh;
}

/* ── Header ───────────────────────────────────────────────────── */
header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(14, 14, 20, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: 800px;
  margin: 0 auto;
  height: 52px;
}

.header-icon { font-size: 1.25rem; }

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

/* ── Main Layout ──────────────────────────────────────────────── */
main {
  max-width: 800px;
  margin: 0 auto;
  padding: 1rem 1rem 4rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* ── Section ──────────────────────────────────────────────────── */
.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.section-title {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ── Voice Grid ───────────────────────────────────────────────── */
.voice-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

@media (min-width: 480px) {
  .voice-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 640px) {
  .voice-grid { grid-template-columns: repeat(5, 1fr); }
}

@media (min-width: 768px) {
  .voice-grid { grid-template-columns: repeat(6, 1fr); }
}

.voice-card {
  position: relative;
  overflow: hidden;
  aspect-ratio: 1;
  padding: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color var(--transition), transform var(--transition), box-shadow var(--transition);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.voice-card:hover {
  border-color: #3a3a50;
}

.voice-card:active {
  transform: scale(0.96);
}

.voice-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.voice-avatar {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  color: #fff;
  background-size: cover;
  background-position: center;
}

.voice-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  font-size: 0.6rem;
  font-weight: 600;
  color: #fff;
  text-align: center;
  line-height: 1.2;
  padding: 0.9rem 0.2rem 0.2rem;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.72));
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.voice-card.selected .voice-name {
  color: #fff;
}

/* ── Loading State ────────────────────────────────────────────── */
.loading-state {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 2rem 0;
  font-size: 0.9rem;
}

/* ── Text Input ───────────────────────────────────────────────── */
.input-wrapper {
  position: relative;
}

textarea {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  padding: 0.75rem;
  padding-bottom: 1.75rem;
  resize: vertical;
  min-height: 100px;
  transition: border-color var(--transition);
  -webkit-appearance: none;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
}

textarea::placeholder { color: var(--muted); }

.char-count {
  position: absolute;
  bottom: 0.5rem;
  right: 0.75rem;
  font-size: 0.7rem;
  color: var(--muted);
  pointer-events: none;
}

.char-count.near-limit { color: #ff9800; }
.char-count.at-limit   { color: var(--danger); }

/* ── Buttons ──────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.75rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition), opacity var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.btn:active:not(:disabled) { transform: scale(0.97); }

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

.btn-primary:hover:not(:disabled) { background: #8d60ff; }

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

.btn-secondary {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  width: 100%;
}

.btn-secondary:hover { background: var(--card-hover); }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 0.35rem 0.6rem;
}

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

/* Spinner animation */
.btn-spinner {
  display: inline-flex;
}

[hidden] { display: none !important; }

.btn-spinner svg {
  width: 18px;
  height: 18px;
  animation: spin 0.8s linear infinite;
}

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

/* ── Audio Player ─────────────────────────────────────────────── */
audio {
  width: 100%;
  border-radius: var(--radius-sm);
  height: 42px;
}

/* Webkit audio player dark tint */
audio::-webkit-media-controls-panel {
  background: var(--card);
}

.text-preview {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── History ──────────────────────────────────────────────────── */
#history-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.history-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.history-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-avatar {
  width: 24px;
  height: 24px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  background-size: cover;
  background-position: center;
}

.history-voice {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
}

.history-time {
  font-size: 0.7rem;
  color: var(--muted);
  margin-left: auto;
}

.history-text {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-audio-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-player {
  flex: 1;
  min-width: 0;
  height: 36px;
  border-radius: 6px;
}

.btn-download {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  background: var(--card-hover);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 1.1rem;
  text-decoration: none;
  transition: color var(--transition), border-color var(--transition);
}

.btn-download:hover {
  color: var(--text);
  border-color: #3a3a50;
}
