:root {
  --ink: #1e2529;
  --muted: #667075;
  --paper: #fbfaf7;
  --line: #d9d3c9;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --sun: #e0a44a;
  --panel: #ffffff;
  --code: #172026;
  --code-text: #e8f1ee;
  font-family:
    "Noto Sans TC",
    "Microsoft JhengHei",
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background: var(--paper);
}

.topbar {
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  background: #fffdf9;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.25;
  font-weight: 800;
}

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

.actions,
.panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

button,
.action-link {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  background: #ffffff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:hover,
.action-link:hover {
  border-color: #b8aa97;
  background: #fff8ea;
}

.action-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.oled-editor-link {
  border-color: #536d95;
  color: #344b70;
  background: #f0f4fa;
}

.oled-editor-link:hover {
  border-color: #40597f;
  background: #e5ecf6;
}

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

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

button.primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

button.download-project {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: #eef7f4;
}

button.download-project:hover {
  border-color: var(--accent-strong);
  background: #dff0eb;
}

.workspace-shell {
  height: calc(100vh - 88px);
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 36vw);
}

.block-area {
  min-width: 0;
  min-height: 0;
  border-right: 1px solid var(--line);
}

#blocklyDiv {
  width: 100%;
  height: 100%;
}

.side-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 48px minmax(0, 1fr) 150px;
  background: #f1f3f2;
}

.panel-tabs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  padding: 6px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

.panel-tab {
  height: 35px;
  border: 0;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 8px;
  text-decoration: none;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.panel-tab:hover {
  border: 0;
  background: #eef5f3;
}

.panel-tab.active {
  color: #ffffff;
  background: var(--accent);
}

.panel-section {
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--line);
}

.panel-section[hidden] {
  display: none;
}

.oled-drawing-area {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: #e8ebe7;
}

.oled-drawing-area[hidden] {
  display: none;
}

.oled-drawing-toolbar {
  min-height: 54px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
  flex-wrap: wrap;
}

.oled-tool-group {
  display: flex;
  gap: 5px;
}

.oled-tool-button,
.oled-icon-button {
  width: 38px;
  padding: 0;
  font-size: 20px;
}

.oled-tool-button.active {
  border-color: var(--accent);
  color: #ffffff;
  background: var(--accent);
}

.oled-brush-control {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.oled-brush-control select {
  height: 34px;
  min-width: 82px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
}

.oled-bitmap-id {
  color: var(--muted);
  font: 13px Consolas, monospace;
}

.oled-cursor-position {
  margin-left: auto;
  color: var(--muted);
  font: 13px Consolas, monospace;
}

.oled-cursor-position b {
  display: inline-block;
  width: 26px;
  color: var(--ink);
}

.oled-canvas-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 22px;
}

#pixelCanvas {
  display: block;
  width: 1024px;
  height: 512px;
  border: 2px solid #313b3e;
  background: #f9fbfa;
  image-rendering: pixelated;
  touch-action: none;
  cursor: crosshair;
  box-shadow: 0 8px 24px rgba(35, 46, 43, 0.13);
}

.side-panel.oled-mode {
  grid-template-rows: 48px minmax(0, 1fr);
}

.integrated-oled-panel {
  overflow: auto;
  background: #ffffff;
}

.oled-control-section {
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.oled-control-section > label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

#imageName {
  width: 100%;
  height: 38px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
  font: inherit;
}

.oled-section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.oled-section-heading h2 {
  font-size: 16px;
}

.oled-section-heading span {
  color: var(--muted);
  font-size: 12px;
}

.oled-frame {
  padding: 15px;
  border: 1px solid #384349;
  border-radius: 6px;
  background: #20292d;
}

#previewCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 2 / 1;
  background: #111a1d;
  image-rendering: pixelated;
}

.oled-save-button {
  width: 100%;
  min-height: 44px;
}

.oled-delete-button {
  min-height: 30px;
  height: 30px;
  color: #a33838;
  font-size: 12px;
}

.oled-delete-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.oled-file-list {
  width: 100%;
  min-height: 150px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #ffffff;
  font: inherit;
}

.oled-file-list option {
  padding: 8px;
}

.oled-status-message {
  padding: 12px 16px 20px;
  color: var(--muted);
  font-size: 13px;
}

.simulator-section {
  overflow: auto;
  background: #f7f8f7;
}

.simulator-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px 8px;
}

.eyebrow {
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.simulator-actions {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

.loop-control {
  height: 32px;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.loop-control input {
  width: 30px;
  height: 18px;
  margin: 0;
  appearance: none;
  position: relative;
  border: 1px solid #aab5b1;
  border-radius: 10px;
  background: #dce2df;
  cursor: pointer;
}

.loop-control input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 2px rgb(23 32 38 / 25%);
  transition: transform 140ms ease;
}

.loop-control input:checked {
  border-color: var(--accent);
  background: var(--accent);
}

.loop-control input:checked::after {
  transform: translateX(12px);
}

.loop-control input:focus-visible {
  outline: 2px solid var(--sun);
  outline-offset: 2px;
}

.speed-control {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.speed-control span {
  display: block;
  margin-bottom: 2px;
}

.speed-control select {
  width: 68px;
  height: 32px;
  margin: 0;
  padding: 0 6px;
  border-radius: 6px;
  font-size: 13px;
}

.icon-button {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 6px;
  font-size: 14px;
}

#playPreviewButton {
  border-color: var(--accent);
  color: #ffffff;
  background: var(--accent);
}

.robot-stage {
  position: relative;
  min-height: 310px;
  margin: 0;
  overflow: hidden;
  background: #dfe8e4;
  cursor: grab;
  touch-action: none;
}

.robot-stage:active { cursor: grabbing; }

.robot-stage canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
}

.scene-loading {
  position: absolute;
  z-index: 1;
  inset: 0;
  display: grid;
  place-items: center;
  color: #44514d;
  background: #dfe8e4;
  font-size: 13px;
  font-weight: 750;
}

.scene-loading.error {
  color: #8a3027;
  background: #f7e9e6;
}

.scene-loading[hidden] {
  display: none;
}

.scene-direction {
  position: absolute;
  z-index: 2;
  left: 10px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  color: #26332f;
  font-size: 11px;
  font-weight: 800;
  pointer-events: none;
}

.scene-direction b {
  color: var(--accent);
  font-size: 18px;
  line-height: 1;
}

.camera-button {
  position: absolute;
  z-index: 3;
  top: 9px;
  right: 9px;
  width: 34px;
  height: 34px;
  padding: 0;
  border-color: rgba(36, 48, 55, 0.24);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.88);
  font-size: 18px;
  backdrop-filter: blur(5px);
}

.motor-hint {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 4px 7px;
  border-radius: 5px;
  color: #ffffff;
  background: rgba(30, 37, 41, 0.8);
  font-size: 11px;
  font-weight: 700;
}

.preview-progress {
  height: 4px;
  margin: 9px 12px 0;
  overflow: hidden;
  border-radius: 2px;
  background: #d8ddda;
}

.preview-progress span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--accent);
  transition: width 120ms linear;
}

.motor-editor {
  padding: 8px 14px 12px;
}

.angle-control + .angle-control { margin-top: 8px; }

.angle-label,
.angle-scale {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.angle-label {
  font-size: 13px;
  font-weight: 750;
}

.angle-label output {
  color: var(--accent-strong);
  font-size: 16px;
  font-weight: 850;
}

.angle-control input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.angle-scale {
  color: var(--muted);
  font-size: 10px;
}

.pwm-readout {
  margin-top: 6px;
  color: var(--muted);
  font-family: Consolas, "Cascadia Mono", monospace;
  font-size: 11px;
  text-align: right;
}

.panel-title {
  justify-content: space-between;
  padding: 12px;
}

.panel-title > div:first-child {
  min-width: 0;
}

h2 {
  font-size: 16px;
}

select {
  width: min(220px, 100%);
  height: 34px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #ffffff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
}

.code-output,
.run-output {
  flex: 1;
  min-height: 0;
  margin: 0 12px 12px;
  padding: 14px;
  overflow: auto;
  border-radius: 8px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.code-output {
  background: var(--code);
  color: var(--code-text);
  font-family: Consolas, "Cascadia Mono", monospace;
  font-size: 13px;
}

.run-output {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font-size: 14px;
}

.blocklyToolboxDiv {
  background: #fffdf9;
  border-right: 1px solid var(--line);
}

.blocklyTreeLabel {
  font-family: inherit;
}

@media (max-width: 900px) {
  .topbar {
    min-height: 128px;
    align-items: flex-start;
    flex-direction: column;
  }

  .workspace-shell {
    height: calc(100vh - 128px);
    grid-template-columns: 1fr;
    grid-template-rows: minmax(360px, 58vh) minmax(300px, 42vh);
  }

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

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

  .side-panel {
    grid-template-columns: 1fr;
    grid-template-rows: 48px minmax(0, 1fr) 130px;
  }

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

@media (max-width: 640px) {
  h1 {
    font-size: 20px;
  }

  button {
    height: 34px;
    padding: 0 10px;
  }

  .side-panel {
    grid-template-columns: 1fr;
    grid-template-rows: 48px minmax(0, 1fr) 120px;
  }

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

  .oled-drawing-toolbar {
    gap: 7px;
    padding: 8px;
  }

  .oled-canvas-scroll {
    padding: 12px;
  }

  .oled-cursor-position {
    margin-left: 0;
  }
}

@media (max-width: 430px) {
  .simulator-heading {
    align-items: flex-start;
  }

  .robot-stage {
    min-height: 270px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .preview-progress span {
    transition-duration: 1ms;
  }
}
