:root {
    --bg: #ffffff; --sidebar: #f7f7f8; --border: #e5e7eb;
    --accent: #111827; --text: #374151; --hover: #f3f4f6;
    --danger: #ef4444; --highlight: #fef08a;
}

/* 1. Reset & Base Layout */
body { 
    font-family: -apple-system, system-ui, sans-serif; 
    margin: 0; display: flex; height: 100vh; width: 100vw;
    color: var(--text); background: white; overflow: hidden; 
}

mark { background-color: var(--highlight); color: inherit; padding: 0 2px; border-radius: 2px; }

/* 2. Sidebar - Stable Flexbox Approach */
#sidebar { 
    width: 260px; min-width: 260px; 
    background: var(--sidebar); border-right: 1px solid var(--border); 
    display: flex; flex-direction: column; position: relative;
    transition: width 0.3s ease, min-width 0.3s ease;
    z-index: 50;
}

/* State when Sidebar is hidden */
#sidebar.hidden {
    width: 0 !important;
    min-width: 0 !important;
    border-right: none;
    overflow: visible; /* Keeps the button visible */
}

.sidebar-header { padding: 16px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
#searchBar { width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; outline: none; font-size: 13px; box-sizing: border-box; }
.list-container { flex: 1; overflow-y: auto; padding: 10px; }

/* Sidebar Footer for Icons - Horizontal Layout */
.sidebar-footer {
    padding: 10px 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    background: white;
    flex-shrink: 0;
}

.sidebar-footer .icon-btn {
    flex: 1;
    min-width: 36px;
}

/* Sidebar List Items */
.folder-header { 
    padding: 10px 12px;
    font-weight: 600; 
    font-size: 13px; 
    color: #374151; 
    cursor: pointer;
    user-select: none;
    transition: all 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 6px;
    margin: 2px 4px;
}

.folder-header:hover {
    background: var(--hover);
}

.folder-header.selected {
    background: #eff6ff;
    color: #2563eb;
    font-weight: 700;
}

.folder-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}

.folder-header:hover .folder-actions {
    opacity: 1;
}

.folder-icon-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    color: #9ca3af;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}

.folder-icon-btn:hover {
    background: var(--hover);
    color: #6b7280;
}

.add-folder-btn {
    margin: 8px 8px 12px 8px;
    padding: 8px 12px;
    background: white;
    border: 1px dashed var(--border);
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: #6b7280;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.2s;
}

.add-folder-btn:hover {
    background: var(--hover);
    border-color: #9ca3af;
    color: var(--accent);
}

.add-folder-btn svg {
    width: 16px;
    height: 16px;
}

.note-item { padding: 8px 12px; cursor: pointer; border-radius: 6px; font-size: 13px; margin: 1px 0; border-left: 3px solid transparent; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-item:hover { background: var(--hover); }
.note-item.active { background: white; border-left-color: var(--accent); font-weight: 500; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }

/* 3. The Sidebar Toggle Button */
#sidebarToggle {
    position: absolute;
    left: 260px; /* Aligned to the width of the sidebar */
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 48px;
    background: white;
    border: 1px solid var(--border);
    border-left: none;
    border-radius: 0 6px 6px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    font-size: 16px;
    color: #9ca3af;
    box-shadow: 2px 0 5px rgba(0,0,0,0.05);
    transition: left 0.3s ease;
}

#sidebarToggle:hover {
    background: var(--hover);
}

#sidebar.hidden #sidebarToggle {
    left: 0; /* Snaps to left edge when sidebar is 0 width */
}

#sidebar.hidden .sidebar-footer {
    display: none;
}

/* 4. Main Content Area */
#main { 
    flex: 1; display: flex; flex-direction: column; 
    min-width: 0; position: relative; background: white; 
}

header { padding: 0 24px; height: 56px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; background: white; z-index: 10; }

.header-actions { display: flex; align-items: center; gap: 4px; }

.icon-btn { 
    width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; 
    border: none; background: transparent; cursor: pointer; border-radius: 8px; 
    transition: all 0.2s ease;
    color: var(--text);
}

.icon-btn svg {
    width: 18px;
    height: 18px;
    stroke-width: 2;
}

.icon-btn:hover { 
    background: var(--hover);
    transform: scale(1.05);
}

.icon-btn:active {
    transform: scale(0.95);
}

.icon-btn.danger:hover { 
    background: #fee2e2;
    color: var(--danger);
}

/* 5. Workspace Grid */
.workspace { 
    flex: 1; display: grid; grid-template-columns: 1fr 6px 1fr; 
    overflow: hidden; background: var(--border); 
}

#editor { border: none; padding: 30px; font-family: 'Fira Code', monospace; font-size: 15px; resize: none; outline: none; line-height: 1.6; background: white; }
.resizer { cursor: col-resize; background: var(--sidebar); position: relative; width: 6px; }
.resizer::after { content: ""; position: absolute; left: 2px; top: 50%; height: 40px; width: 2px; background: #d1d5db; transform: translateY(-50%); border-radius: 2px; }
#preview { padding: 30px; overflow-y: auto; background: white; line-height: 1.6; font-size: 16px; }

/* 6. Library View & Tables */
#libraryView { position: absolute; inset: 0; background: white; z-index: 40; display: none; flex-direction: column; padding: 40px; box-sizing: border-box; overflow-y: auto; }
.lib-table { width: 100%; border-collapse: collapse; margin-top: 20px; table-layout: fixed; }
.lib-table th { text-align: left; padding: 12px; border-bottom: 2px solid var(--border); font-size: 13px; color: #6b7280; cursor: pointer; }
.lib-table td { padding: 14px 12px; border-bottom: 1px solid var(--border); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-pill { background: #eff6ff; color: #2563eb; padding: 2px 8px; border-radius: 4px; font-size: 11px; margin-right: 4px; }
select.btn-folder { border: 1px solid var(--border); background: white; padding: 4px 8px; border-radius: 6px; font-size: 12px; cursor: pointer; outline: none; }