:root {
  color-scheme: light;
  --paper: #f7f4ed;
  --surface: #fbf9f3;
  --field: #f9f6ef;
  --ink: #26251f;
  --muted: #706c63;
  --hairline: rgba(38, 37, 31, 0.16);
  --central: #78664f;
  --central-soft: rgba(120, 102, 79, 0.14);
  --body: #51717a;
  --body-soft: rgba(81, 113, 122, 0.14);
  --velocity: #3f7d79;
  --gravity: #a46543;
  --trail: rgba(63, 98, 105, 0.54);
  --focus: #236f75;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  height: 100%;
  margin: 0;
}

body {
  min-width: 320px;
  background: var(--paper);
  color: var(--ink);
}

button,
select,
input { font: inherit; }

button,
select {
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--hairline);
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.app-shell {
  height: 100svh;
  min-height: 480px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.topbar {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 15px clamp(16px, 4vw, 48px) 10px;
  border-bottom: 1px solid var(--hairline);
}

.identity {
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.series,
.version,
.language-label,
.title-group p {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.2;
}

.divider {
  width: 1px;
  height: 18px;
  background: var(--hairline);
}

.title-group { min-width: 0; }

h1,
.title-group p { margin: 0; }

h1 {
  font-size: clamp(1.3rem, 2.4vw, 2rem);
  font-weight: 540;
  line-height: 1;
}

.title-group p { margin-top: 4px; }

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

select {
  min-height: 34px;
  border-radius: 6px;
  padding: 4px 28px 4px 10px;
}

.icon-button,
.dialog-close {
  width: 34px;
  height: 34px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
}

.icon-button span {
  font: 1rem Georgia, serif;
}

.stage {
  min-height: 0;
  padding: clamp(12px, 2vw, 24px) clamp(14px, 4vw, 48px);
}

.canvas-panel {
  position: relative;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: var(--field);
  border: 1px solid var(--hairline);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
}

canvas.is-dragging { cursor: grabbing; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.legend,
.readout,
.canvas-hint {
  position: absolute;
  z-index: 1;
  color: var(--muted);
  background: rgba(251, 249, 243, 0.9);
  border: 1px solid rgba(38, 37, 31, 0.1);
  font-size: 0.75rem;
  pointer-events: none;
}

.legend {
  top: 14px;
  left: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 8px 10px;
}

.zoom-controls {
  position: absolute;
  z-index: 2;
  top: 14px;
  right: 14px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px;
  color: var(--muted);
  background: rgba(251, 249, 243, 0.92);
  border: 1px solid rgba(38, 37, 31, 0.1);
  font-size: 0.72rem;
}

.zoom-readout {
  min-width: 66px;
  color: var(--ink);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.zoom-controls button {
  min-height: 36px;
  border-radius: 6px;
  cursor: pointer;
}

.zoom-controls button:disabled {
  cursor: default;
  opacity: 0.42;
}

.zoom-step {
  width: 36px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}

.reset-view {
  padding: 4px 9px;
  font-size: 0.7rem;
  white-space: nowrap;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 18px;
  height: 2px;
  display: inline-block;
  background: currentColor;
}

.swatch.velocity { color: var(--velocity); }
.swatch.gravity { color: var(--gravity); }
.swatch.trail { color: var(--trail); }

.readout {
  left: 14px;
  bottom: 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  max-width: min(820px, calc(100% - 340px));
  padding: 8px 10px;
}

.readout strong {
  margin-left: 5px;
  color: var(--ink);
  font-weight: 540;
  font-variant-numeric: tabular-nums;
}

.canvas-hint {
  right: 14px;
  bottom: 14px;
  max-width: 290px;
  margin: 0;
  padding: 8px 10px;
  text-align: right;
}

.control-area {
  padding: 10px clamp(14px, 4vw, 48px) max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--hairline);
}

.learning-panel {
  display: grid;
  grid-template-columns: minmax(260px, 1.5fr) auto minmax(230px, 1fr);
  align-items: center;
  gap: 12px 18px;
  margin-bottom: 9px;
  padding: 8px 10px;
  color: var(--muted);
  background: rgba(251, 249, 243, 0.62);
  border: 1px solid var(--hairline);
  border-radius: 8px;
  font-size: 0.75rem;
}

.learning-panel p { margin: 0; }

.scenario-text {
  color: var(--ink);
  line-height: 1.35;
}

.learning-question {
  margin-top: 3px !important;
  font-style: italic;
  line-height: 1.3;
}

.velocity-adjustment-group > span {
  display: block;
  margin-bottom: 4px;
  color: var(--ink);
  font-weight: 620;
  text-align: center;
}

.velocity-adjustments {
  display: grid;
  grid-template-columns: repeat(6, minmax(42px, 1fr));
  gap: 4px;
}

.velocity-adjustments button {
  min-height: 32px;
  padding: 3px 7px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--ink);
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
}

.prediction-text {
  line-height: 1.4;
}

.prediction-text span {
  display: block;
  margin-bottom: 2px;
  color: var(--ink);
  font-weight: 620;
}

.prediction-text strong {
  color: var(--muted);
  font-weight: 450;
}

.control-bar {
  display: grid;
  grid-template-columns: minmax(145px, 0.9fr) minmax(160px, 1fr) minmax(160px, 1fr) minmax(145px, 0.8fr) auto;
  gap: 10px;
  align-items: stretch;
}

fieldset {
  min-width: 0;
  margin: 0;
  padding: 7px 10px 9px;
  background: rgba(251, 249, 243, 0.62);
  border: 1px solid var(--hairline);
  border-radius: 8px;
}

legend {
  padding: 0 5px;
  font-size: 0.75rem;
  font-weight: 620;
}

label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 0.75rem;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--focus);
}

output {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.preset-fieldset select {
  width: 100%;
  font-size: 0.78rem;
}

.view-fieldset {
  display: grid;
  grid-template-columns: 1fr;
  gap: 4px;
}

.toggle input {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--focus);
}

.button-group {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  min-width: 98px;
}

.control-button {
  min-height: 35px;
  border-radius: 8px;
  padding: 6px 14px;
  cursor: pointer;
  font-weight: 540;
}

.control-button.primary {
  border-color: rgba(63, 113, 117, 0.34);
  background: rgba(63, 113, 117, 0.08);
}

.info-dialog {
  width: min(680px, calc(100% - 28px));
  max-height: min(82svh, 760px);
  overflow: auto;
  padding: 26px 30px 30px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: 10px;
  box-shadow: 0 18px 60px rgba(38, 37, 31, 0.16);
}

.info-dialog::backdrop { background: rgba(38, 37, 31, 0.28); }

.info-dialog form {
  position: sticky;
  top: 0;
  float: right;
}

.info-dialog h2 {
  margin: 0 42px 14px 0;
  font-size: 1.4rem;
}

.info-dialog h3 {
  margin: 22px 0 8px;
  font-size: 0.92rem;
}

.info-dialog p,
.info-dialog li {
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.58;
}

.info-dialog ul { padding-left: 1.2rem; }

@media (max-width: 760px) {
  .app-shell {
    min-height: 600px;
    overflow: auto;
  }

  .topbar { gap: 10px; }
  .series, .divider, .language-label { display: none; }
  .control-bar { grid-template-columns: 1fr 1fr; }
  .learning-panel { grid-template-columns: 1fr; }
  .button-group { grid-template-columns: 1fr 1fr; }
  .canvas-hint { display: none; }
  .readout { max-width: calc(100% - 28px); }
}

@media (max-height: 560px) and (orientation: landscape) {
  .app-shell { min-height: 0; }
  .topbar { min-height: 50px; padding-top: 9px; padding-bottom: 7px; }
  .stage { padding-top: 8px; padding-bottom: 8px; }
  .control-area { padding-top: 6px; padding-bottom: max(7px, env(safe-area-inset-bottom)); }
  .learning-panel {
    grid-template-columns: minmax(200px, 1.25fr) auto minmax(180px, 0.95fr);
    gap: 7px 12px;
    margin-bottom: 5px;
    padding: 5px 8px;
    font-size: 0.68rem;
  }
  .learning-question { margin-top: 1px !important; }
  .velocity-adjustment-group > span { margin-bottom: 2px; }
  .velocity-adjustments { grid-template-columns: repeat(6, minmax(36px, 1fr)); gap: 3px; }
  .velocity-adjustments button { min-height: 27px; padding: 2px 5px; font-size: 0.66rem; }
  .control-bar {
    grid-template-columns: minmax(195px, 1.2fr) minmax(120px, 1fr) minmax(120px, 1fr) minmax(110px, 0.9fr) 156px;
    gap: 7px;
  }
  fieldset { padding-top: 4px; padding-bottom: 5px; }
  .view-fieldset { grid-template-columns: 1fr 1fr; align-items: center; }
  .button-group { grid-template-columns: 1fr 1fr; min-width: 156px; gap: 5px; }
  .control-button { min-height: 44px; padding: 3px 10px; }
  .legend { top: 8px; left: 8px; }
  .zoom-controls { top: 8px; right: 8px; padding: 4px; }
  .zoom-readout { min-width: 58px; }
  .zoom-controls button { min-height: 34px; }
  .zoom-step { width: 34px; }
  .reset-view { padding-right: 7px; padding-left: 7px; }
  .readout { left: 8px; bottom: 8px; max-width: calc(100% - 330px); }
  .canvas-hint { right: 8px; bottom: 8px; }
}

@media (max-width: 580px) {
  .version, .title-group p { display: none; }
  .topbar { padding-left: 12px; padding-right: 12px; }
  .stage { padding-left: 10px; padding-right: 10px; }
  .control-area { padding-left: 10px; padding-right: 10px; }
  .legend-item span { display: none; }
  .readout div:nth-child(4) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
