:root {
  --ink: #223748;
  --ink-soft: #476473;
  --paper: #fffaf0;
  --paper-deep: #f8e8bd;
  --water: #3fb6e8;
  --water-deep: #167ca6;
  --mint: #8be0bb;
  --coral: #ff746d;
  --gold: #ffc954;
  --blueprint: #d9f0f6;
  --line: #243f52;
  --shadow: rgba(20, 55, 72, 0.18);
  font-family: "Trebuchet MS", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 14% 18%, rgba(255, 201, 84, 0.28), transparent 25%),
    linear-gradient(145deg, #effbff 0%, #fff8df 58%, #e1f7ef 100%);
  color: var(--ink);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100%;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 14px 18px 10px;
  pointer-events: auto;
}

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

.brand-mark {
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border: 4px solid var(--line);
  border-radius: 50%;
  background: var(--water);
  color: white;
  box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.12), 0 8px 0 rgba(36, 63, 82, 0.12);
  font-weight: 900;
  letter-spacing: 0;
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 2.3vw, 2rem);
  letter-spacing: 0;
  line-height: 1;
}

.brand p {
  margin: 5px 0 0;
  color: var(--ink-soft);
  font-size: 0.94rem;
}

.actions {
  display: flex;
  gap: 8px;
}

.tool-button {
  width: 42px;
  height: 42px;
  border: 3px solid var(--line);
  border-radius: 12px;
  background: white;
  color: var(--line);
  box-shadow: 0 5px 0 rgba(36, 63, 82, 0.15);
  font-weight: 900;
}

.tool-button:active {
  transform: translateY(2px);
  box-shadow: 0 3px 0 rgba(36, 63, 82, 0.12);
}

.tool-button.danger {
  color: #c63f45;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(150px, 188px) 1fr;
  min-height: 0;
  padding: 0 16px 16px;
  gap: 14px;
}

.palette {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  padding: 12px;
  border: 4px solid var(--line);
  border-radius: 20px;
  background: rgba(255, 250, 240, 0.86);
  box-shadow: 0 12px 0 rgba(36, 63, 82, 0.1), 0 18px 30px var(--shadow);
  backdrop-filter: blur(10px);
}

.palette-title {
  margin: 2px 4px 10px;
  font-size: 0.9rem;
  font-weight: 900;
  color: var(--ink-soft);
}

.palette-list {
  display: grid;
  gap: 10px;
  min-width: 0;
  overflow: auto;
  padding: 0 2px 6px;
}

.part-card {
  display: grid;
  grid-template-columns: 62px 1fr;
  gap: 9px;
  align-items: center;
  min-height: 72px;
  padding: 7px 8px;
  border: 3px solid var(--line);
  border-radius: 16px;
  background: #ffffff;
  box-shadow: 0 5px 0 rgba(36, 63, 82, 0.12);
  transition: transform 140ms ease, background 140ms ease;
}

.part-card:hover {
  transform: translateY(-2px);
  background: #fff7d9;
}

.part-card:active {
  transform: translateY(1px);
}

.part-card svg {
  width: 62px;
  height: 50px;
  overflow: visible;
}

.part-card span {
  font-size: 0.82rem;
  line-height: 1.15;
  font-weight: 900;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 4px solid var(--line);
  border-radius: 24px;
  background: var(--blueprint);
  box-shadow: 0 14px 0 rgba(36, 63, 82, 0.1), 0 22px 38px var(--shadow);
}

.board {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

.board.drawing,
.board.dragging {
  cursor: crosshair;
}

.board.panning {
  cursor: grabbing;
}

.hud {
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  color: var(--ink);
  font-size: 0.86rem;
  font-weight: 900;
}

.hud span {
  padding: 7px 10px;
  border: 3px solid rgba(36, 63, 82, 0.55);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.76);
  box-shadow: 0 4px 0 rgba(36, 63, 82, 0.08);
}

.fps-slider {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 5px 12px 5px 14px;
  border: 3px solid rgba(36, 63, 82, 0.55);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: 0 4px 0 rgba(36, 63, 82, 0.08);
  pointer-events: auto;
  cursor: pointer;
}

.fps-slider .fps-label,
.fps-slider .fps-num {
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  font-weight: 900;
  font-size: 0.86rem;
  color: var(--ink);
}

.fps-slider .fps-num {
  min-width: 22px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.fps-slider input[type="range"] {
  width: 120px;
  height: 18px;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.fps-slider input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--water) 0%, var(--water-deep) 100%);
  border: 2px solid var(--line);
}

.fps-slider input[type="range"]::-moz-range-track {
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--water) 0%, var(--water-deep) 100%);
  border: 2px solid var(--line);
}

.fps-slider input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -8px;
  border-radius: 50%;
  background: var(--gold);
  border: 3px solid var(--line);
  box-shadow: 0 2px 0 rgba(36, 63, 82, 0.2);
  cursor: pointer;
}

.fps-slider input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--gold);
  border: 3px solid var(--line);
  box-shadow: 0 2px 0 rgba(36, 63, 82, 0.2);
  cursor: pointer;
}

.pipe-hit {
  stroke: transparent;
  stroke-width: 24;
  stroke-linecap: round;
  pointer-events: stroke;
}

.pipe-body {
  stroke: var(--water);
  stroke-width: 14;
  stroke-linecap: round;
  fill: none;
  filter: url(#softShadow);
}

.pipe-outline {
  stroke: var(--line);
  stroke-width: 20;
  stroke-linecap: round;
  fill: none;
}

.pipe-highlight {
  stroke: var(--gold);
  stroke-width: 24;
  stroke-linecap: round;
  fill: none;
  opacity: 0.82;
}

.flow-dot {
  fill: rgba(255, 255, 255, 0.92);
  stroke: rgba(22, 124, 166, 0.42);
  stroke-width: 1.2;
  pointer-events: none;
}

.draft-line {
  stroke: rgba(255, 116, 109, 0.92);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 12 9;
  fill: none;
  animation: water-run 1200ms linear infinite;
}

.junction {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 4;
  filter: url(#softShadow);
}

.node {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 4;
  pointer-events: all;
}

.node.plus {
  fill: #fff2b5;
}

.node.minus {
  fill: #dff7ff;
}

.node-label,
.pressure-label,
.part-name {
  fill: var(--ink);
  font-weight: 900;
  paint-order: stroke;
  stroke: rgba(255, 255, 255, 0.88);
  stroke-width: 4;
  stroke-linejoin: round;
  pointer-events: none;
}

.node-label {
  font-size: 15px;
}

.pressure-label {
  font-size: 12px;
}

.part-name {
  font-size: 13px;
}

.part-shell {
  fill: #fff8df;
  stroke: var(--line);
  stroke-width: 6;
  stroke-linejoin: round;
  filter: url(#softShadow);
}

.part-water {
  fill: var(--water);
  stroke: var(--line);
  stroke-width: 5;
  stroke-linejoin: round;
}

.pipe-neck {
  filter: url(#softShadow);
}

.part-detail {
  fill: none;
  stroke: var(--line);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.valve-door {
  fill: var(--gold);
  stroke: var(--line);
  stroke-width: 4;
  transform-box: fill-box;
}

.valve-door-top {
  transform-origin: left center;
  transform: rotate(29deg);
}

.valve-door-bottom {
  transform-origin: left center;
  transform: rotate(-29deg);
}

.door-open-top {
  transform: rotate(-14deg);
}

.door-open-bottom {
  transform: rotate(14deg);
}

.lamp-rays {
  fill: none;
  stroke: var(--gold);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-linejoin: round;
  paint-order: stroke;
  filter: url(#softShadow);
  animation: ray-pop 820ms ease-in-out infinite alternate;
}

.rotate-handle,
.delete-handle {
  pointer-events: all;
}

.handle-bg {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 4;
}

.delete-handle .handle-bg {
  fill: #ffe2e0;
}

.handle-text {
  fill: var(--line);
  font-size: 20px;
  font-weight: 900;
  pointer-events: none;
}

.invalid-drop .part-shell,
.invalid-drop .part-water {
  stroke: var(--coral);
  animation: warning-pop 540ms ease-in-out infinite alternate;
}

.attached .rotate-handle {
  opacity: 0.35;
}

.valve-flap,
.capacitor-bubble,
.flywheel-rotor,
.lamp-glow,
.switch-pulse,
.piston,
.water-valve-piston,
.valve-spring,
.transistor-piston {
  transform-box: fill-box;
  transform-origin: center;
}

.piston-rod,
.piston-head {
  fill: #ffc954;
  stroke: var(--line);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.piston-cap {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 4;
}

.water-valve-piston.valve-open {
  transform: translateY(26px);
}

.water-valve-piston.valve-closed .piston-head {
  fill: #ff746d;
}

.water-valve-piston.valve-open .piston-head {
  fill: #ffc954;
}

.valve-spring {
  transform-origin: center bottom;
  transition: transform 180ms ease;
}

.valve-spring.spring-compressed {
  transform: translateY(5px) scaleY(0.42);
}

.spring-wire,
.spring-base,
.spring-bounce-mark,
.latch-notch {
  fill: none;
  stroke: var(--line);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.spring-wire {
  stroke-width: 4;
}

.spring-bounce-mark {
  opacity: 0.72;
}

.piston-blocking {
  transform: translateX(18px);
}

.transistor-piston {
  transition: transform 140ms linear;
}

.flywheel-weight {
  fill: #ffc954;
  stroke: var(--line);
  stroke-width: 4;
}

.flywheel-mark {
  fill: none;
  stroke: #ff746d;
  stroke-width: 5;
  stroke-linecap: round;
}

.scope-screen {
  fill: #142433;
  stroke: var(--line);
  stroke-width: 5;
  stroke-linejoin: round;
}

.scope-grid {
  stroke: rgba(120, 220, 200, 0.18);
  stroke-width: 1;
  pointer-events: none;
}

.scope-trace {
  fill: none;
  stroke: #66ffb8;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 3px rgba(102, 255, 184, 0.7));
  pointer-events: none;
}

.scope-readout {
  fill: #66ffb8;
  font-size: 10px;
  font-weight: 700;
  font-family: "Trebuchet MS", monospace;
  pointer-events: none;
}

.part-card .valve-flap,
.live-part .valve-flap {
  animation: flap 1.2s ease-in-out infinite alternate;
}

.part-card .valve-door-top {
  animation: door-open-top 1.2s ease-in-out infinite alternate;
}

.part-card .valve-door-bottom {
  animation: door-open-bottom 1.2s ease-in-out infinite alternate;
}

.part-card .capacitor-bubble {
  animation: bubble 1.4s ease-in-out infinite alternate;
}

.accumulator-neck {
  filter: none;
}

.accumulator-membrane {
  opacity: 0.75;
}

.part-card .flywheel-rotor {
  animation: spin 1.4s linear infinite;
}

.part-card .lamp-glow,
.live-part.part-load .lamp-glow {
  animation: glow 1.1s ease-in-out infinite alternate;
}

.part-card .switch-pulse,
.live-part .switch-pulse {
  animation: press 1s ease-in-out infinite alternate;
}

.part-card .piston-demo {
  animation: piston-push 1.25s ease-in-out infinite alternate;
}

.part-card .spring-valve-demo {
  animation: valve-press-down 1.15s ease-in-out infinite alternate;
}

.part-card .part-momentary .valve-spring {
  animation: spring-compress-demo 1.15s ease-in-out infinite alternate;
}

.drag-ghost {
  position: fixed;
  z-index: 50;
  width: 100px;
  height: 78px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  opacity: 0.9;
}

.drag-ghost svg {
  width: 100%;
  height: 100%;
}

.editor {
  width: min(360px, calc(100vw - 28px));
  border: 4px solid var(--line);
  border-radius: 22px;
  padding: 0;
  background: #fffdf4;
  box-shadow: 0 24px 80px rgba(16, 45, 62, 0.32);
  color: var(--ink);
}

.editor::backdrop {
  background: rgba(34, 55, 72, 0.24);
  backdrop-filter: blur(2px);
}

.editor form {
  margin: 0;
  padding: 18px;
}

.editor-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.editor h2 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0;
}

.icon-close {
  width: 36px;
  height: 36px;
  border: 3px solid var(--line);
  border-radius: 50%;
  background: #ffe9e7;
  color: var(--line);
  font-size: 24px;
  line-height: 1;
  font-weight: 900;
}

.editor-fields {
  display: grid;
  gap: 12px;
  margin: 18px 0;
}

.field {
  display: grid;
  gap: 6px;
  font-weight: 900;
}

.field input {
  width: 100%;
  border: 3px solid var(--line);
  border-radius: 13px;
  background: white;
  color: var(--ink);
  padding: 10px 12px;
  outline: none;
}

.field input:focus {
  box-shadow: 0 0 0 4px rgba(63, 182, 232, 0.22);
}

.editor-actions {
  display: flex;
  justify-content: flex-end;
  gap: 9px;
}

.modal-button {
  min-width: 86px;
  border: 3px solid var(--line);
  border-radius: 14px;
  padding: 9px 13px;
  font-weight: 900;
  box-shadow: 0 5px 0 rgba(36, 63, 82, 0.12);
}

.modal-button.primary {
  background: var(--mint);
  color: var(--line);
}

.modal-button.ghost {
  background: #fff;
  color: var(--line);
}

.modal-button.danger {
  background: #ffe2e0;
  color: #be3940;
}

@keyframes water-run {
  to {
    stroke-dashoffset: -34;
  }
}

@keyframes flap {
  0% {
    transform: rotate(-9deg) scaleX(0.85);
  }
  100% {
    transform: rotate(10deg) scaleX(1.05);
  }
}

@keyframes door-open-top {
  0% {
    transform: rotate(29deg);
  }
  100% {
    transform: rotate(-14deg);
  }
}

@keyframes door-open-bottom {
  0% {
    transform: rotate(-29deg);
  }
  100% {
    transform: rotate(14deg);
  }
}

@keyframes bubble {
  0% {
    transform: scale(0.84, 0.72);
  }
  100% {
    transform: scale(1.92, 1.9);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes glow {
  0% {
    opacity: 0.25;
    transform: scale(0.78);
  }
  100% {
    opacity: 0.9;
    transform: scale(1.18);
  }
}

@keyframes ray-pop {
  0% {
    opacity: 0.62;
    transform: translateY(3px) scale(0.9);
  }
  100% {
    opacity: 1;
    transform: translateY(-1px) scale(1.06);
  }
}

@keyframes press {
  0% {
    transform: translateY(-7px) rotate(-8deg);
  }
  100% {
    transform: translateY(5px) rotate(3deg);
  }
}

@keyframes piston-push {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(18px);
  }
}

@keyframes valve-press-down {
  0%,
  38% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(26px);
  }
}

@keyframes spring-compress-demo {
  0%,
  38% {
    transform: translateY(0) scaleY(1);
  }
  100% {
    transform: translateY(5px) scaleY(0.42);
  }
}

@keyframes warning-pop {
  to {
    stroke-width: 9;
  }
}

@media (max-width: 760px) {
  .topbar {
    padding: 10px;
  }

  .brand-mark {
    width: 46px;
    height: 46px;
    font-size: 0.82rem;
  }

  .brand p {
    display: none;
  }

  .workbench {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    padding: 0 10px 10px;
    gap: 10px;
  }

  .palette {
    min-height: 112px;
    padding: 8px;
    border-radius: 18px;
  }

  .palette-title {
    display: none;
  }

  .palette-list {
    display: flex;
    width: 100%;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 4px;
  }

  .part-card {
    flex: 0 0 136px;
    min-height: 78px;
    grid-template-columns: 56px 1fr;
  }

  .part-card svg {
    width: 56px;
  }

  .stage {
    border-radius: 20px;
  }
}
