:root {
  --ground: #eceef4;
  --paper: #f8f9fc;
  --ink: #1f1b3a;
  --muted: #625f7a;
  --line: #d3d6e3;
  --accent: #d92a6a;
  --accent-soft: #fbe3ec;
  --on-accent: #ffffff;
  --sleeve: rgba(255, 255, 255, 0.42);
  --display: "M PLUS Rounded 1c", "Hiragino Sans", "Yu Gothic", sans-serif;
  --body: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans JP", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ground: #15131f;
    --paper: #1f1c2d;
    --ink: #eeebf7;
    --muted: #a19db8;
    --line: #37334d;
    --accent: #ff5d95;
    --accent-soft: #3a1f2e;
    --on-accent: #1a0710;
    --sleeve: rgba(255, 255, 255, 0.16);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font: 15px/1.7 var(--body);
  padding: env(safe-area-inset-top, 0px) 16px 0;
}
#app { max-width: 560px; margin: 0 auto; }
img { max-width: 100%; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.screen { padding-block: 20px 32px; }
.display { font: 800 clamp(40px, 12vw, 60px)/1.2 var(--display); letter-spacing: 0.02em; margin: 4px 0 20px; }
.kicker { margin: 0; color: var(--muted); font-size: 14px; }
.lead { margin: -8px 0 20px; font-size: 15px; line-height: 1.7; }
.lead b { font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.title { font: 800 22px/1.3 var(--display); margin: 6px 0 2px; }
.subtitle { font-size: 15px; font-weight: 700; margin: 24px 0 8px; }
.hint, .note { color: var(--muted); font-size: 13px; margin: 0; }
.note { margin-top: 10px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 18px; border-radius: 999px; border: 1.5px solid transparent;
  font-weight: 700; font-size: 15px; cursor: pointer; text-decoration: none; white-space: nowrap;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.btn--primary { background: var(--accent); color: var(--on-accent); }
.btn--primary:disabled { opacity: 0.4; cursor: default; }
.btn--ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn--quiet { background: var(--paper); color: var(--ink); border-color: var(--line); }
.btn--x { background: #000; color: #fff; border-color: #000; }
@media (prefers-color-scheme: dark) { .btn--x { border-color: rgba(255, 255, 255, 0.28); } }
.btn--wide { width: 100%; min-height: 52px; font-size: 16px; }
.btn:active:not(:disabled) { transform: scale(0.97); }

/* The pocket sheet: the signature element, used for hero, dock and arrangement */
.sheet {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 10px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 18px;
}
.pocket {
  position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden;
  background: var(--ground); border: 1px solid var(--line); padding: 0;
}
.pocket::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; border-radius: inherit;
  background: linear-gradient(128deg, var(--sleeve) 0%, transparent 34%, transparent 70%, rgba(255,255,255,0.1) 100%);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}
.pocket--empty { border-style: dashed; }
.pocket--empty::before {
  content: attr(data-n); position: absolute; inset: 0; display: grid; place-items: center;
  font: 800 22px var(--display); color: var(--line);
}
.sheet--hero { margin-bottom: 24px; transform: rotate(-1.5deg); }
.sheet--main { gap: 6px; padding: 8px; }
.sheet--main .pocket { cursor: pointer; }
.rank {
  position: absolute; left: 6px; top: 6px; z-index: 2; display: inline-flex; align-items: center; justify-content: center;
  gap: 2px; min-width: 24px; height: 24px; padding: 0 5px; border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.9); background: var(--accent); color: #fff;
  font: 800 13px/1 var(--display);
}
.rank--podium { height: 26px; padding: 0 8px 0 6px; border-radius: 13px; font-size: 14px; }
.rank--podium svg { width: 13px; height: 13px; fill: currentColor; }
.rank--1 { color: #583807; background: linear-gradient(135deg, #fff2b5, #e7bb4c 58%, #f8df8c); }
.rank--2 { color: #3b4352; background: linear-gradient(135deg, #f8f9fc, #bec7d4 58%, #e7ebf1); }
.rank--3 { color: #603718; background: linear-gradient(135deg, #ffe4cb, #d89c71 58%, #efc3a1); }
.pocket.is-selected { outline: 3px solid var(--accent); outline-offset: 2px; }
.pocket.is-swapped { animation: settle 260ms ease-out; }
@keyframes settle { from { transform: scale(0.92); } to { transform: scale(1); } }

/* Cropped photo inside any frame */
.frame { position: absolute; inset: 0; overflow: hidden; }
.frame img {
  position: absolute; max-width: none; display: block; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; pointer-events: none;
}
.frame.is-broken { display: grid; place-items: center; font-size: 11px; color: var(--muted); }

/* Start */
.how { list-style: none; counter-reset: how; padding: 0; margin: 0 0 24px; display: grid; gap: 10px; }
.how li { counter-increment: how; display: grid; grid-template-columns: 34px 1fr; column-gap: 10px; color: var(--muted); font-size: 13px; }
.how li::before {
  content: counter(how); grid-row: span 2; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; background: var(--ink); color: var(--ground); font: 800 15px var(--display);
}
.how b { color: var(--ink); font-size: 15px; }
.modes { border: 0; padding: 0; margin: 0 0 16px; display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.modes legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.mode {
  position: relative; display: grid; gap: 2px; padding: 12px 10px; border-radius: 14px;
  border: 1.5px solid var(--line); background: var(--paper); cursor: pointer;
}
.mode input { position: absolute; opacity: 0; }
.mode__name { font-weight: 700; }
.mode__meta { font-size: 12px; color: var(--muted); }
.mode:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.mode:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 2px; }
.resume { margin-bottom: 20px; padding: 14px; border-radius: 16px; background: var(--paper); border: 1px solid var(--line); }

/* Stage header */
.bar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5; margin: -20px -16px 12px; padding: 12px 16px 10px;
  background: color-mix(in srgb, var(--ground) 88%, transparent); backdrop-filter: blur(10px);
}
.steps { list-style: none; display: flex; gap: 6px; padding: 0; margin: 0; font-size: 12px; color: var(--muted); }
.steps li { display: flex; align-items: center; gap: 6px; }
.steps li + li::before { content: ""; width: 14px; height: 1px; background: var(--line); }
.steps li[aria-current="step"] { color: var(--accent); font-weight: 700; }
.steps li.is-past { color: var(--ink); }
.progress { height: 4px; border-radius: 2px; background: var(--line); margin-top: 10px; overflow: hidden; }
.progress__fill { height: 100%; width: 0; background: var(--accent); transition: width 240ms ease; }

/* Picking grid */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.tile {
  position: relative; aspect-ratio: 1; padding: 0; border: 0; border-radius: 6px; overflow: hidden;
  background: var(--line); cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.tile::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  box-shadow: inset 0 0 0 0 var(--accent); transition: box-shadow 120ms ease;
}
.tile[aria-pressed="true"]::after { box-shadow: inset 0 0 0 4px var(--accent); }
.heart {
  position: absolute; right: 6px; bottom: 6px; z-index: 2; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: #fff; font-size: 16px; line-height: 1;
  transform: scale(0); transition: transform 180ms cubic-bezier(.3, 1.6, .5, 1);
}
.tile[aria-pressed="true"] .heart { transform: scale(1); }
.zoom {
  position: absolute; right: 0; top: 0; z-index: 3; width: 36px; height: 36px;
  display: grid; place-items: center; cursor: pointer; color: #fff; font-size: 12px;
}
.zoom::before {
  content: ""; position: absolute; inset: 8px; border-radius: 50%; background: rgba(20, 16, 40, 0.28); z-index: -1;
}
.tile.is-broken { cursor: default; }

/* Dock */
.dock {
  position: sticky; bottom: 0; z-index: 6; margin: 16px -16px 0;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  background: color-mix(in srgb, var(--paper) 92%, transparent); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.dock__body { flex: 1 1 auto; }
.dock p { margin: 0; }
.dock__count { font-weight: 700; flex: 1 1 auto; }
.dock__msg { font-size: 12px; color: var(--muted); }
.dock__actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Picking screen: header, 3x4 grid and dock always fit one screen without scrolling */
#pick:not([hidden]) {
  display: flex; flex-direction: column; padding-block: 8px 0;
  height: calc(100dvh - env(safe-area-inset-top, 0px));
}
.head { flex: none; margin-bottom: 8px; }
.head__row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.head .title { font-size: 18px; margin: 0; }
.head__meta { margin: 0; font-size: 13px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.head .hint { margin-top: 2px; }
.head .progress { margin-top: 6px; }
.grid-wrap { flex: 1 1 auto; min-height: 0; container-type: size; display: grid; place-items: center; }
/* Portrait 3:4 cells (most photos are portrait); sized by whichever of width or height runs out first */
.grid-wrap .grid {
  --cell: min((100cqw - 8px) / 3, (100cqh - 12px) / 4 * 0.75);
  grid-template-columns: repeat(3, var(--cell)); grid-auto-rows: calc(var(--cell) * 4 / 3);
}
.grid-wrap .tile { aspect-ratio: auto; }
.dock--pick {
  position: static; flex: none; margin-top: 8px; padding-block: 8px calc(8px + env(safe-area-inset-bottom, 0px));
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px;
}
.btn--page { min-width: 0; padding: 0 12px; font-size: 14px; gap: 4px; }
.btn--ghost:disabled { opacity: 0.35; cursor: default; }
.dock__center { display: grid; justify-items: center; text-align: center; }
.dock--pick .dock__count { font-size: 14px; white-space: nowrap; }
.dock--pick .dock__msg { font-size: 11px; line-height: 1.3; }
.linkbtn {
  min-height: 24px; padding: 0 4px; border: 0; background: none; color: var(--accent);
  font-size: 12px; font-weight: 700; text-decoration: underline; cursor: pointer;
}

/* Duel */
.versus { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card {
  position: relative; aspect-ratio: 3 / 4; padding: 0; border: 0; border-radius: 14px; overflow: hidden;
  background: var(--line); cursor: pointer; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.card.is-chosen { animation: chosen 220ms ease-out; }
.card.is-chosen::after { content: "♥"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 64px; color: #fff; text-shadow: 0 2px 12px rgba(0,0,0,.35); }
@keyframes chosen { 50% { transform: scale(1.04); } }
.duel-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 12px; }

/* Arrange */
.toggles { display: flex; gap: 16px; margin-top: 12px; flex-wrap: wrap; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.bench { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scroll-snap-type: x proximity; }
.bench .pocket { flex: 0 0 76px; width: 76px; cursor: pointer; scroll-snap-align: start; }
.bench:empty::before { content: "控えはありません"; color: var(--muted); font-size: 13px; }
.actions { margin-top: 24px; }
.actions--row { display: flex; flex-wrap: wrap; gap: 8px; }
.actions--row .btn { flex: 1 1 140px; }

/* Done */
.result { margin: 0; }
.result img {
  display: block; width: 100%; aspect-ratio: 1080 / 1220; border-radius: 12px; background: var(--paper);
  box-shadow: 0 1px 2px rgba(31, 27, 58, 0.12), 0 10px 28px rgba(31, 27, 58, 0.22);
  -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto;
}
.result figcaption { margin-top: 14px; }
.sources { padding: 0; margin: 0; list-style: none; display: grid; gap: 6px; }
.sources li { display: grid; grid-template-columns: 48px 44px 1fr; gap: 10px; align-items: center; font-size: 13px; }
.sources .rank { position: static; justify-self: start; }
.sources .pocket { width: 44px; }
.sources a { color: var(--ink); }

/* Viewer */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); padding: 0; border: 0; border-radius: 16px;
  background: var(--paper); color: var(--ink); overflow: auto;
}
dialog::backdrop { background: rgba(10, 8, 24, 0.72); }
#viewer-img { display: block; width: 100%; max-height: 70dvh; object-fit: contain; background: #000; }
.viewer__bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 12px; }
.viewer__bar p { margin: 0; font-weight: 700; flex: 1 1 auto; }
.viewer__bar a { color: var(--ink); font-size: 14px; }

.fatal { padding: 24px; border-radius: 16px; background: var(--paper); border: 1px solid var(--line); }

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

/* Terms and privacy pages */
.legal { display: flex; justify-content: center; gap: 16px; margin: 24px 0 0; font-size: 13px; }
.legal a, .doc a { color: var(--muted); }
.doc { padding-block: 24px 48px; }
.doc h1 { font: 800 24px/1.4 var(--display); margin: 8px 0 12px; }
.doc h2 { font-size: 16px; margin: 28px 0 8px; }
.doc p, .doc li { font-size: 14px; line-height: 1.85; }
.doc ul { padding-left: 1.2em; margin: 0; }
.doc li + li { margin-top: 4px; }
.doc__back { margin: 0; font-size: 13px; }
.doc__date { margin-top: 32px; color: var(--muted); font-size: 13px; }
