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

:root {
    --bg:         #0a0a0f;
    --sidebar-bg: #111118;
    --border:     #2a2a3a;
    --accent:     #55ff55;
    --accent2:    #55ffff;
    --text:       #cccccc;
    --text-dim:   #556;
    --btn-bg:     #1a1a28;
    --btn-hover:  #252535;
    --btn-active: #1a2a1a;
    --font:       'Courier New', monospace;
}

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

#app {
    display: flex;
    width: 100vw;
    height: 100vh;
}

/* ── Sidebar ──────────────────────────────────────────────────────── */

#sidebar {
    width: 220px;
    min-width: 220px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#sidebar-brand {
    padding: 10px 14px 9px;
    font-size: 15px;
    font-weight: bold;
    color: var(--accent);
    letter-spacing: 3px;
    border-bottom: 1px solid var(--border);
    user-select: none;
}

/* ── Function list ────────────────────────────────────────────────── */

#func-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-bottom: 1px solid var(--border);
    min-height: 0;
}

#func-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 0;
    min-height: 0;
}

#func-list::-webkit-scrollbar { width: 4px; }
#func-list::-webkit-scrollbar-track { background: transparent; }
#func-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.func-row {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 5px 10px 5px 8px;
    cursor: default;
    border-left: 3px solid transparent;
    transition: background 0.1s;
}

.func-row:hover { background: var(--btn-hover); }
.func-row.active {
    background: var(--btn-bg);
    border-left-color: var(--accent);
}

.func-swatch {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    flex-shrink: 0;
    opacity: 0.9;
}

.func-label {
    flex: 1;
    font-size: 12px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    user-select: none;
}
.func-label:hover { color: #fff; }

.func-edit-input {
    flex: 1;
    background: #000;
    border: 1px solid var(--accent);
    border-radius: 2px;
    color: #ffff55;
    font-family: var(--font);
    font-size: 12px;
    padding: 1px 4px;
    outline: none;
    caret-color: #ffff55;
    min-width: 0;
}

.func-del {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 15px;
    padding: 0 2px;
    line-height: 1;
    flex-shrink: 0;
}
.func-del:hover { color: #ff5555; }

.func-list-empty {
    padding: 12px 10px;
    color: var(--text-dim);
    font-size: 11px;
    text-align: center;
}

/* ── Add-function row (top of list) ──────────────────────────────── */

#func-add-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px 8px 6px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#func-add-top {
    display: flex;
    align-items: center;
    gap: 6px;
}

#func-type {
    background: var(--btn-bg);
    border: 1px solid var(--border);
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
    padding: 4px 5px;
    border-radius: 3px;
    cursor: pointer;
    flex-shrink: 0;
    outline: none;
}
#func-type:focus { border-color: var(--accent); }

#func-input {
    width: 100%;
    background: #0d0d1a;
    border: 1px solid #333348;
    border-radius: 4px;
    color: #ffff88;
    font-family: var(--font);
    font-size: 14px;
    padding: 7px 10px;
    outline: none;
    caret-color: #ffff55;
}
#func-input:focus {
    border-color: var(--accent);
    background: #0a0a16;
}
#func-input::placeholder { color: #334; }

#func-add-btn {
    margin-left: auto;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 3px;
    color: var(--accent);
    font-family: var(--font);
    font-size: 12px;
    padding: 4px 10px;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
}
#func-add-btn:hover { background: var(--btn-hover); border-color: var(--accent); }
#func-add-btn:active { background: var(--btn-active); }

/* ── Toolbar ──────────────────────────────────────────────────────── */

#toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#toolbar button {
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 3px;
    color: var(--text);
    font-family: var(--font);
    font-size: 11px;
    padding: 4px 8px;
    cursor: pointer;
    flex: 1;
    min-width: 48px;
    white-space: nowrap;
    outline: none;
    transition: background 0.1s, border-color 0.1s, color 0.1s;
}
#toolbar button:hover { background: var(--btn-hover); border-color: #444466; color: #fff; }
#toolbar button:active { background: var(--btn-active); }
#toolbar button.active { border-color: var(--accent); color: var(--accent); }

/* ── Range panel ──────────────────────────────────────────────────── */

#range-panel {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#range-display {
    font-size: 11px;
    color: var(--text-dim);
    line-height: 1.7;
}

/* ── Reference panel ──────────────────────────────────────────────── */

#ref-panel {
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

#ref-header {
    padding: 8px 12px;
    font-weight: bold;
    color: var(--accent);
    cursor: pointer;
    user-select: none;
    font-size: 12px;
    transition: background 0.1s;
}
#ref-header:hover {
    background: var(--btn-hover);
}

#ref-content {
    padding: 0 12px 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow: hidden;
    transition: max-height 0.25s ease-out, padding-bottom 0.25s ease-out;
    max-height: 300px;
}

#ref-panel.collapsed #ref-content {
    max-height: 0;
    padding-bottom: 0;
}

.ref-group-title {
    font-size: 10px;
    color: var(--text-dim);
    margin-top: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.ref-items {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

.ref-item {
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 3px;
    color: var(--text);
    padding: 3px 6px;
    font-size: 11px;
    cursor: pointer;
    user-select: none;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.ref-item:hover {
    background: var(--btn-hover);
    border-color: var(--accent2);
    color: #fff;
}

.ref-item:active {
    background: var(--btn-active);
}

/* ── Hint ─────────────────────────────────────────────────────────── */

#sidebar-hint {
    padding: 8px 12px;
    font-size: 10px;
    color: #334;
    line-height: 1.8;
    flex-shrink: 0;
}

/* ── Sidebar resizer ──────────────────────────────────────────────── */

#sidebar-resizer {
    width: 5px;
    flex-shrink: 0;
    cursor: col-resize;
    background: transparent;
    position: relative;
    z-index: 10;
    transition: background 0.15s;
}
#sidebar-resizer::after {
    content: '';
    position: absolute;
    top: 0; bottom: 0;
    left: 2px;
    width: 1px;
    background: var(--border);
    transition: background 0.15s;
}
#sidebar-resizer:hover::after,
#sidebar-resizer.dragging::after { background: var(--accent); }

/* ── Graph area ───────────────────────────────────────────────────── */

#graph-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: #000;
}

#screen {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}
#screen:focus { outline: none; }

/* ── Zoom buttons ─────────────────────────────────────────────────── */

#zoom-btns {
    position: absolute;
    bottom: 16px;
    right: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#zoom-btns button {
    width: 32px;
    height: 32px;
    background: rgba(10, 10, 20, 0.85);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.1s, color 0.1s;
    outline: none;
}
#zoom-btns button:hover { border-color: var(--accent2); color: var(--accent2); }

/* ── Coords overlay ───────────────────────────────────────────────── */

#coords-overlay {
    position: absolute;
    bottom: 10px;
    left: 14px;
    font-size: 11px;
    color: var(--accent2);
    font-family: var(--font);
    pointer-events: none;
    background: rgba(0, 0, 0, 0.55);
    padding: 2px 7px;
    border-radius: 3px;
    min-height: 18px;
}

/* ── Toast ────────────────────────────────────────────────────────── */

#toast {
    position: absolute;
    top: 14px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 10, 0.92);
    border: 1px solid #444466;
    border-radius: 4px;
    padding: 6px 16px;
    font-size: 12px;
    color: #ffff55;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 40;
    white-space: nowrap;
    max-width: calc(100% - 40px);
    text-align: center;
}
#toast.show { opacity: 1; }

/* ── Roots dropdown ───────────────────────────────────────────────── */

#roots-dropdown {
    position: fixed;
    z-index: 50;
    background: #0d0d18;
    border: 1px solid var(--border);
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 4px 16px rgba(0,0,0,0.6);
    min-width: 160px;
}

#roots-dropdown button {
    display: block;
    width: 100%;
    background: none;
    border: none;
    border-bottom: 1px solid #1a1a28;
    color: var(--text);
    padding: 8px 14px;
    cursor: pointer;
    text-align: left;
    font-family: var(--font);
    font-size: 12px;
}
#roots-dropdown button:last-child { border-bottom: none; }
#roots-dropdown button:hover { background: var(--btn-hover); color: #fff; }
