:root {
  --bg-top: #2B3A55;
  --bg-bottom: #1B2438;
  --ink: #FFFFFF;
  --ink-edge: #1B2438;
  --accent: #4FC3F7;
  --perfect: #FFD54A;
  --good: #7FDBFF;
  --miss: #A0A8B4;
  --safe: #FF9EC4;
  --font: "Hiragino Maru Gothic ProN", "BIZ UDPGothic", "Noto Sans JP", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--ink);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
  touch-action: none;
}

#gl {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

[hidden] { display: none !important; }

.num { font-variant-numeric: tabular-nums; }

.outlined {
  text-shadow: 0 0 3px var(--ink-edge), 0 2px 0 var(--ink-edge);
}

/* 上部の表示 */
#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(env(safe-area-inset-top) + 10px) calc(env(safe-area-inset-right) + 12px) 0 calc(env(safe-area-inset-left) + 12px);
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "left pause" "center center";
  gap: 6px 8px;
  pointer-events: none;
  text-shadow: 0 0 3px var(--ink-edge), 0 2px 0 var(--ink-edge);
}
.hud-left { grid-area: left; }
.hud-center { grid-area: center; }
#pause-btn { grid-area: pause; pointer-events: auto; }
#score { font-size: 26px; font-weight: 700; }
#combo { font-size: 16px; min-height: 1.2em; }
#gauge {
  height: 12px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.35);
  border: 2px solid rgba(255, 255, 255, 0.8);
  overflow: hidden;
}
#gauge-fill {
  height: 100%;
  width: 70%;
  background: linear-gradient(90deg, #27AE60, #7EE08A);
  transition: width 0.12s linear;
}
#gauge.low #gauge-fill { background: linear-gradient(90deg, #E8553D, #F2B630); }

@media (min-aspect-ratio: 1/1) {
  #hud {
    grid-template-columns: 1fr minmax(200px, 36vw) 1fr;
    grid-template-areas: "left center pause";
  }
  #pause-btn { justify-self: end; }
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  background: rgba(27, 36, 56, 0.6);
  color: var(--ink);
  font: 700 18px var(--font);
  cursor: pointer;
}

/* 判定の文字 */
.judge {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, -50%);
  font: 800 clamp(22px, 6.5vmin, 34px) var(--font);
  letter-spacing: 0.04em;
  pointer-events: none;
  opacity: 0;
  text-shadow: 0 0 4px var(--ink-edge), 0 3px 0 var(--ink-edge);
  white-space: nowrap;
}
.judge.show { animation: judge-pop 0.4s ease-out forwards; }
.judge.perfect { color: var(--perfect); }
.judge.good { color: var(--good); }
.judge.miss { color: var(--miss); }
.judge.safe { color: var(--safe); }
@keyframes judge-pop {
  0% { opacity: 1; transform: translate(-50%, -30%) scale(1.25); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}

#banner {
  position: absolute;
  left: 50%;
  top: 34%;
  transform: translate(-50%, -50%);
  font: 800 44px var(--font);
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
  text-shadow: 0 0 5px var(--ink-edge), 0 3px 0 var(--ink-edge);
}
#banner small { display: block; font-size: 18px; margin-top: 6px; }

#debug {
  position: absolute;
  left: calc(env(safe-area-inset-left) + 8px);
  bottom: calc(env(safe-area-inset-bottom) + 8px);
  margin: 0;
  padding: 6px 8px;
  background: rgba(0, 0, 0, 0.6);
  font: 12px/1.35 ui-monospace, Menlo, monospace;
  color: #cfe;
  pointer-events: none;
  white-space: pre;
}

/* 画面 */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 16px);
  background: rgba(27, 36, 56, 0.72);
  text-align: center;
  touch-action: manipulation;
}
.overlay h1 { font-size: clamp(34px, 9vw, 56px); margin: 0 0 8px; text-shadow: 0 4px 0 var(--ink-edge); }
.overlay h2 { font-size: 28px; margin: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.btn, .big-btn, .diff-btn {
  font-family: var(--font);
  font-weight: 700;
  color: var(--ink);
  border: 3px solid rgba(255, 255, 255, 0.9);
  border-radius: 999px;
  background: rgba(79, 195, 247, 0.25);
  cursor: pointer;
}
.btn { font-size: 18px; padding: 10px 26px; min-width: 180px; }
.big-btn { font-size: 24px; padding: 14px 40px; min-width: 220px; background: #4FC3F7; color: #10233a; }
.diff-btn { font-size: 16px; padding: 10px 16px; }
.diff-btn[aria-checked="true"] { background: #FFFFFF; color: #10233a; }
button:focus-visible { outline: 3px solid var(--perfect); outline-offset: 3px; }
.hint { margin: 0; font-size: 15px; opacity: 0.9; }
.small { margin: 0; font-size: 12px; opacity: 0.75; }
.rank { font-size: 88px; font-weight: 800; line-height: 1; text-shadow: 0 5px 0 var(--ink-edge); }
#result-stats {
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 18px;
  margin: 0;
  font-size: 17px;
}
#result-stats dt { text-align: left; opacity: 0.85; }
#result-stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

/* 手助けの名前（やさしい） */
#labels { position: absolute; inset: 0; pointer-events: none; }
.item-label {
  position: absolute;
  left: 0;
  top: 0;
  padding: 2px 8px;
  border-radius: 999px;
  font: 700 13px var(--font);
  white-space: nowrap;
  color: #10233a;
  background: #fff;
  border: 2px solid currentColor;
  will-change: transform;
}

.settings-panel {
  display: grid;
  gap: 10px;
  margin-top: 10px;
  padding: 12px 16px;
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.25);
  font-size: 15px;
}
.settings-panel label { display: grid; grid-template-columns: 7.5em 1fr; align-items: center; gap: 10px; text-align: left; }
.settings-panel input[type="range"] { width: min(200px, 45vw); accent-color: var(--accent); }
.offset-row { display: flex; align-items: center; gap: 10px; justify-content: center; }
.offset-row output { min-width: 4.5em; text-align: center; }

.result-title { margin: 0; font-size: 20px; font-weight: 800; color: var(--perfect); min-height: 0; }
.result-title:empty, .result-record:empty { display: none; }
.result-record { margin: 0; font-size: 16px; font-weight: 700; color: var(--good); }
.result-comment { margin: 0; max-width: 22em; font-size: 15px; }
#result-stats dt.sub, #result-stats dd.sub { font-size: 14px; opacity: 0.8; }
#result { overflow-y: auto; justify-content: safe center; }

/* 右下の小窓（通信画面のような枠）に、ソータの正面の顔 */
#face-window {
  position: absolute;
  right: calc(env(safe-area-inset-right) + 10px);
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  width: clamp(60px, 15vmin, 104px);
  height: clamp(60px, 15vmin, 104px);
  padding: 4px;
  border-radius: 14px;
  background: linear-gradient(135deg, #4FC3F7, #2F80ED);
  box-shadow: 0 0 0 2px #1B2438, 0 4px 12px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
#face-window canvas { width: 100%; height: 100%; border-radius: 10px; display: block; }

/* タイトル：下のほうはソータが見えるよう、うすくする */
.title-overlay {
  justify-content: flex-start;
  padding-top: calc(env(safe-area-inset-top) + 6vh);
  background: linear-gradient(180deg, rgba(27, 36, 56, 0.85) 0%, rgba(27, 36, 56, 0.6) 55%, rgba(27, 36, 56, 0.15) 100%);
}
.record { margin: 0; font-size: 15px; min-height: 1.2em; opacity: 0.9; }
.small-btn { min-width: 140px; font-size: 16px; padding: 8px 18px; }

/* あそびかた */
.howto-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 220px));
  gap: 12px;
  width: 100%;
  justify-content: center;
}
.howto-card {
  margin: 0;
  padding: 10px;
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.3);
}
.howto-card svg { width: 100%; height: auto; display: block; }
.howto-card figcaption {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin-top: 6px;
  font-size: 15px;
  font-weight: 700;
  word-break: keep-all; /* 分かち書きの空白でだけ折り返す */
  overflow-wrap: anywhere;
}
.howto-card figcaption b {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #10233a;
}
@media (max-aspect-ratio: 1/1) {
  .howto-cards { grid-template-columns: minmax(0, 300px); gap: 8px; }
  .howto-card { display: grid; grid-template-columns: 42% 1fr; align-items: center; gap: 8px; }
  .howto-card figcaption { margin: 0; justify-content: flex-start; text-align: left; }
}
#howto { cursor: pointer; }

/* 設定 */
.settings-panel label.check {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: flex-start;
  cursor: pointer;
}
.settings-panel input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
#settings .settings-panel { margin-top: 0; }

/* 知らせ（コピーしました など） */
#toast {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translate(-50%, 20px);
  padding: 8px 18px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.75);
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 低い画面（横持ちの電話）では、画面の中を詰める */
@media (max-height: 480px) {
  .overlay { gap: 8px; }
  .overlay h1 { font-size: 30px; margin: 0; }
  .big-btn { font-size: 20px; padding: 10px 32px; }
  .btn { padding: 6px 18px; }
  .rank { font-size: 56px; }
  #result-stats { font-size: 14px; }
  .title-overlay { padding-top: calc(env(safe-area-inset-top) + 10px); }
  #pause, #result, #settings, #howto { overflow-y: auto; justify-content: safe center; }
  #pause { flex-wrap: wrap; flex-direction: row; align-content: center; }
  #pause h2 { width: 100%; }
}

/* 画面の中の要素がつぶれて重ならないようにする（はみ出すときは画面ごとスクロール） */
.overlay > * { flex-shrink: 0; }

/* 横長の画面では、結果を左右 2 列にして 1 画面に収める */
@media (min-aspect-ratio: 1/1) {
  #result {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "head stats" "rank stats" "ptitle stats" "record stats" "comment stats" "again stats" "buttons stats" "note note";
    align-content: center;
    justify-items: center;
    column-gap: 24px;
    row-gap: 4px;
  }
  #result-head { grid-area: head; font-size: 20px; }
  #result-rank { grid-area: rank; }
  #result-title { grid-area: ptitle; font-size: 16px; }
  #result-record { grid-area: record; }
  #result-comment { grid-area: comment; font-size: 13px; }
  #again-btn { grid-area: again; }
  #result > .row { grid-area: buttons; }
  #result > .small { grid-area: note; }
  #result-stats { grid-area: stats; align-self: center; gap: 2px 14px; }
}

/* それでも収まらない画面では、結果の画面ごとスクロールする */
#result { overflow-y: auto; }
