/* CSS Variables */
:root {
    --bg-primary: #ffffff;
    --bg-secondary: #f5f5f5;
    --bg-tertiary: #e8e8e8;
    --text-primary: #1a1a1a;
    --text-secondary: #666666;
    --text-muted: #999999;
    --border-color: #e0e0e0;
    --accent-color: #fc6d26;
    --accent-hover: #e55d1a;
    --danger-color: #dc3545;
    --danger-hover: #c82333;
    --success-color: #28a745;
    --link-color: #1068bf;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.1);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --header-height: 56px;
    --sidebar-width: 260px;
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
}

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

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
    background: var(--bg-primary);
}

/* Utility Classes */
.hidden {
    display: none !important;
}

.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media (max-width: 768px) {
    .mobile-only {
        display: flex;
    }
    .desktop-only {
        display: none !important;
    }
}

/* Screens */
.screen {
    height: 100%;
    width: 100%;
}

/* Login Screen */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-container {
    text-align: center;
    padding: 48px;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    max-width: 400px;
    width: 90%;
}

.login-container h1 {
    font-size: 32px;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.login-container p {
    color: var(--text-secondary);
    margin-bottom: 32px;
}

/* Buttons */
.primary-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    background: var(--accent-color);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.primary-btn:hover {
    background: var(--accent-hover);
}

.primary-btn.danger {
    background: var(--danger-color);
}

.primary-btn.danger:hover {
    background: var(--danger-hover);
}

.secondary-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.secondary-btn:hover {
    background: var(--bg-tertiary);
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.icon-btn:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.icon-btn.danger:hover {
    background: rgba(220, 53, 69, 0.1);
    color: var(--danger-color);
}

.icon-btn.small {
    width: 28px;
    height: 28px;
}

.icon-btn.small svg {
    width: 14px;
    height: 14px;
}

/* App Screen */
#app-screen {
    display: flex;
    flex-direction: column;
}

/* Header */
#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 16px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

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

.app-title {
    font-size: 18px;
    font-weight: 600;
    margin-left: 8px;
}

.header-center {
    flex: 1;
    max-width: 400px;
    margin: 0 24px;
}

.search-container {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    transition: all var(--transition-fast);
}

.search-container:focus-within {
    background: var(--bg-primary);
    border-color: var(--accent-color);
}

.search-container svg {
    color: var(--text-muted);
    flex-shrink: 0;
}

.search-container input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 14px;
    outline: none;
}

/* Main Content */
#main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
#sidebar {
    width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: transform var(--transition-normal);
}

#sidebar.collapsed {
    transform: translateX(-100%);
    position: absolute;
    height: calc(100% - var(--header-height));
    z-index: 100;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

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

.notes-tree {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.note-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-primary);
    transition: background var(--transition-fast);
}

.note-item:hover {
    background: var(--bg-tertiary);
}

.note-item.active {
    background: var(--accent-color);
    color: white;
}

.note-item svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

.note-item span {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.folder-item {
    margin-bottom: 4px;
}

.folder-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-secondary);
    font-weight: 500;
    transition: background var(--transition-fast);
}

.folder-header:hover {
    background: var(--bg-tertiary);
}

.folder-header svg {
    width: 16px;
    height: 16px;
    transition: transform var(--transition-fast);
}

.folder-header.expanded svg:first-child {
    transform: rotate(90deg);
}

/* Sidebar Sections */
.sidebar-section {
    border-bottom: 1px solid var(--border-color);
}

.sidebar-section:last-child {
    border-bottom: none;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Calendar */
.calendar {
    padding: 8px;
}

.calendar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0 8px;
}

.calendar-header span {
    font-weight: 600;
    font-size: 13px;
    color: var(--text-primary);
}

.calendar-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    padding: 4px 0;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 4px;
}

.calendar-days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 2px;
}

.calendar-day {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    color: var(--text-primary);
}

.calendar-day:hover {
    background: var(--bg-tertiary);
}

.calendar-day.other-month {
    color: var(--text-muted);
}

.calendar-day.today {
    font-weight: 700;
    color: var(--accent-color);
    border: 1px solid var(--accent-color);
}

.calendar-day.has-note {
    background: rgba(252, 109, 38, 0.15);
}

.calendar-day.has-note:hover {
    background: rgba(252, 109, 38, 0.25);
}

.calendar-day.selected {
    background: var(--accent-color);
    color: white;
}

.calendar-day.selected.has-note {
    background: var(--accent-color);
}

.folder-contents {
    padding-left: 20px;
    display: none;
}

.folder-item.expanded .folder-contents {
    display: block;
}

/* Editor Area */
#editor-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

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

.empty-state svg {
    margin-bottom: 16px;
    opacity: 0.5;
}

/* Note Editor */
.note-editor {
    display: flex;
    flex-direction: column;
    height: 100%;
}

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

.note-title-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 18px;
    font-weight: 600;
    outline: none;
    padding: 4px 0;
}

.note-actions {
    display: flex;
    gap: 4px;
}

/* View Tabs (Mobile) */
.view-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.view-tab {
    flex: 1;
    padding: 12px;
    border: none;
    background: transparent;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
    border-bottom: 2px solid transparent;
}

.view-tab.active {
    color: var(--accent-color);
    border-bottom-color: var(--accent-color);
}

/* Editor Panels */
.editor-panels {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.editor-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.panel-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

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

/* Text Editor */
.text-editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#markdown-editor {
    flex: 1;
    width: 100%;
    padding: 16px;
    border: none;
    resize: none;
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    font-size: 14px;
    line-height: 1.6;
    outline: none;
    background: var(--bg-primary);
}

.markdown-preview {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    background: var(--bg-primary);
}

.markdown-preview h1, .markdown-preview h2, .markdown-preview h3 {
    margin-top: 24px;
    margin-bottom: 16px;
}

.markdown-preview h1 { font-size: 24px; }
.markdown-preview h2 { font-size: 20px; }
.markdown-preview h3 { font-size: 16px; }

.markdown-preview p {
    margin-bottom: 16px;
}

.markdown-preview a {
    color: var(--link-color);
    text-decoration: none;
}

.markdown-preview a:hover {
    text-decoration: underline;
}

.markdown-preview .note-link {
    color: var(--accent-color);
    background: rgba(252, 109, 38, 0.1);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.markdown-preview .note-link:hover {
    background: rgba(252, 109, 38, 0.2);
}

.markdown-preview .note-link.broken {
    color: var(--danger-color);
    background: rgba(220, 53, 69, 0.1);
}

.markdown-preview code {
    background: var(--bg-secondary);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    font-size: 13px;
}

.markdown-preview pre {
    background: var(--bg-secondary);
    padding: 16px;
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin-bottom: 16px;
}

.markdown-preview pre code {
    background: transparent;
    padding: 0;
}

.markdown-preview ul, .markdown-preview ol {
    margin-bottom: 16px;
    padding-left: 24px;
}

.markdown-preview li {
    margin-bottom: 8px;
}

.markdown-preview blockquote {
    border-left: 4px solid var(--accent-color);
    padding-left: 16px;
    margin: 16px 0;
    color: var(--text-secondary);
}

/* Resize Handle */
.resize-handle {
    width: 4px;
    background: var(--border-color);
    cursor: col-resize;
    flex-shrink: 0;
    transition: background var(--transition-fast);
}

.resize-handle:hover {
    background: var(--accent-color);
}

/* Drawing Panel */
.drawing-tools {
    display: flex;
    align-items: center;
    gap: 4px;
}

.tool-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.tool-btn:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.tool-btn.active {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.tool-separator {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 4px;
}

#stroke-color {
    width: 32px;
    height: 32px;
    padding: 2px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
}

#stroke-width {
    width: 80px;
    cursor: pointer;
}

.canvas-container {
    flex: 1;
    overflow: hidden;
    background: white;
    touch-action: none;
}

#drawing-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Search Results */
.search-results {
    position: absolute;
    top: var(--header-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 200;
    display: flex;
    justify-content: center;
    padding-top: 20px;
}

.search-results-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
}

.search-results-list {
    max-width: 600px;
    width: 90%;
    max-height: 80%;
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.search-result-item {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.search-result-item:hover {
    background: var(--bg-secondary);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-title {
    font-weight: 600;
    margin-bottom: 4px;
}

.search-result-path {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.search-result-snippet {
    font-size: 13px;
    color: var(--text-secondary);
}

.search-result-snippet mark {
    background: rgba(252, 109, 38, 0.3);
    padding: 0 2px;
    border-radius: 2px;
}

/* Dialogs */
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
}

.dialog {
    background: var(--bg-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 24px;
    max-width: 400px;
    width: 90%;
}

.dialog h2 {
    margin-bottom: 16px;
    font-size: 18px;
}

.dialog p {
    margin-bottom: 24px;
    color: var(--text-secondary);
}

.dialog input[type="text"] {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    font-size: 14px;
    margin-bottom: 24px;
    outline: none;
    transition: border-color var(--transition-fast);
}

.dialog input[type="text"]:focus {
    border-color: var(--accent-color);
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Download Dialog */
.download-dialog {
    min-width: 350px;
}

.progress-bar-container {
    width: 100%;
    height: 8px;
    background: var(--bg-secondary);
    border-radius: 4px;
    overflow: hidden;
    margin: 16px 0;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: var(--accent-color);
    border-radius: 4px;
    transition: width 0.3s ease;
}

.download-current-note {
    font-size: 13px;
    color: var(--text-secondary);
    text-align: center;
    min-height: 20px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Sync Dialog */
.sync-dialog {
    text-align: center;
    min-width: 280px;
}

.sync-dialog h2 {
    margin-bottom: 8px;
}

.sync-dialog p {
    margin-bottom: 16px;
    color: var(--text-secondary);
}

.sync-spinner {
    display: flex;
    justify-content: center;
    padding: 8px 0;
}

.sync-spinner .spinner {
    margin-bottom: 0;
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 400;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--bg-tertiary);
    border-top-color: var(--accent-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 500;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 12px 20px;
    background: var(--text-primary);
    color: white;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    animation: slideIn 0.3s ease;
    max-width: 350px;
}

.toast.success {
    background: var(--success-color);
}

.toast.error {
    background: var(--danger-color);
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Fullscreen Panel Mode */
.editor-panels.text-fullscreen #text-panel {
    flex: 1 1 100%;
}

.editor-panels.text-fullscreen #drawing-panel {
    display: none !important;
}

.editor-panels.text-fullscreen #resize-handle {
    display: none !important;
}

.editor-panels.drawing-fullscreen #drawing-panel {
    flex: 1 1 100%;
}

.editor-panels.drawing-fullscreen #text-panel {
    display: none !important;
}

.editor-panels.drawing-fullscreen #resize-handle {
    display: none !important;
}

/* Fullscreen button icon toggle */
.icon-btn .collapse-icon {
    display: none;
}

.icon-btn .expand-icon {
    display: block;
}

.icon-btn.fullscreen-active .collapse-icon {
    display: block;
}

.icon-btn.fullscreen-active .expand-icon {
    display: none;
}

/* Split orientation toggle icons */
.icon-btn .vertical-split-icon {
    display: none;
}

.icon-btn .horizontal-split-icon {
    display: block;
}

.icon-btn.vertical-split .vertical-split-icon {
    display: block;
}

.icon-btn.vertical-split .horizontal-split-icon {
    display: none;
}

/* Vertical Split Mode */
.editor-panels.vertical-split {
    flex-direction: column;
}

.editor-panels.vertical-split #text-panel {
    flex: 1 1 50%;
    min-height: 0;
}

.editor-panels.vertical-split #drawing-panel {
    flex: 1 1 50%;
    min-height: 0;
}

.editor-panels.vertical-split #resize-handle {
    width: 100%;
    height: 6px;
    cursor: row-resize;
}

.editor-panels.vertical-split #resize-handle::after {
    width: 60px;
    height: 4px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Vertical split + fullscreen combinations */
.editor-panels.vertical-split.text-fullscreen #text-panel {
    flex: 1 1 100%;
}

.editor-panels.vertical-split.drawing-fullscreen #drawing-panel {
    flex: 1 1 100%;
}

/* Responsive Layout */
@media (max-width: 768px) {
    .header-center {
        display: none;
    }

    #sidebar {
        position: absolute;
        height: calc(100% - var(--header-height));
        z-index: 100;
        transform: translateX(-100%);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    .editor-panels {
        flex-direction: column;
    }

    .editor-panel {
        display: none;
    }

    .editor-panel.active {
        display: flex;
    }

    .resize-handle {
        display: none;
    }

    .panel-header {
        display: none;
    }

    .drawing-tools {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 8px 16px;
        background: var(--bg-primary);
        border-top: 1px solid var(--border-color);
        justify-content: center;
        z-index: 50;
    }

    #drawing-panel.active .canvas-container {
        height: calc(100% - 60px);
    }
}

@media (min-width: 769px) {
    .view-tabs {
        display: none !important;
    }

    .editor-panel {
        display: flex !important;
    }

    #text-panel {
        flex: 1;
        min-width: 300px;
    }

    #drawing-panel {
        flex: 1;
        min-width: 300px;
    }
}

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

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}
