:root {
  color-scheme: dark;
  --bg: #0e0f11;
  --surface: #16171a;
  --surface-2: #1e1f23;
  --surface-3: #27282d;
  --border: #2c2d33;
  --text: #ededef;
  --muted: #9b9ca3;
  --primary: #f2f2f3;
  --primary-text: #0e0f11;
  --record: #e5484d;
  --record-hover: #ec5d61;
  --ok: #46c98b;
  --warn: #e0b453;
  --danger: #ff7a7d;
  --focus: #8fb4ff;
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 48px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

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

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: 16px;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.sprite {
  position: absolute;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.5rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 1.125rem;
  line-height: 1.3;
}

.muted {
  color: var(--muted);
}

a {
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Layout -------------------------------------------------------------------- */

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 64px max(16px, env(safe-area-inset-left));
  display: grid;
  gap: 16px;
}

.page-narrow {
  max-width: 420px;
  padding-top: 15vh;
}

.page-header {
  display: grid;
  gap: 6px;
}

.page-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.stack {
  display: grid;
  gap: 16px;
}

.row {
  display: flex;
  gap: 10px;
}

.row > .btn {
  flex: 1;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* Progress ------------------------------------------------------------------ */

.steps {
  position: sticky;
  top: 0;
  z-index: 5;
  margin: 0 -16px;
  padding: 8px 16px;
  background: var(--bg);
}

.steps ol {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.steps a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: var(--tap);
  padding: 6px 2px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.2;
  text-align: center;
}

.steps-num {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 0.8125rem;
}

.steps li.is-current a {
  color: var(--text);
}

.steps li.is-current .steps-num {
  background: var(--primary);
  color: var(--primary-text);
}

.steps li.is-done .steps-num {
  background: var(--ok);
  color: var(--primary-text);
}

.steps li.is-done a {
  color: var(--text);
}

/* Cards --------------------------------------------------------------------- */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 16px;
  display: grid;
  gap: 14px;
}

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.card-head-split {
  justify-content: space-between;
}

.card-num {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--surface-3);
  font-weight: 700;
  font-size: 0.875rem;
  flex: none;
}

.tips {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 0 14px;
}

.tips summary {
  min-height: var(--tap);
  display: flex;
  align-items: center;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9375rem;
}

.tips[open] {
  padding-bottom: 14px;
}

.checklist {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9375rem;
}

.checklist li {
  padding-left: 22px;
  position: relative;
}

.checklist li::before {
  content: "";
  position: absolute;
  left: 4px;
  top: 0.55em;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ok);
}

/* Language picker ----------------------------------------------------------- */

.lang-pick {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.lang-btn {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 2px;
  min-height: 72px;
  padding: 8px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.lang-btn.is-active {
  background: var(--primary);
  color: var(--primary-text);
}

.lang-name {
  font-size: 1.125rem;
  font-weight: 700;
}

.lang-done {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--ok);
}

.lang-btn.is-active .lang-done {
  color: #1d7a4f;
}

/* Reading text ---------------------------------------------------------------- */

.read-box {
  display: grid;
  gap: 8px;
  padding: 16px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}

.script {
  font-size: 1.1875rem;
  line-height: 1.7;
  color: var(--text);
}

.script[dir="rtl"],
.consent[dir="rtl"] {
  font-size: 1.3125rem;
  line-height: 1.9;
}

.consent {
  margin: 0;
  padding: 16px;
  background: var(--surface-2);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius-sm);
  font-size: 1.125rem;
  line-height: 1.5;
  font-weight: 500;
}

.consent[dir="rtl"] {
  border-left: 0;
  border-right: 3px solid var(--primary);
}

/* Form ---------------------------------------------------------------------- */

.field {
  display: grid;
  gap: 6px;
}

.field > span {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--muted);
}

input[type="text"],
input[type="password"],
select,
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 12px 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 16px; /* 16px stops iOS zooming into the field */
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%239b9ca3' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-position: calc(100% - 16px) 50%;
  background-size: 12px 8px;
  background-repeat: no-repeat;
  padding-right: 40px;
}

textarea {
  resize: vertical;
  min-height: 110px;
}

#tts-voice {
  font-family: var(--mono);
  font-size: 15px;
}

.field-static {
  min-height: var(--tap);
  display: flex;
  align-items: center;
  padding: 0 14px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  color: var(--text);
}

.hint {
  color: var(--muted);
  font-size: 0.875rem;
}

/* Buttons ------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 18px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 0.9375rem;
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background-color 0.15s ease, opacity 0.15s ease;
}

.btn:hover {
  background: #313238;
}

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

.btn-block {
  width: 100%;
}

.btn-large {
  min-height: 56px;
  font-size: 1.0625rem;
}

.btn-small {
  min-height: 40px;
  padding: 0 14px;
  font-size: 0.875rem;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
}

.btn-primary:hover {
  background: #ffffff;
}

.btn-ghost {
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--border);
}

.btn-ghost:hover {
  background: var(--surface-2);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  box-shadow: inset 0 0 0 1px #4a2a2c;
}

.btn-danger:hover {
  background: #2a1b1c;
}

.btn-record {
  min-height: 72px;
  font-size: 1.125rem;
  background: var(--record);
  color: #fff;
}

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

.btn-stop {
  min-height: 64px;
  font-size: 1.0625rem;
  background: var(--primary);
  color: var(--primary-text);
}

.btn-stop:hover {
  background: #fff;
}

.btn.is-busy {
  position: relative;
  color: transparent;
}

.btn.is-busy::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid var(--primary-text);
  border-right-color: transparent;
  animation: spin 0.8s linear infinite;
}

.icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.btn-record .icon,
.btn-stop .icon {
  width: 24px;
  height: 24px;
}

[data-action="play"] .icon,
#player-toggle .icon {
  fill: currentColor;
  stroke-width: 1.5;
}

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

/* Recorder ------------------------------------------------------------------ */

.recorder {
  display: grid;
  gap: 12px;
}

.recorder [data-view] {
  display: grid;
  gap: 12px;
}

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}

.seg-btn {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  font-size: 0.9375rem;
  cursor: pointer;
}

.seg-btn.is-active {
  background: var(--surface-3);
  color: var(--text);
}

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

.rec-status {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
}

.rec-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--record);
  animation: pulse 1.2s ease-in-out infinite;
}

.rec-timer {
  margin-left: auto;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.25rem;
}

@keyframes pulse {
  50% {
    opacity: 0.3;
  }
}

.meter {
  height: 10px;
  border-radius: 5px;
  background: var(--surface-3);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  background: var(--ok);
  border-radius: 5px;
  transition: width 0.06s linear;
}

.meter-fill.is-hot {
  background: var(--warn);
}

.file-pick {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 24px 16px;
  min-height: 120px;
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  cursor: pointer;
  font-weight: 600;
}

.file-pick small {
  color: var(--muted);
  font-weight: 400;
}

.file-pick input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
}

.file-pick:focus-within {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Status and alerts --------------------------------------------------------- */

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9375rem;
}

.status-ok {
  color: var(--ok);
  font-weight: 600;
}

.alert {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  display: grid;
  gap: 4px;
}

.alert-error {
  background: #2a1718;
  color: #ffb3b4;
}

.alert-warn {
  background: #2a2314;
  color: #f1d38f;
}

.alert-detail {
  opacity: 0.8;
  font-size: 0.8125rem;
  word-break: break-word;
}

/* Result -------------------------------------------------------------------- */

.result {
  display: grid;
  gap: 12px;
}

.result-title {
  font-size: 1.25rem;
}

.result-name {
  color: var(--muted);
}

.label {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.voice-id {
  display: block;
  padding: 14px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 1rem;
  word-break: break-all;
  user-select: all;
  -webkit-user-select: all;
}

.voice-id-small {
  padding: 8px 10px;
  font-size: 0.8125rem;
}

/* Player -------------------------------------------------------------------- */

.player {
  display: grid;
  gap: 10px;
  padding-top: 4px;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 14px;
}

.player-bar .btn {
  min-width: 120px;
}

.player-time {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

/* Voice list ---------------------------------------------------------------- */

.voice-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.voice {
  display: grid;
  gap: 8px;
  padding: 14px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}

.voice-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.voice-meta {
  color: var(--muted);
  font-size: 0.8125rem;
}

.voice-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.voice-actions .btn {
  min-height: var(--tap);
  padding: 0 8px;
}

.voice .voice-id {
  background: var(--surface);
}

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
}

/* Toast --------------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  padding: 12px 18px;
  background: var(--primary);
  color: var(--primary-text);
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9375rem;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.4);
  max-width: calc(100% - 32px);
  text-align: center;
}

/* Wider screens ------------------------------------------------------------- */

@media (min-width: 720px) {
  .page {
    padding-top: 40px;
    gap: 20px;
  }

  .card {
    padding: 24px;
  }

  .steps {
    margin: 0;
    padding: 8px 0;
  }

  .steps a {
    flex-direction: row;
    justify-content: center;
    font-size: 0.875rem;
    gap: 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
