:root {
  --bg: #f4efe4;
  --surface: #fbf8f1;
  --ink: #1c1a15;
  --muted: #7a7266;
  --line: #e2dacb;
  --accent: #d9481f;
  --accent-ink: #fff8f2;
  --star: #e3a008;
  --danger: #b42318;
  --radius: 14px;
  --display: "Fraunces", Georgia, serif;
  --ui: "Inter", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --surface: #211e18;
    --ink: #f2ebdc;
    --muted: #9c937f;
    --line: #353026;
    --accent: #f06a3c;
    --accent-ink: #1b0d06;
    --star: #f5b82e;
    --danger: #f97066;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--ui);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Header */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}

.actions {
  display: flex;
}

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
}

.icon-button:active {
  background: var(--line);
}

.modes {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--line);
}

.modes button {
  border: 0;
  border-radius: 999px;
  padding: 8px 16px;
  background: transparent;
  font-weight: 500;
  font-size: 15px;
  color: var(--muted);
}

.modes button[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
}

.modes.small button {
  padding: 6px 14px;
  font-size: 14px;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

main > section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

main > section[hidden] {
  display: none;
}

/* Batch mode */

.batch-list {
  list-style: none;
  margin: 0;
  padding: 4px 16px 120px;
}

.word-card {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 60px;
  border-bottom: 1px solid var(--line);
  animation: rise 0.35s both;
  animation-delay: calc(var(--i, 0) * 30ms);
}

.word-card .word-text {
  flex: 1;
  padding: 12px 0;
  font-family: var(--display);
  font-size: clamp(26px, 7.5vw, 34px);
  font-weight: 500;
  line-height: 1.15;
  overflow-wrap: anywhere;
  hyphens: auto;
}

.word-card.speaking .word-text {
  color: var(--accent);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

.star {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--line);
}

.star svg {
  fill: currentColor;
  stroke: none;
}

.star[aria-pressed="true"] {
  color: var(--star);
}

.star:active {
  transform: scale(0.88);
}

.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px);
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}

.primary {
  flex: 1;
  max-width: 528px;
  height: 56px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 18px;
  font-weight: 600;
}

.primary:active,
.secondary:active {
  transform: scale(0.98);
}

.secondary {
  width: 56px;
  height: 56px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  display: grid;
  place-items: center;
}

.secondary svg path {
  fill: currentColor;
}

.empty {
  margin: 48px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 18px;
}

/* Read-aloud mode */

.stage {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 42dvh;
  padding: 24px 16px;
}

.read-word {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.read-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.read-word .word-text {
  font-family: var(--display);
  font-size: clamp(40px, 13vw, 72px);
  font-weight: 700;
  line-height: 1.05;
  text-align: center;
  overflow-wrap: anywhere;
  hyphens: auto;
  animation: rise 0.3s both;
}

.read-word .star {
  width: 56px;
  height: 56px;
}

.read-word .star svg {
  width: 30px;
  height: 30px;
}

.read-controls .primary {
  flex: 0 0 auto;
  width: 72px;
  height: 72px;
  max-width: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
}

.read-controls {
  align-items: center;
}

.read-controls .primary svg {
  width: 32px;
  height: 32px;
  fill: currentColor;
  stroke: none;
}

.read-controls .icon-stop,
.playing .read-controls .icon-play {
  display: none;
}

.playing .read-controls .icon-stop {
  display: block;
}

.recent {
  list-style: none;
  margin: 0;
  padding: 0 16px 120px;
}

.recent .word-row .word-text {
  font-size: 20px;
  color: var(--muted);
}

/* Word rows in search / lists / recent */

.word-list {
  list-style: none;
  margin: 0;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 24px);
}

.word-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  border-bottom: 1px solid var(--line);
}

.word-row .word-text {
  flex: 1;
  padding: 10px 0;
  font-family: var(--display);
  font-size: 22px;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.word-row .meta {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.segmented.thirds {
  grid-template-columns: 1fr 1fr 1fr;
}

.search-modes {
  margin: 0 16px 10px;
}

.more {
  display: block;
  margin: 8px auto calc(env(safe-area-inset-bottom) + 32px);
  padding: 12px 22px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 500;
}

.more[hidden] {
  display: none;
}

.count {
  margin: 0 16px 4px;
  color: var(--muted);
  font-size: 13px;
  min-height: 1em;
}

/* Sheets */

dialog.sheet {
  width: 100%;
  max-width: 560px;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 auto;
  padding: 0;
  border: 0;
  background: var(--bg);
  color: var(--ink);
  overflow-y: auto;
  overscroll-behavior: contain;
}

dialog.sheet[open] {
  animation: sheet-in 0.22s ease-out;
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.35);
}

@keyframes sheet-in {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
}

.sheet-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 8px 10px 16px;
  background: var(--bg);
}

.sheet-head h2 {
  margin: 0;
  font-family: var(--display);
  font-size: 26px;
  font-weight: 700;
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  font-size: 17px;
  padding: 10px 12px;
}

#search-input {
  flex: 1;
  min-width: 0;
  height: 46px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 17px;
}

#search-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* Settings form */

fieldset {
  margin: 0 16px 20px;
  padding: 4px 16px 12px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
}

legend {
  float: left;
  width: 100%;
  padding: 14px 0 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

fieldset > * {
  clear: both;
}

label {
  font-size: 16px;
}

input[type="number"],
select {
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 16px;
}

input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--accent);
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  margin-bottom: 14px;
  border-radius: 12px;
  background: var(--line);
}

.segmented label {
  position: relative;
}

.segmented input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
}

.segmented span {
  display: block;
  padding: 9px 6px;
  border-radius: 9px;
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: var(--muted);
}

.segmented input:checked + span {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--accent);
}

.range-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.range-row label,
.stacked {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.range-row input,
.stacked select {
  width: 100%;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 16px;
}

.chips button {
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  font-size: 14px;
}

.chips button[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

.slider {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 6px 0 12px;
}

.slider label {
  display: flex;
  justify-content: space-between;
}

.slider output {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.slider input {
  width: 100%;
  accent-color: var(--accent);
}

.slider-ends {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

.slider.disabled {
  opacity: 0.4;
}

.switch,
.inline-number {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  border-top: 1px solid var(--line);
}

.switch input {
  width: 22px;
  height: 22px;
}

.inline-number input {
  width: 92px;
  text-align: right;
}

.categories {
  display: flex;
  flex-direction: column;
}

.category {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 12px;
  align-items: start;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}

.category:first-child {
  border-top: 0;
}

.category input {
  width: 22px;
  height: 22px;
  margin: 1px 0 0;
}

.category .name {
  font-weight: 500;
}

.category .examples {
  grid-column: 2;
  margin-top: 2px;
  font-family: var(--display);
  font-size: 15px;
  color: var(--muted);
  font-style: italic;
}

.history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
}

#seen-count {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.danger {
  padding: 9px 14px;
  border: 1px solid var(--danger);
  border-radius: 999px;
  background: transparent;
  color: var(--danger);
  font-size: 14px;
  font-weight: 500;
}

.danger.armed {
  background: var(--danger);
  color: var(--surface);
}

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