:root {
  --ink: #221b16;
  --paper: #f5efe2;
  --paper-deep: #eadcc1;
  --panel: #fff9ec;
  --line: #c7b894;
  --muted: #766b58;
  --blue: #1f5f7a;
  --blue-deep: #143d4e;
  --red: #a13a32;
  --green: #2f6f4f;
  --gold: #b7842d;
  --shadow: rgba(44, 34, 23, 0.2);
  --gutter: 16px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(31, 95, 122, 0.08) 1px, transparent 1px),
    linear-gradient(rgba(31, 95, 122, 0.06) 1px, transparent 1px),
    radial-gradient(circle at 20% 10%, rgba(183, 132, 45, 0.16), transparent 32%),
    radial-gradient(circle at 85% 20%, rgba(161, 58, 50, 0.12), transparent 28%),
    var(--paper);
  background-size: 44px 44px, 44px 44px, auto, auto, auto;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  width: min(100%, 720px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(18px + env(safe-area-inset-top, 0px)) var(--gutter) calc(18px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 14px;
  transition: background-color 140ms ease;
}

.app.swipe-right {
  background: rgba(47, 111, 79, 0.18);
}

.app.swipe-left {
  background: rgba(161, 58, 50, 0.18);
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.brand-block {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--blue);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 1.6rem;
  line-height: 1;
  font-weight: 800;
}

.header-selects {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

select,
.filter-button,
.search-wrap input {
  border: 1px solid var(--line);
  background: rgba(255, 249, 236, 0.86);
  color: var(--ink);
  border-radius: 8px;
}

select {
  padding: 5px 22px 5px 8px;
  font-size: 0.85rem;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--blue) 50%), linear-gradient(135deg, var(--blue) 50%, transparent 50%);
  background-position: calc(100% - 13px) 55%, calc(100% - 8px) 55%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

.filter-menu {
  position: relative;
  width: 180px;
  flex: 0 0 180px;
}

.filter-button {
  width: 100%;
  padding: 5px 22px 5px 8px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
  background-image: linear-gradient(45deg, transparent 50%, var(--blue) 50%), linear-gradient(135deg, var(--blue) 50%, transparent 50%);
  background-position: calc(100% - 13px) 55%, calc(100% - 8px) 55%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

.filter-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 180px;
  width: max-content;
  max-width: min(340px, calc(100vw - 28px));
  max-height: min(520px, 72dvh);
  overflow: auto;
  z-index: 30;
  padding: 6px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 40px var(--shadow);
}

.hidden {
  display: none !important;
}

.filter-group + .filter-group {
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid rgba(199, 184, 148, 0.75);
}

.filter-group-title {
  margin: 0;
  padding: 7px 14px 5px;
  color: var(--blue-deep);
  font-size: 0.78rem;
  font-weight: 800;
}

.filter-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px;
  color: var(--ink);
  font-size: 0.85rem;
  white-space: nowrap;
  user-select: none;
}

.filter-option input {
  width: 15px;
  height: 15px;
  accent-color: var(--blue);
}

.icon-btn {
  border: 1px solid var(--line);
  background: rgba(255, 249, 236, 0.72);
  color: var(--ink);
  border-radius: 8px;
  padding: 8px 10px;
}

.search-toggle {
  display: none;
  width: 30px;
  height: 30px;
  padding: 0;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 30px;
}

.search-toggle svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.search-toggle[aria-expanded="true"] {
  background: rgba(31, 95, 122, 0.14);
  border-color: var(--blue);
  color: var(--blue-deep);
}

.search-wrap {
  position: relative;
}

.search-wrap input {
  width: 100%;
  padding: 11px 12px;
  font-size: 1rem;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

.search-wrap input:focus,
select:focus,
button:focus-visible {
  outline: 3px solid rgba(31, 95, 122, 0.28);
  outline-offset: 2px;
}

.search-results {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  z-index: 18;
  margin: 0;
  padding: 8px;
  list-style: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 40px var(--shadow);
}

.search-results li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 6px;
}

.search-results li:hover,
.search-results li.active {
  background: rgba(31, 95, 122, 0.1);
}

.search-results small {
  color: var(--muted);
}

.stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
}

.back-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(255, 249, 236, 0.72);
  color: var(--muted);
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 120ms ease, color 120ms ease;
  flex-shrink: 0;
}

.back-btn:hover:not(:disabled) {
  background: rgba(31, 95, 122, 0.1);
  color: var(--ink);
}

.back-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.score-group {
  display: flex;
  gap: 6px;
}

.score-tap {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  padding: 4px 10px;
  border-radius: 8px;
  border: 0;
  background: rgba(255, 249, 236, 0.72);
  color: var(--blue-deep);
  font-weight: 800;
  transition: background 120ms ease;
}

.score-tap:hover {
  background: rgba(31, 95, 122, 0.12);
}

.card-wrap {
  flex: 1;
  display: flex;
  align-items: stretch;
  min-height: min(60dvh, 620px);
  perspective: 1100px;
}

.card {
  width: 100%;
  min-height: min(60dvh, 620px);
  position: relative;
  transform-style: preserve-3d;
  transition: transform 220ms ease;
  touch-action: pan-y pinch-zoom;
  user-select: none;
  will-change: transform;
}

.card.flipped {
  transform: rotateY(180deg);
}

.face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: clamp(16px, 4vw, 28px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.36), transparent 38%),
    var(--panel);
  box-shadow: 0 18px 48px var(--shadow);
  backface-visibility: hidden;
  overflow: hidden;
}

.back {
  transform: rotateY(180deg);
  gap: 10px;
  overflow: hidden;
}

.back-scroll {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.back-series-shell {
  flex: 0 0 auto;
}

.card.correct-flash .face {
  border-color: rgba(47, 111, 79, 0.75);
  box-shadow: 0 18px 48px rgba(47, 111, 79, 0.28);
}

.card.wrong-flash .face {
  border-color: rgba(161, 58, 50, 0.75);
  box-shadow: 0 18px 48px rgba(161, 58, 50, 0.26);
}

.card-image {
  flex: 1;
  min-height: 0;
  margin: 0;
  display: grid;
  place-items: center;
  overflow: visible;
}

.card-image img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 45dvh;
  object-fit: contain;
  display: block;
  border: 1px solid rgba(199, 184, 148, 0.72);
  border-radius: 8px;
  background: rgba(234, 220, 193, 0.48);
}

.card-title {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.6rem, 6vw, 3rem);
  line-height: 1;
  font-weight: 850;
  text-align: center;
}

.stat-grid {
  margin: 0;
  display: grid;
  gap: 0;
  padding: 8px 12px;
  border: 1px solid rgba(199, 184, 148, 0.72);
  border-radius: 8px;
  background: rgba(245, 239, 226, 0.62);
}

.stat-item {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(76px, 0.24fr) 1fr;
  gap: 10px;
  align-items: baseline;
  padding: 5px 0;
  border-bottom: 1px solid rgba(199, 184, 148, 0.62);
}

.stat-item:last-child {
  border-bottom: 0;
}

.stat-item dt {
  margin: 0;
  color: var(--blue);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.7rem;
  text-transform: uppercase;
}

.stat-item dt::after {
  content: ":";
}

.stat-item dd {
  margin: 0;
  overflow-wrap: anywhere;
  font-weight: 800;
  line-height: 1.18;
}

.field-list {
  display: grid;
  gap: 9px;
}

.field-row {
  display: grid;
  grid-template-columns: minmax(88px, 0.36fr) 1fr;
  gap: 12px;
  padding-bottom: 9px;
  border-bottom: 1px solid rgba(199, 184, 148, 0.72);
}

.field-row span {
  color: var(--blue);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  text-transform: uppercase;
}

.field-row strong {
  overflow-wrap: anywhere;
}

.gloss {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1rem, 3vw, 1.24rem);
  line-height: 1.35;
}

.series-nav {
  padding-top: 10px;
  border-top: 1px solid rgba(199, 184, 148, 0.72);
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
}

.series-placeholder {
  min-width: 0;
}

.series-btn {
  min-width: 0;
  border: 1px solid rgba(199, 184, 148, 0.82);
  border-radius: 8px;
  background: rgba(245, 239, 226, 0.74);
  color: var(--ink);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}

.series-btn.next {
  align-items: flex-end;
  text-align: right;
}

.series-btn:hover {
  background: rgba(31, 95, 122, 0.1);
  border-color: rgba(31, 95, 122, 0.42);
}

.series-btn:active {
  transform: scale(0.98);
}

.series-dir {
  color: var(--blue);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.66rem;
  font-weight: 800;
  text-transform: uppercase;
}

.series-card-title {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.92rem;
  font-weight: 850;
  line-height: 1.14;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 12px;
  padding: 18px;
}

.empty-state h2 {
  margin: 0;
  font-size: clamp(2rem, 8vw, 3.4rem);
  line-height: 0.95;
}

.empty-sub {
  margin: 0;
  max-width: 360px;
  color: var(--muted);
}

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

.action-btn {
  min-height: 46px;
  border: 0;
  border-radius: 8px;
  padding: 12px 10px;
  color: #fff;
  font-weight: 850;
  transition: transform 120ms ease, filter 120ms ease;
}

.action-btn:active {
  transform: scale(0.98);
}

.action-btn:disabled {
  opacity: 0.44;
  cursor: default;
}

.action-btn.wrong {
  background: var(--red);
}

.action-btn.right {
  background: var(--green);
}

.action-btn.neutral {
  background: var(--blue);
}

.hint {
  margin: -2px 0 0;
  color: var(--muted);
  text-align: center;
  font-size: 0.9rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  z-index: 40;
  transform: translateX(-50%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--ink);
  color: var(--paper);
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.toast.show {
  opacity: 1;
}

.score-modal {
  width: min(420px, calc(100% - 28px));
  max-height: 78dvh;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 20px 60px var(--shadow);
}

.score-modal::backdrop {
  background: rgba(34, 27, 22, 0.55);
}

.score-modal-inner {
  display: flex;
  flex-direction: column;
  max-height: 78dvh;
  margin: 0;
}

.score-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.score-modal-header h2 {
  margin: 0;
}

.score-modal-header .icon-btn,
.remove-btn {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 249, 236, 0.72);
  color: var(--ink);
  padding: 7px 9px;
}

.modal-close-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  line-height: 1;
  font-weight: 800;
}

.remove-btn {
  width: 30px;
  height: 30px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 30px;
  font-size: 1.2rem;
  line-height: 1;
  font-weight: 800;
}

.score-modal-list {
  flex: 1;
  overflow: auto;
  margin: 0;
  padding: 8px 16px;
  list-style: none;
}

.score-modal-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(199, 184, 148, 0.7);
}

.score-modal-list .empty-list {
  display: block;
  color: var(--muted);
  text-align: center;
  border-bottom: 0;
}

.score-modal-clear {
  margin: 8px 16px 14px;
}

@media (max-width: 768px) {
  :root {
    --gutter: 12px;
  }

  .topbar {
    align-items: flex-start;
  }

  .brand-block {
    min-width: 0;
    width: 100%;
  }

  .header-selects {
    align-items: center;
    width: 100%;
  }

  .filter-menu {
    flex: 1 1 150px;
    min-width: 0;
    width: auto;
  }

  .search-toggle {
    display: inline-flex;
  }

  .search-wrap {
    display: none;
  }

  .app.search-open .search-wrap {
    display: block;
  }

  .card-wrap,
  .card {
    min-height: 48dvh;
  }

  .card-image img {
    max-height: 34dvh;
  }

  .actions {
    display: none;
  }

  .field-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  .series-nav {
    gap: 8px;
  }

  .series-btn {
    padding: 7px 8px;
  }

  .series-card-title {
    font-size: 0.84rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app,
  .card,
  .face,
  .action-btn,
  .toast {
    transition: none !important;
  }
}
