:root {
  /* Colors and fonts come from the shared design system (/assets/design.css);
     these names keep the rest of this stylesheet unchanged. Scene, particle and
     field colors carry meaning and stay as they were. */
  --brand-accent: var(--accent);
  --brand-accent-strong: var(--accent-hover);

  --bg-color: var(--bg);
  --bg-pattern: var(--bg-grid);
  --text-main: var(--ink);
  --text-secondary: var(--ink-2);

  --brand-primary: var(--accent);
  --scene-accent: #0D9488;

  --surface-alt: var(--surface-2);
  --border: var(--line);
  --shadow: var(--shadow-1);

  --btn-primary: var(--accent);
  --btn-primary-hover: var(--accent-hover);
  --btn-text: var(--accent-ink);

  --nav-bg: var(--surface);
  --nav-border: var(--line);

  --slider-track: var(--line-strong);
  --toggle-hover: var(--accent-soft);
  --brand-soft: var(--accent-soft);
  --brand-line: color-mix(in srgb, var(--accent) 35%, transparent);

  /* Phase colors, shared by the badge, thermometer and preset swatches. */
  --phase-solid: #0D9488;
  --phase-liquid: #D97706;
  --phase-gas: #DB2777;

  --thermo-glass: var(--surface);
  --thermo-track: var(--surface-3);
}

[data-theme="dark"] {
  --scene-accent: #2DD4BF;

  --phase-solid: #2DD4BF;
  --phase-liquid: #F59E0B;
  --phase-gas: #F472B6;

}

* {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font-body);
  color: var(--text-main);
  background-color: var(--bg-color);
  background-image: radial-gradient(var(--bg-pattern) 1px, transparent 1px);
  background-size: 30px 30px;
  overflow: hidden;
}

a {
  color: var(--brand-primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

/* Title Banner */
#titleBanner {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  width: calc(100% - 40px);
  max-width: 1360px;
  margin: 16px auto 0;
  background: var(--nav-bg);
  border: 1px solid var(--nav-border);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  min-height: 58px;
  padding: 8px 20px;
  z-index: 10;
}

.banner-logo img {
  width: auto;
  height: 36px;
  display: block;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1));
}

.banner-title {
  margin: 0;
  text-align: center;
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  color: var(--ink);
}

/* Apps keep a plain title; the spectrum is a thin line under the banner instead. */
.banner-title.gradient-text {
  color: var(--ink);
  background: none;
  -webkit-text-fill-color: currentColor;
}

#titleBanner::after {
  content: '';
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: -1px;
  height: 2px;
  border-radius: 2px;
  background: var(--spectrum);
  opacity: 0.85;
  pointer-events: none;
}

.banner-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}

/* Workspace */
#workspace {
  min-height: 0;
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(320px, 1fr);
  gap: 0.9rem;
  width: calc(100% - 40px);
  max-width: 1360px;
  margin: 0 auto;
  padding-bottom: 0.75rem;
  z-index: 1;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

/* Sim Panel */
#simPanel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

#simCanvas {
  display: block;
  width: 100%;
  height: 0;
  flex: 1;
  min-height: 0;
  border-radius: 18px;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

#simCanvas:active {
  cursor: grabbing;
}

.load-status {
  position: absolute;
  inset: 0;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--text-secondary);
}

.load-status[hidden] {
  display: none;
}

.sim-hint {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  font-size: 0.72rem;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  background: var(--nav-bg);
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--nav-border);
  pointer-events: none;
  opacity: 0.85;
  white-space: nowrap;
}

/* Phase badge on the simulation */
.phase-badge {
  position: absolute;
  top: 14px;
  left: 14px;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--nav-bg);
  border: 1px solid var(--nav-border);
  font-weight: 800;
  font-size: 0.9rem;
  pointer-events: none;
  z-index: 5;
}

.phase-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  flex: 0 0 auto;
  background: var(--phase-color, var(--phase-solid));
  transition: background-color 0.3s ease;
}

.ff-badge {
  position: absolute;
  top: 54px;
  left: 14px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--brand-soft);
  border: 1px solid var(--brand-line);
  color: var(--brand-primary);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 600;
  pointer-events: none;
  z-index: 5;
}

.ff-badge[hidden] {
  display: none;
}

[data-phase="solid"] { --phase-color: var(--phase-solid); }
[data-phase="liquid"] { --phase-color: var(--phase-liquid); }
[data-phase="gas"] { --phase-color: var(--phase-gas); }
[data-phase="melting"] { --phase-color: linear-gradient(90deg, var(--phase-solid) 50%, var(--phase-liquid) 50%); }
[data-phase="boiling"] { --phase-color: linear-gradient(90deg, var(--phase-liquid) 50%, var(--phase-gas) 50%); }

/* Thermometer overlay on the simulation */
.sim-thermometer-overlay {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.5rem;
  pointer-events: none;
  z-index: 5;
  --thermo-offset: 23px;
  --thermo-height: 182px;
}

.thermo-wrap {
  position: relative;
}

.thermometer {
  position: relative;
  width: 54px;
  height: 228px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--thermo-glass);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.thermometer-track {
  position: absolute;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  width: 18px;
  height: 182px;
  border-radius: 999px;
  background: var(--thermo-track);
  overflow: hidden;
}

.thermometer-fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--fill, 0) * 100%);
  border-radius: 999px 999px 7px 7px;
  /* The gradient is sized to the whole track, so each color always sits at
     the same temperature. */
  background: linear-gradient(0deg, var(--phase-solid) 0%, var(--phase-solid) 17%, var(--phase-liquid) 23%, var(--phase-liquid) 40%, var(--phase-gas) 46%, var(--phase-gas) 100%);
  background-size: 100% var(--thermo-height);
  background-position: bottom;
  transition: height 0.18s ease;
}

.thermometer-bulb {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--phase-solid);
  border: 2px solid var(--thermo-glass);
}

.thermo-mark {
  position: absolute;
  bottom: calc(var(--thermo-offset) + var(--thermo-height) * var(--p, 0));
  transform: translateY(50%);
}

.thermometer-target-line {
  /* Inside the thermometer's border box, so 1px lower than the markers. */
  --thermo-offset: 22px;
  left: 8px;
  right: 8px;
  height: 2px;
  border-radius: 999px;
  background: var(--text-main);
  transition: bottom 0.18s ease;
}

.thermometer-marker {
  right: calc(100% - 12px);
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
  gap: 0.3rem;
  white-space: nowrap;
}

.thermometer-marker::before {
  content: '';
  width: 20px;
  height: 2px;
  border-radius: 999px;
  background: var(--marker-color, var(--brand-primary));
}

.thermometer-marker--melt { --marker-color: var(--phase-liquid); }
.thermometer-marker--boil { --marker-color: var(--phase-gas); }

.thermometer-marker-label {
  padding: 2px 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.62rem;
  font-weight: 600;
  line-height: 1.2;
}

.thermo-readout {
  font-family: var(--font-mono);
  color: var(--text-main);
  background: var(--nav-bg);
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--nav-border);
  font-size: 0.72rem;
  font-weight: 600;
  /* Room for the widest reading ("1500 K") so the pill never changes width. */
  min-width: calc(6.5ch + 16px);
  text-align: center;
  align-self: flex-end;
  margin-right: 1px;
}

/* Controls Panel */
#controlsPanel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.2rem;
  overflow-y: auto;
  min-height: 0;
}

.controls-header h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 0.92rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.control-card {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.info-copy {
  margin: 0;
  font-size: 0.84rem;
  line-height: 1.5;
  color: var(--text-main);
}

.info-copy--muted {
  font-size: 0.76rem;
  color: var(--text-secondary);
}

.control-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.card-title,
.field-label {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.field-label {
  font-size: 0.74rem;
}

/* Status */
.status-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
}

.status-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.55rem 0.4rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  min-width: 0;
}

.status-label {
  font-size: 0.66rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.status-item strong {
  font-family: var(--font-mono);
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-main);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
}

.status-item .phase-value {
  font-family: var(--font-body);
  font-weight: 800;
}

.status-item .phase-dot {
  width: 10px;
  height: 10px;
}

/* State presets */
.state-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.state-preset {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  min-height: 56px;
  padding: 0.85rem 0.95rem;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text-main);
  font-family: var(--font-body);
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
}

.state-preset:hover {
  transform: translateY(-1px);
  border-color: var(--brand-line);
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.08);
}

.state-preset.is-active {
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 1px var(--brand-primary), 0 10px 22px var(--brand-soft);
}

.state-preset-main {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.state-preset-swatch {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  flex: 0 0 auto;
}

.state-preset-swatch--solid { background: var(--phase-solid); }
.state-preset-swatch--liquid { background: var(--phase-liquid); }
.state-preset-swatch--gas { background: var(--phase-gas); }

.state-preset-label {
  display: flex;
  flex-direction: column;
  gap: 0.18rem;
  min-width: 0;
}

.state-preset-label strong {
  font-size: 0.9rem;
  text-align: left;
}

.state-preset-label span {
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-align: left;
  line-height: 1.4;
}

.state-preset-temp {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* Fast-forward switch */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 48px;
  padding: 0.6rem 0.8rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
}

.toggle-text {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.toggle-text strong {
  font-size: 0.86rem;
}

.toggle-text span {
  font-size: 0.74rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.switch {
  -webkit-appearance: none;
  appearance: none;
  flex: 0 0 auto;
  position: relative;
  width: 46px;
  height: 26px;
  margin: 0;
  border-radius: 999px;
  background: var(--slider-track);
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s ease;
}

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

.switch:checked::after {
  transform: translateX(20px);
}

.switch:focus-visible {
  outline: 2px solid var(--brand-primary);
  outline-offset: 2px;
}

/* Energy */
.energy-units {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.energy-readout {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.5rem;
}

.energy-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.55rem 0.4rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.energy-item span {
  font-size: 0.66rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.energy-item strong {
  font-family: var(--font-mono);
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-main);
}

.energy-item--total {
  border-color: transparent;
  background: linear-gradient(135deg, var(--brand-soft), rgba(13, 148, 136, 0.12));
}

/* Color mode */
.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.35rem;
  padding: 0.3rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.segmented button {
  min-height: 40px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
}

.segmented button[aria-checked="true"] {
  background: var(--brand-soft);
  color: var(--brand-primary);
}

.segmented button:focus-visible {
  outline: 2px solid var(--brand-primary);
  outline-offset: 1px;
}

.legend {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0;
  font-size: 0.74rem;
  color: var(--text-secondary);
}

.legend-bar {
  flex: 1;
  height: 8px;
  border-radius: 999px;
}

.legend-bar--neighbors {
  background: linear-gradient(90deg, #FDE725, #35B779 50%, #31688E);
}

.legend-bar--speed {
  background: linear-gradient(90deg, #0EA5E9, #FACC15 55%, #DC2626);
}

.legend[hidden] {
  display: none;
}

/* Buttons */
.btn {
  border: 1px solid transparent;
  border-radius: 12px;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 700;
  font-family: var(--font-body);
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: transform 0.16s ease, filter 0.16s ease, border-color 0.16s ease;
  white-space: nowrap;
  text-align: center;
}

.btn:focus-visible,
.state-preset:focus-visible {
  outline: 2px solid var(--brand-primary);
  outline-offset: 2px;
}

.btn:hover {
  transform: translateY(-1px);
  filter: brightness(1.02);
}

.btn-primary {
  background: var(--btn-primary);
  color: var(--btn-text);
}

.btn-primary:hover {
  background: var(--btn-primary-hover);
}

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text-main);
  border-color: var(--border);
}

.btn-small {
  min-height: 36px;
  padding: 0.3rem 0.8rem;
  font-size: 0.8rem;
  background: var(--surface);
  color: var(--text-main);
  border-color: var(--border);
}

.sim-actions {
  display: flex;
  gap: 0.5rem;
}

.sim-actions .btn {
  flex: 1;
}

.keyboard-hint {
  margin: 0;
  font-size: 0.7rem;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  line-height: 1.5;
}

.copyright-note {
  margin: auto 0 0;
  font-size: 0.72rem;
  color: var(--text-secondary);
  text-align: center;
}

kbd {
  display: inline-block;
  padding: 1px 5px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 1px 0 var(--border);
}

@media (pointer: coarse) {
  .btn,
  .segmented button {
    min-height: 48px;
  }
}

/* Responsive */
@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  #app {
    height: auto;
    min-height: 100%;
  }

  #workspace {
    grid-template-columns: 1fr;
    align-items: start;
  }

  /* Keep the particles in view while the controls scroll underneath, so
     students can watch the sample change as they pick a state. */
  #simPanel {
    position: sticky;
    top: max(8px, env(safe-area-inset-top), calc(8px + var(--standalone-top)));
    z-index: 4;
    height: 46vh;
    height: 46svh;
    min-height: 260px;
    /* Paint over the gap above the pinned panel so scrolled controls don't
       show through it. */
    box-shadow: 0 -20px 0 6px var(--bg-color), var(--shadow);
  }

  #controlsPanel {
    min-height: auto;
    overflow: visible;
  }
}

@media (max-width: 900px) and (max-height: 520px) {
  #simPanel {
    position: relative;
    top: auto;
    height: 82vh;
    height: 82svh;
    box-shadow: var(--shadow);
  }
}

@media (max-width: 640px) {
  #titleBanner {
    width: calc(100% - 24px);
    margin: 12px auto 0;
    min-height: 52px;
    border-radius: 18px;
    padding: 8px 12px;
  }

  #workspace {
    width: calc(100% - 24px);
  }

  .banner-logo img {
    height: 28px;
  }

  .banner-title {
    font-size: 0.9rem;
    line-height: 1.2;
  }

  .sim-thermometer-overlay {
    right: 8px;
    transform: translateY(-50%) scale(0.72);
    transform-origin: right center;
  }

  .phase-badge {
    top: 10px;
    left: 10px;
    font-size: 0.8rem;
    padding: 4px 10px;
  }

  #controlsPanel {
    padding: 1rem;
  }
}
