:root {
  --bg: #10121a;
  --bg-2: #191d2b;
  --bg-3: #232838;
  --line: #333a50;
  --fg: #eef1f8;
  --fg-dim: #98a0b8;
  --accent: #4ec8c0;
  --accent-2: #ee6dcb;
  --ok: #58d68d;
  --ng: #ff6b81;
  --stage: 460px;
  /* ステージ幅。縦が狭い端末では画面高さに合わせて縮める */
  --stage-w: min(100%, var(--stage), 52vh);
  --pad: max(16px, env(safe-area-inset-left));
}
/* svh はキーボード表示でも値が変わらないのでモバイルではこちらを優先 */
@supports (height: 1svh) {
  :root { --stage-w: min(100%, var(--stage), 52svh); }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: radial-gradient(1200px 600px at 50% -200px, #1e2540, var(--bg)) fixed;
  color: var(--fg);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  line-height: 1.6;
  overscroll-behavior-y: contain; /* ゲーム中の引っぱり更新を抑える */
  -webkit-tap-highlight-color: transparent;
}

button, input, label { touch-action: manipulation; }

.hidden { display: none !important; }

/* ---- topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px var(--pad);
  padding-top: max(12px, env(safe-area-inset-top));
  max-width: 720px;
  margin: 0 auto;
}
.logo {
  margin: 0;
  font-size: 18px;
  letter-spacing: .04em;
}
.logo span {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.icon-btn {
  background: none;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 10px;
  width: 36px;
  height: 34px;
  font-size: 16px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--fg); border-color: var(--accent); }

/* ---- screens ---- */
.screen {
  max-width: 720px;
  margin: 0 auto;
  padding: 8px var(--pad) calc(40px + env(safe-area-inset-bottom));
}
.lead { color: var(--fg-dim); margin-top: 0; }

/* ---- settings ---- */
.settings { display: grid; gap: 20px; margin-bottom: 26px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--fg-dim);
  margin-bottom: 8px;
  letter-spacing: .06em;
}
.segmented { display: flex; flex-wrap: wrap; gap: 8px; }
.segmented button {
  flex: 1 1 auto;
  min-width: 88px;
  padding: 9px 12px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--fg-dim);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  transition: .15s;
}
.segmented button:hover { border-color: var(--accent); color: var(--fg); }
.segmented button.on {
  background: linear-gradient(135deg, rgba(78,200,192,.22), rgba(238,109,203,.22));
  border-color: var(--accent);
  color: var(--fg);
}
.hint { margin: 8px 0 0; font-size: 12.5px; color: var(--fg-dim); }
.check {
  display: flex; align-items: center; gap: 8px;
  font-size: 14px; color: var(--fg); margin: 0;
}
.check input { width: 16px; height: 16px; accent-color: var(--accent); }

button.primary {
  display: block;
  width: 100%;
  padding: 14px;
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  color: #0d1017;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  border-radius: 12px;
  cursor: pointer;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost {
  padding: 10px 14px;
  font: inherit;
  font-size: 14px;
  background: var(--bg-2);
  color: var(--fg-dim);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}
button.ghost:hover:not(:disabled) { color: var(--fg); border-color: var(--accent); }
button.ghost:disabled { opacity: .4; cursor: not-allowed; }
button.ghost small { opacity: .65; font-size: 11px; margin-left: 3px; }
button.ghost.danger:hover:not(:disabled) { border-color: var(--ng); color: var(--ng); }

.credit { font-size: 11.5px; color: #6c7490; margin-top: 22px; }
.credit a { color: #8b93ad; }

/* ---- HUD ---- */
.hud {
  display: flex;
  gap: 10px;
  margin: 0 auto 12px;
  max-width: var(--stage-w);
}
.hud-item {
  flex: 1;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  text-align: center;
}
.hud-label { display: block; font-size: 11px; color: var(--fg-dim); letter-spacing: .08em; }
.hud-item strong { font-size: 17px; font-variant-numeric: tabular-nums; }

/* ---- stage ---- */
.stage-wrap { position: relative; }
.stage {
  position: relative;
  width: var(--stage-w);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  overflow: hidden;
  border-radius: 16px;
  border: 1px solid var(--line);
  background:
    linear-gradient(45deg, #1b1f2c 25%, transparent 25%) 0 0/28px 28px,
    linear-gradient(-45deg, #1b1f2c 25%, transparent 25%) 0 14px/28px 28px,
    linear-gradient(45deg, transparent 75%, #1b1f2c 75%) 14px -14px/28px 28px,
    linear-gradient(-45deg, transparent 75%, #1b1f2c 75%) -14px 0/28px 28px,
    #202533;
  touch-action: pan-y; /* 縦スクロールは通す。横スワイプ等は拾わない */
}
.stage img {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
  transition: transform .55s cubic-bezier(.22,.7,.3,1);
  -webkit-user-drag: none;
  -webkit-touch-callout: none; /* iOS の長押しプレビューで全体像が見えないように */
  user-select: none;
  pointer-events: none;
}
.stage-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 13px;
  color: var(--fg-dim);
  background: var(--bg-2);
  z-index: 2;
}
.crop-marker {
  position: absolute;
  border: 2px solid var(--accent-2);
  border-radius: 4px;
  box-shadow: 0 0 0 9999px rgba(16,18,26,.45);
  z-index: 1;
  pointer-events: none;
  animation: pop .4s ease-out;
}
@keyframes pop { from { opacity: 0; transform: scale(1.25); } to { opacity: 1; transform: scale(1); } }

.zoom-meter {
  text-align: center;
  font-size: 12px;
  color: var(--fg-dim);
  margin: 8px 0 4px;
  font-variant-numeric: tabular-nums;
}

/* ---- hint ---- */
.hint-box {
  max-width: var(--stage-w);
  margin: 0 auto 10px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px dashed var(--line);
  background: var(--bg-2);
  font-size: 13.5px;
  color: var(--fg-dim);
}
.hint-box b { color: var(--fg); }

/* ---- answer ---- */
.answer-area, .result-area { max-width: var(--stage-w); margin: 0 auto; }
.combo { position: relative; }
#answer-input {
  width: 100%;
  padding: 13px 14px;
  font: inherit;
  font-size: 16px;
  color: var(--fg);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  outline: none;
}
#answer-input:focus { border-color: var(--accent); }
#answer-input::placeholder { color: #6c7490; }

.suggest {
  position: absolute;
  z-index: 20;
  left: 0; right: 0;
  bottom: calc(100% + 6px);
  margin: 0;
  padding: 5px;
  list-style: none;
  max-height: 290px;
  overflow-y: auto;
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 34px rgba(0,0,0,.5);
}
.suggest li {
  padding: 9px 11px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 9px;
}
.suggest li .s-en { font-size: 11.5px; color: var(--fg-dim); }
.suggest li.on, .suggest li:hover { background: rgba(78,200,192,.18); }
.suggest mark { background: none; color: var(--accent); font-weight: 700; }

.actions { display: flex; gap: 8px; margin-top: 10px; }
.actions.center { justify-content: center; margin-top: 18px; }
.actions button { flex: 1; }
.input-msg { min-height: 20px; margin: 6px 2px 0; font-size: 12.5px; color: var(--ng); }

/* ---- verdict / answer card ---- */
.verdict { text-align: center; font-size: 22px; font-weight: 800; margin: 4px 0 12px; }
.verdict.ok { color: var(--ok); }
.verdict.ng { color: var(--ng); }
.verdict small { display: block; font-size: 13px; font-weight: 400; color: var(--fg-dim); margin-top: 2px; }

.answer-card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
.answer-name strong { font-size: 20px; }
.answer-name span { display: block; font-size: 12.5px; color: var(--fg-dim); }
.answer-costume { margin-top: 6px; font-size: 12.5px; color: var(--fg-dim); }

/* ---- result ---- */
.result-title { text-align: center; margin: 10px 0 0; font-size: 16px; color: var(--fg-dim); letter-spacing: .1em; }
.score-big { text-align: center; margin: 4px 0 2px; }
.score-big span {
  font-size: 62px; font-weight: 800; font-variant-numeric: tabular-nums;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.score-big small { font-size: 16px; color: var(--fg-dim); margin-left: 4px; }
.result-sub { text-align: center; color: var(--fg-dim); margin: 0 0 22px; }

.miss-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.miss {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  text-align: center;
}
.miss img { width: 100%; aspect-ratio: 1/1; object-fit: contain; }
.miss b { display: block; font-size: 12px; margin-top: 4px; }
.miss span { display: block; font-size: 10.5px; color: var(--ng); }

/* ---- 共有 ---- */
.share-grid {
  max-width: var(--stage-w);
  margin: 0 auto 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-2);
  text-align: center;
  font-size: 19px;
  line-height: 1.5;
  letter-spacing: .12em;
  word-break: break-all;
}
.share-legend {
  margin: 6px 0 0;
  font-size: 11px;
  letter-spacing: 0;
  color: var(--fg-dim);
}
.card-preview {
  max-width: var(--stage-w);
  margin: 0 auto 12px;
  padding: 0;
  text-align: center;
}
.card-preview img {
  width: 100%;
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
  display: block;
}
.card-preview figcaption {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--fg-dim);
}

.share-actions {
  display: flex;
  gap: 8px;
  max-width: var(--stage-w);
  margin: 0 auto 18px;
}
.share-actions button { flex: 1; }
button.x-btn {
  background: #000;
  border-color: #4a4a4a;
  color: #fff;
  font-weight: 700;
}
button.x-btn:hover { border-color: #fff; color: #fff; }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--bg-3);
  border: 1px solid var(--accent);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: .2s;
  z-index: 100;
}
.toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- スマートフォン ---- */
@media (max-width: 560px) {
  .topbar { padding-bottom: 8px; }
  .logo { font-size: 16px; }
  .lead { font-size: 14px; }
  .hud { gap: 6px; margin-bottom: 8px; }
  .hud-item { padding: 5px 6px; }
  .hud-item strong { font-size: 15px; }
  .zoom-meter { margin: 6px 0 2px; }
  .settings { gap: 16px; }
  .segmented { gap: 6px; }
  .segmented button { min-width: 0; padding: 10px 8px; font-size: 13.5px; }
  .actions { gap: 6px; }
  .actions button { font-size: 12.5px; padding: 11px 4px; }
  .actions button small { display: block; margin: 0; }
  #answer-input { padding: 12px; }
  .suggest { max-height: 42svh; }
  .suggest li { padding: 11px; }
  .score-big span { font-size: 52px; }
  .share-grid { font-size: 16px; }
  .miss-list { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
}

/* 縦が特に短い端末ではステージをさらに詰める */
@media (max-height: 560px) {
  :root { --stage-w: min(100%, var(--stage), 46svh); }
  .topbar { padding-top: 6px; padding-bottom: 4px; }
}

/* 横向き: ステージを左、操作を右に置いて縦を稼ぐ */
@media (orientation: landscape) and (max-height: 560px) and (min-width: 620px) {
  :root { --stage-w: min(46vw, var(--stage), 76svh); }

  .screen { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }

  #screen-game {
    display: grid;
    grid-template-columns: auto minmax(240px, 1fr);
    grid-template-rows: auto auto 1fr;
    column-gap: 18px;
    align-items: start;
  }
  #screen-game .stage-wrap { grid-column: 1; grid-row: 1 / span 3; }
  #screen-game .hud        { grid-column: 2; grid-row: 1; }
  #screen-game .hint-box   { grid-column: 2; grid-row: 2; }
  #screen-game .answer-area,
  #screen-game .result-area { grid-column: 2; grid-row: 3; }

  #screen-game .hud,
  #screen-game .hint-box,
  #screen-game .answer-area,
  #screen-game .result-area { max-width: none; margin-left: 0; margin-right: 0; }

  .suggest { max-height: 60svh; }
  .verdict { margin-top: 0; }
}
