:root {
    --bg: #1a1a2e;
    --bg2: #16213e;
    --bg3: #0f3460;
    --accent: #e94560;
    --accent2: #ff6b6b;
    --text: #eee;
    --text2: #aab;
    --success: #4ecdc4;
    --warning: #f9a825;
    --border: #2a3a5e;
    --radius: 6px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ==========================================
   Header
   ========================================== */

header {
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    z-index: 10;
}

.header-left { display: flex; align-items: center; gap: 12px; }
.header-right { display: flex; align-items: center; gap: 10px; }

header h1 { font-size: 1.15rem; font-weight: 600; white-space: nowrap; }
header h1 span { color: var(--accent); }

.badge {
    background: var(--accent);
    color: white;
    font-size: 0.55rem;
    padding: 2px 7px;
    border-radius: 10px;
    text-transform: uppercase;
    font-weight: 700;
}

.user-info { font-size: 0.8rem; color: var(--text2); }

.connection-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text2);
    margin-left: 8px;
    padding-left: 12px;
    border-left: 1px solid var(--border);
}

/* ==========================================
   Editor layout
   ========================================== */

.editor {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.toolbar {
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
    padding: 4px 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    overflow-x: auto;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

.toolbar-divider {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 4px;
}

.toolbar-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 5px 10px;
    background: transparent;
    color: var(--text2);
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s;
}

.toolbar-btn:hover {
    background: var(--bg3);
    color: var(--text);
    border-color: var(--border);
}

.toolbar-btn svg { flex-shrink: 0; }

.toolbar-btn.btn-accent {
    color: var(--accent);
    font-weight: 600;
}

.toolbar-btn.btn-accent:hover {
    background: rgba(233, 69, 96, 0.15);
    border-color: var(--accent);
}

.toolbar-btn.btn-danger-text {
    color: #c0392b;
}

.toolbar-btn.btn-danger-text:hover {
    background: rgba(192, 57, 43, 0.15);
    border-color: #c0392b;
}

/* Connection mode picker */
.connect-picker {
    position: fixed;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px;
    z-index: 20;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 240px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.picker-btn {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 10px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    cursor: pointer;
    text-align: left;
    transition: all 0.15s;
}

.picker-btn:hover {
    border-color: var(--accent);
    background: rgba(233, 69, 96, 0.08);
}

.picker-btn strong { font-size: 0.82rem; }
.picker-btn span { font-size: 0.7rem; color: var(--text2); line-height: 1.3; }

.editor-main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.canvas-wrap {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.canvas-wrap canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* Drop overlay */
.drop-overlay {
    position: absolute;
    inset: 0;
    background: rgba(233, 69, 96, 0.15);
    border: 2px dashed var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--accent);
    pointer-events: none;
    z-index: 5;
}

/* ==========================================
   Sidebar
   ========================================== */

.sidebar {
    width: 260px;
    background: var(--bg2);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.panel {
    border-bottom: 1px solid var(--border);
    padding: 12px;
}

.panel h3 {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text2);
    font-weight: 600;
    margin-bottom: 10px;
}

/* Collapsible panels */
.collapsible .panel-toggle {
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0;
}

.collapsible .panel-body { margin-top: 10px; }
.collapsible.collapsed .panel-body { display: none; }
.collapsible.collapsed .panel-toggle { margin-bottom: 0; }
.caret { font-size: 0.6rem; }

/* ==========================================
   Shape toolbar
   ========================================== */

.shape-toolbar {
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
    padding: 4px 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
    position: relative;
}

.shape-btn {
    width: 36px;
    height: 36px;
    padding: 6px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius);
    color: var(--text2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.shape-btn:hover {
    background: var(--bg3);
    color: var(--text);
    border-color: var(--border);
}

.shape-btn:active {
    background: rgba(233, 69, 96, 0.15);
    border-color: var(--accent);
    color: var(--accent);
}

.shape-btn svg { width: 20px; height: 20px; }

.shape-toolbar-divider {
    width: 1px;
    height: 24px;
    background: var(--border);
    margin: 0 4px;
}

/* Text popover */
.text-popover {
    position: fixed;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px;
    z-index: 20;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 220px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.text-popover input[type="text"] { width: 100%; }

.text-popover-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.text-popover-row label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    color: var(--text2);
    font-weight: 600;
    white-space: nowrap;
}

.text-popover-row label input { width: 50px; }
.text-popover-row select { flex: 1; }

/* ==========================================
   Position panel
   ========================================== */

.pos-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}

.pos-grid label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    color: var(--text2);
    font-weight: 600;
}

.pos-grid input {
    flex: 1;
    width: 100%;
    min-width: 0;
}

.pos-actions {
    display: flex;
    gap: 6px;
    margin-top: 8px;
}

/* ==========================================
   Layer panel
   ========================================== */

.layer-empty {
    color: var(--text2);
    font-size: 0.8rem;
    text-align: center;
    padding: 12px 0;
}

.layer-row {
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 6px;
    background: var(--bg);
}

.layer-row.layer-disabled {
    opacity: 0.45;
}

.layer-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 500;
}

.layer-toggle input[type="checkbox"] {
    accent-color: var(--accent);
    width: 14px;
    height: 14px;
}

.layer-swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid rgba(255,255,255,0.2);
    flex-shrink: 0;
}

.layer-name { flex: 1; }

.layer-params {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    margin-top: 6px;
}

.layer-params label {
    font-size: 0.65rem;
    color: var(--text2);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.layer-params input {
    width: 100%;
    padding: 3px 4px;
    font-size: 0.75rem;
}

/* ==========================================
   Jog controls
   ========================================== */

.jog-grid {
    display: grid;
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 44px);
    gap: 3px;
    justify-content: center;
}

.jog-btn {
    font-size: 1rem;
    background: var(--bg3);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.15s;
}

.jog-btn:hover { background: #1a4a7a; }
.jog-btn:active { background: var(--accent); transform: scale(0.95); }
.jog-center { background: var(--bg) !important; border: none !important; cursor: default !important; font-size: 0.9rem; color: var(--text2); }

.jog-settings {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 8px;
    justify-content: center;
    font-size: 0.78rem;
    color: var(--text2);
}

/* ==========================================
   Machine info (compact)
   ========================================== */

.info-grid-sm {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.info-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.78rem;
    padding: 3px 0;
    border-bottom: 1px solid rgba(42, 58, 94, 0.3);
}

.info-row span:first-child { color: var(--text2); }
.info-row span:last-child { font-weight: 500; }
.mono { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 0.75rem; }

/* ==========================================
   Context menu
   ========================================== */

.context-menu {
    position: fixed;
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    min-width: 180px;
    padding: 4px 0;
    z-index: 10000;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

.context-menu-item {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 6px 14px;
    background: none;
    border: none;
    color: var(--text);
    font-size: 0.8rem;
    cursor: pointer;
    text-align: left;
    gap: 8px;
    transition: background 0.1s;
}

.context-menu-item:hover { background: var(--bg3); }
.context-menu-item:disabled { color: var(--text2); opacity: 0.4; cursor: default; }
.context-menu-item:disabled:hover { background: none; }
.context-menu-label { flex: 1; }
.context-menu-shortcut { font-size: 0.7rem; color: var(--text2); opacity: 0.7; }
.context-menu-divider { height: 1px; background: var(--border); margin: 3px 0; }

/* ==========================================
   Shared components
   ========================================== */

.hidden { display: none !important; }

.status-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #555; transition: background 0.3s;
    flex-shrink: 0;
}
.status-dot.connected { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.error { background: var(--accent); }

/* Buttons */
.btn {
    padding: 7px 14px; border: none; border-radius: var(--radius);
    cursor: pointer; font-size: 0.8rem; font-weight: 600; transition: all 0.2s;
}
.btn-sm { padding: 4px 10px; font-size: 0.75rem; }
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent2); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #1a4a7a; }
.btn-secondary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-danger { background: #c0392b; color: white; }
.btn-danger:hover { background: #e74c3c; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }

/* Alerts */
.alert { padding: 8px 16px; text-align: center; font-weight: 600; font-size: 0.85rem; }
.alert-error { background: var(--accent); color: white; }

/* Forms */
select, input[type="text"], input[type="number"], input[type="email"], input[type="password"] {
    background: var(--bg); border: 1px solid var(--border); color: var(--text);
    padding: 5px 8px; border-radius: 4px; font-size: 0.8rem;
}
select:focus, input:focus { outline: none; border-color: var(--accent); }
.form-group { margin-bottom: 10px; }
.form-group label { display: block; font-size: 0.78rem; color: var(--text2); margin-bottom: 3px; }
.form-group input { width: 100%; }
.text-muted { color: var(--text2); font-size: 0.82rem; margin-bottom: 14px; }

/* Log */
.log-container {
    background: #0d0d1a; border: 1px solid var(--border); border-radius: var(--radius);
    padding: 8px; max-height: 200px; overflow-y: auto;
    font-family: 'SF Mono', 'Fira Code', monospace; font-size: 0.72rem; line-height: 1.5;
}
.log-entry { margin-bottom: 1px; }
.log-info { color: var(--text2); }
.log-send { color: #5dade2; }
.log-recv { color: var(--success); }
.log-error { color: var(--accent2); }
.log-success { color: var(--success); font-weight: 600; }
.log-time { color: #555; margin-right: 6px; }

/* Modal */
.modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.6); display: flex;
    align-items: center; justify-content: center; z-index: 100;
}
.modal-content {
    background: var(--bg2); border: 1px solid var(--border);
    border-radius: var(--radius); width: 90%; max-width: 400px;
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 1rem; }
.modal-close {
    background: none; border: none; color: var(--text2);
    font-size: 1.4rem; cursor: pointer; line-height: 1;
}
.modal-body { padding: 18px; }
.modal-footer {
    padding: 14px 18px; border-top: 1px solid var(--border);
    display: flex; gap: 8px; justify-content: flex-end;
}

/* Responsive */
@media (max-width: 800px) {
    .sidebar { width: 220px; }
}

@media (max-width: 600px) {
    .editor-main { flex-direction: column; }
    .sidebar {
        width: 100%;
        border-left: none;
        border-top: 1px solid var(--border);
        max-height: 40vh;
    }
    .canvas-wrap { min-height: 300px; }
}
