/* ============================================================
   配線コネクト - Wire Connect  style.css
   ============================================================ */

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

:root {
  --bg1: #060d1e;
  --bg2: #142a4d;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-border: rgba(255, 255, 255, 0.14);
  --text: #e9f3ff;
  --text-dim: #8fa2c4;
  --accent: #3fd2f5;
  --accent2: #ffd166;
  --danger: #ff5d5d;
  --radius: 14px;
}

html, body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: radial-gradient(125% 85% at 50% 0%, var(--bg2) 0%, var(--bg1) 72%);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#app {
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));   /* dvh 未対応ブラウザ向けフォールバック */
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 上端のセーフエリアは共通ヘッダー（--sgh-top）が取っているので、ここでは足さない */
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
/* 広告があるときは --ad-space が下端のセーフエリアを含んでいるので、二重に空けない */
html.has-ad-banner #app { padding-bottom: 0; }

/* ---------- ヘッダー ---------- */
#topbar {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px 4px;
}

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

#logo {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: linear-gradient(180deg, #ffffff, #8fd9ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

#mode-badge {
  flex-shrink: 0;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent2);
  background: rgba(255, 209, 102, 0.12);
  border: 1px solid rgba(255, 209, 102, 0.35);
  border-radius: 999px;
  padding: 3px 9px;
  white-space: nowrap;
}

#topbar-buttons { display: flex; gap: 8px; }

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn:active { transform: scale(0.92); }

/* ---------- 難易度バー ---------- */
#mode-bar {
  width: 100%;
  max-width: 420px;
  display: flex;
  gap: 6px;
  padding: 6px 14px 0;
}

.mode-btn {
  flex: 1;
  min-width: 0;
  padding: 8px 3px 7px;
  border-radius: 11px;
  border: 1px solid var(--panel-border);
  background: rgba(0, 0, 0, 0.28);
  color: var(--text-dim);
  font-family: inherit;
  /* 4つ並べても狭い端末で溢れないように画面幅に追従させる */
  font-size: clamp(11px, 3.2vw, 13.5px);
  font-weight: 800;
  line-height: 1.25;
  white-space: nowrap;
  cursor: pointer;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}
.mode-btn .mode-size {
  display: block;
  font-size: clamp(9px, 2.5vw, 10.5px);
  font-weight: 700;
  opacity: 0.75;
  font-family: "SF Mono", "Courier New", monospace;
}
.mode-btn.active {
  color: #06121f;
  background: linear-gradient(180deg, #7ce4ff, #2fb8e0);
  border-color: rgba(255, 255, 255, 0.5);
  box-shadow: 0 4px 14px rgba(63, 210, 245, 0.3);
}
.mode-btn:active { transform: scale(0.97); }

/* ---------- ステータスバー ---------- */
#status-bar {
  width: 100%;
  max-width: 420px;
  display: flex;
  align-items: stretch;
  gap: 7px;
  padding: 7px 14px 0;
}

.status-box {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 10px;
  min-width: 0;
}

.status-icon { font-size: 13px; color: #8fc5ff; }

.status-value {
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 18px;
  font-weight: 700;
  color: #d6ecff;
  text-align: center;
  white-space: nowrap;
}

#time-value { min-width: 3.6ch; }
#moves-value { min-width: 2ch; }

.status-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 56px;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.status-btn:active { transform: scale(0.95); }
.status-btn:disabled { opacity: 0.4; cursor: default; }
#hint-count { font-family: "SF Mono", "Courier New", monospace; }

/* ---------- 進捗バー ---------- */
#progress-wrap {
  position: relative;
  width: calc(100% - 28px);
  max-width: 392px;
  height: 20px;
  margin-top: 8px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.34);
  border: 1px solid var(--panel-border);
  overflow: hidden;
}

#progress-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0%;
  background: linear-gradient(90deg, rgba(63, 210, 245, 0.55), rgba(255, 209, 102, 0.7));
  transition: width 0.28s ease-out;
}

#progress-text {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: #eaf6ff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

/* ---------- 盤面 ---------- */
#board-wrap {
  position: relative;
  width: 100%;
  max-width: 640px;
  flex: 1;
  min-height: 0;
  padding: 6px 6px 8px;
}

#board {
  position: absolute;
  inset: 6px 6px 8px;
  width: calc(100% - 12px);
  height: calc(100% - 14px);
  touch-action: none;
  cursor: pointer;
  border-radius: 16px;
  outline: none;
}
#board:focus-visible { box-shadow: 0 0 0 2px rgba(255, 209, 102, 0.7); }

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 10, 24, 0.74);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
  animation: fadein 0.25s ease-out;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

#result-box {
  width: min(92vw, 360px);
  background: linear-gradient(180deg, #1d3358, #0e1c34);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 26px 22px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  animation: rise 0.3s ease-out;
}
@keyframes rise {
  from { transform: translateY(24px) scale(0.95); opacity: 0; }
  to { transform: none; opacity: 1; }
}

#result-icon {
  font-size: 44px;
  line-height: 1;
  margin-bottom: 8px;
  animation: bounce 0.5s ease-out;
}
@keyframes bounce {
  0% { transform: scale(0.3); }
  60% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

#result-title {
  font-size: 27px;
  font-weight: 900;
  letter-spacing: 0.06em;
  background: linear-gradient(180deg, #ffe9a8, #ffb648);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#result-mode {
  margin-top: 6px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
}

#result-time {
  margin-top: 10px;
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 40px;
  font-weight: 800;
  line-height: 1;
  color: #eaf9ff;
  text-shadow: 0 0 18px rgba(63, 210, 245, 0.5);
}

.time-unit {
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  margin-left: 3px;
  color: var(--text-dim);
  text-shadow: none;
}

#result-sub {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
}

#result-record {
  margin-top: 12px;
  display: inline-block;
  font-size: 13px;
  font-weight: 900;
  color: #4a3200;
  background: linear-gradient(180deg, #ffe08a, #ffb648);
  border-radius: 999px;
  padding: 5px 14px;
  animation: bounce 0.5s ease-out 0.15s backwards;
}

#result-best {
  margin-top: 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent2);
}

#result-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
}

/* ---------- ボタン共通 ---------- */
.btn {
  padding: 13px 18px;
  border-radius: 12px;
  border: none;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(180deg, #5cdcff, #1f9bd0);
  color: #04202e;
  box-shadow: 0 6px 18px rgba(63, 210, 245, 0.35);
}

.btn-sub {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--text);
}
.btn-sub.danger {
  background: rgba(255, 93, 93, 0.18);
  border-color: rgba(255, 93, 93, 0.5);
  color: #ffb3b3;
}

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 10, 24, 0.74);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: 12px;
  animation: fadein 0.2s ease-out;
}

.modal-box {
  width: min(92vw, 400px);
  max-height: 86vh;   /* dvh 未対応ブラウザ向けフォールバック */
  max-height: 86dvh;
  overflow-y: auto;
  background: linear-gradient(180deg, #1d3358, #0e1c34);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-title {
  font-size: 20px;
  font-weight: 900;
  text-align: center;
  margin-bottom: 16px;
}

.menu-section { margin-bottom: 18px; }

.menu-caption {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 2px;
  font-size: 14px;
  font-weight: 600;
}

.toggle {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.1);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}
.toggle .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.toggle.on { background: var(--accent); border-color: var(--accent); }
.toggle.on .knob { transform: translateX(20px); }

.record-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 2px;
  font-size: 14px;
  font-weight: 600;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.08);
}
.record-row span:last-child {
  font-weight: 800;
  color: var(--accent2);
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 13px;
}

#btn-reset {
  width: 100%;
  margin-top: 12px;
  font-size: 13px;
  padding: 10px 14px;
}

.howto {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: #cbdcf2;
}
.howto li { padding-left: 1.1em; position: relative; }
.howto li::before { content: "•"; position: absolute; left: 0; color: var(--accent); }

#btn-close-menu { width: 100%; }

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(80px + env(safe-area-inset-bottom) + var(--ad-space, 0px));
  transform: translateX(-50%);
  background: rgba(16, 30, 54, 0.95);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 70;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: toastin 0.25s ease-out;
}
@keyframes toastin {
  from { transform: translate(-50%, 10px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

/* ---------- 紙吹雪 ---------- */
.confetti {
  position: fixed;
  top: -12px;
  width: 9px;
  height: 14px;
  z-index: 55;
  pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to {
    transform: translateY(105vh) rotate(720deg);   /* dvh 未対応ブラウザ向けフォールバック */
    transform: translateY(105dvh) rotate(720deg);
    opacity: 0.7;
  }
}

/* ---------- フッター ---------- */
#footer {
  padding: 6px 0 12px;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.7;
}
#footer a { color: var(--text-dim); }

/* ---------- 汎用 ---------- */
.hidden { display: none !important; }

/* ---------- レスポンシブ ---------- */
@media (min-width: 700px) {
  #logo { font-size: 22px; }
  #mode-bar, #status-bar { max-width: 460px; }
  #progress-wrap { max-width: 432px; }
}

/* スマホの横向きなど高さの低い画面：縦1列だと盤面に高さが残らない（1マス数px）ので、
   操作パネルを左、盤面を右に並べる。結果・メニューは共通ヘッダーと広告を避けてスクロールできるようにする */
@media (orientation: landscape) and (max-height: 500px) {
  #app {
    display: grid;
    grid-template-columns: minmax(0, 380px) minmax(0, 1fr);
    grid-template-rows: auto auto auto auto 1fr;
    justify-content: center;
    justify-items: center;
    align-items: start;
  }
  #topbar, #mode-bar, #status-bar, #progress-wrap, #footer { grid-column: 1; }
  #topbar { padding-top: 6px; }
  #footer { grid-row: 5; align-self: end; padding-bottom: 6px; }
  #board-wrap {
    grid-column: 2;
    grid-row: 1 / -1;
    align-self: stretch;
    height: 100%;
  }

  #result-overlay, .modal {
    padding: calc(var(--sgh-top, 0px) + 8px) 12px calc(var(--ad-space, 0px) + 8px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  /* 中身が画面より高いときに上下が切れないよう、中央寄せは auto マージンで行う */
  #result-box { margin: auto; }
  .modal-box { max-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px) - 16px); }
}

@media (prefers-reduced-motion: reduce) {
  #result-overlay, #result-box, #result-icon, #result-record, .modal, #toast { animation: none; }
  #progress-fill { transition: none; }
}

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

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000; /* 既存モーダル(60)・トースト(70)・広告バナー(2147482000)より上 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(4, 10, 24, 0.74);
  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, #1d3358, #0e1c34);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 20px 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

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

.sgr-tabs {
  display: flex;
  flex-wrap: wrap;   /* 難易度4つでも狭い端末で溢れないように */
  gap: 6px;
  justify-content: center;
}

.sgr-tab {
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 10px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

.sgr-tab-active {
  background: rgba(63, 210, 245, 0.18);
  border-color: var(--accent);
  color: var(--accent);
}

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

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

.sgr-name {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
}

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

.sgr-submit {
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #5cdcff, #1f9bd0);
  color: #04202e;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(63, 210, 245, 0.35);
}

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

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

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

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

.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: 8px;
  font-size: 14px;
}

.sgr-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.05);
}

.sgr-row.sgr-row-me {
  background: rgba(63, 210, 245, 0.25);
  color: #fff;
}

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

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

.sgr-row-score {
  font-weight: 800;
  color: var(--accent2);
  flex-shrink: 0;
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 13px;
}

.sgr-row.sgr-row-me .sgr-row-score {
  color: #fff;
}

.sgr-close {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.sgr-close:active { transform: scale(0.97); }

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

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
}

.sgr-cta-btn {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #ffe08a, #ffb648);
  color: #4a3200;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(255, 209, 102, 0.35);
}
.sgr-cta-btn:active { transform: scale(0.97); }

.sgr-cta-link {
  border: none;
  background: none;
  padding: 6px;
  font-family: inherit;
  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; }
}
