/* --- 1. Root Variables & System Themes --- */
:root {
    --bg-color: #f7f3ee;
    --toolbar-bg: #e8ded1;
    --toolbar-sub: #f2ece4;
    --border-color: #d5c6b3;
    --text-main: #4a4238;
    --btn-hover: #dfd3c3;
    --btn-active: #c8b8a3;
    --accent: #8f5b45;
    /* Toolbar height exposed as a variable so JS can read it via getComputedStyle */
    --toolbar-height: 88px; /* primary (44px) + sub (44px) */
}

.theme-white { --bg-color: #ffffff; --toolbar-bg: #f8f9fa; --toolbar-sub: #f1f3f5; --border-color: #dee2e6; --text-main: #212529; --btn-hover: #e9ecef; --btn-active: #dee2e6; --accent: #228be6; }
.theme-warm { --bg-color: #f7f3ee; --toolbar-bg: #e8ded1; --toolbar-sub: #f2ece4; --border-color: #d5c6b3; --text-main: #4a4238; --btn-hover: #dfd3c3; --btn-active: #c8b8a3; --accent: #8f5b45; }
.theme-pastel-amber { --bg-color: #fdf6e3; --toolbar-bg: #f5e6c8; --toolbar-sub: #e6d3a8; --border-color: #d6c193; --text-main: #5e4b30; --btn-hover: #e6d3a8; --btn-active: #d6c193; --accent: #b58900; }
.theme-pastel-blue { --bg-color: #edf4f7; --toolbar-bg: #d2dfe3; --toolbar-sub: #bcd0d6; --border-color: #9dbbc4; --text-main: #1e3a5f; --btn-hover: #bcd0d6; --btn-active: #9dbbc4; --accent: #2563eb; }
.theme-pastel-green { --bg-color: #eef4f0; --toolbar-bg: #c8d9ce; --toolbar-sub: #bed0c3; --border-color: #9cb8a5; --text-main: #2d4f3b; --btn-hover: #bed0c3; --btn-active: #9cb8a5; --accent: #059669; }
.theme-charcoal { --bg-color: #2b2622; --toolbar-bg: #37322e; --toolbar-sub: #423c37; --border-color: #564e47; --text-main: #d5c6b3; --btn-hover: #524b44; --btn-active: #564e47; --accent: #e2b464; }
.theme-black { --bg-color: #1c1a18; --toolbar-bg: #292622; --toolbar-sub: #3d3833; --border-color: #524b44; --text-main: #e0e0e0; --btn-hover: #423c37; --btn-active: #564e47; --accent: #f8fafc; }

/* --- 2. Layout Scaffolding --- */
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

body {
    display: flex;
    flex-direction: column;
    background-color: var(--bg-color);
    color: var(--text-main);
    font-family: system-ui, sans-serif;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* --- 3. Toolbar structure ---
   Single combined toolbar row: logo | tools | actions
   Sub-toolbar beneath for context-sensitive settings
*/
#toolbar-wrapper {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
    z-index: 10;
}

/* Primary toolbar — tighter than before, single row */
.toolbar {
    background-color: var(--toolbar-bg);
    padding: 5px 12px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: 44px;
    box-sizing: border-box;
}

/* Sub-toolbar — only shown when there's interactive content */
.sub-toolbar {
    background-color: var(--toolbar-sub);
    padding: 5px 12px;
    border-bottom: 2px solid var(--border-color);
    min-height: 36px;
    display: flex;
    align-items: center;
    box-sizing: border-box;
}

/* Hide the sub-toolbar entirely when its active panel is empty (info-only) */
.sub-toolbar.info-only {
    display: none;
}

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

/* Lesson title — sits inside the primary toolbar */
#lesson-title {
    font-weight: 700;
    font-size: 18px;
    color: var(--text-main);
    border-bottom: 1px dashed transparent;
    padding: 2px 6px;
    border-radius: 4px;
    outline: none;
    transition: background 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 320px;
    min-width: 80px;
    cursor: text;
}
#lesson-title:hover { background-color: var(--btn-hover); border-bottom: 1px dashed var(--text-main); }
#lesson-title:focus { background-color: var(--bg-color); border-bottom: 1px solid var(--btn-active); }
#lesson-title:empty::before { content: attr(placeholder); color: #a49e94; font-weight: 400; font-style: italic; }

.divider {
    width: 1px;
    height: 20px;
    background-color: var(--border-color);
    border-radius: 1px;
    margin: 0 2px;
    flex-shrink: 0;
}

.settings-panel {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

/* --- 4. Buttons --- */
button {
    background: transparent;
    border: none;
    padding: 5px 8px;
    border-radius: 6px;
    color: var(--text-main);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
    line-height: 1;
}
button:hover { background-color: var(--btn-hover); }
button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* Tool buttons — compact square */
.tool-btn {
    padding: 5px 6px;
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}
.tool-btn.active {
    background-color: var(--btn-active);
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.08);
}

/* Action button momentary "applied" feedback (e.g. Set Home view) */
.action-btn.active {
    background-color: var(--accent);
    color: #fff;
}
.action-btn.active i { color: #fff; }

button i, button svg {
    display: inline-block;
    vertical-align: middle;
    width: 18px;
    height: 18px;
}

/* Action buttons — same size as tool buttons */
.action-btn {
    padding: 5px 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sub-btn {
    padding: 4px 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
}

.nav-btn {
    background: transparent;
    border: none;
    padding: 3px;
    border-radius: 4px;
    cursor: pointer;
    color: var(--text-main);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.1s;
}
.nav-btn:hover { background: var(--toolbar-bg); }
.nav-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Pen preview lines — slightly smaller */
.pen-preview-line { }

/* --- 5. Colour swatches — slightly smaller to save toolbar space --- */
.color-palette { display: flex; gap: 5px; flex-wrap: wrap; }

.color-swatch {
    width: 22px;
    height: 22px;
    padding: 0;
    border: none;
    cursor: pointer;
    transition: transform 0.15s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    clip-path: polygon(50% 0%, 90% 18%, 100% 62%, 72% 98%, 28% 98%, 0% 62%, 10% 18%);
}
.color-swatch:hover { transform: scale(1.18); }
.color-swatch.active { box-shadow: 0 0 0 2px var(--text-main); transform: scale(1.1); }
.color-swatch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- 6. File Sidebar --- */
.sidebar {
    position: absolute;
    top: 0;
    left: 0;
    width: 280px;
    height: 100vh;
    background-color: var(--toolbar-bg);
    border-right: 1px solid var(--border-color);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sidebar.collapsed { transform: translateX(-100%); }

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background-color: var(--toolbar-bg);
    border-bottom: 1px solid var(--border-color);
}

.sidebar-actions {
    display: flex;
    gap: 4px;
    padding: 8px 12px;
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
}
.sidebar-actions button {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: var(--toolbar-sub);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    padding: 5px 6px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    border-radius: 5px;
}
.sidebar-actions button:hover { background: var(--toolbar-bg); }

/* --- Settings modal --- */
.settings-overlay {
    position: fixed;
    inset: 0;
    z-index: 5000; /* above the timer (4000) and all canvas UI */
    background: rgba(20, 16, 12, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    animation: settings-fade 0.15s ease;
}
@keyframes settings-fade { from { opacity: 0; } to { opacity: 1; } }

.settings-modal {
    width: 100%;
    max-width: 540px;
    max-height: 85vh;
    overflow-y: auto;
    background: var(--bg-color);
    color: var(--text-main);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    animation: settings-rise 0.18s cubic-bezier(0.2,0.8,0.2,1);
}
@keyframes settings-rise { from { transform: translateY(12px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.settings-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 22px;
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    background: var(--bg-color);
    border-radius: 14px 14px 0 0;
}
.settings-modal-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
}
.settings-close-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: transparent;
    border: none;
    color: var(--text-main);
    cursor: pointer;
}
.settings-close-btn:hover { background: var(--btn-hover); }

.settings-modal-body { padding: 8px 22px 22px; }

.settings-section { padding: 16px 0; border-bottom: 1px solid var(--border-color); }
.settings-section:last-child { border-bottom: none; }
.settings-section-title {
    margin: 0 0 12px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 1.2px;
    text-transform: uppercase;
    opacity: 0.6;
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 8px 0;
}
.settings-label { display: flex; flex-direction: column; gap: 2px; }
.settings-label-main { font-size: 14px; font-weight: 600; }
.settings-label-sub { font-size: 12px; opacity: 0.6; line-height: 1.35; }

.settings-control {
    padding: 7px 10px;
    border-radius: 7px;
    border: 1px solid var(--border-color);
    background: var(--toolbar-sub);
    color: var(--text-main);
    font-size: 13px;
    min-width: 130px;
}

.settings-section-muted { opacity: 0.5; }
.settings-coming-soon { margin: 0; font-size: 13px; opacity: 0.7; font-style: italic; }

/* Toggle switch */
.settings-switch { position: relative; display: inline-flex; cursor: pointer; flex-shrink: 0; }
.settings-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.settings-switch-track {
    width: 44px;
    height: 24px;
    border-radius: 999px;
    background: var(--border-color);
    transition: background 0.18s;
    position: relative;
}
.settings-switch-track::after {
    content: "";
    position: absolute;
    top: 3px; left: 3px;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.25);
    transition: transform 0.18s;
}
.settings-switch input:checked + .settings-switch-track { background: var(--accent); }
.settings-switch input:checked + .settings-switch-track::after { transform: translateX(20px); }
.settings-switch input:focus-visible + .settings-switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- Geometry instruments menu --- */
#instruments-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    z-index: 3000;
    background: var(--toolbar-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.14);
    padding: 5px;
    min-width: 210px;
}
.instrument-item {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    padding: 8px 10px;
    border-radius: 6px;
    background: transparent;
    border: none;
    color: var(--text-main);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    white-space: nowrap;
}
.instrument-item:hover { background: var(--btn-hover); }
.instrument-item i { width: 16px; height: 16px; flex-shrink: 0; opacity: 0.85; }
.instrument-clear { color: #c0392b; }
.instrument-clear i { color: #c0392b; }
.instrument-sep { height: 1px; background: var(--border-color); margin: 4px 6px; }

/* --- Context Menu --- */
#tree-context-menu {
    position: fixed;
    z-index: 9999;
    background: var(--toolbar-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    padding: 4px;
    min-width: 160px;
    font-size: 13px;
    font-family: system-ui, sans-serif;
}
.ctx-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    border-radius: 5px;
    cursor: pointer;
    color: var(--text-main);
    user-select: none;
    transition: background 0.1s;
}
.ctx-item:hover { background: var(--btn-hover); }
.ctx-item.ctx-disabled { opacity: 0.4; pointer-events: none; }
.ctx-item i { width: 14px; height: 14px; flex-shrink: 0; }
.ctx-separator { height: 1px; background: var(--border-color); margin: 3px 8px; }

.file-tree {
    flex-grow: 1;
    overflow-y: auto;
    padding: 12px;
    font-size: 13px;
    color: var(--text-main);
}

.tree-item { margin-bottom: 3px; }

.shelf-container { padding-left: 8px; margin-top: 3px; border-left: 1px solid var(--border-color); margin-left: 8px; }
.book-container { padding-left: 8px; margin-top: 3px; border-left: 1px dashed var(--border-color); margin-left: 8px; }
.chapter-container { padding-left: 8px; margin-top: 3px; border-left: 1px dotted var(--btn-active); margin-left: 8px; }

.page-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 5px 5px 10px;
    border-radius: 5px;
    cursor: pointer;
    user-select: none;
    color: var(--text-main);
    font-size: 12px;
    opacity: 0.8;
}
.page-item:hover { background: var(--toolbar-sub); opacity: 1; }

/* --- 7. Workspace --- */
#workspace-container {
    flex-grow: 1;
    position: relative;
    width: 100%;
    overflow: hidden;
    cursor: crosshair;
    touch-action: none;
}

#main-svg {
    width: 100%;
    height: 100%;
    display: block;
    background-color: var(--bg-color);
}

/* --- 8. Tree node interactions --- */
.node-clickable {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 6px;
    border-radius: 5px;
    cursor: pointer;
    user-select: none;
    transition: background 0.1s;
}
.node-clickable:hover { background: var(--toolbar-sub); }
.node-clickable.selected {
    background: var(--toolbar-sub);
    box-shadow: inset 0 0 0 1.5px var(--accent);
}
.page-item .node-clickable.active-page {
    background: var(--accent);
    color: #ffffff;
    font-weight: bold;
}
.page-item .node-clickable.active-page i { color: #ffffff; }

.item-actions { display: none; gap: 3px; align-items: center; }
.node-clickable:hover .item-actions { display: flex; }
.action-btn {
    background: transparent;
    border: none;
    padding: 2px;
    color: inherit;
    opacity: 0.6;
    cursor: pointer;
    border-radius: 3px;
}
.action-btn:hover { opacity: 1; background: var(--btn-hover); }

.rename-input {
    flex-grow: 1;
    background: var(--bg-color);
    color: var(--text-main);
    border: 1.5px solid var(--accent);
    border-radius: 4px;
    padding: 2px 6px;
    font-family: inherit;
    font-size: inherit;
    outline: none;
}

.tree-item > .node-clickable { display: flex; align-items: center; gap: 4px; }

.toggle-arrow {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    cursor: pointer;
    color: var(--text-main);
    opacity: 0.6;
    transition: opacity 0.1s;
}
.toggle-arrow:hover { opacity: 1; }

/* --- 9. Resizable Side Canvas (Scratchpad) --- */
#scratchpad-panel {
    position: absolute;
    top: 0;
    right: 0;
    width: 380px;
    height: 100%;
    background-color: var(--bg-color);
    border-left: 2px solid var(--border-color);
    box-shadow: -4px 0 20px rgba(0,0,0,0.07);
    z-index: 1500;
    display: flex;
    flex-direction: column;
}

#scratchpad-panel.hidden {
    transform: translateX(calc(100% + 4px));
    transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
    pointer-events: none;
}
#scratchpad-panel:not(.hidden) {
    transform: translateX(0);
    transition: none;
}
#scratchpad-panel.animating {
    transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1) !important;
}

/* Drag-resize handle — straddles the left border of the panel */
#scratchpad-resize-handle {
    position: absolute;
    left: -10px;
    top: 0;
    width: 20px;
    height: 100%;
    cursor: ew-resize;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
}
.scratchpad-handle-grip {
    display: flex;
    flex-direction: column;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.15s;
    pointer-events: none;
}
#scratchpad-resize-handle:hover .scratchpad-handle-grip,
#scratchpad-resize-handle.dragging .scratchpad-handle-grip { opacity: 1; }
.scratchpad-handle-grip span {
    display: block;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--border-color);
}
body.scratchpad-resizing {
    cursor: ew-resize !important;
    user-select: none;
}

/* --- Scratchpad open tab ---
   A small persistent pill on the right edge of the canvas,
   always visible even when the panel is hidden.
   Clicking it opens/closes the side canvas.
*/
#scratchpad-open-tab {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1400; /* below scratchpad panel but above canvas */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    width: 20px;
    padding: 14px 0;
    background: var(--toolbar-bg);
    border: 1px solid var(--border-color);
    border-right: none;
    border-radius: 8px 0 0 8px;
    cursor: pointer;
    box-shadow: -2px 0 8px rgba(0,0,0,0.06);
    transition: background 0.15s, width 0.15s;
    /* Hidden behind the panel when it's open */
    pointer-events: auto;
}
#scratchpad-open-tab:hover {
    background: var(--btn-hover);
    width: 24px;
}
#scratchpad-open-tab:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}
/* Three vertical grip dots */
#scratchpad-open-tab span {
    display: block;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--accent);
    flex-shrink: 0;
}
/* Hide the tab when the panel is open — the resize handle takes over */
#scratchpad-panel:not(.hidden) ~ #scratchpad-open-tab {
    display: none;
}

/* Scratchpad internal */
.scratchpad-header {
    flex-shrink: 0;
    padding: 8px 12px;
    background-color: var(--toolbar-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 700;
    font-size: 13px;
    color: var(--text-main);
}
#scratchpad-workspace {
    flex-grow: 1;
    position: relative;
    overflow: hidden;
    touch-action: none;
}
#scratchpad-svg {
    width: 100%;
    height: 100%;
    display: block;
    background-color: var(--toolbar-sub);
}
#scratchpad-page-settings-btn.active {
    background-color: var(--btn-active);
    color: var(--accent);
}

/* --- 10. Floating overlays --- */
#timer-widget {
    font-family: system-ui, sans-serif;
}

/* Page navigator — smaller and more subtle */
#page-navigator {
    backdrop-filter: blur(4px);
}

/* Tooltip enhancement — longer tooltips for accessibility */
[title] { cursor: default; }