:root {
  --teal: #20a596;
  --teal-dark: #127a70;
  --orange: #ff8a3d;
  --pink: #ff6f91;
  --ink: #233044;
  --soft: #f4fffc;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #cdeee6;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
}

#stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 0;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible {
  outline: 3px solid #ffd166;
  outline-offset: 2px;
}

[hidden] { display: none !important; }

/* ---------- 画面（カード） ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  z-index: 20;
}

.card {
  background: rgba(255, 255, 255, 0.94);
  border-radius: 26px;
  box-shadow: 0 14px 40px rgba(18, 80, 74, 0.22), 0 0 0 4px rgba(255, 255, 255, 0.6);
  padding: 22px 24px 18px;
  width: min(420px, 100%);
  text-align: center;
  max-height: 100%;
  overflow: auto;
}

.title-screen {
  justify-content: flex-start;
  padding-left: max(5vw, calc(16px + var(--safe-l)));
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.back-link {
  font-size: 12px;
  font-weight: 700;
  color: var(--teal-dark);
  text-decoration: none;
  border-radius: 999px;
  padding: 3px 10px;
  background: #e8f7f4;
  white-space: nowrap;
}

.back-link:hover { background: #d4f1eb; }
.back-link:focus-visible { outline: 3px solid #ffd166; outline-offset: 2px; }

.back-link--bottom {
  display: inline-block;
  margin-top: 10px;
}

.kicker {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.2em;
  color: var(--teal);
  font-weight: 700;
}

h1 {
  margin: 4px 0 10px;
  line-height: 1;
  font-weight: 900;
}

h1 .t1 {
  display: block;
  font-size: 30px;
  color: var(--teal-dark);
  letter-spacing: 0.04em;
}

h1 .t2 {
  display: block;
  font-size: 46px;
  color: var(--orange);
  letter-spacing: 0.04em;
  text-shadow: 0 3px 0 #ffd9bf;
}

h1 em {
  font-style: normal;
  font-size: 0.5em;
  margin-left: 6px;
  vertical-align: 0.6em;
  background: var(--teal);
  color: #fff;
  border-radius: 8px;
  padding: 3px 7px;
  text-shadow: none;
}

h2 {
  margin: 6px 0 16px;
  font-size: 26px;
  color: var(--teal-dark);
}

.guide {
  background: var(--soft);
  border: 2px solid #c6eee6;
  border-radius: 16px;
  padding: 10px 12px 10px;
  text-align: left;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.guide p {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 1.65;
}

.guide.is-speaking {
  border-color: var(--orange);
  box-shadow: 0 0 0 4px rgba(255, 138, 61, 0.18);
}

.small-btn {
  background: #fff;
  border: 2px solid var(--teal);
  color: var(--teal-dark);
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 13px;
  font-weight: 700;
}

.guide.is-speaking .small-btn::before { content: "■ "; }

.howto {
  list-style: none;
  padding: 0;
  margin: 12px 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.howto li {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: #fff;
  border-radius: 14px;
  padding: 8px 4px;
  box-shadow: 0 2px 0 #d6efe9;
}

.howto b { font-size: 12px; }
.howto small {
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  background: var(--orange);
  border-radius: 999px;
  padding: 1px 10px;
}

.badge {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  position: relative;
}

.badge.germ {
  background: radial-gradient(circle at 35% 35%, #b07cf0, #7b3fc4);
}
.badge.germ::before, .badge.germ::after {
  content: "";
  position: absolute;
  top: -6px;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 10px solid #fff3c4;
}
.badge.germ::before { left: 5px; transform: rotate(-20deg); }
.badge.germ::after { right: 5px; transform: rotate(20deg); }

.badge.plaque {
  border-radius: 10px 10px 14px 14px;
  background: radial-gradient(circle at 30% 30%, #f6dc6c 0 6px, transparent 7px), radial-gradient(circle at 70% 60%, #e9c43a 0 5px, transparent 6px), #fffdf2;
  box-shadow: inset 0 0 0 2px #e6d38a;
}

.badge.cavity {
  border-radius: 10px 10px 14px 14px;
  background: radial-gradient(circle at 50% 45%, #3a2216 0 8px, transparent 9px), #f3ead8;
  box-shadow: inset 0 0 0 2px #d8c9a8;
}

.primary, .secondary {
  display: block;
  width: 100%;
  border-radius: 999px;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.primary {
  font-size: 22px;
  padding: 13px;
  margin: 6px 0 10px;
  color: #fff;
  background: linear-gradient(#ff9d55, #ff7a2a);
  box-shadow: 0 5px 0 #d85d16, 0 8px 16px rgba(255, 122, 42, 0.35);
  transition: transform 0.08s;
}

.primary:active { transform: translateY(3px); box-shadow: 0 2px 0 #d85d16; }

.secondary {
  font-size: 16px;
  padding: 10px;
  margin-top: 8px;
  background: #e8f7f4;
  color: var(--teal-dark);
}

.toggles {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.toggle {
  flex: 1;
  border-radius: 999px;
  padding: 7px 8px;
  font-size: 13px;
  font-weight: 700;
  background: #e8f7f4;
  color: var(--teal-dark);
}

.toggle b {
  display: inline-block;
  min-width: 2.6em;
  margin-left: 4px;
  border-radius: 999px;
  padding: 1px 6px;
  background: var(--teal);
  color: #fff;
}

.toggle[aria-pressed="false"] b { background: #9aa8b5; }

.best {
  margin: 10px 0 0;
  font-size: 13px;
  color: #5d6b7a;
}

.best b {
  font-size: 18px;
  color: var(--ink);
}

.modal {
  justify-content: center;
  background: rgba(20, 60, 60, 0.28);
}

/* ---------- 結果 ---------- */
.result-screen .card { width: min(400px, 100%); }

/* 横に広い画面：結果カードを左に寄せ、右に患者さんを見せる */
@media (min-aspect-ratio: 5/4) and (min-width: 900px) {
  .result-screen {
    justify-content: flex-start;
    padding-left: max(6vw, calc(16px + var(--safe-l)));
    background: linear-gradient(90deg, rgba(20, 60, 60, 0.25), rgba(20, 60, 60, 0) 60%);
  }
}

.rank {
  margin: 8px 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.rank span {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 34px;
  font-weight: 900;
  color: #fff;
  background: var(--teal);
  box-shadow: 0 4px 0 var(--teal-dark);
}

[data-rank="S"] .rank span { background: linear-gradient(#ffd56b, #ff9f1c); box-shadow: 0 4px 0 #c97a00; }
[data-rank="A"] .rank span { background: var(--pink); box-shadow: 0 4px 0 #c94468; }

.rank b { font-size: 20px; }

.final {
  margin: 6px 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
}

.final span { font-size: 14px; color: #5d6b7a; }
.final b { font-size: 44px; color: var(--orange); }

.record {
  display: inline-block;
  margin: 0 0 6px;
  padding: 3px 14px;
  border-radius: 999px;
  background: #fff1b8;
  color: #b56a00;
  font-weight: 900;
  animation: pulse 0.9s ease-in-out infinite alternate;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin: 8px 0;
}

.stats div {
  background: var(--soft);
  border-radius: 12px;
  padding: 6px;
}

.stats dt { font-size: 12px; color: #5d6b7a; }
.stats dd { margin: 0; font-size: 20px; font-weight: 900; }

/* ---------- HUD ---------- */
.hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  grid-template-rows: auto auto 1fr auto;
  grid-template-areas:
    "time patient score pause"
    "pain pain pain pain"
    ". . . ."
    "hint hint hint hint";
  gap: 6px 8px;
  padding: calc(8px + var(--safe-t)) calc(8px + var(--safe-r)) calc(10px + var(--safe-b)) calc(8px + var(--safe-l));
  align-items: start;
}

.hud-box {
  background: rgba(255, 255, 255, 0.92);
  border-radius: 16px;
  padding: 4px 12px;
  box-shadow: 0 3px 0 rgba(18, 122, 112, 0.18);
  display: flex;
  align-items: baseline;
  gap: 4px;
  min-height: 44px;
  white-space: nowrap;
}

.hud-label { font-size: 11px; color: #5d6b7a; font-weight: 700; }
.hud-unit { font-size: 12px; font-weight: 700; }

.hud-time { grid-area: time; align-items: center; }
.hud-time b { font-size: 28px; font-variant-numeric: tabular-nums; min-width: 1.3em; text-align: right; }
.hud.hurry .hud-time { background: #ffe1e1; animation: pulse 0.5s ease-in-out infinite alternate; }
.hud.hurry .hud-time b { color: #e0294a; }

.hud-patient {
  grid-area: patient;
  justify-self: center;
  align-items: center;
  flex-direction: column;
  gap: 0;
  padding-top: 3px;
  min-width: 0;
}

.hud-patient span { font-size: 11px; color: #5d6b7a; font-weight: 700; line-height: 1.2; }
.hud-patient b { font-size: 16px; line-height: 1.2; }

.hud-score { grid-area: score; align-items: center; }
.hud-score b { font-size: 22px; font-variant-numeric: tabular-nums; min-width: 2.5em; text-align: right; }

.icon-btn {
  grid-area: pause;
  pointer-events: auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 3px 0 rgba(18, 122, 112, 0.18);
  font-weight: 900;
  font-size: 18px;
  color: var(--teal-dark);
}

.hud-pain {
  grid-area: pain;
  justify-self: center;
  width: min(520px, 100%);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  padding: 3px 12px 3px 4px;
  box-shadow: 0 3px 0 rgba(18, 122, 112, 0.18);
}

.hud-pain svg { width: 32px; height: 32px; flex: none; }
.hud-pain svg .tear { fill: #57b8ff; stroke: none; }

.pain-bar {
  flex: 1;
  height: 14px;
  border-radius: 999px;
  background: #e7eef2;
  overflow: hidden;
}

.pain-bar i {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd166, #ff8a3d 55%, #ff3d6e);
  transition: width 0.15s linear;
}

.hud-pain.danger { animation: shake 0.3s linear infinite; }
.hud-pain.danger .pain-bar { background: #ffd7df; }

.combo {
  position: fixed;
  left: 50%;
  top: calc(118px + var(--safe-t));
  transform: translateX(-50%) scale(0.6);
  opacity: 0;
  font-weight: 900;
  font-size: 22px;
  color: #fff;
  -webkit-text-stroke: 1px #d85d16;
  text-shadow: 0 3px 0 #d85d16, 0 0 14px rgba(255, 138, 61, 0.6);
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}

.combo.on { opacity: 1; transform: translateX(-50%) scale(1); }

.hint {
  grid-area: hint;
  justify-self: center;
  margin: 0;
  padding: 7px 16px;
  border-radius: 999px;
  background: rgba(35, 48, 68, 0.78);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  opacity: 0;
  transition: opacity 0.25s;
  max-width: 100%;
  text-align: center;
}

.hint.on { opacity: 1; }

/* 横長で低い画面：HUDを左右に寄せる */
body.hud-side .hud {
  grid-template-columns: 172px 1fr auto;
  grid-template-rows: auto auto auto auto 1fr;
  grid-template-areas:
    "time . score"
    "patient . pause"
    "pain . ."
    "hint . ."
    ". . .";
}

body.hud-side .hud-box { justify-self: start; }
body.hud-side .hud-score { justify-self: end; }
body.hud-side .hud-patient { align-items: flex-start; }
body.hud-side .hud-pain { width: 168px; justify-self: start; }
body.hud-side .icon-btn { justify-self: end; }
body.hud-side .combo { top: auto; left: auto; right: calc(10px + var(--safe-r)); bottom: calc(12px + var(--safe-b)); transform: scale(0.6); font-size: 18px; }
body.hud-side .combo.on { transform: scale(1); }
body.hud-side .hint { justify-self: start; font-size: 12px; padding: 6px 10px; border-radius: 12px; white-space: normal; text-align: left; width: 168px; }
body.hud-side .caption { top: calc(52px + var(--safe-t)); }

/* ---------- 字幕・得点 ---------- */
.caption {
  position: fixed;
  left: 50%;
  top: calc(176px + var(--safe-t));
  transform: translate(-50%, -50%) scale(0.7);
  z-index: 15;
  pointer-events: none;
  text-align: center;
  opacity: 0;
  padding: 8px 22px 10px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 6px 20px rgba(18, 80, 74, 0.25);
  white-space: nowrap;
}

.caption b {
  display: block;
  font-size: 30px;
  font-weight: 900;
  color: var(--orange);
  letter-spacing: 0.02em;
}

.caption span {
  display: block;
  font-size: 14px;
  font-weight: 700;
  color: var(--teal-dark);
}

.caption.show { animation: caption 1.9s ease-out forwards; }

.popups {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 12;
}

.popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: 900;
  font-size: 19px;
  color: #fff;
  white-space: nowrap;
  -webkit-text-stroke: 5px rgba(35, 48, 68, 0.78);
  paint-order: stroke fill;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
  animation: rise 1s ease-out forwards;
}

.popup.zap { color: #fff6a8; }
.popup.king { color: #ffd166; font-size: 26px; }
.popup.hit { color: #ffe08a; font-size: 15px; }
.popup.clean { color: #ffffff; font-size: 15px; }
.popup.fill { color: #bff4ff; }
.popup.bad { color: #ff9aa9; font-size: 16px; }

@keyframes rise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); }
  30% { transform: translate(-50%, -70%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -160%) scale(1); }
}

@keyframes caption {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1.08); }
  22% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}

@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.06); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-2px); } 75% { transform: translateX(2px); } }

/* ---------- 縦長 ---------- */
@media (max-aspect-ratio: 4/5) {
  .title-screen {
    align-items: flex-end;
    justify-content: center;
    padding-left: calc(12px + var(--safe-l));
    padding-right: calc(12px + var(--safe-r));
  }
  .title-screen .card { padding: 14px 16px 12px; }
  h1 .t1 { font-size: 22px; }
  h1 .t2 { font-size: 36px; }
  h1 { margin-bottom: 8px; }
  .guide p { font-size: 13px; line-height: 1.55; }
  .howto { margin: 8px 0; }
  .primary { font-size: 20px; padding: 11px; }
  .hud-time b { font-size: 24px; }
  .hud-score b { font-size: 19px; }
  .hud-box { padding: 3px 10px; }
  .caption b { font-size: 26px; }
}

@media (max-height: 560px) and (orientation: landscape) {
  .title-screen .card, .modal .card { padding: 10px 16px 10px; }
  .title-screen .card { width: min(420px, 54vw); }
  h1 .t1, h1 .t2 { display: inline; }
  h1 .t1 { font-size: 20px; }
  h1 .t2 { font-size: 26px; }
  h1 { margin: 2px 0 6px; }
  .howto li { flex-direction: row; justify-content: center; gap: 4px; flex-wrap: wrap; }
  .howto .badge { display: none; }
  .guide p { font-size: 12px; line-height: 1.5; margin-bottom: 5px; }
  .howto { margin: 6px 0; gap: 6px; }
  .howto li { padding: 4px 2px; }
  .badge { width: 24px; height: 24px; }
  .primary { font-size: 18px; padding: 8px; margin: 4px 0 8px; }
  .best { margin-top: 6px; }
  .final b { font-size: 34px; }
  .rank span { width: 46px; height: 46px; font-size: 26px; }
  .stats dd { font-size: 16px; }
  .result-screen .card, .modal .card { width: min(420px, 70vw); }
  .caption b { font-size: 24px; }
}

/* 幅の狭いスマホ：HUDを詰める */
@media (max-width: 400px) {
  .hud { gap: 6px 5px; }
  .hud-box { padding: 3px 8px; min-height: 42px; border-radius: 14px; }
  .hud-time .hud-label, .hud-score .hud-label { display: none; }
  .hud-time b { font-size: 22px; }
  .hud-score b { font-size: 18px; min-width: 2em; }
  .hud-patient b { font-size: 14px; }
  .icon-btn { width: 42px; height: 42px; }
}

@media (prefers-reduced-motion: reduce) {
  .caption.show, .popup, .record, .hud.hurry .hud-time, .hud-pain.danger { animation-duration: 0.01s; animation-iteration-count: 1; }
  .caption.show { animation: none; opacity: 1; transform: translate(-50%, -50%); }
}
