:root {
  color-scheme: light;
  --bg: #eef2f6;
  --panel: #f9fafb;
  --panel-strong: #ffffff;
  --ink: #18202b;
  --muted: #637083;
  --line: #cbd5e1;
  --grid: rgba(99, 112, 131, 0.22);
  --accent: #0f766e;
  --accent-2: #2563eb;
  --true: #16a34a;
  --false: #94a3b8;
  --danger: #dc2626;
  --warn: #d97706;
  --shadow: 0 10px 30px rgba(24, 32, 43, 0.09);
  --cell: 32px;
  font-family:
    "Segoe UI",
    "Hiragino Kaku Gothic ProN",
    "Yu Gothic",
    Meiryo,
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.hidden {
  display: none !important;
}

.home-screen {
  min-height: 100vh;
  display: grid;
  place-items: start center;
  padding: 13vh 24px 48px;
  background:
    linear-gradient(rgba(248, 250, 252, 0.82), rgba(238, 242, 246, 0.92)),
    linear-gradient(to right, rgba(99, 112, 131, 0.18) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(99, 112, 131, 0.18) 1px, transparent 1px);
  background-size: auto, 32px 32px, 32px 32px;
}

.home-content {
  width: min(560px, 100%);
  text-align: center;
}

.home-content h1 {
  margin: 0 0 34px;
  font-size: clamp(58px, 10vw, 112px);
  line-height: 0.95;
  font-weight: 800;
  color: #102033;
}

.mode-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.mode-button {
  min-height: 58px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-size: 18px;
  font-weight: 700;
  box-shadow: var(--shadow);
}

.mode-button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.mode-button:hover {
  transform: translateY(-1px);
}

.editor-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: relative;
  min-height: 70px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-strong);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.15;
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.top-lesson-progress {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.workspace {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 188px minmax(420px, 1fr);
  grid-template-areas: "palette board";
}

.workspace.learning {
  grid-template-columns: 188px minmax(420px, 1fr);
  grid-template-areas: "palette board";
  padding-right: 300px;
}

.palette {
  grid-area: palette;
}

.board-shell {
  grid-area: board;
}

.lesson-panel {
  grid-area: lesson;
}

.palette,
.inspector,
.lesson-panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 14px;
  overflow: auto;
}

.inspector {
  border-right: 0;
  border-left: 1px solid var(--line);
}

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

.workspace.learning .lesson-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 300px;
  z-index: 4;
}

.palette section + section {
  margin-top: 18px;
}

.palette h2,
.inspector h2,
.lesson-panel h2 {
  margin: 0 0 10px;
  font-size: 12px;
  line-height: 1.2;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0;
}

.lesson-title {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
}

.lesson-copy {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.lesson-progress {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin: 14px 0;
}

.step-dot {
  width: 30px;
  height: 30px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--muted);
  font-size: 12px;
}

.step-dot.active {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #ffffff;
}

.step-dot.done {
  background: #dcfce7;
  border-color: var(--true);
  color: #14532d;
}

.lesson-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 14px;
}

.lesson-actions .wide {
  grid-column: span 2;
}

.lesson-feedback {
  min-height: 38px;
  border-radius: 8px;
  padding: 9px 10px;
  background: #eef2f7;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.lesson-feedback.success {
  background: #dcfce7;
  color: #14532d;
}

.lesson-feedback.error {
  background: #fee2e2;
  color: #991b1b;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.tool-button,
.icon-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  box-shadow: 0 1px 0 rgba(24, 32, 43, 0.04);
}

.tool-button:hover,
.icon-button:hover {
  border-color: #94a3b8;
}

.tool-button:disabled {
  cursor: not-allowed;
  opacity: 0.38;
  background: #f1f5f9;
  color: #94a3b8;
}

.tool-button.active {
  background: #d7f5ef;
  border-color: var(--accent);
  color: #064e3b;
}

.tool-button.wide {
  grid-column: span 2;
}

.icon-button.danger {
  color: var(--danger);
}

.board-shell {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.board-toolbar {
  height: 44px;
  padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: #f8fafc;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.signal-key {
  display: flex;
  align-items: center;
  gap: 12px;
}

.signal-key span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.signal-key i {
  width: 18px;
  height: 4px;
  border-radius: 999px;
}

.signal-on {
  background: var(--true);
}

.signal-off {
  background: var(--false);
}

.board-viewport {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: #e7edf4;
}

.board {
  position: relative;
  width: calc(var(--cell) * 40);
  height: calc(var(--cell) * 28);
  background-color: #f8fafc;
  background-image:
    linear-gradient(to right, var(--grid) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
}

.wire-layer,
.component-layer {
  position: absolute;
  inset: 0;
}

.wire-layer {
  width: 100%;
  height: 100%;
  pointer-events: auto;
  overflow: visible;
}

.wire {
  fill: none;
  stroke: var(--false);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
  cursor: pointer;
}

.wire.hot {
  stroke: var(--true);
}

.wire.selected {
  stroke: var(--accent-2);
  stroke-width: 7;
}

.component {
  position: absolute;
  border: 1px solid #8391a6;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
  user-select: none;
  touch-action: none;
}

.component.selected {
  outline: 3px solid rgba(37, 99, 235, 0.34);
  border-color: var(--accent-2);
}

.component.delete-hover {
  outline: 3px solid rgba(220, 38, 38, 0.26);
}

.component-header {
  height: 24px;
  padding: 4px 8px;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  color: var(--muted);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.component-name {
  position: absolute;
  left: 50%;
  bottom: -20px;
  transform: translateX(-50%);
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.gate-body {
  height: calc(100% - 24px);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 18px;
}

.gate.not .gate-body {
  color: var(--warn);
}

.gate.nand .gate-body,
.gate.nor .gate-body {
  color: #7c2d12;
}

.gate.xor .gate-body {
  color: var(--accent-2);
}

.pin {
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid #475569;
  border-radius: 999px;
  background: #f8fafc;
  transform: translate(-50%, -50%);
  z-index: 3;
}

.pin.output {
  border-color: var(--accent);
}

.pin.hot {
  background: var(--true);
  border-color: #15803d;
}

.pin.pending {
  outline: 4px solid rgba(37, 99, 235, 0.24);
}

.pin-label {
  position: absolute;
  transform: translate(-50%, -50%);
  color: var(--muted);
  font-size: 10px;
  pointer-events: none;
}

.digit-source,
.digit-display {
  overflow: visible;
}

.digit-face {
  width: 70px;
  height: 82px;
  margin: 12px auto 4px;
  border-radius: 8px;
  background: #111827;
  position: relative;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.digit-source .digit-face {
  display: grid;
  place-items: center;
  color: #dcfce7;
  font-size: 42px;
  font-weight: 800;
  font-family: "Segoe UI", sans-serif;
}

.digit-display .digit-face {
  margin-top: 34px;
}

.source-stepper {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-top: 2px;
}

.source-stepper button {
  width: 28px;
  height: 24px;
  border-radius: 6px;
  background: #e2e8f0;
  color: var(--ink);
}

.bit-row {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 6px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
}

.bit-row.top {
  top: 28px;
  bottom: auto;
}

.bit-cell {
  border-radius: 6px;
  background: #eef2f7;
  padding: 2px 0;
}

.bit-cell.on {
  background: #bbf7d0;
  color: #14532d;
}

.seven-seg {
  position: absolute;
  inset: 9px 12px;
}

.segment {
  position: absolute;
  border-radius: 999px;
  background: rgba(248, 113, 113, 0.18);
}

.segment.on {
  background: #ef4444;
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.7);
}

.seg-a,
.seg-d,
.seg-g {
  left: 11px;
  width: 26px;
  height: 6px;
}

.seg-a {
  top: 0;
}

.seg-g {
  top: 32px;
}

.seg-d {
  bottom: 0;
}

.seg-b,
.seg-c,
.seg-e,
.seg-f {
  width: 6px;
  height: 27px;
}

.seg-b,
.seg-c {
  right: 0;
}

.seg-f,
.seg-e {
  left: 0;
}

.seg-b,
.seg-f {
  top: 5px;
}

.seg-c,
.seg-e {
  bottom: 5px;
}

.invalid-value {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #fca5a5;
  font-size: 22px;
  font-weight: 800;
}

.switch-body,
.lamp-body {
  height: calc(100% - 24px);
  display: grid;
  place-items: center;
}

.switch-toggle {
  width: 52px;
  height: 28px;
  border-radius: 999px;
  background: #cbd5e1;
  position: relative;
}

.switch-toggle::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 20px;
  height: 20px;
  border-radius: 999px;
  background: #ffffff;
  transition: left 120ms ease;
}

.switch-toggle.on {
  background: var(--true);
}

.switch-toggle.on::after {
  left: 28px;
}

.lamp-light {
  width: 32px;
  height: 32px;
  border-radius: 999px;
  background: #cbd5e1;
  border: 2px solid #64748b;
}

.lamp-light.on {
  background: #facc15;
  border-color: #ca8a04;
  box-shadow: 0 0 18px rgba(250, 204, 21, 0.75);
}

.inspector-body {
  display: grid;
  gap: 12px;
  font-size: 13px;
}

.empty-state {
  color: var(--muted);
  line-height: 1.55;
}

.field {
  display: grid;
  gap: 5px;
}

.field label {
  color: var(--muted);
  font-size: 12px;
}

.number-input {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #fff;
}

.truth-table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: 8px;
  font-size: 12px;
}

.truth-table th,
.truth-table td {
  border: 1px solid var(--line);
  padding: 6px;
  text-align: center;
  background: #fff;
}

.truth-table th {
  background: #f1f5f9;
  color: var(--muted);
  font-weight: 600;
}

.pill-list {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.pill {
  border-radius: 999px;
  padding: 4px 8px;
  background: #e2e8f0;
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 940px) {
  .workspace {
    grid-template-columns: 150px minmax(320px, 1fr);
  }

  .workspace.learning {
    grid-template-columns: 150px minmax(320px, 1fr);
    padding-right: 260px;
  }

  .inspector {
    display: none;
  }
}

@media (max-width: 720px) {
  .workspace {
    grid-template-columns: 1fr;
    grid-template-areas:
      "palette"
      "board";
  }

  .mode-actions {
    grid-template-columns: 1fr;
  }

  .top-lesson-progress {
    position: static;
    transform: none;
    order: 3;
    width: 100%;
    margin-top: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .palette {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .palette section {
    margin: 0 0 12px;
  }

  .tool-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .tool-button.wide {
    grid-column: span 3;
  }

  .workspace.learning {
    min-width: 830px;
    grid-template-columns: 150px minmax(420px, 1fr);
    grid-template-areas: "palette board";
    padding-right: 260px;
  }

  .workspace.learning .palette {
    border-right: 1px solid var(--line);
    border-bottom: 0;
  }

  .workspace.learning .lesson-panel {
    width: 260px;
  }
}
