:root {
  color-scheme: light;
  --canvas: #eeeee9;
  --panel: #f5f5f1;
  --rule: #e2e2dc;
  --hover: #ebebe5;
  --active: #e0e0d9;
  --ink: #2f2f2c;
  --muted: #8a8a84;
  /* Buttons standing out from the panel, and their hover border. */
  --raised: #fff;
  --raised-edge: #d2d2cb;
  /* Text and icons on ink-coloured buttons, and those buttons' hover. */
  --on-ink: #fff;
  --ink-strong: #000;
  /* The grey panel over the canvas, and rules inside it. */
  --glass: rgba(96, 96, 90, 0.14);
  --hairline: rgba(47, 47, 44, 0.12);
  --switch-off: rgba(47, 47, 44, 0.22);
  --knob: #fff;
  --tooltip: rgba(47, 47, 44, 0.9);
  --tooltip-ink: #f5f5f1;
  /* Overlay arrows, as render::Theme draws them. */
  --contact-force: #0f766e;
  --velocity: #2563eb;
}

/* Dark: the same warm greys, inverted. Matches render::Theme::DARK. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --canvas: #1c1c1a;
  --panel: #232321;
  --rule: #353531;
  --hover: #2c2c29;
  --active: #393935;
  --ink: #e6e6df;
  --muted: #8f8f88;
  --raised: #2b2b28;
  --raised-edge: #4a4a44;
  --on-ink: #1c1c1a;
  --ink-strong: #fff;
  --glass: rgba(150, 150, 140, 0.13);
  --hairline: rgba(230, 230, 223, 0.12);
  --switch-off: rgba(230, 230, 223, 0.22);
  --knob: #1c1c1a;
  --tooltip: rgba(236, 236, 230, 0.94);
  --tooltip-ink: #1c1c1a;
  --contact-force: #2dd4bf;
  --velocity: #60a5fa;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font: 12px/1.4 ui-sans-serif, system-ui, -apple-system, sans-serif;
}

body {
  display: flex;
}

[hidden] {
  display: none !important;
}

main {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
}

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

/* Sidebar */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 208px;
  flex-shrink: 0;
  padding: 16px 14px;
  background: var(--panel);
  border-right: 1px solid var(--rule);
  overflow-y: auto;
  user-select: none;
}

h2 {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

button {
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

button:hover {
  background: var(--hover);
}

button.active {
  background: var(--active);
}

/* Tools and object chooser */

.tools,
.chooser {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
}

.tools {
  grid-template-columns: repeat(auto-fit, minmax(32px, 1fr));
}

.tools button,
.chooser button {
  display: grid;
  place-items: center;
  height: 38px;
}

.tools svg,
.chooser svg {
  width: 20px;
  height: 20px;
}

.tools .fill,
.chooser .fill {
  fill: var(--ink);
}

.tools .line,
.chooser .line {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.4;
  stroke-linejoin: round;
}

.tools .dashed,
.chooser .dashed {
  stroke-width: 1;
  stroke-dasharray: 2 2;
}

.chooser .dots {
  stroke: var(--ink);
  stroke-width: 1.8;
  stroke-linecap: round;
}

/* Properties */

.subject {
  margin: 0 0 10px;
  font-weight: 500;
}

.subject .state {
  color: var(--muted);
  font-weight: 400;
}

.prop {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  row-gap: 2px;
  margin-bottom: 10px;
}

.prop .end {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
}

.prop input.value {
  field-sizing: content;
  min-width: 3ch;
  max-width: 120px;
  margin-right: -4px;
  padding: 1px 4px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.prop input.value:enabled:hover,
.prop input.value:focus {
  background: var(--hover);
  color: var(--ink);
  outline: none;
}

.reset-row {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  padding: 0;
  color: var(--muted);
}

.reset-row[hidden] {
  display: none;
}

.reset-row:hover {
  color: var(--ink);
}

.reset-row svg {
  width: 12px;
  height: 12px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.prop input[type="range"] {
  grid-column: 1 / -1;
}

.prop.disabled {
  opacity: 0.4;
}

.prop.toggle input {
  margin: 0;
  accent-color: var(--ink);
}

input[type="range"] {
  width: 100%;
  height: 14px;
  margin: 0;
  background: transparent;
  appearance: none;
  cursor: pointer;
}

input[type="range"]:disabled {
  cursor: default;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 2px;
  background: var(--rule);
}

input[type="range"]::-moz-range-track {
  height: 2px;
  background: var(--rule);
}

input[type="range"]::-webkit-slider-thumb {
  width: 10px;
  height: 10px;
  margin-top: -4px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  appearance: none;
}

input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
}

/* Physics */

details summary {
  margin-bottom: 10px;
  color: var(--muted);
  cursor: pointer;
  list-style-position: inside;
}

details summary:hover {
  color: var(--ink);
}

.reset {
  margin: -2px 0 0 -6px;
  padding: 4px 6px;
  color: var(--muted);
}

/* Overlay: rates and visualizations, over the top right of the canvas */

.overlay {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 196px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--glass);
  backdrop-filter: blur(6px);
  user-select: none;
}

.stats {
  display: grid;
  grid-template-columns: 1fr auto;
  row-gap: 2px;
  margin: 0;
  font: 11px/1.4 ui-monospace, monospace;
}

.stats dt {
  color: var(--muted);
}

.stats dd {
  margin: 0;
  text-align: right;
}

.overlay hr {
  margin: 8px 0;
  border: 0;
  border-top: 1px solid var(--hairline);
}

.viz {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  cursor: pointer;
}

.viz .swatch {
  height: 8px;
  border-radius: 2px;
}

/* An on/off switch made from a checkbox. */
.switch {
  position: relative;
  width: 24px;
  height: 14px;
  margin: 0;
  border-radius: 7px;
  background: var(--switch-off);
  appearance: none;
  cursor: pointer;
  transition: background 0.15s;
}

.switch::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--knob);
  transition: transform 0.15s;
}

.switch:checked {
  background: var(--ink);
}

.switch:checked::before {
  transform: translateX(10px);
}

/* Footer: the simulation's big buttons, then the playground's controls */

footer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: auto;
}

footer .row,
.baking .row {
  display: flex;
  gap: 6px;
}

footer .row > *,
.controls > * {
  flex: 1;
}

.controls {
  display: flex;
  gap: 6px;
}

.controls button,
button.big {
  padding: 6px 8px;
  border: 1px solid var(--rule);
  background: var(--raised);
  box-shadow: 0 1px 0 var(--hairline);
}

.controls button:hover,
button.big:hover {
  border-color: var(--raised-edge);
  background: var(--hover);
}

button.big {
  padding: 9px 10px;
  font-weight: 500;
}

button.primary {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--on-ink);
}

button.primary:hover {
  border-color: var(--ink-strong);
  background: var(--ink-strong);
}

.baking .progress {
  height: 6px;
  margin-bottom: 6px;
  border-radius: 3px;
  background: var(--rule);
  overflow: hidden;
}

#bake-bar {
  width: 0;
  height: 100%;
  background: var(--ink);
}

.bake-text {
  flex: 1;
  align-self: center;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

button.small {
  flex: 0 0 auto;
  padding: 3px 8px;
  border: 1px solid var(--rule);
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
}

/* Inspect readout, following the pointer */

.inspect {
  position: absolute;
  z-index: 2;
  min-width: 170px;
  padding: 7px 9px;
  border-radius: 7px;
  background: var(--tooltip);
  color: var(--tooltip-ink);
  font: 11px/1.5 ui-monospace, monospace;
  pointer-events: none;
}

.inspect .title {
  margin-bottom: 3px;
  font: 600 12px/1.4 ui-sans-serif, system-ui, sans-serif;
}

.inspect dl {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 12px;
  margin: 0;
}

.inspect dd {
  margin: 0;
  text-align: right;
}

/* Review: the bottom panel */

.review {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 14px 12px;
  border-top: 1px solid var(--rule);
  background: var(--panel);
  user-select: none;
}

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

.transport #timeline {
  flex: 1;
}

.time {
  min-width: 58px;
  font: 11px ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.play {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--ink);
}

.play:hover {
  background: var(--ink-strong);
}

.play svg {
  width: 14px;
  height: 14px;
  fill: var(--on-ink);
}

#playback-speed {
  padding: 5px 6px;
  border: 1px solid var(--rule);
  border-radius: 6px;
  background: var(--raised);
  color: inherit;
  font: inherit;
}

.transport button.big {
  padding: 6px 14px;
}

/* The grey panel, docked into the review panel. */
.overlay.docked {
  position: static;
  display: grid;
  grid-template-columns: repeat(3, minmax(190px, 1fr));
  gap: 18px;
  width: auto;
}

.overlay.docked h3 {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.overlay.docked .stats {
  margin-top: 8px;
}

.viz .swatch.icon {
  display: grid;
  place-items: center;
  height: 10px;
  background: none;
}

.viz .swatch.icon svg {
  width: 28px;
  height: 10px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

/* Light / dark switch, over the top left of the canvas in every mode */

.theme-toggle {
  position: absolute;
  top: 12px;
  left: 12px;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--glass);
  backdrop-filter: blur(6px);
  color: var(--muted);
}

.theme-toggle:hover {
  color: var(--ink);
}

.theme-toggle svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* The icon shows the theme a click switches to. */
:root[data-theme="dark"] .theme-toggle .moon,
:root:not([data-theme="dark"]) .theme-toggle .sun {
  display: none;
}

/* Zoom, beside the theme switch */

.zoom {
  position: absolute;
  top: 12px;
  left: 46px;
  display: flex;
  align-items: center;
  height: 28px;
  padding: 0 2px;
  border-radius: 14px;
  background: var(--glass);
  backdrop-filter: blur(6px);
  color: var(--muted);
  user-select: none;
}

.zoom button {
  height: 24px;
  min-width: 24px;
  padding: 0 6px;
  border-radius: 12px;
  font: 12px/1 ui-monospace, monospace;
}

.zoom button:hover {
  color: var(--ink);
}

#zoom-reset {
  min-width: 46px;
  font-variant-numeric: tabular-nums;
}

/* Touch: no double-tap zoom on buttons, and slider thumbs a finger can catch. */

button {
  touch-action: manipulation;
}

.mobile-only,
.visualize {
  display: none;
}

/* The phone's sheet; on a wide screen its sections are the sidebar's own. */
.sheet {
  display: contents;
}

@media (pointer: coarse) {
  input[type="range"] {
    height: 26px;
  }

  input[type="range"]::-webkit-slider-thumb {
    width: 18px;
    height: 18px;
    margin-top: -8px;
  }

  input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
  }
}

/* A phone: the canvas fills the screen, the sidebar becomes a toolbar beneath it, and
   Properties, Physics and Visualize open in a sheet above the toolbar ("More"). */

@media (max-width: 720px) {
  html,
  body {
    height: 100dvh;
    overscroll-behavior: none;
  }

  body {
    flex-direction: column-reverse;
  }

  .mobile-hide,
  .prop-soft-detail {
    display: none !important;
  }

  .mobile-only {
    display: block;
  }

  .sidebar {
    width: 100%;
    gap: 6px;
    padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
    border-right: 0;
    border-top: 1px solid var(--rule);
    overflow: visible;
  }

  .tool-row h2,
  .object-row h2 {
    display: none;
  }

  .tools,
  .chooser {
    display: flex;
    gap: 2px;
    overflow-x: auto;
  }

  .tools button,
  .chooser button {
    flex: 1 0 40px;
    height: 42px;
  }

  main {
    min-height: 0;
  }

  /* The sheet: closed, only the toolbar shows; open, its sections scroll above it
     while the toolbar (and its "Less") stays put. */
  .sheet {
    display: none;
    order: -1;
    max-height: 50dvh;
    padding: 4px 4px 8px;
    overflow-y: auto;
    border-bottom: 1px solid var(--rule);
  }

  .sidebar.sheet-open > .sheet {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .visualize {
    display: block;
  }

  .visualize .viz {
    padding: 6px 0;
  }

  footer {
    margin-top: 0;
  }

  .controls button {
    padding: 10px 6px;
  }

  .controls button:disabled {
    opacity: 0.4;
  }

  #sheet-toggle[aria-expanded="true"] {
    background: var(--active);
  }

  /* The zoom and theme buttons, out of the way of the tooltip and notch. */
  .theme-toggle,
  .zoom {
    top: calc(10px + env(safe-area-inset-top));
  }
}
