:root {
  --ink: #18272a;
  --muted: #617174;
  --paper: #f4f0e7;
  --panel: #fffcf5;
  --line: #d9d4c8;
  --teal: #0b7d78;
  --teal-dark: #075c58;
  --teal-pale: #dff0ea;
  --coral: #ef6a4c;
  --coral-pale: #fde7dd;
  --blue: #4889a8;
  --blue-pale: #deedf3;
  --gold: #dca834;
  --purple: #7e6fa5;
  --shadow: 0 18px 48px rgba(37, 50, 45, 0.08);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background:
    radial-gradient(circle at 9% 5%, rgba(255, 255, 255, 0.9), transparent 28rem),
    var(--paper);
  font-family: "Manrope", sans-serif;
}

button {
  color: inherit;
  font: inherit;
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(11, 125, 120, 0.3);
  outline-offset: 3px;
}

.site-header {
  width: min(1440px, calc(100% - 64px));
  margin: 0 auto;
  padding: 24px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(84, 99, 95, 0.18);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 11px;
  color: var(--ink);
  font-weight: 800;
  letter-spacing: -0.02em;
  text-decoration: none;
}

.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  color: white;
  background: var(--teal);
  border-radius: 50%;
  font-family: "DM Mono", monospace;
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.18);
}

.cycle-legend {
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.legend-dot {
  width: 8px;
  height: 8px;
  background: var(--coral);
  border-radius: 50%;
  box-shadow: 0 0 0 5px var(--coral-pale);
}

main,
footer {
  width: min(1440px, calc(100% - 64px));
  margin-inline: auto;
}

.intro {
  padding: 48px 0 34px;
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(280px, 0.75fr);
  align-items: end;
  gap: 56px;
}

.eyebrow,
.panel-kicker {
  margin: 0 0 10px;
  color: var(--teal);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  max-width: 850px;
  margin-bottom: 0;
  font-size: clamp(2.45rem, 5vw, 5.15rem);
  line-height: 0.98;
  letter-spacing: -0.065em;
}

h1 em {
  color: var(--teal);
  font-family: Georgia, serif;
  font-weight: 400;
}

.intro-copy {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.75;
}

.visualizer {
  display: grid;
  grid-template-columns: 1.08fr 0.92fr;
  gap: 22px;
}

.panel,
.explanation {
  background: rgba(255, 252, 245, 0.92);
  border: 1px solid rgba(102, 108, 98, 0.16);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.panel {
  min-height: 580px;
  padding: 26px;
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

.panel-heading h2 {
  margin-bottom: 0;
  font-size: 1.45rem;
  letter-spacing: -0.035em;
}

.help-button {
  padding: 8px 11px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 700;
}

.help-button:hover {
  background: #f0ece3;
}

.help-button span {
  width: 19px;
  height: 19px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
}

.graph-help {
  margin: 16px 0 -4px;
  padding: 12px 14px;
  color: var(--muted);
  background: var(--teal-pale);
  border-radius: 10px;
  font-size: 0.8rem;
  line-height: 1.5;
}

.graph-wrap {
  position: relative;
  height: 380px;
  margin-top: 10px;
}

#pvCanvas,
#particleCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

#pvCanvas {
  cursor: crosshair;
}

.canvas-tooltip {
  position: absolute;
  z-index: 4;
  padding: 7px 10px;
  color: white;
  background: var(--ink);
  border-radius: 7px;
  font-size: 0.7rem;
  font-weight: 700;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -145%);
  transition: opacity 120ms ease;
  white-space: nowrap;
}

.canvas-tooltip.is-visible {
  opacity: 1;
}

.stage-selector {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 3px;
}

.stage-tab {
  min-height: 45px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 700;
  text-align: left;
  transition: 160ms ease;
}

.stage-tab:hover {
  color: var(--ink);
  background: #f5f1e9;
}

.stage-tab span {
  width: 25px;
  height: 25px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  color: var(--teal);
  background: var(--teal-pale);
  border-radius: 50%;
  font-family: "DM Mono", monospace;
  font-size: 0.68rem;
}

.stage-tab.is-active {
  color: var(--teal-dark);
  background: var(--teal-pale);
  border-color: rgba(11, 125, 120, 0.16);
}

.stage-tab.is-active span {
  color: white;
  background: var(--teal);
}

.animation-panel {
  position: relative;
  overflow: hidden;
}

.temperature-badge {
  min-height: 36px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #a6432d;
  background: var(--coral-pale);
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 800;
}

.temperature-badge.cool {
  color: #2e6f8f;
  background: var(--blue-pale);
}

.temperature-icon {
  width: 7px;
  height: 17px;
  position: relative;
  border: 2px solid currentColor;
  border-radius: 5px;
}

.temperature-icon::after {
  content: "";
  width: 9px;
  height: 9px;
  position: absolute;
  left: 50%;
  bottom: -6px;
  background: currentColor;
  border-radius: 50%;
  transform: translateX(-50%);
}

.experiment {
  height: 400px;
  position: relative;
  display: grid;
  place-items: end center;
  overflow: hidden;
}

.cylinder {
  width: min(235px, 48%);
  height: 286px;
  position: absolute;
  left: 50%;
  bottom: 65px;
  overflow: visible;
  background: linear-gradient(90deg, rgba(222, 238, 235, 0.48), rgba(248, 252, 247, 0.86) 45%, rgba(211, 231, 229, 0.52));
  border: 3px solid #70908d;
  border-top: 0;
  border-radius: 0 0 20px 20px;
  transform: translateX(-50%);
}

.cylinder::after {
  content: "GAS";
  position: absolute;
  right: 10px;
  bottom: 9px;
  color: rgba(46, 87, 82, 0.36);
  font-family: "DM Mono", monospace;
  font-size: 0.6rem;
  letter-spacing: 0.12em;
}

.piston {
  width: calc(100% + 8px);
  height: 24px;
  position: absolute;
  z-index: 4;
  left: -4px;
  top: 100px;
  background: linear-gradient(#456966, #284b49);
  border: 3px solid #183c3a;
  border-radius: 4px;
  box-shadow: 0 7px 0 rgba(22, 56, 54, 0.12);
}

.piston-rod {
  width: 28px;
  height: 175px;
  position: absolute;
  z-index: -1;
  left: 50%;
  bottom: 13px;
  background: linear-gradient(90deg, #9cb0ac, #dae2df 45%, #819893);
  border: 2px solid #667f7b;
  transform: translateX(-50%);
}

.cylinder-shine {
  width: 4px;
  height: 74%;
  position: absolute;
  z-index: 5;
  right: 12px;
  bottom: 16px;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 10px;
  pointer-events: none;
}

.reservoir {
  width: min(310px, 66%);
  height: 48px;
  position: absolute;
  left: 50%;
  bottom: 7px;
  display: grid;
  place-items: center;
  color: #8f3b27;
  background: repeating-linear-gradient(135deg, #f9c7b6 0 10px, #f7bba7 10px 20px);
  border: 2px solid #e7886f;
  border-radius: 8px;
  font-size: 0.72rem;
  font-weight: 800;
  transform: translateX(-50%);
  transition: 250ms ease;
}

.reservoir.cold {
  color: #2b6380;
  background: repeating-linear-gradient(135deg, #c7e2eb 0 10px, #b9d9e5 10px 20px);
  border-color: #78afc4;
}

.reservoir.insulated {
  color: #5d6764;
  background: repeating-linear-gradient(135deg, #e0ded5 0 10px, #d4d2c9 10px 20px);
  border-color: #aaa99f;
}

.heat-flow {
  width: min(210px, 45%);
  height: 116px;
  position: absolute;
  z-index: 8;
  left: 50%;
  bottom: 42px;
  color: var(--coral);
  pointer-events: none;
  transform: translateX(-50%);
}

.heat-arrow {
  width: 3px;
  height: 42px;
  position: absolute;
  bottom: 0;
  left: 50%;
  display: block;
  background: currentColor;
  border-radius: 99px;
  box-shadow: 0 0 10px currentColor;
  opacity: 0;
  animation: heatIntoGas 1.65s ease-in-out infinite;
}

.heat-arrow::before {
  content: "";
  width: 11px;
  height: 11px;
  position: absolute;
  left: 50%;
  top: -1px;
  border-top: 3px solid currentColor;
  border-left: 3px solid currentColor;
  transform: translate(-50%, -2px) rotate(45deg);
}

.heat-arrow:nth-child(1) {
  margin-left: -58px;
  transform: rotate(-7deg);
  animation-delay: -1.1s;
}

.heat-arrow:nth-child(2) {
  animation-delay: -0.55s;
}

.heat-arrow:nth-child(3) {
  margin-left: 58px;
  transform: rotate(7deg);
}

.heat-flow b {
  position: absolute;
  right: -8px;
  bottom: 9px;
  color: currentColor;
  font-family: "DM Mono", monospace;
  font-size: 0.7rem;
  letter-spacing: 0.04em;
}

.heat-flow-out {
  color: var(--blue);
}

.heat-flow-out .heat-arrow {
  top: 0;
  bottom: auto;
  animation-name: heatOutOfGas;
}

.heat-flow-out .heat-arrow::before {
  top: auto;
  bottom: -1px;
  transform: translate(-50%, 2px) rotate(225deg);
}

.heat-flow-out b {
  top: 9px;
  bottom: auto;
}

.heat-flow-none {
  opacity: 0;
}

.work-label {
  position: absolute;
  z-index: 8;
  left: calc(50% + 145px);
  top: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  color: var(--teal-dark);
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.work-arrow {
  margin-bottom: 4px;
  font-size: 2rem;
  line-height: 1;
}

.playback {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.control-button {
  min-height: 41px;
  padding: 9px 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  font-size: 0.73rem;
  font-weight: 800;
}

.control-button:hover {
  color: var(--ink);
  background: #f4f0e8;
}

.control-button.primary {
  min-width: 92px;
  color: white;
  background: var(--teal);
  border-color: var(--teal);
}

.control-button.primary:hover {
  background: var(--teal-dark);
}

.next-button {
  margin-left: auto;
  color: var(--teal-dark);
  border-color: rgba(11, 125, 120, 0.3);
}

.explanation {
  margin-top: 22px;
  padding: 30px 34px;
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) minmax(280px, 0.68fr);
  gap: 32px;
}

.stage-number {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  color: white;
  background: var(--teal);
  border-radius: 50%;
  font-family: "DM Mono", monospace;
  font-weight: 500;
}

.explanation-copy h2 {
  margin-bottom: 12px;
  font-size: clamp(1.45rem, 2.2vw, 2rem);
  letter-spacing: -0.04em;
}

.explanation-copy > p:not(.panel-kicker) {
  max-width: 700px;
  margin-bottom: 18px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.7;
}

.equation {
  padding: 12px 15px;
  display: inline-flex;
  align-items: center;
  gap: 18px;
  background: #f0ece3;
  border-radius: 9px;
}

.equation span {
  font-family: "DM Mono", monospace;
  font-size: 0.88rem;
  font-weight: 500;
}

.equation small {
  color: var(--muted);
  font-size: 0.68rem;
}

.energy-balance {
  padding-left: 30px;
  border-left: 1px solid var(--line);
}

.energy-balance h3 {
  margin-bottom: 18px;
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.energy-row {
  margin-top: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 0.72rem;
}

.energy-row > div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.energy-row strong {
  color: var(--ink);
  font-family: "DM Mono", monospace;
}

.energy-symbol {
  width: 23px;
  height: 23px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  font-family: "DM Mono", monospace;
  font-weight: 500;
}

.energy-symbol.heat {
  color: #b4462e;
  background: var(--coral-pale);
}

.energy-symbol.work {
  color: var(--teal-dark);
  background: var(--teal-pale);
}

.energy-symbol.internal {
  color: #625487;
  background: #ece7f3;
}

.energy-meter {
  height: 5px;
  margin-top: 7px;
  overflow: hidden;
  background: #e7e3da;
  border-radius: 99px;
}

.energy-meter span {
  width: 75%;
  height: 100%;
  display: block;
  background: var(--coral);
  border-radius: inherit;
  transform-origin: left;
  transition: width 350ms ease, margin-left 350ms ease, background 350ms ease;
}

.energy-meter:nth-of-type(2) span {
  background: var(--teal);
}

.energy-meter.split {
  position: relative;
  overflow: visible;
}

.energy-meter.split::after {
  content: "";
  width: 1px;
  height: 11px;
  position: absolute;
  left: 50%;
  top: -3px;
  background: #938f87;
}

.energy-meter.split span {
  width: 0;
  margin-left: 50%;
  background: var(--purple);
}

.sign-note {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.66rem;
  line-height: 1.5;
}

footer {
  padding: 24px 0 38px;
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.68rem;
}

footer p {
  margin: 0;
}

@keyframes heatIntoGas {
  0% { bottom: 0; opacity: 0; }
  18% { opacity: 0.95; }
  72% { opacity: 0.95; }
  100% { bottom: 66px; opacity: 0; }
}

@keyframes heatOutOfGas {
  0% { top: 0; opacity: 0; }
  18% { opacity: 0.95; }
  72% { opacity: 0.95; }
  100% { top: 66px; opacity: 0; }
}

@media (max-width: 1050px) {
  .visualizer {
    grid-template-columns: 1fr;
  }

  .panel {
    min-height: 0;
  }

  .explanation {
    grid-template-columns: 60px 1fr;
  }

  .energy-balance {
    grid-column: 2;
    padding: 24px 0 0;
    border-top: 1px solid var(--line);
    border-left: 0;
  }
}

@media (max-width: 700px) {
  .site-header,
  main,
  footer {
    width: min(100% - 30px, 1440px);
  }

  .cycle-legend {
    display: none;
  }

  .intro {
    padding-top: 36px;
    grid-template-columns: 1fr;
    gap: 20px;
  }

  h1 {
    font-size: clamp(2.35rem, 13vw, 3.8rem);
  }

  .panel {
    padding: 20px 16px;
    border-radius: 16px;
  }

  .graph-wrap {
    height: 330px;
  }

  .stage-selector {
    grid-template-columns: 1fr;
  }

  .experiment {
    height: 370px;
  }

  .cylinder {
    width: 52%;
  }

  .reservoir {
    width: 70%;
  }

  .work-label {
    left: auto;
    right: 2%;
  }

  .explanation {
    padding: 24px 20px;
    grid-template-columns: 1fr;
    gap: 18px;
  }

  .stage-number {
    width: 46px;
    height: 46px;
  }

  .energy-balance {
    grid-column: 1;
  }

  .equation {
    display: flex;
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }

  footer {
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
