:root {
  color-scheme: light;
  --paper: #f7f4ed;
  --surface: #fbf9f3;
  --field: #f9f6ef;
  --ink: #26251f;
  --muted: #706c63;
  --hairline: rgba(38, 37, 31, 0.16);
  --copper: #a56a43;
  --current: #47777e;
  --current-soft: rgba(71, 119, 126, 0.12);
  --band-low: #6a7d7f;
  --band-medium: #557276;
  --band-high: #3f656a;
  --visual-guide: #4d7478;
  --visual-guide-soft: rgba(77, 116, 120, 0.14);
  --power-block: #737b78;
  --power-block-soft: rgba(115, 123, 120, 0.12);
  --focus: #286f76;
  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;
  overflow-x: hidden;
  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;
  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: 20px;
  padding: 14px clamp(16px, 4vw, 48px) 10px;
  border-bottom: 1px solid var(--hairline);
}

.identity,
.top-actions {
  display: flex;
  align-items: center;
}

.identity { gap: 12px; min-width: 0; }
.top-actions { gap: 10px; flex: 0 0 auto; }
.divider { width: 1px; height: 24px; background: var(--hairline); }
.title-group { min-width: 0; }

h1 {
  margin: 0;
  font-size: clamp(1.25rem, 2.4vw, 1.9rem);
  font-weight: 540;
  line-height: 1;
}

.title-group p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.series,
.version,
.language-label {
  color: var(--muted);
  font-size: 0.76rem;
}

select {
  min-height: 34px;
  max-width: 100%;
  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(10px, 2vw, 24px) clamp(14px, 4vw, 48px);
  overflow: hidden;
}

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

canvas {
  width: 100%;
  height: 100%;
  display: block;
  min-height: 0;
}

.measurement-panel {
  position: absolute;
}

.measurement-panel,
.formula-strip {
  background: rgba(251, 249, 243, 0.92);
  border: 1px solid rgba(38, 37, 31, 0.11);
  backdrop-filter: blur(5px);
}

.measurement-panel {
  left: clamp(10px, 2vw, 22px);
  bottom: clamp(10px, 2vw, 20px);
  display: grid;
  grid-template-columns: repeat(6, auto);
  gap: 8px 18px;
  padding: 9px 12px;
  color: var(--muted);
  font-size: 0.76rem;
}

.measurement-panel strong {
  display: block;
  margin-top: 2px;
  color: var(--ink);
  font-size: 0.88rem;
  font-weight: 570;
  font-variant-numeric: tabular-nums;
}

.power-meter-item {
  min-width: 112px;
}

.power-meter {
  width: 112px;
  height: 10px;
  margin-top: 7px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}

.power-meter > span {
  display: block;
  height: 100%;
  border: 1px solid rgba(38, 37, 31, 0.17);
  border-radius: 2px;
  background: var(--power-block-soft);
  transition: background-color 160ms ease-out, border-color 160ms ease-out;
}

.power-meter > span.is-active {
  border-color: rgba(38, 37, 31, 0.24);
  background: var(--power-block);
}

.power-band strong[data-band="low"] { color: var(--band-low); }
.power-band strong[data-band="medium"] { color: var(--band-medium); }
.power-band strong[data-band="high"] { color: var(--band-high); }

.canvas-top {
  position: absolute;
  top: clamp(10px, 2vw, 20px);
  left: clamp(10px, 2vw, 20px);
  right: clamp(10px, 2vw, 20px);
  display: grid;
  grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  align-items: stretch;
  gap: 16px;
}

.formula-strip {
  min-width: 0;
  padding: 9px 12px;
}

.cause-panel {
  min-width: 0;
  padding: 9px 12px;
  border-left: 2px solid var(--visual-guide);
  background: rgba(251, 249, 243, 0.9);
}

.cause-panel h2 {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.cause-panel p {
  margin: 0;
  color: var(--ink);
  font-size: 0.73rem;
  line-height: 1.4;
}

.formula-strip h2 {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.formula-strip-equations {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 18px;
}

.formula-strip p {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
}

.formula-strip .formula-base {
  color: var(--muted);
  font-size: 0.7rem;
}

.formula-strip .formula-substitution {
  margin-top: 2px;
  color: var(--ink);
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}

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

.experiment-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 7px;
}

.discovery-prompt {
  min-width: 0;
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.3;
}

.discovery-prompt span + span {
  margin-left: 12px;
}

.experiment-buttons {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  gap: 5px;
  flex: 0 0 auto;
}

.experiment-buttons button {
  min-height: 27px;
  border-radius: 6px;
  padding: 3px 8px;
  color: var(--muted);
  font-size: 0.67rem;
  cursor: pointer;
}

.experiment-buttons button:hover {
  color: var(--ink);
  border-color: rgba(77, 116, 120, 0.42);
  background: var(--visual-guide-soft);
}

.control-bar {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) minmax(190px, 1.2fr) minmax(190px, 1.2fr) auto;
  gap: 10px;
  align-items: stretch;
}

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

legend {
  padding: 0 5px;
  font-size: 0.73rem;
  font-weight: 600;
}

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

.preset-fieldset select { width: 100%; }

.preset-disclaimer {
  margin: 5px 1px 0;
  color: var(--muted);
  font-size: 0.62rem;
  line-height: 1.25;
}

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

input[type="range"] {
  width: 100%;
  margin: 7px 0 0;
  accent-color: var(--current);
}

.reset-button {
  min-width: 92px;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
}

.info-dialog {
  width: min(650px, calc(100vw - 32px));
  max-height: min(78vh, 720px);
  overflow: auto;
  border: 1px solid var(--hairline);
  border-radius: 10px;
  padding: 24px 28px 28px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 18px 60px rgba(38, 37, 31, 0.18);
}

.info-dialog::backdrop { background: rgba(38, 37, 31, 0.22); }
.info-dialog form { float: right; margin: -10px -12px 0 12px; }
.info-dialog h2 { margin: 0 0 12px; font-size: 1.25rem; }
.info-dialog h3 { margin: 20px 0 8px; font-size: 0.88rem; }
.info-dialog p,
.info-dialog li { color: var(--muted); font-size: 0.82rem; line-height: 1.6; }
.info-dialog ul { padding-left: 20px; }

@media (max-width: 780px) {
  .topbar { min-height: 52px; padding-top: 8px; padding-bottom: 7px; }
  .series,
  .language-label { display: none; }
  .identity { gap: 8px; }
  .title-group p { font-size: 0.66rem; }
  .control-area { padding-top: 7px; padding-bottom: 7px; }
  .experiment-panel { gap: 7px; margin-bottom: 5px; }
  .discovery-prompt { font-size: 0.62rem; }
  .experiment-buttons { gap: 3px; }
  .experiment-buttons button { min-height: 24px; padding: 2px 5px; font-size: 0.58rem; }
  .control-bar { grid-template-columns: minmax(128px, 0.9fr) minmax(145px, 1fr) minmax(145px, 1fr) 72px; gap: 6px; }
  fieldset { padding: 4px 7px 6px; }
  legend { font-size: 0.65rem; }
  fieldset label { font-size: 0.65rem; }
  select { min-height: 30px; font-size: 0.72rem; }
  input[type="range"] { margin-top: 3px; }
  .reset-button { min-width: 0; padding: 6px; font-size: 0.72rem; }
  .preset-disclaimer { margin-top: 3px; font-size: 0.56rem; }
  .measurement-panel { grid-template-columns: repeat(5, auto); gap: 5px 10px; padding: 6px 8px; font-size: 0.64rem; }
  .measurement-panel strong { font-size: 0.74rem; }
  .power-meter-item { min-width: 84px; }
  .power-meter { width: 84px; height: 8px; gap: 1px; margin-top: 5px; }
  .canvas-top { grid-template-columns: minmax(220px, 300px) minmax(0, 1fr); gap: 12px; }
  .formula-strip { padding: 6px 9px; }
  .formula-strip h2 { margin-bottom: 2px; font-size: 0.55rem; }
  .formula-strip-equations { gap: 5px 12px; }
  .formula-strip p { font-size: 0.66rem; }
  .formula-strip .formula-base { font-size: 0.55rem; }
  .formula-strip .formula-substitution { margin-top: 1px; font-size: 0.61rem; }
  .cause-panel { padding: 6px 8px; }
  .cause-panel h2 { font-size: 0.54rem; }
  .cause-panel p { font-size: 0.62rem; }
}

@media (max-height: 440px) and (orientation: landscape) {
  .stage { padding-top: 6px; padding-bottom: 6px; }
  .topbar { min-height: 46px; }
  h1 { font-size: 1.12rem; }
  .title-group p { margin-top: 2px; }
  .icon-button { width: 30px; height: 30px; }
  .control-area { padding-top: 5px; padding-bottom: max(5px, env(safe-area-inset-bottom)); }
  .experiment-panel { margin-bottom: 4px; }
  .discovery-prompt { max-width: 300px; font-size: 0.58rem; }
  .discovery-prompt span { display: block; }
  .discovery-prompt span + span { margin-left: 0; }
  .experiment-buttons button { min-height: 22px; }
  .control-bar { gap: 5px; }
  fieldset { padding-top: 2px; padding-bottom: 4px; }
  input[type="range"] { height: 14px; }
  .measurement-panel { bottom: 6px; left: 6px; }
  .canvas-top {
    top: 6px;
    left: 6px;
    right: 6px;
    grid-template-columns: 340px minmax(0, 1fr);
    gap: 14px;
  }
  .formula-strip {
    padding: 5px 8px;
  }
  .formula-strip h2 { margin-bottom: 1px; }
  .formula-strip-equations { gap: 4px 10px; }
  .formula-strip p { margin: 0; }
  .formula-strip .formula-base { font-size: 0.48rem; }
  .formula-strip .formula-substitution { margin: 0; font-size: 0.55rem; }
  .cause-panel { padding: 5px 8px; }
  .cause-panel h2 { margin-bottom: 2px; }
  .cause-panel p { font-size: 0.58rem; line-height: 1.25; }
}

@media (max-width: 640px) and (orientation: portrait) {
  .app-shell { height: auto; min-height: 100svh; overflow: visible; }
  .stage { min-height: 410px; }
  .control-bar { grid-template-columns: 1fr; }
  .measurement-panel { grid-template-columns: repeat(2, auto); }
  .experiment-panel { align-items: stretch; flex-direction: column; }
  .experiment-buttons { grid-template-columns: repeat(2, 1fr); }
}

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