/* ===== Tokens ===== */
:root {
  --set: #97c0dc;          /* current set colour (set by JS) */
  --on-set: #2b2522;       /* text colour on the set colour (set by JS) */
  --ink: #2b2522;
  --paper: #fffdf9;
  --radius: 22px;
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --font: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Hiragino Sans",
          "Noto Sans JP", system-ui, sans-serif;
}

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

html, body { margin: 0; height: 100%; }

body {
  font-family: var(--font);
  color: var(--ink);
  background-color: color-mix(in oklab, var(--set) 22%, var(--paper));
  transition: background-color .6s ease;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }

/* ===== Layout ===== */
.app {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  max-width: 520px;
  margin: 0 auto;
  padding:
    max(14px, env(safe-area-inset-top))
    max(18px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(18px, env(safe-area-inset-left));
  gap: 12px;
}

/* ===== Header ===== */
.title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: .02em;
  text-align: center;
}

.title-row {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 8px;
}

.swap {
  position: relative;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 4px 12px -5px rgb(0 0 0 / .3);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform .15s ease, background-color .2s ease;
}

.swap:active { transform: scale(.92); }
.swap:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }

.swap-glyph {
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1;
}

.swap-badge {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--set);
  box-shadow: 0 0 0 2px var(--paper);
  display: grid;
  place-items: center;
  transition: background-color .4s ease;
}

.swap-badge svg {
  width: 13px;
  height: 13px;
  fill: none;
  stroke: var(--on-set);
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (hover: hover) {
  .swap:hover { background: white; }
}

.meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.set-pill {
  background: var(--set);
  color: var(--on-set);
  padding: 4px 12px;
  border-radius: 999px;
  font-weight: 700;
  font-size: .9rem;
  transition: background-color .4s ease, color .4s ease;
}

.counter {
  font-size: .9rem;
  font-variant-numeric: tabular-nums;
  opacity: .7;
}

.progress {
  display: flex;
  gap: 3px;
  margin-top: 10px;
}

.seg {
  position: relative;
  flex: var(--count) 1 0;
  height: 12px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  background: color-mix(in oklab, var(--c) 48%, white);
}

/* Bigger tap target than the visible bar */
.seg::before {
  content: "";
  position: absolute;
  inset: -10px 0;
}

.seg .fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--fill, 0%);
  background: var(--c);
  transition: width .35s var(--ease-out);
}

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

/* ===== Card ===== */
.stage {
  display: grid;
  place-items: center;
  min-height: 0;
}

.card {
  /* Fit width on phones, height on short screens, cap on desktop. */
  width: min(100%, calc((100dvh - 300px) * .75), 400px);
  min-width: 200px;
  aspect-ratio: 3 / 4;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  perspective: 1200px;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  container-type: inline-size;
  will-change: transform;
}

.card:focus-visible { outline: none; }
.card:focus-visible .card-inner {
  outline: 3px solid var(--ink);
  outline-offset: 4px;
  border-radius: var(--radius);
}

.card-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform .55s var(--ease-out);
}

.card.is-flipped .card-inner { transform: rotateY(180deg); }
.card-inner.no-anim { transition: none; }

.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow:
    0 1px 2px rgb(0 0 0 / .08),
    0 12px 30px -8px rgb(0 0 0 / .25);
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Front: big kana on graph paper, white hill at the bottom */
.card-face--front {
  color: var(--on-set);
  background:
    linear-gradient(rgb(255 255 255 / .22) 1px, transparent 1px) 0 0 / 100% 6.25cqi,
    linear-gradient(90deg, rgb(255 255 255 / .22) 1px, transparent 1px) 0 0 / 6.25cqi 100%,
    var(--set);
  transition: background-color .4s ease;
}

.glyph {
  flex: 1;
  display: grid;
  place-items: center;
  font-size: 56cqi;
  font-weight: 500;
  line-height: 1;
  padding-bottom: 6cqi;
  text-shadow: 0 2px 0 rgb(0 0 0 / .06);
}

.hill {
  position: absolute;
  left: -8%;
  right: -8%;
  bottom: 0;
  height: 22%;
  background: var(--paper);
  border-radius: 50% 50% 0 0 / 70% 70% 0 0;
  display: grid;
  place-items: center;
  padding-top: 3cqi;
}

.hint {
  color: var(--ink);
  opacity: .45;
  font-size: max(.8rem, 4.2cqi);
  letter-spacing: .05em;
}

/* Back: romaji on a pale tint, set colour band on top */
.card-face--back {
  transform: rotateY(180deg);
  background:
    linear-gradient(color-mix(in oklab, var(--set) 12%, transparent) 1px, transparent 1px) 0 0 / 100% 6.25cqi,
    linear-gradient(90deg, color-mix(in oklab, var(--set) 12%, transparent) 1px, transparent 1px) 0 0 / 6.25cqi 100%,
    color-mix(in oklab, var(--set) 18%, var(--paper));
  color: var(--ink);
}

.band {
  width: 100%;
  padding: 4.5cqi 0;
  background: var(--set);
  color: var(--on-set);
  font-weight: 700;
  font-size: max(.9rem, 5.5cqi);
  text-align: center;
  letter-spacing: .04em;
}

.romaji {
  flex: 1;
  display: grid;
  place-items: center;
  font-size: 30cqi;
  font-weight: 700;
  line-height: 1;
  letter-spacing: .02em;
}

.glyph-small {
  font-size: 14cqi;
  margin-bottom: 9cqi;
  opacity: .55;
  line-height: 1;
}

/* ===== Controls ===== */
.controls {
  display: flex;
  justify-content: center;
  gap: 56px;
  padding: 4px 0;
}

.arrow {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 0;
  background: var(--paper);
  box-shadow: 0 6px 16px -6px rgb(0 0 0 / .3);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform .15s ease, opacity .2s ease, background-color .2s ease;
}

.arrow svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.arrow:active { transform: scale(.92); }
.arrow:disabled { opacity: .35; cursor: default; transform: none; }
.arrow:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }

@media (hover: hover) {
  .arrow:not(:disabled):hover { background: white; }
}

/* ===== Credit ===== */
.credit {
  text-align: center;
  font-size: .78rem;
  line-height: 1.5;
  opacity: .75;
  padding: 0 8px;
  text-wrap: balance;
}

.credit a {
  color: inherit;
  font-weight: 700;
  text-decoration-thickness: 2px;
  text-underline-offset: 2px;
  text-decoration-color: color-mix(in oklab, var(--set) 80%, var(--ink));
}

/* ===== Script picker dialog ===== */
.picker {
  width: min(92vw, 360px);
  padding: 18px;
  border: 0;
  border-radius: var(--radius);
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 24px 60px -18px rgb(0 0 0 / .45);

  /* Closed / exit state */
  opacity: 0;
  scale: .94;
  transition:
    opacity .22s ease,
    scale .22s var(--ease-out),
    display .22s,
    overlay .22s;
  transition-behavior: allow-discrete;
}

.picker::backdrop {
  background-color: rgb(30 24 20 / 0);
  transition:
    background-color .22s ease,
    display .22s,
    overlay .22s;
  transition-behavior: allow-discrete;
}

/* Open state; [data-closing] lets the JS fallback run the exit animation. */
.picker[open]:where(:not([data-closing])) {
  opacity: 1;
  scale: 1;

  @starting-style {
    opacity: 0;
    scale: .94;
  }

  &::backdrop {
    background-color: rgb(30 24 20 / .4);

    @starting-style {
      background-color: rgb(30 24 20 / 0);
    }
  }
}

.picker-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.picker-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.picker-close {
  width: 36px;
  height: 36px;
  margin: -6px -6px -6px 0;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  display: grid;
  place-items: center;
  cursor: pointer;
}

.picker-close svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: var(--ink);
  stroke-width: 2.4;
  stroke-linecap: round;
}

.picker-close:focus-visible { outline: 3px solid var(--ink); outline-offset: 1px; }

.picker-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.option {
  position: relative;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: color-mix(in oklab, var(--set) 16%, white);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  cursor: pointer;
  transition: transform .15s ease, background-color .2s ease;
}

.option:active { transform: scale(.96); }
.option:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

.option-glyph {
  font-size: 3.4rem;
  font-weight: 500;
  line-height: 1;
}

.option-label {
  font-weight: 700;
  font-size: .95rem;
}

.option[aria-pressed="true"] {
  background: color-mix(in oklab, var(--set) 38%, white);
  box-shadow: inset 0 0 0 3px var(--set);
}

.option[aria-pressed="true"]::after {
  content: "✓";
  position: absolute;
  top: 8px;
  right: 10px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--set);
  color: var(--on-set);
  font-size: .8rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ===== Reduced motion: fade instead of 3D flip ===== */
@media (prefers-reduced-motion: reduce) {
  body, .set-pill, .card-face--front, .seg .fill { transition: none; }
  .card-inner { transition: none; }
  .card.is-flipped .card-inner { transform: none; }
  .card-face { transition: opacity .2s ease; }
  .card-face--back { transform: none; opacity: 0; }
  .card.is-flipped .card-face--back { opacity: 1; }
  .card.is-flipped .card-face--front { opacity: 0; }
  .picker { scale: 1; transition-duration: .1s; }
  .picker[open]:where(:not([data-closing])) {
    @starting-style { scale: 1; }
  }
}
