/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #e0e7ff;
  --x-color: #ef4444;
  --o-color: #3b82f6;
  --win-color: #f59e0b;
  --success: #22c55e;
  --bg: #f9fafb;
  --surface: #ffffff;
  --surface-2: #f3f4f6;
  --text: #1f2937;
  --text-2: #6b7280;
  --border: #e5e7eb;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.06);
  --shadow: 0 1px 3px rgba(0,0,0,.10), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,.10), 0 2px 4px -2px rgba(0,0,0,.06);
  --radius: 10px;
  --radius-sm: 6px;
}

html { height: 100%; }

body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
               'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== App Container ===== */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ===== Header ===== */
.app-header {
  flex-shrink: 0;
  background: var(--primary);
  color: #fff;
  padding: 14px 16px 12px;
  padding-top: calc(14px + env(safe-area-inset-top));
  text-align: center;
  user-select: none;
}

.app-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.3px;
  line-height: 1.3;
}

.app-header h2 {
  font-size: 0.8125rem;
  font-weight: 400;
  opacity: 0.82;
  margin-top: 3px;
}

/* ===== Ad Slot (CSS-only show/hide) ===== */
.ad-slot {
  display: none;
  min-height: 60px;
  max-height: 60px;
  margin: 8px 16px;
  color: #aaa;
  font-size: 14px;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}

.ad-slot:not(:empty) {
  display: flex;
  flex: 0 0 60px;
}

/* ===== Main Area ===== */
.app-main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  min-height: 0;
}

/* ===== Game Area ===== */
.game-area {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 16px 12px;
  gap: 12px;
  overflow: hidden;
  min-height: 0;
}

.game-status {
  font-size: 1rem;
  font-weight: 600;
  color: var(--primary);
  text-align: center;
  min-height: 1.5em;
  letter-spacing: 0.2px;
}

#gameCanvas {
  display: block;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  background: var(--surface);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  max-width: 100%;
  max-height: calc(100vh - 260px);
  width: 400px;
  height: 400px;
}

.game-controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

/* ===== Decision Panel ===== */
.decision-panel {
  flex: 0 0 256px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 16px;
  gap: 16px;
}

.panel-title {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.decision-hint {
  font-size: 0.8125rem;
  color: var(--text-2);
  margin-bottom: 6px;
}

.decision-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.decision-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid transparent;
  font-size: 0.875rem;
  transition: background 0.18s, border-color 0.18s;
}

.cell-pos {
  color: var(--text-2);
  font-size: 0.8125rem;
  font-weight: 500;
}

.cell-score {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-size: 0.875rem;
  color: var(--text-2);
}

/* Decision item states */
.decision-item.best {
  background: #dcfce7;
  border-color: #86efac;
}
.decision-item.best .cell-score { color: #16a34a; }

.decision-item.score-positive .cell-score { color: #22c55e; }
.decision-item.score-negative .cell-score { color: #ef4444; }
.decision-item.score-neutral  .cell-score { color: #d97706; }

.decision-item.occupied {
  opacity: 0.38;
}

/* Stats Section */
.stats-section {
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label {
  font-size: 0.6875rem;
  color: var(--text-2);
}

.stat-value {
  font-size: 1.375rem;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.stat-value.win  { color: #22c55e; }
.stat-value.lose { color: #ef4444; }
.stat-value.draw { color: #d97706; }

/* ===== Article Content ===== */
.article-content {
  display: none;
}

.content-section {
  display: none;
}

.content-section.active {
  display: block;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 18px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
  min-height: 36px;
  line-height: 1;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover  { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-primary:active { background: #3730a3; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}
.btn-secondary:hover  { background: var(--surface-2); }
.btn-secondary:active { background: var(--border); }

.btn-outline {
  background: transparent;
  color: var(--primary);
  border-color: var(--primary);
}
.btn-outline:hover  { background: var(--primary-light); }
.btn-outline:active { background: #c7d2fe; }

.btn-sm {
  padding: 5px 12px;
  font-size: 0.8125rem;
  min-height: 30px;
}

.btn-lg {
  padding: 12px 28px;
  font-size: 1rem;
  min-height: 44px;
}

.btn-danger {
  background: #fee2e2;
  color: #dc2626;
  border-color: #fecaca;
}
.btn-danger:hover { background: #fecaca; }

/* ===== Result Overlay ===== */
.result-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.52);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.result-overlay.visible {
  display: flex;
}

.result-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 36px 28px 28px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
  max-width: 300px;
  width: 100%;
  animation: popIn 0.25s cubic-bezier(0.34,1.56,0.64,1) both;
}

@keyframes popIn {
  from { transform: scale(0.8); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

.result-icon {
  font-size: 3rem;
  line-height: 1;
  margin-bottom: 14px;
}

.result-text {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 22px;
  color: var(--text);
}

/* ===== PC Footer ===== */
.pc-footer {
  flex-shrink: 0;
  text-align: center;
  padding: 14px 16px;
  font-size: 0.8125rem;
  color: var(--text-2);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.pc-footer a {
  color: var(--text-2);
  text-decoration: none;
  margin: 0 6px;
}
.pc-footer a:hover { color: var(--primary); text-decoration: underline; }

/* ===== Tab Bar ===== */
.tab-bar {
  flex-shrink: 0;
  order: 999;
  display: none;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  position: relative;
  z-index: 50;
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 7px 4px 6px;
  gap: 3px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-2);
  font-size: 0.6875rem;
  font-weight: 500;
  transition: color 0.15s;
  min-height: 52px;
  -webkit-tap-highlight-color: transparent;
}

.tab-btn.active {
  color: var(--primary);
}

.tab-icon {
  font-size: 1.25rem;
  line-height: 1;
  display: block;
}

/* ===== Mobile Footer ===== */
.mobile-footer {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  text-align: center;
  font-size: 0.8125rem;
  color: var(--text-2);
}

.mobile-footer a {
  color: var(--text-2);
  text-decoration: none;
  margin: 0 4px;
}
.mobile-footer a:hover { color: var(--primary); }

/* ===== Records Card ===== */
.records-card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  margin-bottom: 12px;
}

.records-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9375rem;
}

.records-row:last-child { border-bottom: none; }

.records-row strong { font-weight: 700; }
.records-row strong.win  { color: #22c55e; }
.records-row strong.lose { color: #ef4444; }
.records-row strong.draw { color: #d97706; }

.records-tip {
  font-size: 0.8125rem;
  color: var(--text-2);
}

/* ===== PC Layout (≥769px) ===== */
@media (min-width: 769px) {
  .app-container {
    min-height: 100vh;
    overflow-y: auto;
  }

  .app-main {
    flex-direction: row;
    flex: 0 0 auto;
    height: calc(100vh - 80px); /* 减去 header 高度约 80px */
    overflow: hidden;
    width: 100%;
    max-width: 1024px;
    align-self: center;
    padding-top: 16px;
    align-items: flex-start;
  }

  .game-area {
    flex: 1 1 auto;
    padding: 20px;
    min-width: 0;
    align-self: center;
    padding-top: 0;
  }

  #gameCanvas {
    max-height: calc(100vh - 220px);
  }

  .decision-panel {
    display: flex;
    flex: 0 0 280px;
    align-self: flex-start;
    border-left: none;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    max-height: calc(100vh - 120px);
  }

  .tab-bar  { display: none !important; }
  .pc-footer { display: block; }

  /* 文案区域在 PC 端显示于游戏区域下方 */
  .article-content {
    display: block;
    padding: 32px 40px;
    max-width: 960px;
    margin: 0 auto;
    width: 100%;
  }

  .article-content .content-section {
    display: block;
    margin-bottom: 40px;
  }

  .mobile-footer { display: none; }
}

/* ===== Mobile Layout (≤768px) ===== */
@media (max-width: 768px) {
  .app-container {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .tab-bar { display: flex; }
  .pc-footer { display: none; }

  /* ── App main states ── */
  .app-main {
    flex-direction: column;
    flex: 1 1 auto;
    overflow: hidden;
  }

  .app-main.main-collapse {
    flex: 0 0 0 !important;
    overflow: hidden;
    min-height: 0;
  }

  /* Article content when visible on mobile */
  .article-content.visible {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    overflow-y: auto;
    padding: 16px;
    gap: 20px;
  }

  /* Decision tab: hide game area, show decision panel */
  .app-main.tab-decision .game-area {
    display: none;
  }

  .app-main.tab-decision .decision-panel {
    display: flex;
    flex: 1 1 auto;
    border-left: none;
    border-top: 1px solid var(--border);
    overflow-y: auto;
  }

  /* Hide decision panel in game tab */
  .decision-panel {
    display: none;
  }

  .game-area {
    padding: 10px 12px 8px;
    gap: 10px;
  }

  #gameCanvas {
    max-height: calc(100dvh - 230px);
    width: 100% !important;
    height: auto !important;
    aspect-ratio: 1;
  }

  .game-status {
    font-size: 0.9375rem;
  }
}

/* ===== Article Typography ===== */
.article-content h2 {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 14px;
}

.article-content h3 {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
  margin: 18px 0 8px;
}

.article-content h3:first-child {
  margin-top: 0;
}

.article-content p {
  font-size: 0.9375rem;
  color: var(--text);
  line-height: 1.75;
  margin-bottom: 10px;
}

.article-content ul {
  padding-left: 18px;
  margin-bottom: 12px;
}

.article-content li {
  font-size: 0.9375rem;
  line-height: 1.75;
  margin-bottom: 6px;
  color: var(--text);
}

.article-content dl {
  margin-bottom: 12px;
}

.article-content dt {
  font-weight: 600;
  color: var(--text);
  margin-top: 14px;
  font-size: 0.9375rem;
}

.article-content dd {
  margin-left: 0;
  padding-left: 14px;
  border-left: 3px solid var(--primary-light);
  color: var(--text-2);
  font-size: 0.9375rem;
  margin-top: 4px;
  line-height: 1.7;
}

/* ===== Scrollbar ===== */
.decision-panel::-webkit-scrollbar,
.article-content::-webkit-scrollbar {
  width: 4px;
}
.decision-panel::-webkit-scrollbar-thumb,
.article-content::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}
