/* ==========================================================================
   こよりでハクション！（koyori）
   夜の寝室の藍と紫・こよりの白・くしゃみの金
   ========================================================================== */
:root {
  --navy: #221d4a;
  --navy-2: #3a2d63;
  --ink: #2a2148;
  --snow: #ffffff;
  --snow-dim: #d5d0ee;
  --gold: #ffd94a;
  --red: #ff5a4d;
  --cyan: #9fd8ff;
  --green: #7dffb0;
  --panel: rgba(34, 28, 74, 0.88);
  --line: rgba(220, 215, 255, 0.34);
  --sgh-top: 0px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--navy);
  color: var(--snow);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Sans", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }

#app {
  position: fixed;
  left: 0;
  right: 0;
  top: var(--sgh-top, 0px);
  bottom: var(--ad-space, 0px);
  overflow: hidden;
  background: var(--navy);
}

/* ---------- ねている顔（キャンバス） ---------- */
#stage {
  position: absolute;
  inset: 0;
  outline: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: pointer;
}
#cv {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: calc(6px + env(safe-area-inset-top, 0px));
  left: calc(8px + env(safe-area-inset-left, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.hud-chip {
  display: flex;
  align-items: baseline;
  gap: 3px;
  padding: 6px 11px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.hud-chip b { font-size: 15px; font-weight: 900; }
.hud-chip small { font-size: 11px; font-weight: 700; color: var(--snow-dim); }
#hud-score { margin-left: auto; }
#hud-score b { font-size: 17px; color: var(--gold); }
#hud .icon-btn { pointer-events: auto; }

.icon-btn {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--snow);
  font-family: inherit;
  font-size: 15px;
  cursor: pointer;
}
.icon-btn:focus-visible, .btn:focus-visible { outline: 3px solid var(--cyan); outline-offset: 2px; }

/* つよさメーターと むずむず・おきそう（画面のいちばん下） */
#bars {
  position: absolute;
  left: calc(8px + env(safe-area-inset-left, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 5px;
  pointer-events: none;
}
#meter-row {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 11px;
  font-weight: 900;
}
.meter-cap { color: var(--snow-dim); white-space: nowrap; }
.meter-cap-hard { color: #ffa197; }
#meter {
  position: relative;
  flex: 1;
  height: 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.4) 55%, rgba(170, 40, 40, 0.75));
  overflow: hidden;
}
#meter-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 20%;
  width: 40%;
  background: linear-gradient(180deg, #7dffb0, #3fcf82);
  opacity: 0.8;
}
#meter.good #meter-zone { opacity: 1; box-shadow: 0 0 10px #7dffb0; }
#meter-needle {
  position: absolute;
  top: -2px;
  bottom: -2px;
  left: -3px;
  width: 6px;
  border-radius: 3px;
  background: #fff;
  box-shadow: 0 0 0 1.5px rgba(30, 20, 50, 0.9);
  will-change: transform;
}
#meter.hard #meter-needle { background: var(--red); }
.bar-row { display: flex; gap: 6px; }
.bar {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px 4px 7px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 11px;
  font-weight: 900;
  white-space: nowrap;
}
.bar-icon { font-size: 13px; line-height: 1; }
.bar-track { flex: 1; height: 10px; border-radius: 999px; background: rgba(0, 0, 0, 0.4); overflow: hidden; }
.bar-fill { height: 100%; width: 100%; transform: scaleX(0); transform-origin: left center; border-radius: 999px; will-change: transform; }
.bar-itch .bar-fill { background: linear-gradient(90deg, #ffb02e, var(--gold)); }
.bar-wake .bar-fill { background: linear-gradient(90deg, #ff9a8f, var(--red)); }
.bar-wake.danger { border-color: var(--red); animation: hurry 0.4s steps(2) infinite; }
#hud-time.hurry { border-color: var(--red); animation: hurry 0.5s steps(2) infinite; }
#hud-time b { min-width: 1.3em; text-align: right; font-size: 17px; }
#hud-who { min-width: 0; overflow: hidden; }
#hud-who b { overflow: hidden; text-overflow: ellipsis; }
@keyframes hurry { 50% { background: rgba(150, 30, 40, 0.92); } }
@keyframes pop {
  from { transform: scale(0.6); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

#banner {
  position: absolute;
  left: 50%;
  top: 17%;
  transform: translate(-50%, -50%);
  max-width: calc(100% - 20px);
  width: max-content;
  padding: 10px 22px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  text-align: center;
  font-size: 14px;
  font-weight: 800;
  line-height: 1.5;
  pointer-events: none;
  animation: fadein 0.3s ease-out;
}
#banner b { display: block; font-size: 24px; font-weight: 900; color: var(--gold); white-space: nowrap; }
#banner.who { background: rgba(44, 37, 96, 0.94); border: 3px solid #ffd94a; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35); }
#banner.who b { color: #fff; font-size: 26px; animation: pop 0.35s cubic-bezier(0.2, 1.6, 0.4, 1); }
#banner.clear { background: #ffb02e; border-color: #fff; color: var(--ink); white-space: nowrap; font-size: 13px; }
#banner.clear b { color: #fff; font-size: 38px; font-style: italic; text-shadow: 0 3px 0 #c2571c; animation: pop 0.35s cubic-bezier(0.2, 1.6, 0.4, 1); }
#banner.end { background: rgba(40, 40, 60, 0.92); }
@keyframes fadein {
  from { opacity: 0; }
  to { opacity: 1; }
}

#prompt {
  position: absolute;
  left: 50%;
  top: auto;
  bottom: calc(74px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 0);
  max-width: calc(100% - 24px);
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 15px;
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
}
#prompt.go { background: var(--gold); color: var(--ink); border-color: #fff; }
#prompt.now { background: var(--red); color: #fff; border-color: #fff; transform: translate(-50%, 0) scale(1.12); }

/* ---------- オーバーレイ ---------- */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  background: rgba(14, 10, 36, 0.5);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#title-overlay { background: linear-gradient(180deg, rgba(14, 10, 36, 0.05), rgba(14, 10, 36, 0.4)); }
.overlay-box {
  position: relative;
  width: min(400px, 100%);
  margin: auto;
  padding: 20px 18px 16px;
  border-radius: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 16px 44px rgba(8, 5, 24, 0.55);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  text-align: center;
}
.overlay-title { margin: 0 0 10px; font-size: 22px; font-weight: 900; }
.overlay-text { margin: 0 0 14px; font-size: 14px; color: var(--snow-dim); }

#title-head { margin-bottom: 8px; }
#logo-kicker {
  display: inline-block;
  margin: 0 0 6px;
  padding: 3px 12px;
  border-radius: 999px;
  background: rgba(255, 217, 74, 0.16);
  color: var(--gold);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
}
#logo {
  margin: 0;
  font-size: clamp(26px, 9vw, 40px);
  white-space: nowrap;
  font-weight: 900;
  font-style: italic;
  line-height: 1.1;
  letter-spacing: 0.02em;
  color: #fff;
  text-shadow: 0 3px 0 var(--red), 0 0 22px rgba(255, 150, 190, 0.5);
}
#logo span { display: block; color: var(--gold); font-size: 1.12em; }
#logo-en { margin: 4px 0 0; font-size: 13px; font-weight: 900; letter-spacing: 0.42em; color: var(--gold); padding-left: 0.42em; }
#tagline { margin: 8px 0 12px; font-size: 14px; line-height: 1.6; color: var(--snow-dim); }
#tagline b { color: #fff; }
#title-best { margin: 0 0 10px; font-size: 14px; font-weight: 800; color: var(--snow-dim); }
#title-best b { font-size: 20px; color: var(--gold); font-variant-numeric: tabular-nums; }

.btn {
  display: block;
  width: 100%;
  margin: 0 0 8px;
  padding: 13px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  font-family: inherit;
  font-size: 16px;
  font-weight: 900;
  cursor: pointer;
  transition: transform 0.08s;
}
.btn:active { transform: translateY(2px); }
.btn-primary {
  background: linear-gradient(180deg, #ffe88a, var(--gold));
  color: var(--ink);
  border-color: #fff3bf;
  box-shadow: 0 4px 0 #b88a00;
}
.btn-primary:active { box-shadow: 0 2px 0 #b88a00; }
.btn-sub { background: rgba(255, 255, 255, 0.09); color: var(--snow); font-size: 14px; padding: 11px 10px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

#title-box #btn-sound, #title-box #btn-restart { position: absolute; top: 10px; right: 10px; }

.howto {
  margin: 4px 0 12px;
  padding: 0 0 0 1.4em;
  text-align: left;
  font-size: 13px;
  line-height: 1.6;
  color: var(--snow-dim);
}
.howto li { margin-bottom: 5px; }
.howto b { color: #fff; }
.howto .c-red { color: #ffa197; }
.howto .c-green { color: var(--green); }
.howto-short { margin-top: 6px; padding: 10px 12px 6px 2.1em; border-radius: 12px; background: rgba(255, 255, 255, 0.06); }

#footer { margin-top: 4px; font-size: 11px; line-height: 1.6; color: rgba(213, 208, 238, 0.85); }
#footer a { color: inherit; }

#howto-box { text-align: center; }
#howto-box .howto { font-size: 13.5px; }

/* ---------- 結果 ---------- */
#result-rank-name { font-size: 15px; font-weight: 900; color: var(--cyan); letter-spacing: 0.06em; }
#result-score { font-size: 54px; font-weight: 900; line-height: 1.1; color: var(--gold); font-variant-numeric: tabular-nums; }
#result-score small { font-size: 22px; margin-left: 4px; }
#result-new { margin: 2px 0 8px; font-size: 15px; font-weight: 900; color: var(--green); animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1); }
#result-why { margin: 2px 0 8px; font-size: 13px; font-weight: 800; color: var(--snow-dim); line-height: 1.5; }
#result-list { list-style: none; margin: 8px 0 14px; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
#result-list li { padding: 7px 4px; border-radius: 10px; background: rgba(255, 255, 255, 0.08); display: flex; flex-direction: column; gap: 2px; }
#result-list span { font-size: 11px; color: var(--snow-dim); font-weight: 700; }
#result-list b { font-size: 15px; font-weight: 900; }

@media (max-width: 380px) {
  #hud { gap: 4px; left: calc(6px + env(safe-area-inset-left, 0px)); right: calc(6px + env(safe-area-inset-right, 0px)); }
  .hud-chip { padding: 5px 9px; font-size: 11.5px; }
  .hud-chip b { font-size: 13px; }
  #hud-score b { font-size: 15px; }
  #hud .icon-btn { width: 33px; height: 33px; font-size: 14px; }
  .bar { font-size: 10px; gap: 3px; padding: 4px 8px 4px 6px; }
  #hud-who { font-size: 10.5px; }
}
@media (max-height: 620px) {
  .overlay-box { padding: 14px 14px 12px; }
  #tagline { margin: 4px 0 8px; font-size: 13px; }
  .howto-short { display: none; }
  #result-score { font-size: 42px; }
}
@media (max-height: 440px) {
  #logo-kicker, #logo-en, #tagline { display: none; }
  #logo { font-size: 28px; }
  .btn { padding: 9px 12px; margin-bottom: 6px; }
  #result-list { display: none; }
  .howto { font-size: 12px; line-height: 1.45; }
  #banner { top: 22%; padding: 6px 16px; }
  #banner b, #banner.who b { font-size: 20px; }
}
@media (min-width: 768px) {
  .hud-chip { font-size: 15px; padding: 8px 14px; }
  .hud-chip b { font-size: 18px; }
  #hud-score b { font-size: 21px; }
  #prompt { font-size: 17px; }
  #bars { left: 50%; right: auto; width: min(560px, 90%); transform: translateX(-50%); }
  .bar, #meter-row { font-size: 13px; padding: 6px 12px 6px 9px; }
  .bar-track { height: 12px; }
  #meter { height: 16px; }
}

@media (min-width: 900px) {
  /* 広い画面では、うしろのお手本が見えるように箱を右へ寄せる */
  #title-overlay { justify-content: flex-end; padding-right: 5vw; }
  #title-overlay .overlay-box { margin: auto 0; }
}

@media (prefers-reduced-motion: reduce) {
  #banner, #banner b, #result-new, .bar-wake.danger, #hud-time.hurry { animation: none; }
}

/* ---------- 全国ランキング（SGRanking 共通モジュール） ---------- */
.sgr-hidden { display: none !important; }

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(14, 10, 36, 0.74);
}
.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #2c2560;
  color: var(--snow);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 18px 16px;
  box-shadow: 0 16px 44px rgba(8, 5, 24, 0.6);
}
.sgr-title { margin: 0; font-size: 20px; font-weight: 900; text-align: center; }
.sgr-tabs { display: flex; gap: 6px; justify-content: center; }
.sgr-tab {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  background: rgba(255, 255, 255, 0.08);
  color: var(--snow);
  cursor: pointer;
}
.sgr-tab-active { background: var(--gold); color: var(--ink); }
.sgr-register-msg { margin: 0 0 6px; font-size: 14px; font-weight: 800; color: var(--gold); text-align: center; }
.sgr-form { display: flex; gap: 8px; }
.sgr-name {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
}
.sgr-name:focus { outline: 3px solid var(--cyan); outline-offset: 0; }
.sgr-submit {
  border: 1px solid #fff3bf;
  border-radius: 10px;
  padding: 9px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  background: var(--gold);
  color: var(--ink);
  cursor: pointer;
}
.sgr-submit:disabled { opacity: 0.5; cursor: default; }
.sgr-error { margin: 4px 0 0; font-size: 12px; font-weight: 700; color: #ff9a8f; }
.sgr-status { margin: 0; font-size: 13px; color: var(--snow-dim); text-align: center; }
.sgr-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
  border-top: 1px solid var(--line);
}
.sgr-row { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 8px; font-size: 14px; }
.sgr-row:nth-child(even) { background: rgba(255, 255, 255, 0.05); }
.sgr-row.sgr-row-me { background: var(--gold); color: var(--ink); font-weight: 800; }
.sgr-rank { width: 2em; text-align: right; font-weight: 900; flex-shrink: 0; }
.sgr-row-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sgr-row-score { font-weight: 900; color: var(--gold); flex-shrink: 0; font-variant-numeric: tabular-nums; }
.sgr-row.sgr-row-me .sgr-row-score { color: var(--ink); }
.sgr-close {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 900;
  background: rgba(255, 255, 255, 0.09);
  color: var(--snow);
  cursor: pointer;
}
.sgr-cta { margin: 0 0 12px; text-align: center; }
.sgr-cta-msg { margin: 0 0 6px; font-size: 14px; font-weight: 900; color: var(--green); }
.sgr-cta-btn {
  border: 1px solid #fff3bf;
  border-radius: 12px;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  background: var(--gold);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 3px 0 #b88a00;
}
.sgr-cta-link {
  border: none;
  background: none;
  color: var(--cyan);
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  text-decoration: underline;
  cursor: pointer;
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / js/ad-banner.js が挿入する）
   --ad-h    : 広告そのものの高さ。広告なしのときは 0px
   --ad-space: レイアウトが空けるべき高さ（広告 + セーフエリア）
   ========================================================================== */
:root {
  --ad-h: 0px;
  --ad-space: 0px;
}
html.has-ad-banner {
  --ad-space: calc(var(--ad-h, 0px) + env(safe-area-inset-bottom, 0px));
}

.ad-banner {
  /* height は広告そのものの高さ。セーフエリアぶんの padding は外側に足す */
  box-sizing: content-box;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* ランキング・リザルト等の全画面オーバーレイ（2147483000）よりは下に置く */
  z-index: 2147482000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: var(--ad-h, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.86);
  /* border ではなく inset shadow にして、枠線ぶん高さがずれないようにする */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.ad-banner__frame {
  display: block;
  width: 100%;
  height: var(--ad-h, 0px);
  border: 0;
  color-scheme: light;
}

@media print {
  .ad-banner { display: none; }
}
