/* ヒット＆ブロー */

:root {
  --bg-top: #141a44;
  --bg-mid: #0d1130;
  --bg-deep: #090c22;
  --panel: rgba(20, 26, 68, 0.75);
  --line: rgba(154, 163, 208, 0.28);
  --text: #eef1ff;
  --text-dim: #9aa3d0;
  --tile-bg: #f5f7ff;
  --tile-text: #1c2340;
  --hit: #ff5a5f;
  --blow: #ffb830;
  --accent: #3ec9ff;
  --accent3: #ffe14d;
  --ok: #4ade80;
  --font-jp: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", Meiryo, sans-serif;
  --font-num: "SF Mono", "Menlo", "Consolas", "Roboto Mono", monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: var(--font-jp);
  background: linear-gradient(180deg, var(--bg-top) 0%, var(--bg-mid) 55%, var(--bg-deep) 100%);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font-family: var(--font-jp);
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

#app {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  max-width: 520px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 0px) max(8px, env(safe-area-inset-right, 0px)) env(safe-area-inset-bottom, 0px) max(8px, env(safe-area-inset-left, 0px));
}

.hidden { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- ヘッダー ---------- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 2px 6px;
  gap: 8px;
}

#logo-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

#logo {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
  background: linear-gradient(180deg, #ffffff 30%, #a8d8ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#diff-badge {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: bold;
  color: var(--bg-deep);
  background: linear-gradient(180deg, #8fe0ff 0%, var(--accent) 100%);
  border-radius: 999px;
  padding: 4px 10px;
}

#topbar-buttons {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  width: 36px;
  height: 36px;
  font-size: 17px;
  line-height: 1;
  border-radius: 10px;
  background: rgba(154, 163, 208, 0.14);
  border: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:active { transform: scale(0.92); }

/* ---------- ステータスバー ---------- */

#status-bar {
  display: flex;
  gap: 6px;
  padding: 0 2px 8px;
}

.status-box {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 5px 4px;
}

.status-label {
  font-size: 11px;
  color: var(--text-dim);
}

.status-value {
  font-family: var(--font-num);
  font-size: 15px;
  font-weight: bold;
  font-variant-numeric: tabular-nums;
}

/* ---------- 履歴 ---------- */

#history-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  background: rgba(9, 12, 34, 0.5);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

#history {
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  list-style: none;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#history-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 20px;
  text-align: center;
  font-size: 14px;
  line-height: 1.8;
  color: var(--text-dim);
  pointer-events: none;
}

#history-empty b { color: var(--text); }

.empty-sub { font-size: 12px; }

.h-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 8px;
  animation: rowIn 0.25s ease;
}

.h-row-win {
  border-color: var(--hit);
  box-shadow: 0 0 14px rgba(255, 90, 95, 0.35);
}

@keyframes rowIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: none; }
}

.h-num {
  width: 2em;
  flex-shrink: 0;
  text-align: center;
  font-family: var(--font-num);
  font-size: 12px;
  color: var(--text-dim);
}

.h-digits {
  flex: 1;
  display: flex;
  gap: 4px;
  min-width: 0;
}

.h-tile {
  width: clamp(24px, 7.6vw, 34px);
  height: clamp(30px, 9.4vw, 42px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  color: var(--tile-text);
  border-radius: 6px;
  font-family: var(--font-num);
  font-size: clamp(15px, 4.8vw, 21px);
  font-weight: bold;
}

.h-tile-hit {
  background: linear-gradient(180deg, #ff8a8d 0%, var(--hit) 100%);
  color: #fff;
}

.h-res {
  flex-shrink: 0;
  display: flex;
  gap: 4px;
  min-width: 4.6em;
  justify-content: flex-end;
}

.badge {
  font-family: var(--font-num);
  font-size: 13px;
  font-weight: bold;
  border-radius: 6px;
  padding: 3px 6px;
  color: #fff;
}

.badge-hit { background: var(--hit); }
.badge-blow { background: var(--blow); color: #3a2c00; }
.badge-zero { background: rgba(154, 163, 208, 0.25); color: var(--text-dim); }

.txt-hit { color: var(--hit); }
.txt-blow { color: var(--blow); }

/* ---------- 入力エリア ---------- */

#input-area {
  padding: 10px 2px 4px;
}

#slots {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-bottom: 10px;
}

.slot {
  width: clamp(38px, 12vw, 52px);
  height: clamp(48px, 15vw, 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(245, 247, 255, 0.08);
  border: 2px dashed rgba(154, 163, 208, 0.4);
  border-radius: 10px;
  font-family: var(--font-num);
  font-size: clamp(22px, 7vw, 30px);
  font-weight: bold;
  color: var(--text);
  transition: border-color 0.12s, background 0.12s;
}

.slot-filled {
  background: var(--tile-bg);
  color: var(--tile-text);
  border: 2px solid var(--tile-bg);
}

.slot-active { border-color: var(--accent); }

@keyframes slotsShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

#slots.shake { animation: slotsShake 0.28s ease; }

#keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.key {
  position: relative;
  height: clamp(44px, 13vw, 56px);
  border-radius: 10px;
  background: rgba(154, 163, 208, 0.16);
  border: 1px solid var(--line);
  font-family: var(--font-num);
  font-size: 20px;
  font-weight: bold;
  color: var(--text);
  transition: transform 0.08s;
}

.key:active { transform: scale(0.92); background: rgba(154, 163, 208, 0.3); }

.key:disabled {
  opacity: 0.35;
  transform: none;
}

.key-del { font-size: 17px; }

/* メモ状態 */
.key-memo-x { opacity: 0.35; }
.key-memo-x::after,
.key-memo-o::after {
  position: absolute;
  top: 1px;
  right: 4px;
  font-size: 11px;
  line-height: 1;
}
.key-memo-x::after { content: "✕"; color: var(--hit); }
.key-memo-o::after { content: "○"; color: var(--ok); }
.key-memo-o { border-color: var(--ok); box-shadow: inset 0 0 0 1px var(--ok); }

#btn-judge {
  grid-column: span 4;
  height: clamp(44px, 13vw, 56px);
  border-radius: 10px;
  font-size: 17px;
  font-weight: 800;
  color: #3a2c00;
  background: linear-gradient(180deg, #fff0a0 0%, var(--accent3) 55%, #ffc93e 100%);
  box-shadow: 0 3px 0 #b8892a;
  transition: transform 0.08s, box-shadow 0.08s, opacity 0.15s;
}

#btn-judge:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #b8892a;
}

#btn-judge:disabled {
  opacity: 0.4;
  cursor: default;
  box-shadow: none;
}

#memo-hint {
  margin-top: 6px;
  text-align: center;
  font-size: 10px;
  color: var(--text-dim);
}

.memo-x { color: var(--hit); }
.memo-o { color: var(--ok); }

/* ---------- 結果オーバーレイ ---------- */

#result-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(9, 12, 34, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

#result-box {
  width: min(360px, 100%);
  background: linear-gradient(180deg, #1c2158 0%, var(--bg-mid) 100%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 22px 18px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  animation: popIn 0.3s ease;
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1); }
}

#result-title {
  font-size: 30px;
  font-weight: 800;
}

#result-title.lose { color: var(--text-dim); font-size: 24px; }

#result-sub {
  font-size: 14px;
  font-weight: bold;
  color: var(--accent3);
  text-shadow: 0 0 12px rgba(255, 225, 77, 0.4);
}

#result-answer {
  display: flex;
  justify-content: center;
  gap: 5px;
}

#result-answer .h-tile {
  width: clamp(32px, 10vw, 42px);
  height: clamp(40px, 12.5vw, 52px);
  font-size: clamp(19px, 6vw, 26px);
}

#result-main {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  font-family: var(--font-num);
}

#result-attempts {
  font-size: 44px;
  font-weight: 800;
  color: var(--accent);
}

.result-unit { font-size: 16px; color: var(--text-dim); font-family: var(--font-jp); margin-left: 4px; }

#result-time {
  font-size: 20px;
  font-weight: bold;
  margin-left: 8px;
  color: var(--text);
}

#result-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
}

.rstat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.rstat-label { font-size: 11px; color: var(--text-dim); }

.rstat span:last-child {
  font-family: var(--font-num);
  font-size: 15px;
  font-weight: bold;
}

#result-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- ボタン共通 ---------- */

.btn {
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-weight: bold;
  transition: transform 0.08s;
}

.btn:active { transform: scale(0.96); }

.btn-primary {
  background: linear-gradient(180deg, #8fe0ff 0%, var(--accent) 60%, #1fa8e6 100%);
  color: #06122a;
}

.btn-sub {
  background: rgba(154, 163, 208, 0.16);
  border: 1px solid var(--line);
  color: var(--text);
}

.btn-danger {
  background: rgba(255, 90, 95, 0.14);
  border: 1px solid rgba(255, 90, 95, 0.5);
  color: #ff9a9d;
}

/* ---------- メニューモーダル ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(9, 12, 34, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-box {
  width: min(400px, 100%);
  max-height: min(84vh, 640px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #1c2158 0%, var(--bg-mid) 100%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-title {
  font-size: 20px;
  font-weight: 800;
  text-align: center;
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-caption {
  font-size: 12px;
  font-weight: bold;
  color: var(--text-dim);
}

.menu-note {
  font-size: 11px;
  color: var(--text-dim);
}

.diff-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.diff-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 9px 2px;
  border-radius: 10px;
  background: rgba(154, 163, 208, 0.14);
  border: 1px solid var(--line);
  font-size: 15px;
  font-weight: bold;
  color: var(--text);
}

.diff-btn small {
  font-size: 10px;
  font-weight: normal;
  color: var(--text-dim);
}

.diff-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #06122a;
}

.diff-btn.active small { color: rgba(6, 18, 42, 0.7); }

#best-list {
  display: flex;
  flex-direction: column;
}

.best-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 8px;
  font-size: 14px;
  border-radius: 6px;
}

.best-row:nth-child(odd) { background: rgba(154, 163, 208, 0.08); }

.best-row span:last-child {
  font-family: var(--font-num);
  font-weight: bold;
}

.howto {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}

.howto li {
  padding-left: 1.1em;
  position: relative;
}

.howto li::before {
  content: "•";
  position: absolute;
  left: 0;
  color: var(--accent);
}

/* ---------- トースト ---------- */

#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(90px + env(safe-area-inset-bottom, 0px) + var(--ad-space, 0px));
  transform: translateX(-50%);
  z-index: 300;
  background: rgba(9, 12, 34, 0.92);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 13px;
  font-weight: bold;
  white-space: nowrap;
  pointer-events: none;
  animation: toastIn 0.2s ease;
}

@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- フッター ---------- */

#footer {
  padding: 8px 2px calc(6px + env(safe-area-inset-bottom, 0px));
  text-align: center;
  font-size: 10px;
  line-height: 1.7;
  color: var(--text-dim);
}

#footer a {
  color: var(--text-dim);
  text-decoration: underline;
}

/* ---------- PC向け調整 ---------- */

@media (min-width: 600px) and (min-height: 700px) {
  #logo { font-size: 22px; }
  .key { font-size: 22px; }
  #history { padding: 10px; }
}

/* 背の低い画面（スマホ横向き・小型端末）: 結果画面が上下にはみ出して「もう一度」などのボタンに
   届かなくなり、メニューの上端も共通ヘッダーに隠れるため、ヘッダーと広告の間に収めて中をスクロールさせる */
@media (max-height: 600px) {
  #result-overlay,
  .modal {
    top: var(--sgh-top, 0px);
    bottom: var(--ad-space, 0px);
  }
  #result-box,
  .modal-box {
    max-height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ---------- 全国ランキング（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(9, 12, 34, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, #1c2158 0%, var(--bg-mid) 100%);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5), 0 0 30px rgba(62, 201, 255, 0.12);
}

.sgr-title {
  margin: 0;
  font-size: 20px;
  font-weight: 800;
  text-align: center;
}

.sgr-tabs {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.sgr-tab {
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: bold;
  background: rgba(154, 163, 208, 0.16);
  color: var(--text);
  cursor: pointer;
}

.sgr-tab-active {
  background: var(--accent);
  color: #06122a;
}

.sgr-register-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: bold;
  text-align: center;
  color: var(--accent3);
}

.sgr-form {
  display: flex;
  gap: 8px;
}

.sgr-name {
  flex: 1;
  min-width: 0;
  font-size: 16px;
  font-family: var(--font-jp);
  padding: 8px 10px;
  border: 2px solid rgba(154, 163, 208, 0.45);
  border-radius: 8px;
  background: rgba(13, 16, 48, 0.65);
  color: var(--text);
}

.sgr-name::placeholder { color: var(--text-dim); }

.sgr-name:focus {
  outline: none;
  border-color: var(--accent);
}

.sgr-submit {
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 15px;
  font-weight: bold;
  font-family: var(--font-jp);
  background: linear-gradient(180deg, #8fe0ff 0%, var(--accent) 60%, #1fa8e6 100%);
  color: #06122a;
  cursor: pointer;
}

.sgr-submit:hover { filter: brightness(1.08); }

.sgr-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

.sgr-error {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--hit);
}

.sgr-status {
  margin: 0;
  font-size: 13px;
  text-align: center;
  color: var(--text-dim);
}

.sgr-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.sgr-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.sgr-row:nth-child(even) { background: rgba(154, 163, 208, 0.1); }

.sgr-row.sgr-row-me {
  background: var(--accent);
  color: #06122a;
}

.sgr-rank {
  width: 2em;
  text-align: right;
  font-weight: bold;
  flex-shrink: 0;
}

.sgr-row-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sgr-row-score {
  font-weight: bold;
  flex-shrink: 0;
}

.sgr-close {
  border: 1px solid rgba(154, 163, 208, 0.4);
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: bold;
  font-family: var(--font-jp);
  background: rgba(154, 163, 208, 0.16);
  color: var(--text);
  cursor: pointer;
}

.sgr-close:hover { background: rgba(154, 163, 208, 0.28); }

.sgr-cta {
  margin: 0;
  text-align: center;
}

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: bold;
  color: var(--accent3);
  text-shadow: 0 0 12px rgba(255, 225, 77, 0.4);
}

.sgr-cta-btn {
  border: none;
  border-radius: 999px;
  padding: 10px 22px;
  font-size: 15px;
  font-weight: 800;
  font-family: var(--font-jp);
  background: linear-gradient(180deg, #fff0a0 0%, var(--accent3) 55%, #ffc93e 100%);
  color: #3a2c00;
  cursor: pointer;
  box-shadow: 0 4px 0 #b8892a, 0 8px 22px rgba(255, 225, 77, 0.35);
  transition: transform 0.15s, box-shadow 0.15s;
}

.sgr-cta-btn:active {
  transform: scale(0.94);
  box-shadow: 0 2px 0 #b8892a, 0 4px 12px rgba(255, 225, 77, 0.35);
}

.sgr-cta-link {
  border: none;
  background: none;
  padding: 6px;
  font-size: 13px;
  color: var(--text-dim);
  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; }
}
