/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #1e1e1e;
    --bg-secondary: #252526;
    --bg-tertiary: #2d2d2d;
    --bg-hover: #3c3c3c;
    --border: #3c3c3c;
    --text-primary: #cccccc;
    --text-secondary: #969696;
    --text-bright: #e0e0e0;
    --accent: #007acc;
    --accent-hover: #1a8ad4;
    --danger: #ef4444;
    --success: #22c55e;
    --warning: #f59e0b;
    --tab-active-bg: #1e1e1e;
    --tab-inactive-bg: #2d2d2d;
    --console-bg: #1a1a1a;
    --sidebar-width: 220px;
    --titlebar-height: 40px;
    --tabbar-height: 35px;
    --statusbar-height: 24px;
    --console-height: 200px;
}

/* === Light Theme === */
[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f3f3f3;
    --bg-tertiary: #e8e8e8;
    --bg-hover: #d4d4d4;
    --border: #d4d4d4;
    --text-primary: #333333;
    --text-secondary: #666666;
    --text-bright: #1a1a1a;
    --accent: #007acc;
    --accent-hover: #005a9e;
    --danger: #d32f2f;
    --success: #2e7d32;
    --warning: #ed6c02;
    --tab-active-bg: #ffffff;
    --tab-inactive-bg: #ececec;
    --console-bg: #f8f8f8;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 13px;
    color: var(--text-primary);
    background: var(--bg-primary);
}

button {
    font-family: inherit;
    font-size: inherit;
    cursor: pointer;
    border: none;
    border-radius: 3px;
    padding: 4px 10px;
    color: var(--text-bright);
    background: var(--bg-tertiary);
    transition: background 0.15s;
}

button:hover:not(:disabled) {
    background: var(--bg-hover);
}

button:disabled {
    opacity: 0.4;
    cursor: default;
}

/* === Title Bar === */
#titleBar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--titlebar-height);
    padding: 0 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    user-select: none;
}

#titleBrand {
    font-weight: 600;
    font-size: 14px;
    color: var(--text-bright);
}

#versionLabel {
    font-weight: 400;
    font-size: 11px;
    color: var(--text-secondary);
    margin-left: 4px;
}

#titleRight {
    display: flex;
    align-items: center;
    gap: 8px;
}

#helpBtn {
    width: 24px;
    height: 24px;
    padding: 0;
    border-radius: 50%;
    font-weight: 700;
    font-size: 13px;
    line-height: 24px;
    text-align: center;
}

#connectBtn {
    background: var(--accent);
    color: #fff;
}

#connectBtn:hover:not(:disabled) {
    background: var(--accent-hover);
}

#runBtn {
    background: #2ea043;
    color: #fff;
}

#runBtn:hover:not(:disabled) {
    background: #3fb950;
}

#stopBtn {
    background: var(--danger);
    color: #fff;
}

#stopBtn:hover:not(:disabled) {
    background: #f87171;
}

#firmwareBtn {
    background: var(--bg-tertiary);
}

#statusLabel {
    font-size: 12px;
    color: var(--text-secondary);
    margin-left: 8px;
    min-width: 80px;
    text-align: right;
}

/* === Main Content === */
#mainContent {
    display: flex;
    height: calc(100% - var(--titlebar-height) - var(--statusbar-height));
}

/* === Sidebar === */
#sidebar {
    width: var(--sidebar-width);
    min-width: 140px;
    max-width: 500px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* === Sidebar Sections === */
.sidebar-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.sidebar-section:first-child {
    flex: 2;
}

.sidebar-section:nth-child(2) {
    flex: 1;
    border-top: 1px solid var(--border);
}

.sidebar-section-header {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    flex-shrink: 0;
}

.sidebar-section-header:hover {
    color: var(--text-primary);
}

.sidebar-arrow {
    font-size: 8px;
    display: inline-block;
    transition: transform 0.15s;
}

.sidebar-arrow.expanded {
    transform: rotate(90deg);
}

.sidebar-action-btn {
    margin-left: auto;
    font-size: 14px;
    line-height: 1;
    padding: 0 4px;
    background: none;
    color: var(--text-secondary);
}

.sidebar-action-btn:hover {
    color: var(--text-bright);
}

.sidebar-section-body {
    flex: 1;
    overflow-y: auto;
    padding: 2px 0;
}

.sidebar-section-body.collapsed {
    display: none;
}

#fileTree, #localTree {
    /* sizing handled by sidebar-section-body */
}

.tree-placeholder {
    padding: 16px 12px;
    color: var(--text-secondary);
    font-style: italic;
    font-size: 12px;
}

.tree-reopen:hover {
    color: var(--accent);
    background: var(--bg-hover);
}

/* === File Tree === */
.tree-item {
    display: flex;
    align-items: center;
    padding: 3px 12px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
    user-select: none;
    gap: 4px;
}

.tree-item:hover {
    background: var(--bg-hover);
}



.tree-arrow {
    display: inline-block;
    width: 14px;
    font-size: 8px;
    text-align: center;
    color: var(--text-secondary);
    transition: transform 0.15s;
    flex-shrink: 0;
}

.tree-arrow.expanded {
    transform: rotate(90deg);
}

.tree-arrow-spacer {
    display: inline-block;
    width: 14px;
    flex-shrink: 0;
}

.tree-icon {
    font-size: 14px;
    width: 18px;
    text-align: center;
    flex-shrink: 0;
}

.tree-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tree-size {
    font-size: 11px;
    color: var(--text-secondary);
    margin-left: auto;
    padding-left: 8px;
    white-space: nowrap;
}

.tree-children {
    /* children are indented by renderEntries via paddingLeft */
}

/* === Context Menu === */
.context-menu {
    position: fixed;
    z-index: 2000;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px 0;
    min-width: 160px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.context-menu-item {
    padding: 6px 16px;
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
}

.context-menu-item:hover {
    background: var(--accent);
    color: #fff;
}

.context-menu-separator {
    height: 1px;
    background: var(--border);
    margin: 4px 0;
}

/* === Tab Origin Icon === */
.tab-origin {
    font-size: 8px;
    color: var(--accent);
    margin-right: 2px;
}

.tab-origin-local {
    color: var(--success);
}

/* === Drag and Drop === */
.drop-target {
    outline: 2px dashed var(--accent);
    outline-offset: -2px;
    background: rgba(0, 122, 204, 0.08);
}

.tree-local-root {
    cursor: default;
}

/* === Resizers === */
.resizer {
    width: 4px;
    cursor: col-resize;
    background: transparent;
    transition: background 0.15s;
    flex-shrink: 0;
}

.resizer:hover,
.resizer.active {
    background: var(--accent);
}

.resizer.horizontal {
    width: auto;
    height: 4px;
    cursor: row-resize;
}

/* === Right Pane === */
#rightPane {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

/* === Tab Bar === */
#tabBar {
    height: var(--tabbar-height);
    background: var(--bg-tertiary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: stretch;
    overflow-x: auto;
    overflow-y: hidden;
}

#tabList {
    display: flex;
    align-items: stretch;
    flex: 1;
}

.tab {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 14px;
    font-size: 12px;
    color: var(--text-secondary);
    background: var(--tab-inactive-bg);
    border-right: 1px solid var(--border);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
    min-width: 0;
}

.tab:hover {
    color: var(--text-primary);
}

.tab.active {
    color: var(--text-bright);
    background: var(--tab-active-bg);
    border-bottom: 2px solid var(--accent);
}

.tab .tab-close {
    font-size: 14px;
    line-height: 1;
    color: var(--text-secondary);
    opacity: 0;
    padding: 0 2px;
    background: none;
    border-radius: 3px;
}

.tab:hover .tab-close,
.tab.active .tab-close {
    opacity: 1;
}

.tab .tab-close:hover {
    color: var(--text-bright);
    background: var(--bg-hover);
}

.tab .tab-modified {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-secondary);
    display: none;
}

.tab.modified .tab-modified {
    display: block;
}

.tab.modified .tab-close {
    display: none;
}

/* === Editor === */
#editorContainer {
    flex: 1;
    min-height: 100px;
    overflow: hidden;
}

/* === Markdown Preview === */
.markdown-preview {
    flex: 1;
    min-height: 100px;
    overflow-y: auto;
    padding: 24px 32px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.6;
}

.markdown-preview h1, .markdown-preview h2, .markdown-preview h3,
.markdown-preview h4, .markdown-preview h5, .markdown-preview h6 {
    color: var(--text-bright);
    margin: 1em 0 0.5em;
    line-height: 1.3;
}

.markdown-preview h1 { font-size: 1.8em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; }
.markdown-preview h2 { font-size: 1.4em; border-bottom: 1px solid var(--border); padding-bottom: 0.25em; }
.markdown-preview h3 { font-size: 1.2em; }

.markdown-preview p { margin: 0.6em 0; }

.markdown-preview code {
    background: var(--bg-tertiary);
    padding: 2px 5px;
    border-radius: 3px;
    font-family: 'Cascadia Code', 'Fira Code', Consolas, monospace;
    font-size: 0.9em;
}

.markdown-preview pre {
    background: var(--bg-tertiary);
    padding: 12px 16px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 0.8em 0;
}

.markdown-preview pre code {
    background: none;
    padding: 0;
}

.markdown-preview blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    margin: 0.6em 0;
    color: var(--text-secondary);
}

.markdown-preview ul, .markdown-preview ol {
    padding-left: 24px;
    margin: 0.4em 0;
}

.markdown-preview li { margin: 0.2em 0; }

.markdown-preview hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1.2em 0;
}

.markdown-preview a { color: var(--accent); }
.markdown-preview a:hover { color: var(--accent-hover); }

.markdown-preview img { border-radius: 4px; }

.md-toggle-btn {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    padding: 2px 8px;
    font-size: 11px;
    border-radius: 3px;
    cursor: pointer;
    margin-left: auto;
    white-space: nowrap;
}

.md-toggle-btn:hover {
    color: var(--text-bright);
    background: var(--bg-hover);
}

/* Welcome screen when no file is open */
#editorContainer .welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    gap: 8px;
}

.welcome h2 {
    font-weight: 400;
    font-size: 20px;
    color: var(--text-primary);
}

.welcome p {
    font-size: 13px;
}

.welcome kbd {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 1px 5px;
    font-size: 12px;
}

/* === Console === */
#consolePanel {
    height: var(--console-height);
    min-height: 80px;
    max-height: 60vh;
    display: flex;
    flex-direction: column;
    background: var(--console-bg);
    border-top: 1px solid var(--border);
}

#consolePanel.collapsed {
    height: auto !important;
    min-height: 0;
}

#consolePanel.collapsed #consoleOutput,
#consolePanel.collapsed #consoleInputRow {
    display: none;
}

#consolePanel.collapsed #consoleToggleBtn {
    transform: rotate(180deg);
}

#consoleHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 8px 0 4px;
    border-bottom: 1px solid var(--border);
    user-select: none;
    flex-shrink: 0;
}

#consoleTabs {
    display: flex;
    gap: 0;
}

.console-tab {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 4px 10px;
    background: none;
    color: var(--text-secondary);
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    cursor: pointer;
    font-weight: 600;
}

.console-tab:hover {
    color: var(--text-primary);
}

.console-tab.active {
    color: var(--text-bright);
    border-bottom-color: var(--accent);
}

#consoleActions {
    display: flex;
    gap: 4px;
}

#consoleActions button {
    font-size: 11px;
    padding: 2px 6px;
    background: none;
    color: var(--text-secondary);
}

#consoleActions button:hover {
    color: var(--text-bright);
}

#consoleToggleBtn {
    transition: transform 0.2s;
}

#consoleOutput {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px;
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
}

#consoleOutput .line-sys {
    color: var(--accent);
}

#consoleOutput .line-error {
    color: var(--danger);
}

/* === Plotter === */
#plotterContainer {
    flex: 1;
    position: relative;
    overflow: hidden;
    min-height: 0;
    background: var(--console-bg);
}

#plotterCanvas {
    display: block;
}

#plotterLegend {
    position: absolute;
    top: 8px;
    right: 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    font-family: 'Cascadia Code', 'Fira Code', Consolas, monospace;
    font-size: 11px;
    pointer-events: none;
}

.plotter-legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-primary);
    background: var(--console-bg);
    opacity: 0.9;
    padding: 1px 6px;
    border-radius: 3px;
}

.plotter-legend-swatch {
    width: 10px;
    height: 3px;
    border-radius: 1px;
    flex-shrink: 0;
}

#consoleOutput .line-stdout {
    color: var(--text-primary);
}

#consoleInputRow {
    display: flex;
    align-items: center;
    padding: 4px 12px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

#consolePrompt {
    color: var(--success);
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
    margin-right: 8px;
    user-select: none;
}

#consoleInput {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-bright);
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', 'Courier New', monospace;
    font-size: 13px;
}

/* === Status Bar === */
#statusBar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--statusbar-height);
    padding: 0 12px;
    background: var(--accent);
    color: #fff;
    font-size: 11px;
    gap: 12px;
}

#deviceInfo {
    opacity: 0.9;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#statusRight {
    margin-left: auto;
}

/* === Modals === */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 480px;
    max-width: 90vw;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
}

.modal-close {
    font-size: 18px;
    background: none;
    color: var(--text-secondary);
    padding: 2px 6px;
}

.modal-close:hover {
    color: var(--text-bright);
}

.modal-body {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-y: auto;
}

.modal-body label {
    font-size: 12px;
    color: var(--text-secondary);
}

.modal-body select {
    padding: 6px 8px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 3px;
    font-family: inherit;
    font-size: 13px;
}

.modal-body button {
    align-self: flex-start;
    background: var(--accent);
    color: #fff;
    padding: 6px 16px;
}

.log-area {
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 8px;
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', 'Courier New', monospace;
    font-size: 12px;
    max-height: 250px;
    overflow-y: auto;
    line-height: 1.5;
}

/* === Help Modal === */
.help-body {
    font-size: 13px;
    line-height: 1.5;
    max-height: 70vh;
}

.help-body h3 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-bright);
    margin-top: 8px;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--border);
}

.help-table {
    width: 100%;
    border-collapse: collapse;
}

.help-table td {
    padding: 3px 8px;
    font-size: 12px;
}

.help-table td:first-child {
    white-space: nowrap;
    width: 1%;
}

.help-body kbd {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 1px 5px;
    font-family: 'Cascadia Code', 'Fira Code', Consolas, monospace;
    font-size: 11px;
    color: var(--text-bright);
}

.help-steps, .help-tips {
    padding-left: 20px;
    font-size: 12px;
}

.help-steps li, .help-tips li {
    margin: 3px 0;
}

.help-body code {
    background: var(--bg-tertiary);
    padding: 1px 4px;
    border-radius: 2px;
    font-size: 11px;
}

.help-links {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 16px;
}

.help-links a {
    color: var(--accent);
    text-decoration: none;
    font-size: 12px;
}

.help-links a:hover {
    text-decoration: underline;
}

.help-about {
    font-size: 11px;
    color: var(--text-secondary);
    padding-top: 8px;
    border-top: 1px solid var(--border);
}

.help-about a {
    color: var(--accent);
    text-decoration: none;
}

/* === Library Panel === */
.sidebar-section-libs {
    flex: 0 0 auto;
    max-height: 35%;
    border-top: 1px solid var(--border);
}

.lib-item {
    gap: 6px;
}

.lib-delete-btn {
    font-size: 14px;
    line-height: 1;
    padding: 0 3px;
    background: none;
    color: var(--text-secondary);
    opacity: 0;
    border-radius: 3px;
    flex-shrink: 0;
}

.lib-item:hover .lib-delete-btn {
    opacity: 1;
}

.lib-delete-btn:hover {
    color: var(--danger);
    background: var(--bg-hover);
}

/* === Library Installer Modal === */
.modal-wide {
    width: 560px;
}

.lib-search {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: inherit;
    font-size: 13px;
    outline: none;
}

.lib-search:focus {
    border-color: var(--accent);
}

.lib-catalog {
    max-height: 400px;
    overflow-y: auto;
}

.lib-catalog-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 4px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
}

.lib-catalog-info {
    flex: 1;
    min-width: 0;
}

.lib-catalog-name {
    font-weight: 600;
    font-size: 13px;
    color: var(--text-bright);
}

.lib-catalog-badge {
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 8px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    margin-left: 6px;
}

.lib-catalog-desc {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 2px;
}

.lib-catalog-empty {
    padding: 16px;
    text-align: center;
    color: var(--text-secondary);
    font-style: italic;
}

.lib-install-btn {
    padding: 4px 12px;
    font-size: 12px;
    background: var(--accent);
    color: #fff;
    flex-shrink: 0;
}

.lib-install-btn:hover:not(:disabled) {
    background: var(--accent-hover);
}

.lib-install-btn.installed {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
}

/* === Theme Button === */
#themeBtn {
    background: var(--bg-tertiary);
    font-size: 12px;
}

/* === Scrollbar === */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}
