:root {
  --c-redbrown: #6B3D27;
  --c-brown: #B89460;
  --c-white: #FFFFFF;
  --c-sign-blue: #1F3D8A;
  --c-bg-dark: #1a1410;
  --c-panel: rgba(28, 22, 18, 0.78);
  --c-panel-border: rgba(255, 255, 255, 0.12);
  --c-text: #f4ece4;
  --c-text-dim: #c6b8a8;
  --c-accent: #8d5234;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  color: var(--c-text);
  background: var(--c-bg-dark);
  overflow-x: hidden;
  overflow-y: auto;
}


.bg-image {
  position: fixed;
  inset: 0;
  background-image: url("assets/background.svg");
  background-size: cover;
  background-position: center;
  filter: blur(2px) brightness(0.55);
  z-index: 0;
}

.bg-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0.15) 0%, rgba(0,0,0,0.65) 100%);
  z-index: 1;
}

.stage {
  position: relative;
  z-index: 2;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(24px, 3vw, 48px);
  padding: clamp(16px, 2vh, 32px);
}

.playfield {
  position: relative;
  /* PC では縦幅基準でビューポートに応じて拡縮。短い window でも収まるよう最小値を下げる */
  height: clamp(360px, 84vh, 900px);
  aspect-ratio: 2 / 3;
  background: rgba(10, 8, 6, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.sign-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  /* キャンバスの 2/15（上2行ぶん）を看板スペースとして確保 */
  height: 13.34%;
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.2s ease-in-out;
}

.sign-overlay.visible {
  opacity: 1;
}

.sign-overlay img {
  width: 100%;
  height: 100%;
  display: block;
}

.screen {
  position: absolute;
  inset: 0;
  background: rgba(10, 8, 6, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  border-radius: 6px;
  z-index: 5;
  overflow-y: auto;
}

.screen.hidden { display: none; }

/* === リザルトダイアログ === */
/* viewport 全体のオーバーレイ + 中央に大きめの dialog カード */
#cleared-screen,
#gameover-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(8, 6, 4, 0.88);
  padding: clamp(12px, 2vh, 24px);
  justify-content: flex-start;
}
.result-dialog {
  width: 100%;
  max-width: 760px;
  background: rgba(28, 22, 18, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  padding: clamp(14px, 2.2vh, 22px);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: clamp(10px, 1.6vh, 16px);
  margin: auto;
}
.dialog-title {
  margin: 0;
  font-size: clamp(20px, 3vh, 30px);
  letter-spacing: 0.18em;
  font-weight: 800;
  color: var(--c-text);
  text-align: center;
}
.share-preview-wrap {
  width: 100%;
  aspect-ratio: 1200 / 675;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
}
.share-preview {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.share-preview:not([src]) {
  opacity: 0;  /* 生成前は非表示 */
}
.share-actions {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;  /* 表示中のボタンが等幅で並ぶ（非表示は枠を取らない）*/
  gap: 6px;
  width: 100%;
}
.btn-action {
  font-family: inherit;
  font-size: clamp(12px, 1.5vh, 14px);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--c-text);
  background: #1F3D8A;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 5px;
  padding: clamp(10px, 1.5vh, 14px) 6px;
  cursor: pointer;
  transition: background 0.12s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.btn-action:hover  { background: #2E5BB8; }
.btn-action:active { background: #18306B; transform: translateY(1px); }
.btn-action:disabled { opacity: 0.4; cursor: wait; }
.btn-action.is-hidden { display: none; }
.btn-restart {
  width: 100%;
  margin-top: 2px;
}

/* 画面オーバーレイ内の AdSense 配置 */
.ad-slot {
  width: 100%;
  max-width: 320px;
  margin: 14px auto 6px;
  /* AdSense 未承認時は ins が空のままなので、margin だけで場所を確保 */
}
.ad-slot ins.adsbygoogle {
  display: block;
  min-height: 50px; /* ロード前のレイアウト崩れ防止の最低限 */
}

.screen h1 {
  font-size: clamp(28px, 4vh, 44px);
  margin: 0 0 12px 0;
  letter-spacing: 0.08em;
  color: var(--c-text);
}

.screen .subtitle {
  font-size: clamp(13px, 1.6vh, 17px);
  color: var(--c-text-dim);
  margin: 0 0 20px 0;
  line-height: 1.5;
}

.screen p {
  font-size: clamp(13px, 1.6vh, 17px);
  color: var(--c-text-dim);
  margin: 0 0 20px 0;
}

.rules {
  text-align: left;
  font-size: clamp(11px, 1.4vh, 14px);
  color: var(--c-text-dim);
  list-style: disc;
  padding-left: 20px;
  margin: 16px 0 0 0;
  line-height: 1.6;
}

.rules strong {
  color: var(--c-text);
  font-weight: 700;
}
/* 色名 (赤茶/茶/白) は実際の色背景で表示 */
.rules strong.c-redbrown {
  background: var(--c-redbrown);
  color: #FFFFFF;
  padding: 1px 8px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}
.rules strong.c-brown {
  background: var(--c-brown);
  color: #FFFFFF;
  padding: 1px 8px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}
.rules strong.c-white {
  background: var(--c-white);
  color: #2a1a12;
  padding: 1px 8px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.18);
}

/* NORMAL / EASY スタートボタン群（ルール下） */
.start-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: stretch;
  width: 100%;
  max-width: 260px;
  margin: 18px auto 0;
}
.btn-start {
  width: 100%;
  padding: 12px 20px;
  font-size: clamp(13px, 1.7vh, 15px);
}
.btn-easy {
  background: #E8B83A;
  color: #1a1410;
  border-color: rgba(0, 0, 0, 0.25);
}
.btn-easy:hover  { background: #F0C850; }
.btn-easy:active { background: #D8A82A; }
.start-buttons .start-buttons-note {
  font-size: clamp(10px, 1.2vh, 12px);
  color: var(--c-text-dim);
  margin: 4px 0 0;
  line-height: 1.4;
}

.btn {
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.1em;
  background: var(--c-redbrown);
  color: var(--c-white);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 4px;
  padding: 10px 28px;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}

.btn:hover { background: var(--c-accent); }
.btn:active { transform: translateY(1px); }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: clamp(200px, 18vw, 260px);
}

.panel {
  background: var(--c-panel);
  border: 1px solid var(--c-panel-border);
  border-radius: 6px;
  padding: 12px 14px;
}

.panel h2 {
  font-size: 11px;
  letter-spacing: 0.15em;
  margin: 0 0 8px 0;
  color: var(--c-text-dim);
  font-weight: 600;
}

.panel h2 small {
  font-size: 9px;
  letter-spacing: 0.1em;
  color: rgba(198, 184, 168, 0.5);
  font-weight: 400;
  margin-left: 4px;
}

.panel p {
  margin: 0;
  font-size: 13px;
}

.panel ul {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 12px;
  line-height: 1.7;
}

#next, #hold {
  display: block;
  /* width/height の両方が viewport に応じて縮むよう、vw と vh の最小を採用 */
  width: min(14vw, 16vh, 180px);
  height: min(14vw, 16vh, 180px);
  min-width: 72px;
  min-height: 72px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 3px;
  image-rendering: pixelated;
}

.keys ul { font-size: 11px; color: var(--c-text-dim); }

.btn-mini {
  margin-top: 8px;
  width: 100%;
  font-family: inherit;
  font-size: 10px;
  letter-spacing: 0.05em;
  color: var(--c-text);
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 3px;
  padding: 6px 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn-mini:hover  { background: rgba(255, 255, 255, 0.14); border-color: rgba(255, 255, 255, 0.3); }
.btn-mini:active { background: rgba(255, 255, 255, 0.05); }

.debug-badge {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 10;
  padding: 6px 12px;
  background: #c14a2a;
  color: #FFFFFF;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.15em;
  border-radius: 3px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  cursor: help;
  user-select: none;
}
.easy-badge {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 10;
  padding: 4px 10px;
  background: #E8B83A;
  color: #1a1410;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.2em;
  border-radius: 3px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.35);
  user-select: none;
}

/* === タッチコントロール（デスクトップでは非表示） === */
.touch-controls {
  display: none;
}

.tc-btn {
  font-family: inherit;
  font-size: 28px;
  font-weight: 600;
  color: var(--c-text);
  background: rgba(40, 30, 24, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  min-height: 56px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: 0 2px 0 rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
}
.tc-btn:active {
  background: rgba(232, 184, 58, 0.35);
  border-color: rgba(232, 184, 58, 0.7);
  transform: translateY(1px);
  box-shadow: 0 1px 0 rgba(0,0,0,0.4);
}
.tc-btn.tc-wide {
  font-size: 13px;
  letter-spacing: 0.18em;
}

/* PC で縦が狭めの window: サイドバーを圧縮、操作ヘルプは隠す */
@media (min-width: 901px) and (max-height: 720px) {
  .sidebar {
    width: clamp(160px, 14vw, 200px);
    gap: 10px;
  }
  .panel {
    padding: 8px 10px;
  }
  .panel h2 {
    font-size: 10px;
    margin-bottom: 4px;
  }
  .keys ul {
    font-size: 10px;
    line-height: 1.5;
  }
}
@media (min-width: 901px) and (max-height: 580px) {
  /* 操作ヘルプを隠して場所をあける */
  .sidebar .keys { display: none; }
}

/* === モバイル / 狭い画面用レイアウト === */
@media (max-width: 900px), (pointer: coarse) and (max-width: 1200px) {
  html, body {
    overflow-y: auto;
  }
  .stage {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
    padding: 8px;
    gap: 10px;
    justify-content: flex-start;
  }
  /* 盤面：横幅と高さの両方を満たすサイズ。操作系と一緒に1画面に収める */
  .playfield {
    width: min(360px, 96vw, calc(50vh * 2 / 3));
    height: auto;
    aspect-ratio: 2 / 3;
    touch-action: none;
    order: 1;
  }
  .sign-overlay {
    /* キャンバス比率に合わせて高さを %指定（2/15 = ~13.33%） */
    height: 13.34%;
  }
  /* サイドバー：HOLD / NEXT / 進捗 を1行で水平に並べる
     順序は playfield(1) → sidebar(2) → touch-controls(3) */
  .sidebar {
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: stretch;
    width: min(360px, 96vw);
    gap: 6px;
    order: 2;
  }
  .sidebar .panel {
    flex: 0 0 auto;
    padding: 6px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  /* HOLD / NEXT は小さく */
  #next, #hold {
    width: 60px;
    height: 60px;
  }
  .sidebar .panel h2 {
    margin: 0 0 4px 0;
    font-size: 10px;
  }
  /* モバイルではキー操作パネルは非表示（場所節約） */
  .sidebar .keys {
    display: none;
  }
  /* タッチコントロール表示 */
  .touch-controls {
    order: 3;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    width: min(360px, 96vw);
    margin-top: 2px;
  }
  .touch-controls .tc-btn {
    min-height: 50px;
    font-size: 26px;
  }
  .touch-controls .tc-wide {
    grid-column: 1 / -1;
    min-height: 40px;
    font-size: 12px;
  }
  .debug-badge {
    top: 4px;
    left: 4px;
    font-size: 9px;
    padding: 3px 8px;
  }
}

/* iPhone safe area 対応 */
@supports (padding: max(0px)) {
  .stage {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }
}
