:root {
  --bg-page: #f5f0e1;
  --bg-panel: #ffffff;
  --bg-grid: #faf8f5;
  --bg-hit: #e8f5e9;
  --bg-target: #fff3cd;
  --red: #c0392b;
  --yellow: #e6b422;
  --green: #27ae60;
  --wood: #8b5e3c;
  --text: #3e2723;
  --text-sub: #8d6e63;
  --line: #d4c5a9;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.04);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --nav-h: 48px;
  --tab-h: 50px;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  background: #fff;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: #fff;
  color: var(--text);
  font: 16px/1.6 "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button,
a {
  font: inherit;
}

button {
  border: 0;
  background: none;
  color: inherit;
}

a {
  color: var(--wood);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

canvas {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
}

#attentionChart,
#trendChart {
  aspect-ratio: 640 / 260;
}

.mini-chart {
  aspect-ratio: 320 / 120;
}

.record-chart {
  aspect-ratio: 320 / 140;
}

.app-shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: #fff;
}

.top-nav {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  height: var(--nav-h);
  min-height: calc(var(--nav-h) + var(--safe-top));
  padding: var(--safe-top) 12px 0;
  background: #fff;
  border-bottom: 1px solid rgba(212, 197, 169, 0.4);
}

.nav-title {
  flex: 1;
  text-align: center;
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}

.nav-icon {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--wood);
  border-radius: 12px;
  cursor: pointer;
  flex-shrink: 0;
}

.nav-icon svg {
  width: 22px;
  height: 22px;
}

.nav-icon:active {
  background: rgba(139, 94, 60, 0.08);
}

.back-link:hover {
  text-decoration: none;
}

#soundToggle .sound-on {
  display: none;
}

#soundToggle .sound-off {
  display: block;
}

#soundToggle.is-on .sound-on {
  display: block;
}

#soundToggle.is-on .sound-off {
  display: none;
}

.app-main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 10px 16px 0;
  overflow: visible;
  background: var(--bg-page);
}

.training-pane,
.tab-pane {
  min-height: 0;
}

.training-pane {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1 1 auto;
  overflow: visible;
  padding-bottom: 12px;
}

.training-pane::-webkit-scrollbar,
.tab-pane::-webkit-scrollbar {
  width: 0;
  height: 0;
}

body[data-tab="analysis"] .training-pane,
body[data-tab="records"] .training-pane,
body[data-tab="tutorial"] .training-pane {
  flex: 0 0 0;
  overflow: hidden;
  padding: 0;
}

.hero-copy {
  text-align: center;
  padding: 2px 4px 0;
}

.hero-copy h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.35;
  font-weight: 700;
  color: var(--text);
}

.hero-sub {
  margin: 4px 0 0;
  font-size: 16px;
  color: var(--text-sub);
}

.difficulty-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}

.difficulty-strip::-webkit-scrollbar {
  display: none;
}

.difficulty-btn {
  flex: 0 0 auto;
  min-height: 40px;
  padding: 9px 14px;
  border-radius: 999px;
  border: 1px solid rgba(141, 110, 99, 0.32);
  background: rgba(255, 255, 255, 0.9);
  color: var(--text-sub);
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
}

.difficulty-btn.active {
  background: var(--wood);
  border-color: var(--wood);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

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

.grid-card,
.analysis-card,
.chart-card,
.insight-card,
.metric-item,
.history-item,
.info-section,
.pane-title,
.trend-card {
  background: var(--bg-panel);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.grid-card {
  position: relative;
  padding: 18px 16px 16px;
}

.timer-panel {
  text-align: center;
  margin-bottom: 16px;
}

.timer-value {
  font: 600 28px/1 "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  color: var(--text);
  letter-spacing: 0.04em;
}

.timer-tip {
  margin: 8px 0 0;
  color: var(--text-sub);
}

.grid-stage {
  position: relative;
}

.grid-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), minmax(0, 1fr));
  gap: 0;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  background:
    linear-gradient(rgba(212, 197, 169, 0.12), rgba(212, 197, 169, 0.12)),
    var(--bg-grid);
}

.grid-board.is-error {
  animation: boardError 0.3s ease;
}

@keyframes boardError {
  0%, 100% { box-shadow: inset 0 0 0 0 rgba(192, 57, 43, 0); }
  50% { box-shadow: inset 0 0 0 999px rgba(192, 57, 43, 0.12); }
}

.grid-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  background: rgba(250, 248, 245, 0.95);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--text);
  font-weight: 600;
  font-size: clamp(20px, calc(110px / var(--grid-size) + 6px), 24px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.12s ease, box-shadow 0.18s ease, color 0.18s ease;
}

.grid-cell[data-hidden="true"] {
  display: none;
}

.grid-cell:active {
  transform: scale(0.97);
}

.grid-cell.is-hit {
  background: var(--bg-hit);
  color: var(--green);
}

.grid-cell.is-target {
  background: linear-gradient(180deg, rgba(255, 243, 205, 0.96), rgba(250, 248, 245, 0.98));
  box-shadow: inset 0 0 0 2px rgba(230, 180, 34, 0.8);
}

.grid-cell.is-wrong {
  background: rgba(255, 230, 227, 0.98);
  color: var(--red);
}

.grid-cell[data-edge-right="true"] {
  border-right: 0;
}

.progress-panel {
  margin-top: 16px;
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--text-sub);
}

.progress-track {
  position: relative;
  margin-top: 8px;
  height: 10px;
  border-radius: 999px;
  background: rgba(212, 197, 169, 0.4);
  overflow: hidden;
}

.progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #27ae60 0%, #e6b422 100%);
  transition: width 0.2s ease;
}

.result-layer {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(245, 240, 225, 0.7);
  backdrop-filter: blur(8px);
  z-index: 3;
}

.result-layer[hidden] {
  display: none;
}

.result-card {
  width: 100%;
  border-radius: 18px;
  background: #fff;
  padding: 18px 16px;
  box-shadow: 0 10px 30px rgba(62, 39, 35, 0.16);
  animation: popIn 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

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

.result-kicker {
  margin: 0;
  color: var(--red);
  font-weight: 600;
}

.result-title {
  margin: 6px 0 14px;
  font-size: 24px;
  line-height: 1.25;
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.result-stat {
  padding: 12px 10px;
  border-radius: 12px;
  background: rgba(245, 240, 225, 0.95);
  text-align: center;
}

.result-stat span {
  display: block;
  color: var(--text-sub);
}

.result-stat strong {
  display: block;
  margin-top: 6px;
  font-size: 18px;
  color: var(--text);
}

.result-actions,
.action-strip {
  display: flex;
  gap: 10px;
}

.result-actions {
  margin-top: 14px;
  flex-wrap: wrap;
}

.primary-btn,
.ghost-btn {
  min-height: 48px;
  border-radius: 14px;
  padding: 0 16px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.18s ease, background 0.18s ease, color 0.18s ease, border-color 0.18s ease;
}

.primary-btn {
  flex: 1 1 0;
  background: var(--red);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

.ghost-btn {
  flex: 1 1 0;
  border: 1px solid rgba(139, 94, 60, 0.32);
  background: rgba(255, 255, 255, 0.92);
  color: var(--wood);
}

.primary-btn:active,
.ghost-btn:active {
  transform: scale(0.98);
}

.analysis-card {
  overflow: hidden;
}

.analysis-card summary {
  padding: 14px 16px;
  list-style: none;
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  border-bottom: 1px solid transparent;
}

.analysis-card summary::-webkit-details-marker {
  display: none;
}

.analysis-card[open] summary {
  border-bottom-color: rgba(212, 197, 169, 0.45);
}

.analysis-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.chart-card,
.trend-card,
.pane-title,
.insight-card,
.info-section {
  padding: 16px;
}

.chart-head h2,
.pane-title h2,
.section-head h3,
.insight-card h3,
.info-section h2,
.faq-item h3 {
  margin: 0;
  line-height: 1.35;
}

.chart-head p,
.pane-title p,
.insight-card p,
.info-section p,
.info-section li,
.faq-item p,
.section-head span,
#historyHint,
#trendSummary {
  margin: 8px 0 0;
  color: var(--text-sub);
}

#attentionChart,
#trendChart,
.mini-chart,
.record-chart {
  margin-top: 14px;
  border-radius: 12px;
  background:
    linear-gradient(to bottom, rgba(212, 197, 169, 0.14), rgba(212, 197, 169, 0.02) 40%, rgba(255, 255, 255, 0.96)),
    #fff;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.large-metric-grid {
  margin-top: 12px;
}

.metric-item {
  padding: 14px 12px;
}

.metric-item span {
  display: block;
  color: var(--text-sub);
}

.metric-item strong {
  display: block;
  margin-top: 8px;
  font-size: 20px;
  color: var(--text);
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 12px;
}

.history-item {
  overflow: hidden;
}

.history-item summary {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  list-style: none;
}

.history-item summary::-webkit-details-marker {
  display: none;
}

.history-main,
.history-side,
.history-detail-time,
.history-detail-rating {
  color: var(--text-sub);
}

.history-main {
  font-weight: 600;
  color: var(--text);
}

.history-side {
  text-align: right;
  flex-shrink: 0;
}

.history-item[open] summary {
  border-bottom: 1px solid rgba(212, 197, 169, 0.4);
}

.history-item canvas {
  margin: 0 16px 16px;
  width: calc(100% - 32px);
}

.history-detail {
  padding: 12px 0 0;
}

.history-detail-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
}

.empty-state {
  padding: 16px;
  border-radius: 12px;
  text-align: center;
  color: var(--text-sub);
  background: rgba(255, 255, 255, 0.75);
}

.tab-pane {
  display: none;
  flex: 1 1 auto;
  overflow: visible;
  padding-bottom: 12px;
}

body[data-tab="analysis"] .analysis-pane,
body[data-tab="records"] .records-pane,
body[data-tab="tutorial"] .tutorial-pane {
  display: block;
}

.pane-inner {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.prose-pane {
  gap: 14px;
}

.info-list {
  margin: 12px 0 0;
  padding-left: 1.25em;
  color: var(--text-sub);
}

.info-list li + li {
  margin-top: 8px;
}

.faq-section .faq-item + .faq-item {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px dashed rgba(212, 197, 169, 0.7);
}

.action-strip {
  flex-shrink: 0;
}

.ad-slot {
  display: none;
  min-height: 60px;
  max-height: 60px;
  margin: 10px 0 0;
  color: #999;
  font-size: 16px;
  border-radius: 8px;
  border: 1px dashed #ccc;
  background: #fafafa;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.ad-slot:not(:empty) {
  display: flex;
  flex: 0 0 60px;
}

.site-footer {
  padding: 16px 16px calc(10px + var(--safe-bottom));
  color: var(--text-sub);
  text-align: center;
  background: var(--bg-page);
  font-size: 16px;
  line-height: 1.7;
}

.site-footer p {
  margin: 0;
}

.site-footer p + p {
  margin-top: 6px;
}

.tab-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  order: 999;
  flex-shrink: 0;
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: #fff;
  border-top: 1px solid rgba(212, 197, 169, 0.45);
  box-shadow: 0 -2px 12px rgba(62, 39, 35, 0.04);
  z-index: 40;
}

.tab-btn {
  flex: 1 1 0;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  color: var(--text-sub);
}

.tab-btn.active {
  color: var(--red);
}

.tab-icon {
  font-size: 18px;
  line-height: 1;
}

.tab-text {
  font-size: 16px;
  line-height: 1;
}

@media (max-width: 767px) {
  .hero-copy {
    padding-top: 0;
  }

  .hero-sub {
    display: none;
  }

  .grid-card {
    padding-bottom: 12px;
  }

  .analysis-card {
    order: 6;
  }

  .action-strip {
    order: 5;
    position: sticky;
    bottom: 10px;
    z-index: 8;
    margin-top: -2px;
    padding: 10px;
    border-radius: 16px;
    background: rgba(245, 240, 225, 0.94);
    box-shadow: 0 -2px 14px rgba(62, 39, 35, 0.08);
    backdrop-filter: blur(10px);
  }

  .training-pane {
    padding-bottom: 18px;
  }
}

@media (min-width: 768px) {
  html {
    background:
      radial-gradient(circle at 15% 20%, rgba(230, 180, 34, 0.08), transparent 28%),
      radial-gradient(circle at 85% 20%, rgba(39, 174, 96, 0.06), transparent 26%),
      var(--bg-page);
  }

  body {
    background:
      radial-gradient(circle at 15% 20%, rgba(230, 180, 34, 0.08), transparent 28%),
      radial-gradient(circle at 85% 20%, rgba(39, 174, 96, 0.06), transparent 26%),
      var(--bg-page);
  }

  .app-shell {
    width: 100%;
    max-width: none;
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: transparent;
    box-shadow: none;
  }

  .top-nav,
  .site-footer {
    padding-left: max(20px, calc((100vw - 1200px) / 2));
    padding-right: max(20px, calc((100vw - 1200px) / 2));
  }

  .app-main {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 18px 20px 0;
  }

  .grid-card,
  .analysis-card,
  .chart-card,
  .insight-card,
  .history-item,
  .info-section,
  .pane-title,
  .trend-card {
    border-radius: 18px;
  }

  .grid-card {
    padding: 20px;
  }

  .training-pane,
  .tab-pane {
    width: 100%;
  }

  .result-card {
    max-width: 380px;
    margin: 0 auto;
  }
}

@media (min-width: 1024px) {
  .training-pane {
    display: grid;
    grid-template-columns: minmax(0, 560px) minmax(320px, 1fr);
    grid-template-areas:
      "hero analysis"
      "difficulty analysis"
      "grid analysis"
      "actions analysis"
      "ad analysis";
    align-items: start;
    column-gap: 20px;
    row-gap: 12px;
    overflow: hidden;
    padding-bottom: 18px;
  }

  .hero-copy {
    grid-area: hero;
    text-align: left;
    padding: 0;
  }

  .hero-copy h1 {
    font-size: 28px;
  }

  .hero-sub {
    margin-top: 6px;
  }

  .difficulty-strip {
    grid-area: difficulty;
    flex-wrap: wrap;
    overflow: visible;
    padding-bottom: 0;
  }

  .grid-card {
    grid-area: grid;
    padding: 18px;
  }

  .analysis-card {
    grid-area: analysis;
    display: flex;
    flex-direction: column;
    max-height: min(720px, calc(100dvh - 140px));
    overflow: hidden;
  }

  .analysis-card summary {
    flex: 0 0 auto;
  }

  .analysis-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
  }

  .action-strip {
    grid-area: actions;
  }

  .ad-slot {
    grid-area: ad;
  }

  .result-card {
    max-width: 420px;
  }

  #trendChart {
    height: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
