:root {
  color-scheme: light;
  --bg: #f3f6f2;
  --surface: #ffffff;
  --surface-soft: #e9f0eb;
  --surface-warm: #fff6e8;
  --ink: #17231d;
  --muted: #627168;
  --line: #d9e1db;
  --primary: #285f48;
  --primary-dark: #1d4936;
  --primary-soft: #dbeae1;
  --accent: #e49a3d;
  --danger: #a54238;
  --shadow: 0 18px 52px rgba(29, 55, 40, 0.09);
  --radius-lg: 28px;
  --radius-md: 18px;
  --radius-sm: 12px;
  --board-size: min(88vw, 570px);
  font-family: Inter, "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--bg); scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 4%, rgba(228, 154, 61, 0.11), transparent 24rem),
    radial-gradient(circle at 96% 12%, rgba(40, 95, 72, 0.10), transparent 26rem),
    var(--bg);
}

button, input, select { font: inherit; }
button { color: inherit; }
button:not(:disabled) { cursor: pointer; }

.skip-link {
  position: absolute;
  left: 1rem;
  top: -4rem;
  z-index: 100;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  color: #fff;
  background: var(--primary);
}
.skip-link:focus { top: 1rem; }

.app-shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; }

.app-header {
  min-height: 82px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  width: 42px;
  height: 42px;
  position: relative;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: inset 0 0 0 8px rgba(255, 255, 255, 0.10);
}
.brand-mark::before, .brand-mark::after, .brand-mark span {
  content: "";
  position: absolute;
  border: 1.5px solid rgba(255,255,255,.9);
  border-radius: 50%;
}
.brand-mark::before { width: 26px; height: 26px; }
.brand-mark::after { width: 15px; height: 15px; }
.brand-mark span { width: 4px; height: 4px; background: #fff; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 1.03rem; letter-spacing: .04em; }
.brand small { margin-top: 1px; color: var(--muted); font-size: .72rem; letter-spacing: .08em; }

.header-actions { display: flex; gap: .5rem; }
.icon-button, .text-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  padding: .65rem .8rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.72);
}
.icon-button:hover, .text-button:hover { background: var(--surface); border-color: #bdc9c0; }

.view { padding: 2.25rem 0 1rem; }
.hero-grid { display: grid; grid-template-columns: minmax(0, .9fr) minmax(520px, 1.1fr); align-items: center; gap: 4rem; }
.intro-panel { padding: 1.5rem 0; }
.eyebrow, .step-label {
  margin: 0 0 .55rem;
  color: var(--primary);
  font-size: .77rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}
h1, h2, h3, p { margin-top: 0; }
h1 { max-width: 11ch; margin-bottom: 1.25rem; font-size: clamp(2.45rem, 5vw, 4.8rem); line-height: 1.04; letter-spacing: -.045em; }
h2 { margin-bottom: 0; font-size: clamp(1.45rem, 2.5vw, 2rem); letter-spacing: -.025em; }
h3 { margin: 0; font-size: 1rem; }
.intro-copy { max-width: 42rem; margin-bottom: 2rem; color: var(--muted); font-size: 1.03rem; line-height: 1.8; }

.profile-strip {
  max-width: 510px;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  border: 1px solid rgba(40, 95, 72, 0.12);
  border-radius: var(--radius-md);
  background: rgba(255,255,255,.55);
}
.level-orb {
  width: 70px;
  height: 70px;
  flex: 0 0 auto;
  display: grid;
  place-content: center;
  text-align: center;
  border-radius: 50%;
  color: var(--primary-dark);
  background: var(--primary-soft);
}
.level-orb span { color: var(--muted); font-size: .67rem; }
.level-orb strong { font-size: 1.45rem; }
.xp-block { min-width: 0; flex: 1; }
.xp-copy { display: flex; justify-content: space-between; gap: .75rem; margin-bottom: .6rem; color: var(--muted); font-size: .78rem; }
.progress-track { height: 8px; overflow: hidden; border-radius: 99px; background: #d9e3dc; }
.progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--primary); transition: width .35s ease; }

.setup-card {
  padding: clamp(1.15rem, 3vw, 2rem);
  border: 1px solid rgba(40, 95, 72, 0.10);
  border-radius: var(--radius-lg);
  background: rgba(255,255,255,.9);
  box-shadow: var(--shadow);
}
.section-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; }
.choice-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .7rem; }
.choice-card {
  min-width: 0;
  min-height: 158px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  gap: .9rem;
  padding: 1rem;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  transition: transform .16s ease, border-color .16s ease, background .16s ease;
}
.choice-card:hover { transform: translateY(-2px); border-color: #a9bcae; }
.choice-card.is-selected { border-color: var(--primary); background: var(--primary-soft); box-shadow: inset 0 0 0 1px var(--primary); }
.choice-card strong, .choice-card small { display: block; }
.choice-card strong { margin-bottom: .3rem; font-size: .92rem; }
.choice-card small { color: var(--muted); font-size: .7rem; line-height: 1.45; }

.layout-preview { width: 56px; height: 56px; position: relative; display: block; }
.grid-preview { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; }
.grid-preview i { border-radius: 4px; background: var(--primary); opacity: .74; }
.ring-preview i { position: absolute; inset: 5px; border: 2px solid var(--primary); border-radius: 50%; }
.ring-preview i:nth-child(2) { inset: 14px; opacity: .75; }
.ring-preview i:nth-child(3) { inset: 23px; background: var(--primary); }
.butterfly-preview i { position: absolute; width: 25px; height: 31px; border-radius: 70% 30% 62% 38%; background: var(--primary); opacity: .72; }
.butterfly-preview i:nth-child(1) { left: 2px; top: 1px; transform: rotate(21deg); }
.butterfly-preview i:nth-child(2) { right: 2px; top: 1px; transform: scaleX(-1) rotate(21deg); }
.butterfly-preview i:nth-child(3) { left: 9px; bottom: 1px; width: 18px; height: 23px; transform: rotate(-17deg); }
.butterfly-preview i:nth-child(4) { right: 9px; bottom: 1px; width: 18px; height: 23px; transform: scaleX(-1) rotate(-17deg); }

.difficulty-heading { display: flex; align-items: baseline; gap: .75rem; margin: 1.4rem 0 .7rem; }
.difficulty-heading .step-label { margin: 0; }
.segmented-control { display: grid; grid-template-columns: repeat(3, 1fr); padding: 4px; border-radius: 15px; background: var(--surface-soft); }
.segmented-control button { min-height: 54px; border: 0; border-radius: 11px; background: transparent; }
.segmented-control button strong, .segmented-control button small { display: block; }
.segmented-control button small { margin-top: 2px; color: var(--muted); font-size: .69rem; }
.segmented-control button.is-selected { color: var(--primary-dark); background: var(--surface); box-shadow: 0 3px 12px rgba(31,60,42,.10); }

.primary-button, .secondary-button, .danger-button {
  min-height: 48px;
  padding: .78rem 1.15rem;
  border-radius: 14px;
  font-weight: 700;
}
.primary-button { border: 1px solid var(--primary); color: #fff; background: var(--primary); }
.primary-button:hover { background: var(--primary-dark); }
.secondary-button { border: 1px solid var(--line); background: var(--surface); }
.secondary-button:hover { background: var(--surface-soft); }
.danger-button { border: 1px solid #e3bcb7; color: var(--danger); background: #fff1ef; }
.full-button { width: 100%; }
.start-button { width: 100%; margin-top: 1rem; display: flex; align-items: center; justify-content: center; gap: .55rem; }

.stats-section { margin: 5rem 0 3rem; padding-top: 2rem; border-top: 1px solid var(--line); }
.with-action { align-items: center; }
.quiet-stat { color: var(--muted); font-size: .8rem; }
.empty-state { min-height: 150px; display: grid; place-content: center; justify-items: center; color: var(--muted); text-align: center; }
.empty-state p { margin: .5rem 0 0; }
.empty-symbol { width: 52px; height: 52px; display: grid; place-items: center; border-radius: 50%; background: var(--surface-soft); font-size: 1.5rem; }
.history-list { display: grid; gap: .55rem; }
.history-item { display: grid; grid-template-columns: 1.2fr .8fr .6fr .55fr; align-items: center; gap: 1rem; padding: .95rem 1.05rem; border-radius: 14px; background: rgba(255,255,255,.68); }
.history-item strong, .history-item span { min-width: 0; }
.history-item span { color: var(--muted); font-size: .78rem; }
.history-stars { color: #b86d15 !important; letter-spacing: .08em; }

.training-view { min-height: calc(100vh - 150px); padding-top: .75rem; }
.training-header { min-height: 88px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 1rem; }
.training-header .text-button:last-child { justify-self: end; }
.training-title-wrap { text-align: center; }
.training-title-wrap p { margin: 0 0 .2rem; color: var(--muted); font-size: .75rem; }
.training-title-wrap h1 { max-width: none; margin: 0; font-size: clamp(1.45rem, 4vw, 2rem); letter-spacing: -.02em; }
.training-title-wrap h1 strong { color: var(--primary); font-size: 1.25em; }
.training-metrics { width: min(560px, 100%); display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; margin: .2rem auto 1rem; overflow: hidden; border: 1px solid var(--line); border-radius: 14px; background: var(--line); }
.training-metrics div { padding: .65rem; text-align: center; background: rgba(255,255,255,.78); }
.training-metrics span, .training-metrics strong { display: block; }
.training-metrics span { margin-bottom: .15rem; color: var(--muted); font-size: .69rem; }
.training-metrics strong { font-variant-numeric: tabular-nums; font-size: .95rem; }

.board-wrap { width: var(--board-size); max-width: 100%; position: relative; margin: 0 auto; }
.training-board { width: 100%; aspect-ratio: 1; position: relative; overflow: hidden; touch-action: manipulation; user-select: none; border: 1px solid rgba(40,95,72,.12); border-radius: 32px; background: rgba(255,255,255,.92); box-shadow: var(--shadow); }
.training-board.grid-board { display: grid; padding: clamp(9px, 2vw, 16px); gap: clamp(6px, 1.5vw, 11px); }
.number-node {
  width: clamp(44px, 10.5vw, 58px);
  height: clamp(44px, 10.5vw, 58px);
  display: grid;
  place-items: center;
  z-index: 3;
  padding: 0;
  border: 1px solid #cbd7cf;
  border-radius: 50%;
  color: var(--ink);
  background: rgba(255,255,255,.96);
  box-shadow: 0 5px 13px rgba(24,50,34,.10);
  font-size: clamp(.92rem, 3.7vw, 1.18rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: background .12s ease, color .12s ease, transform .12s ease, border-color .12s ease;
}
.grid-board .number-node { width: 100%; height: 100%; min-width: 44px; min-height: 44px; border-radius: clamp(10px, 2vw, 16px); }
.number-node:hover { border-color: var(--primary); }
.number-node:focus-visible { outline: 3px solid rgba(228,154,61,.65); outline-offset: 2px; }
.number-node.is-correct { color: #fff; border-color: var(--primary); background: var(--primary); }
.number-node.is-wrong { color: #fff; border-color: var(--danger); background: var(--danger); animation: wrong-shake .22s ease; }

.ring-guide { position: absolute; left: 50%; top: 50%; z-index: 1; pointer-events: none; border: 1px solid rgba(40,95,72,.14); border-radius: 50%; transform: translate(-50%,-50%); }
.ring-layer { position: absolute; inset: 0; z-index: 2; pointer-events: none; transform: rotate(var(--ring-angle, 0deg)); transform-origin: center; }
.ring-layer .number-node { position: absolute; left: var(--x); top: var(--y); pointer-events: auto; transform: translate(-50%,-50%) rotate(var(--counter-angle, 0deg)); }
.ring-layer .number-node.is-correct { transform: translate(-50%,-50%) rotate(var(--counter-angle, 0deg)) scale(1.08); }

.butterfly-board::before {
  content: "";
  position: absolute;
  inset: 6%;
  opacity: .48;
  background:
    radial-gradient(ellipse 32% 40% at 27% 36%, rgba(40,95,72,.15) 0 68%, transparent 70%),
    radial-gradient(ellipse 32% 40% at 73% 36%, rgba(40,95,72,.15) 0 68%, transparent 70%),
    radial-gradient(ellipse 25% 32% at 32% 71%, rgba(228,154,61,.13) 0 68%, transparent 70%),
    radial-gradient(ellipse 25% 32% at 68% 71%, rgba(228,154,61,.13) 0 68%, transparent 70%),
    linear-gradient(90deg, transparent 47%, rgba(40,95,72,.18) 48% 52%, transparent 53%);
  filter: saturate(.8);
}
.butterfly-board .number-node { position: absolute; left: var(--x); top: var(--y); transform: translate(-50%,-50%); }
.butterfly-board .number-node.is-correct { transform: translate(-50%,-50%) scale(1.08); }

.countdown-layer, .pause-layer { position: absolute; inset: 0; z-index: 10; display: grid; place-content: center; justify-items: center; border-radius: 32px; background: rgba(244,247,243,.88); backdrop-filter: blur(8px); }
.countdown-layer[hidden], .pause-layer[hidden] { display: none; }
.countdown-layer strong { width: 112px; height: 112px; display: grid; place-items: center; border-radius: 50%; color: #fff; background: var(--primary); font-size: 3rem; box-shadow: 0 16px 40px rgba(40,95,72,.25); animation: countdown-pop .8s ease both; }
.pause-layer { text-align: center; padding: 2rem; }
.pause-layer > span { width: 60px; height: 60px; display: grid; place-items: center; margin-bottom: 1rem; border-radius: 50%; color: var(--primary); background: var(--primary-soft); font-weight: 800; }
.pause-layer h2 { margin-bottom: .5rem; }
.pause-layer p { margin-bottom: 1.25rem; color: var(--muted); }
.training-hint { margin: 1rem auto 0; color: var(--muted); text-align: center; font-size: .82rem; }

.modal { width: min(460px, calc(100% - 32px)); padding: 0; border: 0; border-radius: 24px; color: var(--ink); background: var(--surface); box-shadow: 0 24px 90px rgba(15,35,23,.26); }
.modal::backdrop { background: rgba(19,31,24,.48); backdrop-filter: blur(4px); }
.modal form, .result-content { padding: 2rem; text-align: center; }
.modal-icon, .result-mark { width: 58px; height: 58px; display: grid; place-items: center; margin: 0 auto 1rem; border-radius: 50%; color: var(--primary); background: var(--primary-soft); font-size: 1.6rem; font-weight: 800; }
.modal h2 { margin-bottom: .6rem; }
.modal p:not(.eyebrow) { color: var(--muted); line-height: 1.6; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .7rem; margin-top: 1.5rem; }
.result-mark { color: #fff; background: var(--primary); }
.result-modal .eyebrow { text-align: center; }
.stars { margin: 1rem 0 1.3rem; color: #ca791a; font-size: 2rem; letter-spacing: .12em; }
.stars .empty-star { color: #dfe4e0; }
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.result-grid div { padding: .9rem; border-radius: 13px; background: var(--surface-soft); }
.result-grid span, .result-grid strong { display: block; }
.result-grid span { margin-bottom: .25rem; color: var(--muted); font-size: .72rem; }
.result-grid strong { font-size: 1.05rem; font-variant-numeric: tabular-nums; }

.app-footer { min-height: 76px; display: flex; align-items: center; justify-content: space-between; gap: 1rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .72rem; }
.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }

@keyframes wrong-shake { 0%,100%{ translate: 0; } 35%{ translate: -4px 0; } 70%{ translate: 4px 0; } }
@keyframes countdown-pop { 0%{ transform: scale(.72); opacity: 0; } 28%{ transform: scale(1.05); opacity: 1; } 100%{ transform: scale(1); opacity: 1; } }

@media (max-width: 900px) {
  .hero-grid { grid-template-columns: 1fr; gap: 1.5rem; }
  .intro-panel { padding-bottom: 0; }
  h1 { max-width: 14ch; }
}

@media (max-width: 620px) {
  :root { --board-size: min(calc(100vw - 32px), 570px); }
  .app-shell { width: min(100% - 24px, 1180px); }
  .app-header { min-height: 70px; }
  .brand-mark { width: 38px; height: 38px; }
  .brand strong { font-size: .9rem; }
  .button-label { display: none; }
  .icon-button { width: 44px; padding: 0; }
  .view { padding-top: 1.4rem; }
  h1 { font-size: clamp(2.2rem, 12vw, 3.2rem); }
  .profile-strip { align-items: stretch; }
  .level-orb { width: 62px; height: 62px; }
  .choice-grid { grid-template-columns: 1fr; }
  .choice-card { min-height: 92px; flex-direction: row; align-items: center; justify-content: flex-start; }
  .choice-card > span:last-child { flex: 1; }
  .layout-preview { flex: 0 0 54px; }
  .setup-card { border-radius: 22px; }
  .stats-section { margin-top: 3rem; }
  .history-item { grid-template-columns: 1fr auto; }
  .history-item span:nth-child(2) { text-align: right; }
  .history-item span:nth-child(4) { display: none; }
  .training-header { grid-template-columns: 52px 1fr 52px; gap: .35rem; }
  .training-header .text-button { width: 48px; padding: 0; }
  .training-header .text-button span:last-child { display: none; }
  .training-metrics { margin-bottom: .7rem; }
  .training-board { border-radius: 24px; }
  .countdown-layer, .pause-layer { border-radius: 24px; }
  .app-footer { flex-direction: column; align-items: flex-start; justify-content: center; }
}

@media (max-width: 365px) {
  .app-shell { width: calc(100% - 16px); }
  :root { --board-size: calc(100vw - 16px); }
  .setup-card { padding: 1rem; }
  .segmented-control button { min-height: 50px; }
  .profile-strip { padding: .75rem; }
  .xp-copy { flex-direction: column; gap: .2rem; }
  .number-node { width: 44px; height: 44px; }
}

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