:root {
  --page-bg: #f7f9f6;
  --left-panel-bg: #fdfefd;
  --center-bg: #ffffff;
  --right-panel-bg: #fdfefd;
  --panel-border: rgba(30, 41, 59, 0.10);
  --text-light: #1a1a1a;
  --text-dark: #1a1a1a;
  --muted-dark: #5f6d5f;
  --muted-light: #6c7a6c;
  --green: #2d7a2d;
  --green-dark: #1f5d1f;
  --green-soft: #e5f4e8;
  --warning: #c0841a;
  --error: #d66a6a;
  --success: #2f9e44;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
  --header-h: 40px;
  --left-width: 235px;
  --right-width: 300px;
  --resizer-width: 10px;
  --drawer-height: 160px;
  --mobile-toolbar-h: 34px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  font-family: Manrope, system-ui, sans-serif;
  color: var(--text-dark);
  background: var(--page-bg);
}
body.is-resizing,
body.is-resizing * {
  user-select: none !important;
  cursor: col-resize !important;
}

header, main { width: 100%; }

.topbar {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  background: #ffffff;
  border-bottom: 1px solid rgba(30, 41, 59, 0.08);
}

.mobile-toolbar {
  display: none;
  height: var(--mobile-toolbar-h);
  align-items: stretch;
  background: #ffffff;
  border-bottom: 1px solid rgba(30, 41, 59, 0.08);
}

.mobile-tab {
  flex: 1 1 0;
  border: 0;
  border-right: 1px solid rgba(30, 41, 59, 0.08);
  border-radius: 0;
  background: #ffffff;
  color: var(--muted-dark);
  font-size: 0.68rem;
  font-weight: 800;
  padding: 0 8px;
}

.mobile-tab:last-child {
  border-right: 0;
}

.mobile-tab.is-active {
  background: rgba(45, 122, 45, 0.08);
  color: var(--green-dark);
}

.brand {
  color: var(--green);
  text-decoration: none;
  font-weight: 800;
  font-size: 0.9rem;
  letter-spacing: -0.03em;
}
.brand span { color: var(--green-dark); }

.header-copy {
  color: var(--muted-dark);
  font-weight: 700;
  font-size: 0.74rem;
}

.app {
  height: calc(100vh - var(--header-h));
  min-height: 0;
}

.workspace {
  height: 100%;
  min-height: 0;
  display: flex;
  align-items: stretch;
  overflow: hidden;
}

.workspace.mobile-view-chat .desk-panel,
.workspace.mobile-view-chat .editor-stack,
.workspace.mobile-view-code .assistant-panel,
.workspace.mobile-view-code .desk-panel,
.workspace.mobile-view-tools .assistant-panel,
.workspace.mobile-view-tools .editor-stack {
  display: none !important;
}

.workspace.mobile-view-chat .assistant-panel,
.workspace.mobile-view-code .editor-stack,
.workspace.mobile-view-tools .desk-panel {
  display: flex !important;
  width: 100% !important;
  max-width: none !important;
  min-width: 0 !important;
  flex: 1 1 auto !important;
}

.panel,
.editor-panel {
  min-height: 0;
  box-shadow: var(--shadow);
}

.panel {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--panel-border);
}

.assistant-panel {
  width: var(--left-width);
  min-width: 220px;
  max-width: 300px;
  flex: 0 0 var(--left-width);
  background: var(--right-panel-bg);
  color: var(--text-dark);
}

.editor-panel {
  flex: 1 1 auto;
  min-width: 400px;
  display: flex;
  flex-direction: column;
  background: var(--center-bg);
  color: var(--text-dark);
  border-top: 1px solid rgba(30, 41, 59, 0.04);
  border-bottom: 1px solid rgba(30, 41, 59, 0.04);
}

.desk-panel {
  width: var(--right-width);
  min-width: 220px;
  max-width: 360px;
  flex: 0 0 var(--right-width);
  background: var(--left-panel-bg);
  color: var(--text-dark);
  border: 1px solid rgba(30, 41, 59, 0.10);
}

.panel,
.editor-panel,
.editor-stack {
  padding: 12px;
}

.editor-stack {
  flex: 1 1 auto;
  min-width: 400px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.panel-title {
  margin: 0;
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 800;
}
.assistant-panel .panel-title { color: var(--green-dark); }
.desk-panel .panel-title { color: var(--green); }

.hardware-toggle {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 0;
  font-size: 0.9rem;
  line-height: 1;
}
.hardware-icons {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--green);
  font-size: 0.82rem;
}

.assistant-state {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted-dark);
  font-size: 0.68rem;
  white-space: nowrap;
}
.assistant-state.is-thinking #thinkingText { color: var(--green-dark); }
.assistant-state.is-streaming #thinkingText { color: var(--green); }
.model-badge {
  padding: 4px 8px;
  border-radius: 0;
  font-size: 0.64rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  border: 1px solid rgba(45, 122, 45, 0.12);
  background: rgba(45, 122, 45, 0.08);
  color: var(--green-dark);
}
.model-badge.is-smart {
  background: rgba(45, 122, 45, 0.12);
  color: var(--green-dark);
}
.thinking-indicator {
  width: 8px;
  height: 8px;
  border-radius: 0;
  background: rgba(45, 122, 45, 0.18);
  opacity: 0;
}
.thinking-indicator.is-active {
  opacity: 1;
  animation: pulse 1.1s infinite;
}

.field-group {
  display: grid;
  gap: 6px;
  margin-bottom: 8px;
}
.compact-field { margin-bottom: 6px; }
.field-group span {
  color: var(--muted-dark);
  font-size: 0.68rem;
  font-weight: 600;
}

select,
textarea,
button {
  font: inherit;
}
select,
textarea {
  width: 100%;
  border: 1px solid rgba(30, 41, 59, 0.16);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.98);
  color: var(--text-dark);
  font-size: 0.82rem;
}
select {
  padding: 8px 10px;
}
textarea {
  padding: 8px 9px;
  min-height: 88px;
  resize: none;
  line-height: 1.42;
}

.chat-thread {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: grid;
  gap: 6px;
  padding-right: 2px;
  margin-bottom: 8px;
}
.bubble {
  border-radius: 4px;
  padding: 7px 8px;
  line-height: 1.42;
  overflow: hidden;
  width: fit-content;
  max-width: 92%;
  font-size: 0.82rem;
}
.bubble.user {
  align-self: end;
  background: var(--green-soft);
  border: 1px solid rgba(45, 122, 45, 0.12);
  color: var(--text-dark);
  margin-left: auto;
}
.bubble.assistant {
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(30, 41, 59, 0.12);
  color: var(--text-dark);
  margin-right: auto;
}
.ai-message,
.ai-bubble,
.chat-message.ai {
  color: #1a1a1a !important;
  background: #ffffff;
  border: 1px solid #e0e0e0;
}
.user-message,
.chat-message.user {
  color: #ffffff !important;
  background: #2d7a2d;
}
.markdown-body {
  color: var(--text-dark);
}
.markdown-body :is(p, ul, ol, blockquote, table, pre, h1, h2, h3, h4, h5, h6) {
  margin: 0 0 0.45rem;
}
.markdown-body p:last-child,
.markdown-body ul:last-child,
.markdown-body ol:last-child,
.markdown-body blockquote:last-child,
.markdown-body table:last-child,
.markdown-body pre:last-child { margin-bottom: 0; }
.markdown-body code {
  padding: 0.06rem 0.25rem;
  border-radius: 0;
  background: rgba(45, 122, 45, 0.1);
  color: inherit;
  font-size: 0.78rem;
}
.markdown-body pre {
  overflow: auto;
  padding: 8px;
  border-radius: 4px;
  background: #f6f8f5;
  border: 1px solid rgba(30, 41, 59, 0.12);
  color: var(--text-dark);
}
.markdown-body pre code {
  padding: 0;
  background: transparent;
}
.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: 0;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid rgba(30, 41, 59, 0.12);
  padding: 6px 8px;
  text-align: left;
  font-size: 0.79rem;
}
.markdown-body th {
  background: rgba(45, 122, 45, 0.08);
  color: inherit;
}

.composer {
  display: grid;
  gap: 8px;
}
.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.composer-hint {
  color: var(--muted-dark);
  font-size: 0.68rem;
  line-height: 1.3;
}

button {
  border: 1px solid rgba(30, 41, 59, 0.14);
  border-radius: 0;
  background: #2d7a2d;
  color: #ffffff;
  cursor: pointer;
  padding: 8px 10px;
  font-size: 0.74rem;
  font-weight: 700;
  transition: transform .14s ease, opacity .14s ease, background .14s ease;
}
button:hover:not(:disabled) {
  transform: translateY(-1px);
  background: #266a26;
}
button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
.primary { background: #2d7a2d; color: #fff; }
.secondary {
  background: rgba(45, 122, 45, 0.12);
  color: var(--text-dark);
}
.ghost {
  background: rgba(255, 255, 255, 0.78);
  color: var(--text-dark);
}
.ghost:disabled {
  background: rgba(255, 255, 255, 0.55);
}

.editor {
  flex: 1;
  min-height: 0;
}
.editor-statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 10px;
  border-top: 1px solid rgba(30, 41, 59, 0.08);
  background: #f4f6f2;
  color: var(--muted-dark);
  font-size: 0.72rem;
}
.editor-status {
  color: var(--text-dark);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}
.compile-state {
  color: var(--muted-dark);
  text-align: right;
}

.action-stack {
  display: grid;
  gap: 8px;
  margin-bottom: 10px;
}

.artifact-card {
  margin-bottom: 10px;
  padding: 8px 10px;
  border-radius: 0;
  background: rgba(45, 122, 45, 0.08);
  border: 1px solid rgba(45, 122, 45, 0.12);
}
.artifact-card a {
  color: var(--green);
  text-decoration: none;
  font-weight: 800;
}

.mini-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 10px;
  padding: 8px;
  border-radius: 0;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid rgba(30, 41, 59, 0.08);
  min-height: 0;
}
.mini-panel:last-child { margin-bottom: 0; }

.desk-panel .panel-subtitle,
.desk-panel .field-group span {
  color: var(--muted-dark);
}

.wiring-guide {
  display: grid;
  gap: 8px;
}
.wiring-guide table {
  width: 100%;
  border-collapse: collapse;
}
.wiring-guide th,
.wiring-guide td {
  border: 1px solid rgba(30, 41, 59, 0.12);
  padding: 5px 7px;
  font-size: 0.76rem;
}
.wiring-guide th {
  background: rgba(45, 122, 45, 0.08);
  color: var(--text-dark);
  width: 40%;
}

.status-console {
  flex: 1;
  min-height: 140px;
  overflow: auto;
  border-radius: 0;
  padding: 8px;
  background: #f7f9f6;
  border: 1px solid rgba(30, 41, 59, 0.12);
  color: var(--text-dark);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.74rem;
  line-height: 1.45;
}
.panel-head .panel-title {
  line-height: 1;
}
.compact-field select {
  padding-left: 10px;
  padding-right: 10px;
}
.compact-field select,
.action-stack button {
  border-radius: 0;
}
.log-line {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
  white-space: pre-wrap;
  word-break: break-word;
}
.log-tag {
  flex: 0 0 auto;
  min-width: 66px;
  font-weight: 700;
}
.log-text { flex: 1; }
.log-info { color: var(--text-dark); }
.log-compile { color: var(--warning); }
.log-error { color: var(--error); }
.log-success { color: var(--success); }

.resizer {
  flex: 0 0 var(--resizer-width);
  width: var(--resizer-width);
  cursor: col-resize;
  position: relative;
  background: transparent;
  touch-action: none;
  user-select: none;
}
.resizer::before {
  content: "";
  position: absolute;
  top: 10px;
  bottom: 10px;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
  border-radius: 0;
  background: rgba(45, 122, 45, 0.22);
}
.resizer:hover::before,
.resizer.is-dragging::before {
  background: rgba(45, 122, 45, 0.72);
}

.monaco-compile-error-inline {
  background: rgba(214, 106, 106, 0.12);
  text-decoration: wavy underline #d66a6a;
  text-decoration-thickness: 2px;
}

.editor-panel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--center-bg);
  color: var(--text-light);
  border-top: 1px solid rgba(33, 59, 33, 0.12);
  border-bottom: 1px solid rgba(33, 59, 33, 0.12);
  border-left: 1px solid rgba(33, 59, 33, 0.12);
  border-right: 0;
}

.drawer {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: rgba(245, 247, 245, 0.98);
  border: 1px solid rgba(33, 59, 33, 0.12);
  border-radius: 0;
  overflow: hidden;
}
.bottom-drawer {
  height: var(--drawer-height);
  min-height: 120px;
  background: rgba(245, 247, 245, 0.98);
  border: 1px solid rgba(33, 59, 33, 0.12);
  border-radius: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.bottom-drawer.is-collapsed {
  height: 36px;
  min-height: 36px;
}
.bottom-drawer.is-simulation {
  height: min(78vh, 820px);
  min-height: 520px;
}
.drawer-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 8px;
  border-bottom: 1px solid rgba(33, 59, 33, 0.08);
}
.drawer-tabs {
  display: flex;
  gap: 6px;
}
.drawer-tab {
  padding: 5px 8px;
  border-radius: 0;
  background: #fff;
  color: var(--text-dark);
  border: 1px solid rgba(33, 59, 33, 0.12);
  font-size: 0.7rem;
  font-weight: 700;
}
.drawer-tab.is-active {
  background: var(--green);
  color: #fff;
}
.drawer-toggle {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 0;
  font-size: 0.8rem;
}
.drawer-body {
  flex: 1;
  min-height: 0;
  display: grid;
}
.drawer-panel {
  display: none;
  min-height: 0;
  padding: 8px 10px;
  overflow: auto;
  color: var(--text-dark);
  font-size: 0.78rem;
}
.drawer-panel.is-active { display: block; }
.bottom-drawer.is-simulation .drawer-panel.is-active {
  height: 100%;
}
.bottom-drawer.is-simulation .drawer-body {
  min-height: 0;
}
#compilerOutput,
#serialMonitor {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.74rem;
  line-height: 1.38;
}
#aiStatsLog {
  display: grid;
  gap: 4px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.74rem;
  line-height: 1.38;
}
.drawer-panel:not(.is-active) {
  display: none;
}
.drawer-line {
  display: flex;
  gap: 8px;
  margin-bottom: 4px;
  white-space: pre-wrap;
}
.drawer-time {
  color: #889188;
  min-width: 62px;
}
.drawer-success { color: var(--success); }
.drawer-error { color: var(--error); }
.drawer-info { color: #4a4a4a; }

.hardware-panel-collapsed {
  width: 40px !important;
  min-width: 40px !important;
  max-width: 40px !important;
  padding: 8px 6px;
  overflow: hidden;
}
.hardware-panel-collapsed > :not(.panel-head) {
  display: none !important;
}
.hardware-panel-collapsed .panel-head {
  justify-content: center;
}
.hardware-panel-collapsed .hardware-icons {
  flex-direction: column;
}
.hardware-panel-collapsed .hardware-toggle {
  width: 28px;
  height: 28px;
  font-size: 0.8rem;
}

@media (max-width: 1023px) {
  body {
    overflow-x: hidden;
    overflow-y: auto;
  }

  .topbar {
    height: 38px;
    padding: 0 10px;
  }

  .brand {
    font-size: 0.84rem;
  }

  .header-copy {
    font-size: 0.66rem;
  }

  .mobile-toolbar {
    display: flex;
  }

  .app {
    height: auto;
    min-height: calc(100vh - var(--header-h) - var(--mobile-toolbar-h));
  }

  .workspace {
    flex-direction: column;
    overflow: visible;
  }

  .assistant-panel,
  .desk-panel,
  .editor-panel,
  .editor-stack {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    flex: 0 0 auto !important;
  }

  .panel,
  .editor-panel,
  .editor-stack {
    padding: 8px;
  }

  .editor-stack {
    gap: 8px;
  }

  .editor-panel {
    min-height: 52vh;
  }

  .editor {
    min-height: 42vh;
  }

  .bottom-drawer {
    height: 140px;
    min-height: 108px;
  }

  .resizer {
    display: none;
  }

  .hardware-panel-collapsed {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    padding: 8px !important;
  }

  .hardware-panel-collapsed > :not(.panel-head) {
    display: block !important;
  }

  .hardware-panel-collapsed .panel-head {
    justify-content: space-between;
  }
}

.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;
}

@keyframes pulse {
  0% { transform: scale(0.85); opacity: 0.7; }
  70% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(0.85); opacity: 0.7; }
}

.toast-host {
  position: fixed;
  top: calc(var(--header-h) + 12px);
  right: 14px;
  z-index: 9999;
  display: grid;
  gap: 10px;
  pointer-events: none;
}

.toast {
  min-width: 220px;
  max-width: min(360px, calc(100vw - 28px));
  padding: 12px 14px;
  border-radius: 0;
  border: 1px solid rgba(45, 122, 45, 0.16);
  background: rgba(255, 255, 255, 0.98);
  color: var(--text-dark);
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.12);
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.18s ease, transform 0.18s ease;
  font-size: 0.9rem;
  line-height: 1.35;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast-warning {
  border-color: rgba(192, 132, 26, 0.24);
  background: #fff8e8;
}

.toast-success {
  border-color: rgba(47, 158, 68, 0.2);
  background: #effaf2;
}

.toast-error {
  border-color: rgba(214, 106, 106, 0.24);
  background: #fff0f0;
}
