:root {
  color-scheme: light dark;
  --page: #f7f7f4;
  --surface: #ffffff;
  --surface-soft: #eeeeea;
  --ink: #171815;
  --text-secondary: #65665f;
  --text-muted: #96978f;
  --line: rgba(23, 24, 21, 0.13);
  --line-strong: rgba(23, 24, 21, 0.2);
  --accent: #9b2f2c;
  --accent-ink: #ffffff;
  --accent-soft: rgba(155, 47, 44, 0.09);
  --shadow: rgba(28, 24, 18, 0.08);
  --nav-height: 64px;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --page: #0f1110;
  --surface: #171918;
  --surface-soft: #202321;
  --ink: #f2f1ec;
  --text-secondary: #adaca5;
  --text-muted: #73756f;
  --line: rgba(242, 241, 236, 0.13);
  --line-strong: rgba(242, 241, 236, 0.24);
  --accent: #d0625c;
  --accent-ink: #160f0e;
  --accent-soft: rgba(208, 98, 92, 0.14);
  --shadow: rgba(0, 0, 0, 0.26);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100dvh;
  background: var(--page);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

button:active {
  transform: translateY(1px);
}

input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}

input:focus {
  border-color: var(--line-strong);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

input[type="checkbox"] {
  width: 18px;
  min-height: 18px;
  accent-color: var(--accent);
  flex: 0 0 auto;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.site-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: var(--nav-height) 1fr;
}

.site-top {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  height: var(--nav-height);
  padding: 0 clamp(18px, 4vw, 34px);
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(16px);
}

.left-nav {
  display: flex;
  align-items: baseline;
  gap: clamp(18px, 3vw, 30px);
  min-width: 0;
}

.wordmark {
  color: var(--ink);
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  text-decoration: none;
}

.mode-nav,
.review-edge,
.review-actions,
.review-options,
.ipa-search-row,
.filter-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mode-nav button {
  min-height: 30px;
  border: 0;
  padding: 2px 0;
  color: var(--text-muted);
  font-size: 14px;
}

.mode-nav button + button {
  margin-left: 14px;
}

.mode-nav button.active {
  color: var(--ink);
  font-weight: 650;
}

.theme-toggle {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  padding: 0;
  color: var(--text-secondary);
}

.theme-toggle:hover,
.mode-nav button:hover {
  color: var(--ink);
}

.theme-icon {
  position: relative;
  width: 19px;
  height: 19px;
  border: 1.6px solid currentColor;
  border-radius: 50%;
}

html[data-theme="light"] .theme-icon::after {
  content: "";
  position: absolute;
  top: -2px;
  right: -2px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--page);
}

html[data-theme="dark"] .theme-icon::before,
html[data-theme="dark"] .theme-icon::after {
  content: "";
  position: absolute;
  inset: -5px;
  border-top: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  border-radius: 50%;
}

html[data-theme="dark"] .theme-icon::after {
  transform: rotate(90deg);
}

main {
  min-height: 0;
}

.screen {
  display: none;
  min-height: calc(100dvh - var(--nav-height));
  padding: clamp(14px, 3vw, 32px);
}

.screen.active {
  display: block;
}

.search-screen.active {
  display: grid;
  align-content: start;
}

.search-stage {
  width: min(680px, calc(100vw - 32px));
  margin: clamp(120px, 26dvh, 240px) auto 0;
  transition: margin 180ms ease, width 180ms ease;
}

.search-screen.has-search .search-stage {
  width: min(760px, calc(100vw - 32px));
  margin-top: 22px;
}

.search-control {
  position: relative;
}

.search-control input {
  min-height: 58px;
  padding: 0 74px 0 24px;
  border-color: var(--line);
  font-size: 18px;
  box-shadow: 0 16px 48px var(--shadow);
}

.search-control input::placeholder {
  color: var(--text-muted);
}

.ipa-popover {
  position: absolute;
  top: 50%;
  right: 10px;
  z-index: 5;
  transform: translateY(-50%);
}

.ipa-toggle {
  display: grid;
  place-items: center;
  min-width: 48px;
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  list-style: none;
  cursor: pointer;
}

.ipa-toggle::-webkit-details-marker {
  display: none;
}

.ipa-popover[open] .ipa-toggle {
  border-color: var(--accent);
  color: var(--accent);
}

.ipa-panel {
  position: absolute;
  top: 43px;
  right: 0;
  width: min(620px, calc(100vw - 32px));
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: 0 18px 52px var(--shadow);
}

.ipa-search-row {
  align-items: end;
}

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

.field span,
.result-head,
.review-options {
  color: var(--text-secondary);
  font-size: 12px;
}

.field input {
  min-height: 42px;
  padding: 0 13px;
  border-radius: 12px;
}

#clearIpaBtn,
#clearSearchBtn,
#clearSemanticBtn {
  min-height: 36px;
  padding: 0 13px;
  color: var(--text-secondary);
}

.ipa-keyboard {
  display: grid;
  gap: 9px;
  margin-top: 12px;
}

.ipa-group {
  display: grid;
  gap: 6px;
}

.ipa-group-title {
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
}

.ipa-keys,
.semantic-groups,
.active-semantics {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.ipa-key {
  min-width: 34px;
  min-height: 31px;
  padding: 0 8px;
  color: var(--text-secondary);
  font-size: 14px;
}

.result-head,
.word-list {
  display: none;
}

.search-screen.has-search .result-head {
  max-width: 1020px;
  margin: 24px auto 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.search-screen.has-search .word-list {
  max-width: 1020px;
  margin: 6px auto 0;
  display: grid;
}

.word-row {
  display: grid;
  grid-template-columns: minmax(150px, 1.05fr) minmax(150px, 0.8fr) minmax(180px, 1.25fr);
  gap: 14px;
  align-items: start;
  padding: 15px 0;
  border-top: 1px solid var(--line);
}

.word-row:hover .word-meaning {
  color: var(--accent);
}

.word-meaning {
  font-weight: 760;
}

.pronunciation,
.review-answer {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}

.pronunciation-line {
  display: flex;
  align-items: center;
  gap: 8px;
}

.audio-button {
  min-height: 30px;
  padding: 0 10px;
  border-color: color-mix(in srgb, var(--accent) 26%, var(--line));
  color: var(--accent);
  font-size: 12px;
  white-space: nowrap;
}

.audio-button.is-playing {
  background: var(--accent-soft);
}

.word-meta {
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.45;
}

.tag-line {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 7px;
}

.tag {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 7px;
  color: var(--text-secondary);
  font-size: 12px;
}

.tag.semantic {
  border-color: color-mix(in srgb, var(--accent) 38%, var(--line));
  color: var(--accent);
}

.tag.loan {
  border-color: color-mix(in srgb, #2f7d69 42%, var(--line));
  color: color-mix(in srgb, #2f7d69 88%, var(--ink));
}

.word-notes {
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.word-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.word-image-button {
  width: 52px;
  height: 52px;
  padding: 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--surface-soft);
}

.word-image-button img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.72);
}

.image-viewer-panel {
  position: relative;
  width: min(980px, 100%);
  height: min(760px, calc(100dvh - 36px));
  display: grid;
  place-items: center;
}

.image-viewer-figure {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 10px;
  margin: 0;
}

.image-viewer-figure img {
  max-width: 100%;
  max-height: 100%;
  min-width: 0;
  min-height: 0;
  place-self: center;
  object-fit: contain;
  border-radius: 8px;
  background: var(--surface);
}

.image-viewer-figure figcaption {
  color: #ffffff;
  text-align: center;
  font-size: 14px;
}

.image-viewer-close,
.image-viewer-nav {
  position: absolute;
  z-index: 1;
  display: grid;
  place-items: center;
  border-color: rgba(255, 255, 255, 0.34);
  background: rgba(0, 0, 0, 0.34);
  color: #ffffff;
}

.image-viewer-close {
  top: 0;
  right: 0;
  width: 42px;
  height: 42px;
  font-size: 26px;
}

.image-viewer-nav {
  top: 50%;
  width: 44px;
  height: 56px;
  border-radius: 999px;
  font-size: 34px;
  transform: translateY(-50%);
}

.image-viewer-nav.prev {
  left: 0;
}

.image-viewer-nav.next {
  right: 0;
}

.review-screen.active {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
}

.review-top,
.review-bottom {
  justify-content: space-between;
}

.segmented {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
}

.segmented button {
  min-height: 32px;
  border: 0;
  padding: 0 13px;
  color: var(--text-secondary);
}

.segmented button.active {
  background: var(--ink);
  color: var(--page);
  font-weight: 700;
}

#reviewFilterBtn,
.speak-button,
#againBtn,
#nextBtn {
  min-height: 38px;
  padding: 0 16px;
}

#reviewFilterBtn {
  color: var(--text-secondary);
}

#nextBtn.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 750;
}

.toggle-line {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.review-card {
  align-self: stretch;
  min-height: 360px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 18px;
  border: 1px solid transparent;
  border-radius: 18px;
  outline: none;
  cursor: pointer;
}

.review-card:focus-visible {
  border-color: var(--line-strong);
}

.review-prompt {
  max-width: min(860px, 92vw);
  text-align: center;
  font-size: clamp(38px, 9vw, 96px);
  line-height: 1.08;
  font-weight: 820;
  overflow-wrap: anywhere;
}

.review-answer {
  max-width: min(820px, 92vw);
  text-align: center;
  color: var(--text-secondary);
  font-size: clamp(28px, 6vw, 54px);
  line-height: 1.18;
  overflow-wrap: anywhere;
}

.audio-status {
  min-height: 18px;
  color: var(--text-muted);
  font-size: 13px;
}

.review-filter-panel {
  position: fixed;
  top: calc(var(--nav-height) + 14px);
  right: clamp(14px, 4vw, 28px);
  z-index: 30;
  width: min(520px, calc(100vw - 28px));
  max-height: min(620px, calc(100dvh - 112px));
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 18px 45px var(--shadow);
}

.semantic-groups,
.active-semantics {
  margin-top: 10px;
}

.semantic-chip,
.active-semantic {
  min-height: 30px;
  border-radius: 999px;
  padding: 5px 10px;
  color: var(--text-secondary);
}

.semantic-chip.active,
.active-semantic {
  border-color: var(--accent);
  color: var(--accent);
}

.hidden {
  display: none !important;
}

@media (max-width: 860px) {
  .word-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .word-extra {
    grid-column: auto;
  }
}

@media (max-width: 560px) {
  :root {
    --nav-height: 58px;
  }

  .site-top {
    padding-inline: 14px;
    gap: 10px;
  }

  .left-nav {
    gap: 16px;
  }

  .wordmark {
    font-size: 18px;
  }

  .mode-nav button {
    font-size: 13px;
  }

  .mode-nav button + button {
    margin-left: 10px;
  }

  .theme-toggle {
    width: 32px;
    height: 32px;
  }

  .search-stage {
    width: calc(100vw - 28px);
    margin-top: clamp(108px, 25dvh, 180px);
  }

  .search-screen.has-search .search-stage {
    width: calc(100vw - 28px);
    margin-top: 12px;
  }

  .search-control input {
    min-height: 54px;
    padding-left: 18px;
    padding-right: 68px;
    font-size: 17px;
  }

  .ipa-toggle {
    right: 8px;
    min-width: 44px;
    min-height: 31px;
  }

  .ipa-popover {
    right: 8px;
  }

  .ipa-panel {
    right: -8px;
    width: calc(100vw - 28px);
    border-radius: 17px;
    padding: 10px;
  }

  .ipa-search-row,
  .filter-head,
  .review-bottom {
    align-items: stretch;
    flex-direction: column;
  }

  .ipa-key {
    min-width: 31px;
    min-height: 30px;
    padding-inline: 7px;
    font-size: 13px;
  }

  .word-row {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .review-edge {
    width: 100%;
  }

  .review-top .segmented {
    flex: 1;
  }

  .review-top .segmented button,
  .review-actions button {
    flex: 1;
  }

  .review-actions {
    width: 100%;
  }

  .review-card {
    min-height: clamp(260px, 43dvh, 320px);
    gap: 14px;
  }

  .review-prompt {
    font-size: clamp(34px, 14vw, 68px);
  }

  .review-answer {
    font-size: clamp(24px, 9vw, 42px);
  }

  .review-filter-panel {
    top: 68px;
    right: 14px;
    left: 14px;
    width: auto;
  }
}
