:root {
  --bg: #0b0f17;
  --panel: #111827;
  --panel-2: #161f30;
  --border: #243047;
  --border-strong: #334155;
  --text: #e5edf8;
  --text-dim: #94a3b8;
  --text-muted: #64748b;
  --accent: #38bdf8;
  --accent-dim: rgba(56, 189, 248, 0.14);
  --green: #10b981;
  --green-hover: #059669;
  --green-dim: rgba(16, 185, 129, 0.16);
  --amber: #f59e0b;
  --amber-dim: rgba(245, 158, 11, 0.16);
  --red: #f43f5e;
  --red-dim: rgba(244, 63, 94, 0.16);
  --mono: ui-monospace, 'Cascadia Code', 'JetBrains Mono', Consolas, 'SFMono-Regular', Menlo, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 13px;
  overflow: hidden;
}

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

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.2px;
  white-space: nowrap;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 10px var(--green);
}

.brand-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid rgba(56, 189, 248, 0.28);
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

select, input[type="text"], button {
  font-family: inherit;
  font-size: 12.5px;
  border-radius: 6px;
  outline: none;
}

select, input[type="text"] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 5px 9px;
}

select:focus, input[type="text"]:focus {
  border-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  transition: background 0.14s, border-color 0.14s, opacity 0.14s;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: #1f2b42;
  border-color: #475569;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: #0284c7;
  border-color: #0369a1;
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: #0369a1;
}

.btn-flash {
  background: var(--green);
  border-color: var(--green-hover);
  color: #041f15;
}

.btn-flash:hover:not(:disabled) {
  background: #34d399;
}

.btn-danger {
  background: var(--red-dim);
  border-color: rgba(244, 63, 94, 0.4);
  color: #fda4af;
}

.btn-sm {
  padding: 4px 8px;
  font-size: 12px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.pill-green { background: var(--green-dim); color: #6ee7b7; border-color: rgba(16,185,129,0.3); }
.pill-amber { background: var(--amber-dim); color: #fcd34d; border-color: rgba(245,158,11,0.3); }
.pill-red   { background: var(--red-dim); color: #fda4af; border-color: rgba(244,63,94,0.3); }
.pill-blue  { background: var(--accent-dim); color: #7dd3fc; border-color: rgba(56,189,248,0.3); }

/* Board Options Drawer */
.options-drawer {
  background: #0f1624;
  border-bottom: 1px solid var(--border);
  padding: 10px 14px 12px;
  display: none;
}

.options-drawer.open {
  display: block;
}

.options-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.preset-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.preset-btn {
  padding: 4px 9px;
  font-size: 11.5px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
}

.preset-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.options-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(215px, 1fr));
  gap: 8px 12px;
}

.opt-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.opt-item label {
  font-size: 11.5px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.opt-tag {
  font-size: 10px;
  padding: 1px 5px;
  border-radius: 4px;
}

.opt-tag-fast { background: var(--green-dim); color: #6ee7b7; }
.opt-tag-dyn  { background: var(--amber-dim); color: #fcd34d; }

.opt-item select.modified {
  border-color: var(--accent);
  background: #13253a;
}

/* Main Workspace */
.workspace {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 248px;
  min-width: 210px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.sidebar-section.grow {
  flex: 1;
  min-height: 0;
  border-bottom: none;
}

.sidebar-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  background: #0e1522;
}

.file-list {
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 175px;
  overflow-y: auto;
}

.file-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px;
  border-radius: 5px;
  cursor: pointer;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 12px;
}

.file-item:hover {
  background: var(--panel-2);
  color: var(--text);
}

.file-item.active {
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 600;
}

.file-del {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 4px;
  font-size: 13px;
}

.file-del:hover { color: var(--red); }

.lib-search {
  margin: 6px 8px;
  width: calc(100% - 16px);
}

.lib-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.lib-card {
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.lib-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.lib-name {
  font-weight: 600;
  font-size: 12px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lib-ver {
  font-size: 10.5px;
  color: var(--text-muted);
  font-family: var(--mono);
}

.lib-desc {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.lib-card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 2px;
}

.lib-hdr {
  font-family: var(--mono);
  font-size: 10.5px;
  color: #7dd3fc;
}

.lib-insert-btn {
  padding: 2px 7px;
  font-size: 11px;
  border-radius: 4px;
  border: 1px solid rgba(56, 189, 248, 0.35);
  background: var(--accent-dim);
  color: #7dd3fc;
  cursor: pointer;
}

.lib-insert-btn:hover {
  background: rgba(56, 189, 248, 0.25);
}

/* Editor + Bottom Console Column */
.main-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.editor-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 12px;
  background: #0e1522;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
  gap: 10px;
}

.editor-wrap {
  flex: 1;
  display: flex;
  min-height: 160px;
  position: relative;
  background: #0b0f17;
  overflow: hidden;
}

.gutter {
  width: 50px;
  padding: 12px 8px 12px 4px;
  text-align: right;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-muted);
  background: #0d131f;
  border-right: 1px solid var(--border);
  user-select: none;
  overflow: hidden;
  white-space: pre;
}

.code-input {
  flex: 1;
  padding: 12px 14px;
  background: transparent;
  color: #e2e8f0;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}

/* Console Area */
.console-pane {
  height: 270px;
  min-height: 160px;
  border-top: 2px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
}

.console-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: #0e1522;
  border-bottom: 1px solid var(--border);
  gap: 10px;
  flex-wrap: wrap;
}

.tab-group {
  display: flex;
  gap: 4px;
}

.tab-btn {
  padding: 4px 10px;
  border-radius: 5px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
}

.tab-btn.active {
  background: var(--panel-2);
  border-color: var(--border-strong);
  color: var(--text);
}

.progress-strip {
  height: 3px;
  background: #172033;
  width: 100%;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--green));
  transition: width 0.2s ease;
}

.progress-fill.indeterminate {
  width: 35%;
  animation: slide 1.2s infinite ease-in-out;
}

@keyframes slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(350%); }
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  background: #0c121e;
  border-bottom: 1px solid var(--border);
  font-size: 11.5px;
  flex-wrap: wrap;
}

.log-output {
  flex: 1;
  margin: 0;
  padding: 10px 14px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  color: #cbd5e1;
  white-space: pre-wrap;
  word-break: break-word;
}

.log-line-err  { color: #fda4af; }
.log-line-ok   { color: #6ee7b7; }
.log-line-info { color: #7dd3fc; }
.log-line-warn { color: #fcd34d; }

.serial-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #0c121e;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.serial-send-row {
  display: flex;
  gap: 6px;
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  background: #0e1522;
}

.serial-send-row input {
  flex: 1;
}

.hidden { display: none !important; }

.check-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

@media (max-width: 900px) {
  .sidebar { width: 190px; min-width: 170px; }
}
@media (max-width: 680px) {
  .sidebar { display: none; }
}
