:root {
  color-scheme: light dark;

  --bg: #f4f4f2;
  --bg-tint: #ececeb;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --text: #17171b;
  --muted: #6c6c78;
  --faint: #9a9aa4;
  --line: #e3e3df;
  --line-strong: #cfcfc9;
  --accent: #4338ca;
  --accent-soft: #eef0fe;
  --accent-text: #ffffff;
  --ok: #15803d;
  --bad: #c02b22;
  --shadow: 0 1px 2px rgba(20, 20, 30, .05), 0 8px 24px -14px rgba(20, 20, 30, .22);

  --radius: 12px;
  --radius-sm: 8px;

  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Noto Sans TC",
    "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d11;
    --bg-tint: #15151b;
    --surface: #17171d;
    --surface-2: #1d1d24;
    --text: #ececf1;
    --muted: #9b9baa;
    --faint: #6f6f80;
    --line: #282831;
    --line-strong: #3a3a46;
    --accent: #8b83ff;
    --accent-soft: #221f3d;
    --accent-text: #100e22;
    --ok: #4ade80;
    --bad: #f87171;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px -18px rgba(0, 0, 0, .9);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button, select, input[type="range"], label.file-picker { cursor: pointer; }

main {
  width: min(820px, calc(100% - 32px));
  margin: 28px auto 72px;
}

/* ---------- header ---------- */

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

header strong {
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -.015em;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 11px 4px 9px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 12px;
  white-space: nowrap;
}

.status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--faint);
}

.status.ok::before { background: var(--ok); }
.status.bad::before { background: var(--bad); }
.status.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); }

/* ---------- tabs ---------- */

.main-tabs,
.source-tabs {
  display: flex;
  gap: 3px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--bg-tint);
}

.main-tabs { margin-bottom: 14px; }
.source-tabs { margin-bottom: 18px; }

.main-tab,
.source-tab,
.tts-mode,
.clone-mode,
.ref-tab,
.engine-tab {
  flex: 1;
  min-height: 36px;
  padding: 0 8px;
  color: var(--muted);
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 14px;
  transition: color .12s, background .12s;
}

.main-tab:hover,
.source-tab:hover,
.tts-mode:hover,
.clone-mode:hover,
.ref-tab:hover { color: var(--text); }

.main-tab.active,
.source-tab.active,
.tts-mode.active,
.clone-mode.active,
.ref-tab.active,
.engine-tab.active {
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(20, 20, 30, .1);
  font-weight: 600;
}

.main-tab.active { color: var(--accent); }

/* ---------- panels ---------- */

.panel {
  display: none;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel.active,
.source-panel.active { display: block; }
.source-panel { display: none; }

/* ---------- inputs ---------- */

input[type="url"],
input[type="text"],
input[type="number"],
select,
textarea {
  width: 100%;
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  outline: none;
  transition: border-color .12s, box-shadow .12s;
}

input[type="url"],
input[type="text"],
input[type="number"],
select {
  min-height: 42px;
  padding: 0 12px;
}

select {
  appearance: none;
  padding-right: 34px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 19px, calc(100% - 12px) 19px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

textarea {
  padding: 13px;
  line-height: 1.65;
  resize: vertical;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.settings-row {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1fr;
  gap: 12px;
  margin-top: 18px;
}

label { min-width: 0; }

label > span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

label b { color: var(--text); font-weight: 600; }
input[type="range"] { width: 100%; accent-color: var(--accent); }

.file-picker {
  display: grid;
  min-height: 118px;
  place-items: center;
  padding: 16px;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  text-align: center;
  transition: border-color .12s, color .12s;
}

.file-picker:hover { color: var(--text); border-color: var(--accent); }
.file-picker input { position: absolute; opacity: 0; pointer-events: none; }
.file-picker.compact { min-height: 88px; }

details {
  margin-top: 16px;
  border-top: 1px solid var(--line);
}

summary {
  padding: 13px 0;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  list-style: none;
}

summary::before { content: "＋ "; }
details[open] summary::before { content: "－ "; }

.more-settings {
  display: grid;
  grid-template-columns: auto auto 96px 1fr;
  gap: 12px;
  align-items: end;
  padding-bottom: 6px;
}

.check {
  display: flex;
  min-height: 42px;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.check input { accent-color: var(--accent); }

/* ---------- buttons ---------- */

.primary {
  width: 100%;
  min-height: 48px;
  margin-top: 20px;
  color: var(--accent-text);
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  font-size: 15px;
  font-weight: 600;
  transition: filter .12s;
}

.primary:hover:not(:disabled) { filter: brightness(1.1); }
.primary:disabled { opacity: .5; cursor: progress; }

.secondary,
.text-button {
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color .12s, color .12s;
}

.secondary { min-height: 42px; padding: 0 20px; font-size: 14px; }
.secondary:hover, .text-button:hover { border-color: var(--accent); color: var(--accent); }

.secondary.recording {
  color: var(--bad);
  border-color: var(--bad);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% { background: color-mix(in srgb, var(--bad) 12%, transparent); }
}

.text-button { padding: 6px 11px; color: var(--muted); font-size: 13px; }

.mic-row { display: flex; align-items: center; gap: 14px; }
.mic-row span { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 15px; }

/* ---------- clone panel ---------- */

.clone-banner {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 14px 16px;
  border: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line));
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
}

.clone-banner p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.65;
}

.badge {
  flex: 0 0 auto;
  padding: 4px 10px;
  color: var(--accent-text);
  border-radius: 999px;
  background: var(--accent);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}

.step {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 26px 0 12px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  font-weight: 600;
}

.sample-list { display: grid; gap: 8px; }

.sample {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  transition: border-color .12s, background .12s;
}

.sample:hover { border-color: var(--line-strong); }

.sample.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.sample .check { min-height: 0; color: var(--text); font-size: 14px; }
.sample-head { margin-bottom: 9px; }
.sample-head small { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }
.sample audio { width: 100%; height: 32px; }

.ref-text { display: block; margin-top: 16px; }

#clonePanel .modes { margin: 14px 0 0; }
#chatterboxFields[hidden], #refTextField[hidden] { display: none; }
.engine-tab:disabled { opacity: .4; cursor: not-allowed; }
#engineTabs { margin-bottom: 10px; }

/* ---------- output ---------- */

.output {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.output-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 600;
}

.output-head div { display: flex; gap: 8px; }

.audio-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 13px 0;
  border-bottom: 1px solid var(--line);
}

.audio-item:last-child { border-bottom: 0; }
.audio-meta { min-width: 0; flex: 1; }
.audio-meta small {
  display: block;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.tag {
  display: inline-block;
  margin-bottom: 5px;
  padding: 2px 8px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.tag.accent {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 40%, transparent);
  background: var(--accent-soft);
}

.audio-actions { display: flex; align-items: center; gap: 10px; }
audio { width: min(400px, 100%); }
.download { color: var(--muted); font-size: 13px; text-decoration: none; }
.download:hover { color: var(--accent); text-decoration: underline; }

pre {
  max-height: 460px;
  margin: 0;
  padding: 15px;
  overflow: auto;
  white-space: pre-wrap;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  font: 13px/1.75 ui-monospace, SFMono-Regular, Consolas, monospace;
}

/* ---------- misc ---------- */

.hint {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.6;
}

.hint.warn { color: var(--bad); }

#cloneNote.warn {
  padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--bad) 45%, var(--line));
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--bad) 9%, var(--surface));
  font-weight: 700;
  line-height: 1.5;
}
a.hint { display: inline-block; color: var(--accent); }
a.hint[hidden] { display: none; }

.youtube-preview {
  display: flex;
  align-items: center;
  gap: 13px;
  margin-top: 12px;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
}

.youtube-preview[hidden] { display: none; }

.youtube-preview img {
  width: 128px;
  aspect-ratio: 16 / 9;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--bg-tint);
}

.youtube-preview span {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 10;
  max-width: calc(100% - 32px);
  padding: 11px 16px;
  color: #fff;
  border-radius: var(--radius-sm);
  background: #1c1c22;
  box-shadow: 0 12px 32px -12px rgba(0, 0, 0, .5);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: 160ms ease;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--bad); }

@media (max-width: 640px) {
  main { width: calc(100% - 20px); margin: 12px auto 48px; }
  .panel { padding: 16px; }
  .settings-row { grid-template-columns: 1fr; }
  .more-settings { grid-template-columns: 1fr 1fr; }
  .more-settings .wide { grid-column: 1 / -1; }
  .main-tab, .source-tab, .tts-mode, .clone-mode, .ref-tab { font-size: 13px; }
  .audio-item { align-items: stretch; flex-direction: column; }
  .audio-actions { flex-direction: column; align-items: stretch; }
  audio { width: 100%; }
  .clone-banner { flex-direction: column; gap: 10px; }
}
