:root {
  --safe-area-inset-top: env(safe-area-inset-top);
  --safe-area-inset-right: env(safe-area-inset-right);
  --safe-area-inset-bottom: env(safe-area-inset-bottom);
  --safe-area-inset-left: env(safe-area-inset-left);

  --bg-color: #0b1120;
  --card-bg: #1e293b;
  --board-bg: #065f46;
  --board-border: #047857;
  --board-line: #044e3b;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent: #10b981;
  --accent-hover: #059669;
  --danger: #ef4444;
  --warning: #f59e0b;
  --retro-yellow: #facc15;
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.25);
  --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Hiragino Sans", "Noto Sans JP", sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100dvw;
  height: 100dvh;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background-color: var(--bg-color);
  color: var(--text-main);
  font-family: var(--font-family);
}

.game-area,
#ad-area {
  width: 100%;
  overflow: hidden;
}

.game-area {
  position: relative;
  flex: 1;
  height: 100%;
  padding-top: var(--safe-area-inset-top);
  padding-right: var(--safe-area-inset-right);
  padding-bottom: var(--safe-area-inset-bottom);
  padding-left: var(--safe-area-inset-left);
}

#ad-area {
  display: none;
  height: 60px;
  margin-top: 10px;
}

@media (min-width: 800px) {
  #ad-area {
    height: 90px;
  }
}

.assets {
  display: none;
}

/* 言語切り替え */
html[lang="ja"] .lang-en,
html:not([lang="ja"]) .lang-ja {
  display: none;
}

/****************************************
** Ad Container
*/

body.has-ad-area .game-area {
  padding-bottom: 0;
}

body.has-ad-area #ad-area {
  display: block;
}

#ad-area .adsbygoogle {
  display: block;
  width: 100%;
  height: 100%;
}

/****************************************
** Screen Switcher
*/

.title-screen,
.game-screen {
  width: 100%;
  height: 100%;
}

body:not(.title-screen-active) .title-screen,
body:not(.game-screen-active) .game-screen {
  display: none;
}

/****************************************
** Title Screen
*/

.title-screen {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(circle at center, #1e293b 0%, #0b1120 100%);
}

.title-close-button {
  position: absolute;
  top: 18px;
  left: 18px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(220, 0, 0, 0.75);
  cursor: pointer;
}

body:not(.is-web-page) .title-close-button {
  display: none;
}

.title-close-button img {
  width: 22px;
  height: 22px;
}

.title-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  max-width: 360px;
  text-align: center;
}

.pixel-title {
  font-size: 3.1rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: #facc15;
  text-shadow:
    3px 0 #dc2626, -3px 0 #dc2626, 0 3px #dc2626, 0 -3px #dc2626,
    3px 3px #dc2626, -3px -3px #dc2626, 3px -3px #dc2626, -3px 3px #dc2626,
    6px 6px 0 #000, 8px 8px 0 rgba(0, 0, 0, 0.5);
  font-family: monospace, system-ui, sans-serif;
  animation: float-retro 2.2s ease-in-out infinite alternate;
  line-height: 1.1;
  margin-bottom: 4px;
}

@keyframes float-retro {
  0% { transform: translateY(0) rotate(-1deg); }
  100% { transform: translateY(-8px) rotate(1deg); }
}

.title-canvas-banner {
  width: 280px;
  height: 70px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 10px;
  background: #090d16;
  border: 2px solid #334155;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
}

.title-desc-card {
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  padding: 14px 18px;
  font-size: 0.85rem;
  color: #cbd5e1;
  line-height: 1.6;
  box-shadow: var(--shadow-sm);
}

.title-desc-highlight {
  color: #facc15;
  font-weight: 700;
}

.start-btn {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  color: #022c22;
  font-size: 1.25rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  padding: 14px 38px;
  border-radius: 9999px;
  border: 2px solid #34d399;
  cursor: pointer;
  box-shadow: 0 0 20px rgba(16, 185, 129, 0.4), 0 4px 10px rgba(0, 0, 0, 0.3);
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  animation: pulse-start 1.6s infinite ease-in-out;
}

.start-btn:hover {
  transform: scale(1.05) translateY(-2px);
  box-shadow: 0 0 30px rgba(16, 185, 129, 0.7);
}

.start-btn:active {
  transform: scale(0.98);
}

@keyframes pulse-start {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

/****************************************
** Game Screen
*/

.game-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  overflow-y: auto;
}

.container {
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* ヘッダー */
.game-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px;
}

.title-group h1 {
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #ffffff 0%, #cbd5e1 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  display: flex;
  align-items: center;
  gap: 6px;
}

.title-group p {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 1px;
}

.header-controls {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--card-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-main);
  padding: 8px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  width: 36px;
  height: 36px;
}

.icon-btn:hover {
  background: #334155;
}

.icon-btn img {
  width: 18px;
  height: 18px;
  display: block;
}

.sound-button img {
  filter: brightness(0) invert(1);
}

body.sound-enabled .sound-button.sound-disabled-button,
body.sound-disabled .sound-button.sound-enabled-button {
  display: none;
}

body.sound-disabled .sound-button.sound-disabled-button {
  opacity: 0.45;
}

body.hints-disabled .hint-button {
  opacity: 0.4;
}

/* スコア＆タイマーボード */
.dashboard {
  width: 100%;
  background: var(--card-bg);
  border-radius: 14px;
  padding: 9px 12px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.player-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 10px;
  transition: all 0.25s ease;
}

body.turn-player .player-card-you,
body.turn-ai .player-card-ai {
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 1.5px var(--accent);
}

.player-card.ai {
  flex-direction: row-reverse;
  text-align: right;
}

.badge-icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
}

.badge-icon.black-badge {
  background: #111827;
  border: 2px solid #374151;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.25);
}

.badge-icon.white-badge {
  background: #f8fafc;
  border: 2px solid #cbd5e1;
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.25);
}

.player-info {
  display: flex;
  flex-direction: column;
}

.player-name {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.player-score {
  font-size: 1.35rem;
  font-weight: 800;
  line-height: 1.1;
}

/* 中央タイマー表示 */
.center-timer-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 8px;
}

.timer-badge {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 1.15rem;
  font-weight: 800;
  color: #38bdf8;
  letter-spacing: 0.05em;
  transition: all 0.2s ease;
}

body.timer-danger .timer-badge {
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.5);
  background: rgba(239, 68, 68, 0.15);
  animation: pulse-danger 0.5s infinite alternate;
}

@keyframes pulse-danger {
  from { transform: scale(1); box-shadow: 0 0 4px rgba(239, 68, 68, 0.4); }
  to { transform: scale(1.08); box-shadow: 0 0 14px rgba(239, 68, 68, 0.85); }
}

.timer-label {
  font-size: 0.62rem;
  color: var(--text-muted);
  margin-top: 3px;
  font-weight: 600;
}

/* ステータスバー */
.status-bar {
  width: 100%;
  text-align: center;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.status-msg {
  display: none;
}

body.turn-player:not(.game-over) .status-msg-player,
body.turn-ai:not(.game-over) .status-msg-ai,
body.game-over:not(.game-timeout) .status-msg-gameover,
body.game-timeout .status-msg-timeout {
  display: inline;
}

.thinking-spinner {
  display: none;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

body.turn-ai:not(.game-over) .thinking-spinner {
  display: inline-block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* オセロ盤 */
.board-wrapper {
  position: relative;
  width: min(88vw, 380px, 45dvh);
  height: min(88vw, 380px, 45dvh);
  padding: 6px;
  background: #044e3b;
  border-radius: 14px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4), inset 0 2px 4px rgba(255, 255, 255, 0.1);
  border: 2px solid #064e3b;
}

.board {
  width: 100%;
  height: 100%;
  background: var(--board-line);
  border-radius: 10px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 2px;
  overflow: hidden;
}

.cell {
  background-color: var(--board-bg);
  border: none;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background-color 0.12s ease;
}

.cell:hover {
  background-color: #076d50;
}

.cell.has-star::after {
  content: "";
  position: absolute;
  bottom: -3.5px;
  right: -3.5px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.45);
  pointer-events: none;
  z-index: 1;
}

.valid-hint {
  display: none;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.28);
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.35);
  pointer-events: none;
  animation: pulse 1.4s infinite ease-in-out;
}

body.hints-enabled .cell.is-hint .valid-hint {
  display: block;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.9); }
  50% { opacity: 0.85; transform: scale(1.15); }
}

.disc {
  display: none;
  width: 84%;
  height: 84%;
  border-radius: 50%;
  position: relative;
  perspective: 800px;
  transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  transform-style: preserve-3d;
  z-index: 2;
  pointer-events: none;
}

.cell.is-black .disc,
.cell.is-white .disc {
  display: block;
}

.disc-inner {
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
}

.cell.is-black .disc-inner {
  background: radial-gradient(circle at 35% 35%, #374151 0%, #111827 65%, #030712 100%);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.cell.is-white .disc-inner {
  background: radial-gradient(circle at 35% 35%, #ffffff 0%, #f1f5f9 65%, #cbd5e1 100%);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.cell.is-flipping .disc {
  animation: flip-3d 0.4s ease-in-out;
}

@keyframes flip-3d {
  0% { transform: scale(1) rotateY(0); }
  50% { transform: scale(1.15) rotateY(90deg); }
  100% { transform: scale(1) rotateY(180deg); }
}

.last-move-marker {
  display: none;
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #ef4444;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 6px #ef4444;
  z-index: 4;
  pointer-events: none;
}

.cell.is-last-move .last-move-marker {
  display: block;
}

/* ピクセルアート演出エリア */
.pixel-stage-card {
  width: 100%;
  background: #111827;
  border-radius: 14px;
  padding: 7px 10px;
  border: 2px solid #1f2937;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5), var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
  overflow: hidden;
}

.stage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.68rem;
  color: #9ca3af;
  font-family: monospace;
  padding: 0 4px;
}

.stage-header-title {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #e5e7eb;
  font-weight: 700;
}

.stage-msg {
  display: none;
  font-weight: 700;
}

body.chara-panicking .stage-msg-panicking {
  display: inline;
  color: #f87171;
}

body.chara-relieved .stage-msg-relieved {
  display: inline;
  color: #34d399;
}

body.chara-exploded .stage-msg-exploded {
  display: inline;
  color: #ef4444;
}

.canvas-container {
  width: 100%;
  height: 76px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

#pixelCanvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 8px;
  background: #090d16;
}

/* フッターアクション */
.actions {
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 2px;
}

.btn {
  background: var(--card-bg);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 9px 18px;
  border-radius: 10px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: all 0.2s ease;
}

.btn:hover {
  background: #334155;
  transform: translateY(-1px);
}

.btn img {
  width: 15px;
  height: 15px;
  display: block;
}

.btn-primary {
  background: var(--accent);
  color: #022c22;
  border: none;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

/* トースト */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(-100px);
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: var(--text-main);
  padding: 8px 18px;
  border-radius: 9999px;
  font-size: 0.82rem;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  z-index: 100;
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
}

body.toast-visible .toast {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast-msg {
  display: none;
}

body.toast-cpu-pass .toast-msg-cpu-pass,
body.toast-player-pass .toast-msg-player-pass,
body.toast-hint-on .toast-msg-hint-on,
body.toast-hint-off .toast-msg-hint-off,
body.toast-sound-on .toast-msg-sound-on,
body.toast-sound-off .toast-msg-sound-off {
  display: inline;
}

/* モーダル */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(11, 17, 32, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  padding: 16px;
}

body.modal-active .modal-overlay {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--card-bg);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 18px;
  padding: 24px 20px;
  width: 100%;
  max-width: 330px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  transform: scale(0.9);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

body.modal-active .modal-card {
  transform: scale(1);
}

.modal-result-block {
  display: none;
}

body.result-win .modal-result-win,
body.result-lose .modal-result-lose,
body.result-draw .modal-result-draw,
body.result-timeout .modal-result-timeout {
  display: block;
}

.modal-result-icon {
  font-size: 2.6rem;
  margin-bottom: 8px;
}

.modal-title {
  font-size: 1.4rem;
  font-weight: 800;
  margin-bottom: 4px;
}

.modal-desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 18px;
}

.modal-score-box {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 12px;
  padding: 12px 10px;
  margin-bottom: 20px;
}

.modal-score-col {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.modal-score-val {
  font-size: 1.6rem;
  font-weight: 800;
}

.modal-score-lbl {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.modal-score-dash {
  font-weight: 700;
  color: #64748b;
}

.modal-action-btn {
  width: 100%;
  justify-content: center;
}

.modal-action-btn-primary {
  margin-bottom: 8px;
}

@media (max-height: 700px) {
  .container {
    gap: 6px;
  }
  .board-wrapper {
    width: min(80vw, 330px, 42dvh);
    height: min(80vw, 330px, 42dvh);
  }
  .canvas-container {
    height: 64px;
  }
  .pixel-title {
    font-size: 2.5rem;
  }
}
