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

:root {
    /* Default Dark Theme */
    --bg-app: #0f172a;
    --bg-sidebar: rgba(30, 41, 59, 0.7);
    --bg-panel: #1e293b;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent-color: #38bdf8;
    --accent-hover: #0ea5e9;
    --border-color: rgba(148, 163, 184, 0.1);
    --code-bg: #0f1117;
    --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
    --font-mono: 'Fira Code', 'Roboto Mono', monospace;
    --glass-border: 1px solid rgba(255, 255, 255, 0.05);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    --pane-header-bg: rgba(0, 0, 0, 0.2);
    --editor-text: #e2e8f0;
    --input-placeholder: rgba(255, 255, 255, 0.3);

    /* Markdown Specific Defaults */
    --md-heading: #fff;
    --md-code-bg: rgba(255, 255, 255, 0.1);
    --md-pre-bg: #0f1117;
    --md-bq-bg: rgba(56, 189, 248, 0.05);
    --md-th-bg: rgba(255, 255, 255, 0.05);
}

/* Light Theme Overrides */
[data-theme="light"] {
    --bg-app: #ffffff;
    --bg-sidebar: #f8fafc;
    /* Slate 50 */
    --bg-panel: #ffffff;
    --text-primary: #0f172a;
    /* Slate 900 */
    --text-secondary: #64748b;
    /* Slate 500 */
    --accent-color: #0284c7;
    /* Sky 600 */
    --accent-hover: #0369a1;
    --border-color: rgba(0, 0, 0, 0.1);
    --code-bg: #f1f5f9;
    /* Slate 100 */
    --glass-border: 1px solid rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --pane-header-bg: #f1f5f9;
    --editor-text: #334155;
    --input-placeholder: rgba(0, 0, 0, 0.3);

    /* Markdown Specific Light */
    --md-heading: #1e293b;
    --md-code-bg: rgba(0, 0, 0, 0.05);
    --md-pre-bg: #f8fafc;
    /* Slate 50 */
    --md-bq-bg: rgba(2, 132, 199, 0.05);
    --md-th-bg: #f1f5f9;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-app);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    line-height: 1.6;
    transition: background-color 0.3s, color 0.3s;
}

/* Layout */
.app-container {
    display: grid;
    grid-template-columns: 280px 1fr;
    height: 100vh;
    width: 100vw;
}

/* Sidebar */
.sidebar {
    background: var(--bg-sidebar);
    border-right: var(--glass-border);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
    height: 100%;
    min-height: 0;
    /* Critical for flex children with overflow */
    overflow: hidden;
    /* Prevent sidebar itself from scrolling */
    transition: background-color 0.3s;
}

.brand {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    letter-spacing: -0.5px;
}

.brand span {
    color: var(--accent-color);
}

/* Sidebar Controllers */
.sidebar-controllers {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0 0.5rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.control-group label {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-secondary);
    letter-spacing: 0.05em;
    opacity: 0.8;
}

.control-group select {
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.5rem;
    color: var(--text-primary);
    font-size: 0.85rem;
    outline: none;
    cursor: pointer;
    transition: all 0.2s;
}

[data-theme="light"] .control-group select {
    background: #fff;
}

.control-group select:focus {
    border-color: var(--accent-color);
}

.type-toggle {
    display: flex;
    background: rgba(0, 0, 0, 0.2);
    padding: 3px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

[data-theme="light"] .type-toggle {
    background: rgba(0, 0, 0, 0.03);
}

.type-toggle button {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s;
}

.type-toggle button.active {
    background: var(--bg-panel);
    color: var(--accent-color);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

[data-theme="light"] .type-toggle button.active {
    background: #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Search Box */
.search-container {
    padding: 0 0.5rem;
}

.search-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 0.75rem;
    color: var(--text-secondary);
    pointer-events: none;
    width: 16px;
    height: 16px;
    opacity: 0.7;
}

#sidebar-search {
    width: 100%;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 0.5rem 0.75rem 0.5rem 2.25rem;
    color: var(--text-primary);
    font-size: 0.85rem;
    font-family: var(--font-sans);
    transition: all 0.2s;
    outline: none;
}

#sidebar-search:focus {
    border-color: var(--accent-color);
    background: rgba(0, 0, 0, 0.3);
    box-shadow: 0 0 0 1px var(--accent-color);
}

[data-theme="light"] #sidebar-search {
    background: rgba(0, 0, 0, 0.05);
}

[data-theme="light"] #sidebar-search:focus {
    background: rgba(0, 0, 0, 0.08);
}

/* Hide Search when sidebar collapsed */
.app-container.sidebar-collapsed .search-container {
    display: none;
}

.nav-menu {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto;
    overflow-x: hidden;
    flex: 1;
    /* Take remaining space in sidebar */
    min-height: 0;
    /* Critical for flex children with overflow */
    padding-bottom: 2rem;
    /* Ensure scrollbar is visible */
    -webkit-overflow-scrolling: touch;
}

/* Functional Tree Nodes */
.nav-node {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-node-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border-radius: 0.5rem;
    cursor: pointer;
    color: var(--text-primary);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s;
    user-select: none;
}

.nav-node-header:hover {
    background: rgba(255, 255, 255, 0.05);
}

.nav-node-header svg.chevron {
    width: 14px;
    height: 14px;
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.nav-node.collapsed>.nav-node-content {
    display: none;
}

.nav-node.collapsed>.nav-node-header svg.chevron {
    transform: rotate(-90deg);
}

.nav-node-content {
    margin-left: 1.25rem;
    border-left: 1px solid var(--border-color);
    padding-left: 0.25rem;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-item {
    padding: 0.5rem 0.75rem;
    border-radius: 0.4rem;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.nav-item::before {
    content: '';
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--text-secondary);
    opacity: 0.3;
    flex-shrink: 0;
}

.nav-item:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    transform: translateX(2px);
}

.nav-item.active {
    background: rgba(56, 189, 248, 0.1);
    color: var(--accent-color);
    font-weight: 600;
}

.nav-item.active::before {
    background: var(--accent-color);
    opacity: 1;
}

.nav-item.disabled {
    opacity: 0.4;
    cursor: not-allowed;
    filter: grayscale(1);
}

.nav-item.disabled:hover {
    background: transparent;
    transform: none;
    color: var(--text-secondary);
}

[data-theme="light"] .nav-node-header:hover,
[data-theme="light"] .nav-item:hover {
    background: rgba(0, 0, 0, 0.03);
}

[data-theme="light"] .nav-item.active {
    background: rgba(2, 132, 199, 0.08);
}

/* Functional Node Enhancements */
.functional-node .nav-node-header {
    color: var(--text-primary);
    font-weight: 600;
    opacity: 0.9;
}

.version-links {
    margin-left: 1.5rem !important;
    border-left: 1px dashed var(--border-color) !important;
}

.version-item {
    padding: 0.35rem 0.75rem !important;
    font-size: 0.8rem !important;
    opacity: 0.8;
}

.version-item:hover {
    opacity: 1;
}

.ver-tag {
    font-size: 0.65rem;
    font-weight: 800;
    padding: 1px 4px;
    border-radius: 4px;
    margin-right: 4px;
    text-transform: uppercase;
}

.ver-tag.sa {
    background: rgba(56, 189, 248, 0.15);
    color: #38bdf8;
    border: 1px solid rgba(56, 189, 248, 0.3);
}

.ver-tag.sd {
    background: rgba(168, 85, 247, 0.15);
    color: #a855f7;
    border: 1px solid rgba(168, 85, 247, 0.3);
}

[data-theme="light"] .ver-tag.sa {
    background: rgba(2, 132, 199, 0.1);
    color: #0284c7;
}

[data-theme="light"] .ver-tag.sd {
    background: rgba(147, 51, 234, 0.1);
    color: #9333ea;
}

/* Main Content */
.main-content {
    display: grid;
    grid-template-rows: auto 1fr;
    height: 100%;
    overflow: hidden;
    background: var(--bg-panel);
    /* Removed radial gradient for cleaner light mode switch */
    transition: background-color 0.3s;
}

.header {
    padding: 1rem 2rem;
    border-bottom: var(--glass-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60px;
}

.file-info {
    font-size: 0.9rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.file-name {
    color: var(--text-primary);
    font-weight: 600;
}

#file-name-sub {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0;
}

#file-name-sub a {
    color: var(--accent-color);
    text-decoration: none;
    cursor: pointer;
    padding: 0.1rem 0.25rem;
    border-radius: 3px;
    transition: all 0.2s;
    opacity: 0.8;
}

#file-name-sub a:hover {
    opacity: 1;
    background: rgba(56, 189, 248, 0.1);
    text-decoration: underline;
}

#file-name-sub a:active {
    opacity: 0.9;
}

/* Actions in Header */
.header-actions {
    display: flex;
    gap: 0.5rem;
}

.btn-toggle {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.4rem;
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-toggle:hover {
    background: rgba(125, 125, 125, 0.1);
    color: var(--text-primary);
}


.editor-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    height: 100%;
    min-height: 0;
    /* Critical for grid children in flex */
    overflow: hidden;
    position: relative;
    transition: grid-template-columns 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Collapsed State Logic */
.editor-container.editor-collapsed {
    grid-template-columns: 0px 1fr;
}


.pane {
    height: 100%;
    min-height: 0;
    /* Critical for flex children in grid */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-width: 0;
    position: relative;
    background-color: transparent;
}

.pane-header {
    padding: 0.5rem 1.5rem;
    background: var(--pane-header-bg);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    font-weight: 600;
    border-bottom: var(--glass-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    border-right: var(--glass-border);
}

.pane-actions {
    display: flex;
    gap: 4px;
    align-items: center;
}

.btn-icon {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon:hover {
    color: var(--text-primary);
    background: rgba(125, 125, 125, 0.1);
}

/* Restore Button */
#restore-syntax-btn {
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    z-index: 50;
    background: var(--bg-panel);
    border: var(--glass-border);
    border-left: none;
    padding: 1rem 0.5rem;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    color: var(--accent-color);
    box-shadow: var(--shadow-lg);
    display: none;
}

.editor-container.editor-collapsed #restore-syntax-btn {
    display: block;
}

.pane-content {
    flex: 1 1 0;
    /* flex-grow: 1, flex-shrink: 1, flex-basis: 0 */
    min-height: 0;
    /* Critical for flex children with overflow */
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0;
    position: relative;
    /* Ensure scrolling works correctly */
    -webkit-overflow-scrolling: touch;
}

/* MD Editor Textarea */
.code-editor {
    width: 100%;
    height: 100%;
    background-color: var(--code-bg);
    color: var(--editor-text);
    font-family: var(--font-mono);
    padding: 1.5rem;
    font-size: 0.9rem;
    border: none;
    resize: none;
    outline: none;
    line-height: 1.7;
    tab-size: 4;
    border-right: var(--glass-border);
    transition: background-color 0.3s, color 0.3s;
}

.code-editor::placeholder {
    color: var(--input-placeholder);
}

.code-editor::selection {
    background: rgba(56, 189, 248, 0.2);
}

.preview-view {
    padding: 2rem 3rem;
    background: transparent;
    box-sizing: border-box;
    /* Ensure content height is calculated correctly for scrolling */
    /* Note: .preview-view also has .pane-content class, so it inherits flex: 1 and overflow-y: auto */
    /* The scrollable area should be the .preview-view itself, not its content */
    /* Content should expand naturally within the scrollable container */
    /* Explicitly ensure overflow is set - critical for scrollbar visibility */
    overflow-y: auto !important;
    overflow-x: hidden !important;
}

/* Markdown Styles */
.markdown-body {
    color: var(--text-primary);
    line-height: 1.8;
    max-width: 800px;
    margin: 0 auto;
    /* Ensure content can expand fully - critical for proper scrolling */
    display: block;
    width: 100%;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
    margin-top: 1.5em;
    margin-bottom: 0.5em;
    font-weight: 700;
    letter-spacing: -0.025em;
    color: var(--md-heading);
}

.markdown-body h1 {
    font-size: 2.25rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.3em;
}

.markdown-body h2 {
    font-size: 1.75rem;
}

.markdown-body p {
    margin-bottom: 1.25em;
}

.markdown-body a {
    color: var(--accent-color);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s;
}

.markdown-body a:hover {
    border-bottom-color: var(--accent-color);
}

.markdown-body code {
    background: var(--md-code-bg);
    padding: 0.2em 0.4em;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 0.85em;
    color: var(--text-primary);
    /* Ensure code text is visible */
}

.markdown-body pre {
    background: var(--md-pre-bg);
    padding: 1.25rem;
    border-radius: 0.75rem;
    overflow-x: auto;
    margin-bottom: 1.5em;
    border: 1px solid var(--border-color);
}

.markdown-body pre code {
    background: transparent;
    padding: 0;
    color: var(--editor-text);
    /* Use editor text color for homogeneity */
}

.markdown-body blockquote {
    border-left: 4px solid var(--accent-color);
    padding-left: 1rem;
    margin-left: 0;
    color: var(--text-secondary);
    font-style: italic;
    background: var(--md-bq-bg);
    padding: 1rem;
    border-radius: 0 0.5rem 0.5rem 0;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1.5em;
}

.markdown-body th,
.markdown-body td {
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    text-align: left;
}

.markdown-body th {
    background: var(--md-th-bg);
    font-weight: 600;
    color: var(--text-primary);
}

.markdown-body ul,
.markdown-body ol {
    margin-bottom: 1.25em;
    padding-left: 1.5em;
}

.markdown-body li {
    margin-bottom: 0.5em;
}

.markdown-body img {
    max-width: 100%;
    border-radius: 0.5rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
}

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

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

::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.2);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(148, 163, 184, 0.4);
}

/* ... existing styles ... */
/* Sidebar Collapsed State */
.app-container.sidebar-collapsed {
    grid-template-columns: 0px 1fr;
}

.app-container.sidebar-collapsed .sidebar {
    transform: translateX(-100%);
    opacity: 0;
}

.app-container.sidebar-collapsed #sidebar-restore-area {
    display: block !important;
}


/* Loading State */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    font-size: 1.1rem;
}

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    color: var(--text-secondary);
    gap: 1rem;
    opacity: 0.6;
}

.icon-doc {
    width: 64px;
    height: 64px;
    stroke: currentColor;
    stroke-width: 1.5;
}

/* Full Width Preview when Editor is Collapsed */
.editor-container.editor-collapsed .markdown-body {
    max-width: 95%;
    /* Make it almost full width, keeping some breathing room */
    transition: max-width 0.3s ease;
}

/* Preview Search */
.preview-search-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#preview-search-input {
    background: rgba(0, 0, 0, 0.1);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    width: 120px;
    outline: none;
    transition: all 0.2s;
}

#preview-search-input:focus {
    border-color: var(--accent-color);
    background: rgba(0, 0, 0, 0.2);
    width: 160px;
}

[data-theme="light"] #preview-search-input {
    background: rgba(0, 0, 0, 0.05);
}

#preview-search-count {
    font-size: 0.75rem;
    color: var(--text-secondary);
    min-width: 3rem;
    text-align: center;
}

.preview-search-actions {
    display: flex;
    gap: 2px;
}

/* Search Highlights */
mark.search-highlight {
    background-color: rgba(255, 255, 0, 0.4);
    color: inherit;
    padding: 0;
    border-radius: 2px;
}

mark.search-highlight.active {
    background-color: rgba(255, 140, 0, 0.8);
    color: white;
    box-shadow: 0 0 0 2px rgba(255, 140, 0, 0.4);
    z-index: 10;
    position: relative;
}

[data-theme="dark"] mark.search-highlight {
    background-color: rgba(255, 215, 0, 0.3);
    color: #fff;
}

[data-theme="dark"] mark.search-highlight.active {
    background-color: rgba(255, 165, 0, 0.8);
}