:root {
  color-scheme: light;
  --ink: #304b49;
  --muted: #718582;
  --paper: #fffdf5;
  --green: #3c8873;
  --coral: #e78255;
  --shadow: 0 12px 32px #37594d12, 0 3px 0 #c6d3c6;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: 'Avenir Next', 'Nunito', 'PingFang SC', 'Microsoft YaHei', system-ui, sans-serif;
  color: var(--ink);
  background: radial-gradient(ellipse at 22% 10%, #fff7df 0, transparent 58%),
    radial-gradient(ellipse at 90% 90%, #cbded9 0, transparent 65%), #e6eee5;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; }
button {
  border: 1px solid #ffffffb3;
  border-radius: 14px;
  background: var(--coral);
  color: #fffdf5;
  padding: 10px 20px;
  font-size: .85rem;
  font-weight: 750;
  cursor: pointer;
  box-shadow: 0 4px 0 #bf633e, inset 0 2px 0 #ffffff40;
  transition: transform .15s, box-shadow .15s, background .15s;
  touch-action: manipulation;
}
button:hover { background: #ec9064; transform: translateY(-2px); }
button:active { transform: translateY(3px); box-shadow: 0 1px 0 #bf633e; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 3px solid #e78255; outline-offset: 5px; }
button.btn2, .lang-toggle {
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 4px 0 #c6d3c6, inset 0 2px 0 #fff;
}
button.btn2:hover, .lang-toggle:hover { background: #ffffff; }

/* Surface collection */
body[data-page='selection'] { min-height: 100vh; padding: 60px 20px 32px; display: grid; place-items: center; }
.container { width: min(900px, 100%); text-align: center; }
.lang-toggle { position: absolute; top: 18px; right: 22px; padding: 8px 15px; font-size: .75rem; }
.version-toggle { left: 22px; right: auto; border: 1px solid #ffffffb3; border-radius: 14px; font-weight: 750; text-decoration: none; }
h1 { font-size: clamp(1.65rem, 4vw, 2.6rem); font-weight: 850; letter-spacing: -.045em; color: #315c50; }
.subtitle { color: var(--muted); font-size: .88rem; margin: 8px 0 23px; }
.speed-control { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; margin-bottom: 26px; }
.speed-control label { font-size: .8rem; font-weight: 650; }
.speed-value { width: 30px; font-size: .86rem; font-weight: 800; color: #c47545; font-variant-numeric: tabular-nums; }
.speed-slider { appearance: none; -webkit-appearance: none; width: 150px; height: 7px; border-radius: 8px; background: #c9d9cd; cursor: pointer; }
.speed-slider::-webkit-slider-thumb { appearance: none; -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%; background: #fff9e6; border: 4px solid #589d84; box-shadow: 0 2px 3px #37594d25; }
.speed-slider::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: #fff9e6; border: 4px solid #589d84; }
.maps { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 20px; }
.map-card { padding: 10px 10px 18px; text-decoration: none; color: inherit; background: #fffdf5e6; border: 1px solid #fffef8; border-radius: 26px; box-shadow: var(--shadow); transition: transform .2s, box-shadow .2s; }
.map-card:hover { transform: translateY(-6px) rotate(-.5deg); box-shadow: 0 18px 35px #37594d20, 0 4px 0 #9ab4a1; }
.surface-preview { height: 168px; border-radius: 19px; overflow: hidden; margin-bottom: 14px; background: radial-gradient(ellipse at 40% 28%, #f6faf0, #d3e5db); }
.map-card[data-map='genus2'] .surface-preview, .map-card[data-map='projective'] .surface-preview { background: radial-gradient(ellipse at 40% 28%, #f8f3ff, #e1dce9); }
.map-card[data-map='mobius'] .surface-preview { background: radial-gradient(ellipse at 40% 28%, #fff9e9, #f0e0c6); }
.map-card[data-map='klein'] .surface-preview { background: radial-gradient(ellipse at 40% 28%, #eef9fa, #d7e5eb); }
.surface-preview canvas, .surface-preview img { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.map-name { font-size: 1rem; font-weight: 750; letter-spacing: .035em; }
.repo-link { margin-top: 25px; font-size: .7rem; }
.repo-link a { color: var(--muted); text-decoration: none; }
.repo-link a:hover { color: var(--green); text-decoration: underline; }

/* Manifold and local chart */
body[data-page='game'] { overflow: hidden; user-select: none; --chart-width: clamp(280px, 33vw, 430px); }
#canvas-container { position: fixed; inset: 112px calc(var(--chart-width) + 58px) 132px 24px; z-index: 1; border-radius: 32px; background: radial-gradient(ellipse at 45% 30%, #f6f8ecb3, #dfeadf40 70%); }
#canvas-container canvas { display: block; border-radius: inherit; }
#overlay { position: fixed; inset: 0; z-index: 2; pointer-events: none; display: flex; flex-direction: column; justify-content: space-between; padding: 22px 24px; }
.top-panel { display: flex; flex-direction: column; align-items: center; gap: 10px; align-self: center; }
.top-bar { display: flex; justify-content: center; gap: 28px; font-size: .8rem; background: #fffdf5eb; border: 1px solid #fff; padding: 9px 24px; border-radius: 18px; box-shadow: var(--shadow); }
.top-bar span { font-weight: 750; }
#score, #best { color: #c47545 !important; font-size: 1.12rem; padding-left: 4px; font-variant-numeric: tabular-nums; }
.goal-line { max-width: calc(100vw - 32px); text-align: center; font-size: .85rem; line-height: 1.5; color: #5a786b; }
.goal-line span { font-weight: 650; }
#goalText { color: #315c50; }
.msg-area { position: absolute; left: 50%; top: 112px; transform: translateX(-50%); text-align: center; font-size: .85rem; line-height: 1.5; white-space: nowrap; }
.msg-area:not(:empty) { padding: 8px 17px; background: #fffdf5ed; border: 1px solid #fff; border-radius: 14px; box-shadow: 0 4px 15px #37594d12; }
.msg-area.good { color: #318163; }
.msg-area.warn { color: #bd7633; }
.msg-area.dead { color: #ba5549; }
.bottom-bar { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.btn-row { display: flex; gap: 10px; pointer-events: auto; }
.legend { display: flex; gap: 12px; font-size: .7rem; color: #647b70; flex-wrap: wrap; justify-content: center; line-height: 1.6; }
.legend .dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; box-shadow: inset 0 2px 2px #ffffff66, inset 0 -2px 1px #00000018, 0 1px 2px #37594d22; }
#foodLabel { color: var(--green) !important; font-weight: 650; }
.legend .sepa { color: #aebdb2; margin: 0 2px; }
.tip { font-size: .68rem; color: #7e9287; text-align: center; }
.pause-overlay { position: fixed; inset: 108px calc(var(--chart-width) + 58px) 132px 24px; z-index: 5; border-radius: 32px; background: #edf3e77a; backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; font-size: 2rem; font-weight: 850; color: #315c50; pointer-events: none; text-shadow: 0 2px #fff; }
#map-panel { position: fixed; right: 24px; top: calc(50% - 10px); transform: translateY(-50%); width: var(--chart-width); z-index: 3; background: var(--paper); border: 1px solid #fff; border-radius: 28px; padding: 12px; box-shadow: var(--shadow); pointer-events: auto; touch-action: none; display: flex; flex-direction: column; gap: 10px; }
#mapCanvas { touch-action: none; display: block; width: 100%; aspect-ratio: 1; border-radius: 20px; }
.map-title { order: -1; padding: 5px 5px 0; text-align: center; font-size: .74rem; font-weight: 750; color: #5c7d70; letter-spacing: .05em; }
.chart-controls { display: flex; justify-content: space-around; color: #678a76; font-size: 22px; line-height: 22px; pointer-events: none; }
#touch-control-zone { display: none; }
#pi1-panel { position: fixed; left: 38px; top: 162px; z-index: 3; width: 280px; max-height: calc(100vh - 310px); overflow: auto; background: #fffdf5f2; border: 1px solid #fff; border-radius: 22px; padding: 14px 16px; box-shadow: var(--shadow); font-size: .68rem; line-height: 1.65; color: var(--ink); pointer-events: auto; }
.pi1-title { color: var(--green); font-weight: 750; text-align: center; margin-bottom: 8px; }
.pi1-row { display: grid; grid-template-columns: 65px 1fr; gap: 6px; align-items: start; }
.pi1-key { color: #839087; }
.pi1-val { color: #36524b; overflow-wrap: anywhere; font-family: ui-monospace, monospace; }
.pi1-warn { color: #bd7633; }
@media (max-width: 880px) {
  .maps { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .container { max-width: 600px; }
}
@media (max-width: 700px), (pointer: coarse) and (max-width: 980px) and (orientation: portrait) {
  body[data-page='game'] { --chart-width: min(68vw, 34dvh, 280px); }
  #canvas-container, .pause-overlay { inset: 88px 12px calc(var(--chart-width) + 164px) 12px; border-radius: 24px; }
  #overlay { padding: calc(10px + env(safe-area-inset-top)) 10px calc(10px + env(safe-area-inset-bottom)); }
  .top-panel { gap: 7px; }
  .top-bar { gap: 18px; font-size: .7rem; padding: 6px 16px; border-radius: 14px; }
  #score, #best { font-size: .95rem; }
  .goal-line { font-size: .73rem; }
  .msg-area { top: 92px; font-size: .75rem; max-width: 95vw; white-space: normal; }
  .bottom-bar { gap: 10px; }
  .btn-row { flex-wrap: wrap; justify-content: center; gap: 7px; }
  button { min-height: 42px; padding: 8px 12px; font-size: .73rem; border-radius: 11px; }
  .legend { gap: 8px; font-size: .61rem; }
  .tip { display: none; }
  #map-panel { left: 50%; right: auto; top: auto; bottom: calc(88px + env(safe-area-inset-bottom)); transform: translateX(-50%); width: calc(var(--chart-width) + 16px); padding: 8px; gap: 6px; z-index: 4; border-radius: 22px; }
  #mapCanvas { border-radius: 15px; }
  .map-title { font-size: .65rem; padding: 0; }
  .chart-controls { font-size: 20px; line-height: 18px; }
  #touch-control-zone { display: block; position: fixed; inset: auto 0 calc(88px + env(safe-area-inset-bottom)); height: calc(var(--chart-width) + 52px); z-index: 3; touch-action: none; }
  #pi1-panel { left: 24px; top: 118px; width: min(300px, calc(100vw - 48px)); max-height: 28vh; font-size: .62rem; padding: 10px 12px; }
}
@media (max-height: 560px) and (min-width: 701px) {
  body[data-page='game'] { --chart-width: min(34vw, 62vh); }
  #canvas-container, .pause-overlay { top: 80px; bottom: 76px; }
  #map-panel { top: 49%; padding: 8px; gap: 5px; }
  #overlay { padding: 10px 20px; }
  .top-panel { gap: 5px; }
  .top-bar { padding: 5px 20px; }
  .bottom-bar { gap: 8px; }
  .tip, .chart-controls { display: none; }
  #pi1-panel { top: 106px; max-height: calc(100vh - 190px); }
}
@media (max-width: 450px) {
  body[data-page='selection'] { padding: 76px 16px 28px; }
  .maps { gap: 12px; }
  .map-card { padding: 7px 7px 14px; border-radius: 20px; }
  .surface-preview { height: 132px; border-radius: 14px; margin-bottom: 10px; }
  .map-name { font-size: .8rem; letter-spacing: 0; }
  .speed-control { gap: 7px; }
  .speed-slider { width: 110px; }
}
@media (prefers-reduced-motion: reduce) { button, .map-card { transition: none; } }
