:root,
html.light {
  color-scheme: light;
  --bg: #d8d8d6;
  --bg-glow: #ececeb;
  --panel: #efefed;
  --ink: #141414;
  --muted: #5c5c5a;
  --line: #c2c2bf;
  --accent: #1a1a1a;
  --accent-ink: #f4f4f2;
  --ghost: #9a9a96;
  --hex-fill: rgba(20, 20, 20, 0.04);
  --hex-hover: rgba(20, 20, 20, 0.08);
  --piece: #141414;
  --piece-hi: #2a2a2a;
  --piece-groove: rgba(255, 255, 255, 0.12);
  --field: #f7f7f5;
  --danger: #a33b3b;
  --shadow: rgba(20, 20, 20, 0.12);
}

html.dark {
  color-scheme: dark;
  --bg: #2f2f2c;
  --bg-glow: #3a3a36;
  --panel: #252522;
  --ink: #f0f0ee;
  --muted: #a3a39c;
  --line: #40403b;
  --accent: #f0f0ee;
  --accent-ink: #141414;
  --ghost: #7a7a72;
  --hex-fill: rgba(240, 240, 238, 0.05);
  --hex-hover: rgba(240, 240, 238, 0.09);
  --piece: #e4e4df;
  --piece-hi: #ffffff;
  --piece-groove: rgba(0, 0, 0, 0.28);
  --field: #1b1b18;
  --danger: #e08a8a;
  --shadow: rgba(0, 0, 0, 0.4);
}

:root {
  --radius: 14px;
  --font: "Inter", -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(1200px 700px at 20% -10%, var(--bg-glow), transparent 55%),
    radial-gradient(900px 600px at 100% 100%, var(--bg-glow), transparent 50%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1rem 1.5rem;
  padding: 0.9rem 1.35rem 0.8rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(10px);
}

.toolbar-brand h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 650;
  letter-spacing: -0.03em;
}

.tagline {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.84rem;
  max-width: 36rem;
  line-height: 1.35;
}

.toolbar-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.85rem 1.1rem;
  margin-left: auto;
}

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

.grid-size {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.grid-size label,
.part-count label {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 500;
}

input[type="number"] {
  width: 4.2rem;
  padding: 0.42rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
}

button {
  padding: 0.48rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font: inherit;
  font-size: 0.84rem;
  font-weight: 550;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease;
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}

button:hover {
  filter: brightness(1.06);
}

button:active {
  transform: translateY(1px);
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.error {
  flex-basis: 100%;
  margin: 0;
  color: var(--danger);
  font-size: 0.85rem;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 22.5rem;
  min-height: 0;
}

.canvas-panel {
  position: relative;
  min-height: 0;
  padding: 0.75rem;
  display: flex;
}

#grid {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  flex: 1;
  background:
    radial-gradient(circle at 30% 20%, color-mix(in srgb, var(--bg-glow) 70%, transparent), transparent 50%),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 2px);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.hex {
  cursor: pointer;
}

.hex.ghost {
  fill: transparent;
  stroke: var(--ghost);
  stroke-width: 0.65;
  stroke-dasharray: 1.8 1.5;
  opacity: 0.75;
}

.hex.filled {
  fill: var(--hex-fill);
  stroke: none;
}

.hex:hover {
  fill: var(--hex-hover);
}

.edge-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 7;
  stroke-linecap: round;
  cursor: pointer;
}

.edge-hit:hover {
  stroke: var(--ghost);
  stroke-opacity: 0.9;
}

.piece {
  pointer-events: none;
}

.bar-shape,
.stand-body,
.stick-body,
.joint-hub,
.joint-arm-body,
.joint-tab {
  fill: var(--piece);
  stroke: var(--piece-hi);
  stroke-width: 0.35;
  stroke-linejoin: miter;
  stroke-miterlimit: 6;
  shape-rendering: geometricPrecision;
}

.bar-groove {
  fill: none;
  stroke: var(--piece-groove);
  stroke-linecap: round;
}

.joint-tab {
  fill: var(--piece-hi);
}

.edge-menu {
  position: fixed;
  z-index: 20;
  min-width: 12rem;
  padding: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 14px 36px var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
}

.edge-menu[hidden] {
  display: none;
}

.edge-menu-title {
  margin: 0;
  padding: 0.4rem 0.7rem 0.25rem;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.3;
  font-weight: 500;
}

.edge-menu button {
  margin: 0;
  width: 100%;
  justify-content: flex-start;
  background: transparent;
  color: var(--ink);
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 0.5rem 0.7rem;
  font-weight: 525;
}

.edge-menu button:hover:not(:disabled),
.edge-menu button:focus-visible:not(:disabled) {
  background: var(--hex-hover);
  border-color: var(--line);
  filter: none;
}

.edge-menu button:disabled {
  color: var(--muted);
  opacity: 0.38;
  cursor: not-allowed;
}

.edge-menu button.danger {
  color: var(--danger);
  border-top: 1px solid var(--line);
  border-radius: 0 0 9px 9px;
  margin-top: 0.15rem;
  padding-top: 0.6rem;
}

.parts-panel {
  padding: 1.1rem 1.25rem 1.35rem;
  border-left: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  overflow: auto;
}

.parts-panel h2 {
  margin: 0;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
}

.hex-count {
  margin: 0.3rem 0 1.05rem;
  color: var(--muted);
  font-size: 0.88rem;
}

.parts {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.parts li {
  display: grid;
  grid-template-columns: 3rem 1fr auto;
  gap: 0.6rem;
  align-items: center;
  font-size: 0.9rem;
}

.parts li.selectable {
  margin: 0 -0.35rem;
  padding: 0.4rem 0.45rem;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
}

.parts li.selectable:hover {
  background: var(--hex-hover);
}

.parts li.selectable.selected {
  border-color: color-mix(in srgb, var(--ink) 35%, transparent);
  background: var(--hex-fill);
}

.part-icon {
  width: 3rem;
  height: 1.55rem;
  display: block;
  overflow: visible;
}

.part-count input {
  width: 3.6rem;
  text-align: right;
}

.hint {
  margin: 1.15rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

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

  .parts-panel {
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .toolbar-controls {
    margin-left: 0;
  }
}
