:root {
  --ink: #1f2a26;
  --ink-soft: #4b5a54;
  --paper: rgba(246, 247, 242, 0.9);
  --glass: rgba(20, 30, 28, 0.32);
  --glass-line: rgba(255, 255, 255, 0.55);
  --accent: #2f8f7c;
  --accent-deep: #226d5e;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #cdd5d2;
  color: var(--ink);
  font-family: "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#touch-layer {
  position: fixed;
  inset: 0;
  touch-action: none;
}

/* ---------- HUD ---------- */
.hud-btn, .hud-panel {
  position: fixed;
  z-index: 5;
  color: #fff;
  background: var(--glass);
  border: 1.5px solid var(--glass-line);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

#sound-btn {
  top: calc(14px + var(--safe-t));
  left: calc(14px + var(--safe-l));
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
}
#sound-btn svg { width: 24px; height: 24px; }
#sound-btn .icon-off { display: none; }
#sound-btn.off .icon-on { display: none; }
#sound-btn.off .icon-off { display: block; }
#sound-btn:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }

#home-btn {
  top: calc(14px + var(--safe-t));
  left: calc(72px + var(--safe-l));
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
}
#home-btn svg { width: 22px; height: 22px; }
#home-btn:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }

#stage-label {
  top: calc(14px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  pointer-events: none;
  white-space: nowrap;
}

#timer {
  top: calc(14px + var(--safe-t));
  right: calc(14px + var(--safe-r));
  padding: 8px 16px;
  border-radius: 12px;
  font-size: 26px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  pointer-events: none;
}

/* タッチ操作の表示（タッチ端末だけ） */
#drag-hint, #jump-btn { display: none; }
body.touch #drag-hint, body.touch #jump-btn { display: block; }

#drag-hint {
  position: fixed;
  z-index: 3;
  left: calc(18px + var(--safe-l));
  bottom: calc(14px + var(--safe-b));
  pointer-events: none;
  color: rgba(255, 255, 255, 0.85);
  text-align: center;
  transition: opacity 0.2s;
}
body.dragging #drag-hint { opacity: 0; }
#drag-hint p, #jump-btn p {
  margin: 6px 0 0;
  font-size: 11px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.45);
  white-space: nowrap;
}
.hint-ring {
  position: relative;
  width: 96px;
  height: 96px;
  margin: 0 auto;
  border-radius: 50%;
  border: 1.5px dashed rgba(255, 255, 255, 0.55);
  background: rgba(255, 255, 255, 0.06);
}
.arr {
  position: absolute;
  width: 10px;
  height: 10px;
  border-left: 2px solid rgba(255, 255, 255, 0.8);
  border-top: 2px solid rgba(255, 255, 255, 0.8);
}
.arr.up { top: 10px; left: 50%; transform: translateX(-50%) rotate(45deg); }
.arr.down { bottom: 10px; left: 50%; transform: translateX(-50%) rotate(225deg); }
.arr.left { left: 10px; top: 50%; transform: translateY(-50%) rotate(-45deg); }
.arr.right { right: 10px; top: 50%; transform: translateY(-50%) rotate(135deg); }

#drag-ring, #drag-knob {
  position: fixed;
  z-index: 4;
  left: 0;
  top: 0;
  display: none;
  pointer-events: none;
}
#drag-ring {
  margin: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  background: rgba(255, 255, 255, 0.07);
  translate: -50% -50%;
}
#drag-knob {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.35);
  border: 1.5px solid rgba(255, 255, 255, 0.8);
  translate: -50% -50%;
}

#jump-btn {
  position: fixed;
  z-index: 3;
  right: calc(22px + var(--safe-r));
  bottom: calc(14px + var(--safe-b));
  pointer-events: none;
  color: #fff;
  text-align: center;
}
#jump-btn svg {
  display: block;
  width: 84px;
  height: 84px;
  margin: 0 auto;
  padding: 18px;
  border-radius: 50%;
  background: var(--glass);
  border: 2px solid rgba(255, 255, 255, 0.75);
}
#jump-btn.pulse svg { animation: pulse 0.25s ease-out; }
@keyframes pulse {
  0% { transform: scale(0.88); background: rgba(255, 255, 255, 0.4); }
  100% { transform: scale(1); }
}

#message {
  position: fixed;
  z-index: 6;
  left: 50%;
  top: 22%;
  transform: translate(-50%, -8px);
  padding: 10px 22px;
  border-radius: 999px;
  background: rgba(30, 36, 34, 0.55);
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#message.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 画面 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  grid-template-columns: minmax(0, 1fr);
  display: none;
  place-items: center;
  padding: 16px;
  background: rgba(205, 213, 210, 0.35);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  overflow: auto;
  touch-action: pan-y;
}
.overlay.show { display: grid; }

.card {
  width: min(680px, 100%);
  max-width: 100%;
  min-width: 0;
  padding: 28px 32px;
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 20px 60px rgba(30, 50, 44, 0.25);
  text-align: center;
}
.card.small { width: min(380px, 100%); }

.eyebrow {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.3em;
  color: var(--accent);
  font-weight: 700;
}
h1 {
  margin: 6px 0 10px;
  font-size: 34px;
  letter-spacing: 0.08em;
}
.lead {
  margin: 0 auto 18px;
  max-width: 36em;
  text-wrap: pretty;
  font-size: 14px;
  line-height: 1.8;
  color: var(--ink-soft);
}
.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
  text-align: left;
}
.ctrl {
  padding: 12px 16px;
  border-radius: 12px;
  background: rgba(47, 143, 124, 0.08);
  border: 1px solid rgba(47, 143, 124, 0.2);
}
.ctrl h2 { margin: 0 0 6px; font-size: 13px; color: var(--accent-deep); }
.ctrl p { margin: 4px 0; font-size: 13px; }
kbd {
  display: inline-block;
  min-width: 24px;
  padding: 1px 6px;
  margin-right: 3px;
  border-radius: 5px;
  border: 1px solid #b7c0bc;
  border-bottom-width: 2px;
  background: #fff;
  font-family: inherit;
  font-size: 12px;
  text-align: center;
}
button.primary {
  appearance: none;
  border: 0;
  padding: 14px 42px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  font-family: inherit;
  letter-spacing: 0.08em;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(47, 143, 124, 0.35);
}
button.primary:hover { background: var(--accent-deep); }
button.primary:focus-visible { outline: 3px solid #fff; outline-offset: 3px; }

.clear-title { font-size: 34px; color: var(--accent-deep); word-break: keep-all; overflow-wrap: anywhere; }
.time-label { margin: 0; font-size: 13px; color: var(--ink-soft); }
.time {
  margin: 2px 0 20px;
  font-size: 44px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

#rotate-screen { z-index: 20; background: rgba(205, 213, 210, 0.92); }
#rotate-btn { margin-top: 14px; }
@media (prefers-reduced-motion: reduce) {
  .phone-icon { animation: none; transform: rotate(-90deg); }
}
#rotate-screen .rank-status { margin-bottom: 0; }
.phone-icon {
  width: 40px;
  height: 64px;
  margin: 4px auto 14px;
  border: 3px solid var(--ink);
  border-radius: 8px;
  animation: rotate-hint 2s ease-in-out infinite;
}
@keyframes rotate-hint {
  0%, 30% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}

/* ステージ選択 */
.grid-title {
  margin: 20px 0 8px;
  font-size: 13px;
  color: var(--ink-soft);
  letter-spacing: 0.1em;
}
#stage-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.stage-btn {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 8px 4px;
  border-radius: 12px;
  border: 1px solid rgba(47, 143, 124, 0.3);
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  cursor: pointer;
}
.stage-btn:hover:not(:disabled) { border-color: var(--accent); background: rgba(47, 143, 124, 0.06); }
.stage-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.stage-btn:disabled { cursor: default; opacity: 0.45; background: #eef0ec; }
.stage-btn.done { background: rgba(47, 143, 124, 0.1); }
.stage-btn .num { font-size: 18px; font-weight: 700; color: var(--accent-deep); line-height: 1.2; }
.stage-btn .nm { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.stage-btn .bt { font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.best { margin: -14px 0 16px; min-height: 1.2em; font-size: 14px; font-weight: 700; color: var(--accent-deep); }
.clear-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.clear-actions [hidden] { display: none; }
button.secondary {
  appearance: none;
  padding: 12px 22px;
  border-radius: 999px;
  border: 1.5px solid var(--accent);
  background: transparent;
  color: var(--accent-deep);
  font-size: 15px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
}
button.secondary:hover { background: rgba(47, 143, 124, 0.08); }
button.secondary:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

/* ランキング */
.title-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.title-name { max-width: 360px; margin: 0 auto 14px; text-align: left; }
.title-name label { display: block; margin: 0 0 6px 4px; font-size: 13px; font-weight: 700; color: var(--accent-deep); }
.title-name label span { font-weight: 400; color: var(--ink-soft); }
.title-name input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 999px;
  border: 1.5px solid #b7c0bc;
  background: #fff;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  -webkit-user-select: text;
  user-select: text;
}
.title-name input:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.title-name .rank-status { margin: 4px 0 0; min-height: 0; text-align: left; }
.card.small:has(.rank-box), .rank-card { width: min(460px, 100%); }
.rank-box { margin: 0 0 18px; text-align: left; }
.rank-title { margin: 0 0 8px; font-size: 13px; color: var(--ink-soft); letter-spacing: 0.1em; text-align: center; }
.rank-form { display: flex; gap: 8px; }
.rank-form input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: 999px;
  border: 1.5px solid #b7c0bc;
  background: #fff;
  font: inherit;
  font-size: 16px; /* iOS で入力時に拡大されないよう 16px 以上 */
  color: var(--ink);
  -webkit-user-select: text;
  user-select: text;
}
.rank-form input:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.rank-form button { padding: 10px 16px; font-size: 14px; white-space: nowrap; }
.rank-status { min-height: 1.2em; margin: 8px 0 6px; font-size: 13px; color: var(--accent-deep); text-align: center; word-break: keep-all; overflow-wrap: anywhere; }
.rank-status.error { color: #a4412c; }
.rank-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid #e3e6e2; }
.rank-list li {
  display: grid;
  grid-template-columns: 2.2em 1fr auto;
  gap: 8px;
  align-items: baseline;
  padding: 6px 10px;
  border-bottom: 1px solid #e3e6e2;
  font-size: 14px;
}
.rank-list li.me { background: rgba(47, 143, 124, 0.12); font-weight: 700; }
.rank-list li.empty { display: block; text-align: center; color: var(--ink-soft); }
.rank-list .rk { font-weight: 700; color: var(--accent-deep); font-variant-numeric: tabular-nums; }
.rank-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-list .tm { font-variant-numeric: tabular-nums; }
.rank-heading { font-size: 26px; }
.rank-tabs { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 10px; }
.rank-tabs button {
  appearance: none;
  padding: 6px 0;
  border-radius: 10px;
  border: 1px solid rgba(47, 143, 124, 0.3);
  background: #fff;
  font: inherit;
  font-weight: 700;
  color: var(--accent-deep);
  cursor: pointer;
}
.rank-tabs button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.rank-tabs button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.rank-view-title { margin: 0 0 6px; font-weight: 700; }
.rank-card .rank-list { margin-bottom: 4px; text-align: left; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
@media (max-height: 500px) {
  .rank-list li { padding: 3px 10px; font-size: 13px; }
  .rank-heading { font-size: 20px; }
}
/* 横向きスマートフォン：クリア画面は左に結果とボタン、右にランキング */
@media (max-height: 500px) and (min-width: 600px) {
  #clear-screen .card {
    width: min(760px, 100%);
    display: grid;
    grid-template-columns: 1fr 1.25fr;
    column-gap: 22px;
    align-items: start;
  }
  #clear-screen .card > :not(.rank-box) { grid-column: 1; }
  #clear-screen .rank-box { grid-column: 2; grid-row: 1 / span 5; margin: 0; }
  #clear-screen .rank-list { max-height: 170px; overflow-y: auto; }
  #clear-screen .clear-actions { flex-direction: column; align-items: stretch; gap: 6px; }
  #clear-screen .card { padding: 14px 18px; }
  #clear-screen .clear-title { font-size: 24px; margin: 0 0 4px; }
  #clear-screen .time { font-size: 30px; margin-bottom: 8px; }
  #clear-screen .best { margin: -6px 0 10px; }
  #clear-screen .clear-actions button { padding: 8px 12px; font-size: 14px; white-space: nowrap; letter-spacing: 0.02em; }
  #clear-screen .rank-list { max-height: 150px; }
}

/* 横向きスマートフォン（高さが低い） */
@media (max-height: 500px) {
  .card { padding: 16px 22px; }
  h1 { font-size: 24px; margin: 2px 0 6px; }
  .lead { font-size: 12px; line-height: 1.6; margin-bottom: 10px; }
  .controls { margin-bottom: 12px; }
  .ctrl { padding: 8px 12px; }
  .ctrl p { font-size: 12px; }
  button.primary { padding: 10px 34px; font-size: 15px; }
  #timer { font-size: 20px; padding: 6px 12px; }
  #sound-btn, #home-btn { width: 42px; height: 42px; }
  #home-btn { left: calc(64px + var(--safe-l)); }
  #stage-label { font-size: 12px; padding: 4px 12px; }
  .grid-title { margin: 12px 0 6px; }
  .title-name { margin-bottom: 8px; }
  .title-name input { padding: 7px 12px; }
  .stage-btn { padding: 5px 2px; }
  .stage-btn .num { font-size: 15px; }
  button.secondary { padding: 9px 18px; font-size: 14px; }
  .hint-ring { width: 80px; height: 80px; }
  #jump-btn svg { width: 72px; height: 72px; padding: 16px; }
  .clear-title { font-size: 30px; }
  .time { font-size: 34px; margin-bottom: 12px; }
}
@media (max-width: 560px) {
  .controls { grid-template-columns: 1fr; }
  #stage-grid { grid-template-columns: repeat(3, 1fr); }
}
