/* ========================================
   CSS Variables & Design Tokens
   ======================================== */
:root {
    /* Light Mode Colors (Default) */
    --bg-primary: #f8f9fa;
    --bg-secondary: #ffffff;
    --bg-sidebar: #ffffff;
    --bg-card: #ffffff;
    --bg-card-hover: #f3f4f6;
    --bg-input: #f3f4f6;

    --text-primary: #1a1a2e;
    --text-secondary: #4a4a5a;
    --text-muted: #8a8a9a;

    --accent-primary: #6366f1;
    --accent-primary-hover: #4f46e5;
    --accent-glow: rgba(99, 102, 241, 0.2);

    --border-color: rgba(0, 0, 0, 0.08);
    --border-focus: rgba(99, 102, 241, 0.5);

    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #22c55e;

    /* Spacing */
    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.5rem;
    --space-xl: 2rem;
    --space-2xl: 3rem;

    /* Layout */
    --sidebar-width: 320px;
    --sidebar-collapsed-width: 0px;

    /* Border Radius */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;

    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
    --transition-slow: 350ms ease;
}

/* Dark Mode */
[data-theme="dark"] {
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-sidebar: #0f0f15;
    --bg-card: rgba(25, 25, 35, 0.8);
    --bg-card-hover: rgba(35, 35, 50, 0.9);
    --bg-input: #1a1a25;

    --text-primary: #f0f0f5;
    --text-secondary: #a0a0b0;
    --text-muted: #606070;

    --border-color: rgba(255, 255, 255, 0.08);

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
}

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

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
    transition: background var(--transition-normal), color var(--transition-normal);
}

/* ========================================
   App Layout
   ======================================== */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ========================================
   Sidebar
   ======================================== */
.sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: transform var(--transition-normal), width var(--transition-normal);
    z-index: 100;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-md) var(--space-lg);
    border-bottom: 1px solid var(--border-color);
}

.logo {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.logo-icon {
    width: 24px;
    height: 24px;
    color: var(--accent-primary);
}

.logo h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
}

.sidebar-toggle {
    display: none;
}

.btn-new-note {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    margin: var(--space-md);
    padding: var(--space-sm) var(--space-md);
    background: var(--accent-primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-normal);
}

.btn-new-note:hover {
    background: var(--accent-primary-hover);
    box-shadow: var(--shadow-md);
}

.btn-new-note svg {
    width: 18px;
    height: 18px;
}

.search-wrapper {
    position: relative;
    margin: 0 var(--space-md) var(--space-md);
}

.search-icon {
    position: absolute;
    left: var(--space-sm);
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--text-muted);
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: var(--space-sm) var(--space-sm) var(--space-sm) calc(var(--space-md) + 16px);
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 0.875rem;
    transition: all var(--transition-normal);
}

.search-input:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.search-input::placeholder {
    color: var(--text-muted);
}

/* Notes List */
.notes-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 var(--space-sm);
}

.note-item {
    display: flex;
    flex-direction: column;
    padding: var(--space-md);
    margin-bottom: var(--space-xs);
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    text-align: left;
    width: 100%;
    transition: all var(--transition-fast);
}

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

.note-item.active {
    background: var(--accent-glow);
    border-left: 3px solid var(--accent-primary);
}

.note-item-title {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: var(--space-xs);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-item-preview {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-item-date {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: var(--space-xs);
}

.sidebar-footer {
    padding: var(--space-md);
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: center;
}

/* ========================================
   Button Styles
   ======================================== */
.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: transparent;
    border: none;
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

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

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

.btn-icon.btn-delete:hover {
    color: var(--danger);
    background: rgba(239, 68, 68, 0.1);
}

/* Theme Toggle Icons */
.icon-moon {
    display: none;
}

[data-theme="dark"] .icon-sun {
    display: none;
}

[data-theme="dark"] .icon-moon {
    display: block;
}

/* Markdown Toggle Icons */
.icon-preview {
    display: none;
}

.markdown-active .icon-edit {
    display: none;
}

.markdown-active .icon-preview {
    display: block;
}

/* ========================================
   Main Content
   ======================================== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    overflow: hidden;
}

.mobile-header {
    display: none;
}

/* Empty State */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    text-align: center;
    color: var(--text-muted);
}

.empty-state svg {
    width: 80px;
    height: 80px;
    margin-bottom: var(--space-lg);
    opacity: 0.4;
}

.empty-state h3 {
    font-size: 1.25rem;
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
}

/* ========================================
   Note Editor
   ======================================== */
.note-editor {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.editor-header {
    display: flex;
    align-items: center;
    padding: var(--space-lg);
    gap: var(--space-md);
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.note-title-input {
    flex: 1;
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
    background: transparent;
    border: none;
    outline: none;
    padding: 0;
}

.note-title-input::placeholder {
    color: var(--text-muted);
}

.editor-actions {
    display: flex;
    gap: var(--space-xs);
}

.editor-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-sm) var(--space-lg);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.note-date {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.save-status {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-style: italic;
}

.save-status.saving {
    color: var(--accent-primary);
}

.save-status.saved {
    color: var(--success);
}

.save-status.error {
    color: var(--danger);
}

/* Character Counter */
.char-counter {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    margin-left: auto;
    padding-left: var(--space-sm);
    transition: color var(--transition-fast);
}

.char-counter.warning {
    color: #f59e0b;
    font-weight: 500;
}

.char-counter.danger {
    color: var(--danger);
    font-weight: 600;
    animation: pulse-danger 1.5s ease-in-out infinite;
}

@keyframes pulse-danger {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.6;
    }
}

.editor-body {
    flex: 1;
    display: flex;
    overflow: hidden;
    background: var(--bg-secondary);
}

.note-content-input {
    flex: 1;
    padding: var(--space-lg);
    font-size: 1rem;
    font-family: 'Inter', sans-serif;
    line-height: 1.8;
    color: var(--text-primary);
    background: transparent;
    border: none;
    outline: none;
    resize: none;
    overflow-y: auto;
}

.note-content-input::placeholder {
    color: var(--text-muted);
}

.markdown-preview {
    flex: 1;
    padding: var(--space-lg);
    overflow-y: auto;
    line-height: 1.8;
}

.markdown-preview h1,
.markdown-preview h2,
.markdown-preview h3,
.markdown-preview h4,
.markdown-preview h5,
.markdown-preview h6 {
    margin-top: 1.5em;
    margin-bottom: 0.5em;
    font-weight: 600;
    line-height: 1.3;
}

.markdown-preview h1 {
    font-size: 2em;
}

.markdown-preview h2 {
    font-size: 1.5em;
}

.markdown-preview h3 {
    font-size: 1.25em;
}

.markdown-preview p {
    margin-bottom: 1em;
}

.markdown-preview code {
    background: var(--bg-input);
    padding: 0.2em 0.4em;
    border-radius: var(--radius-sm);
    font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
    font-size: 0.9em;
}

.markdown-preview pre {
    background: var(--bg-input);
    padding: var(--space-md);
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin-bottom: 1em;
}

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

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

.markdown-preview li {
    margin-bottom: 0.25em;
}

.markdown-preview blockquote {
    border-left: 3px solid var(--accent-primary);
    padding-left: var(--space-md);
    margin: 1em 0;
    color: var(--text-secondary);
    font-style: italic;
}

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

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

.markdown-preview hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 2em 0;
}

/* ========================================
   Loading State
   ======================================== */
.loading-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
}

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

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

/* ========================================
   Modal
   ======================================== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-md);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition-normal);
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 400px;
    transform: scale(0.95);
    transition: transform var(--transition-normal);
}

.modal-overlay.active .modal {
    transform: scale(1);
}

.modal-header {
    padding: var(--space-lg);
    border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.modal-body {
    padding: var(--space-lg);
    color: var(--text-secondary);
}

.modal-actions {
    display: flex;
    gap: var(--space-md);
    padding: var(--space-lg);
    padding-top: 0;
}

.btn-secondary,
.btn-danger {
    flex: 1;
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
    border: none;
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-secondary);
}

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

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

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

/* ========================================
   Toast Notification
   ======================================== */
.toast {
    position: fixed;
    bottom: var(--space-xl);
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: var(--space-md) var(--space-lg);
    box-shadow: var(--shadow-lg);
    z-index: 2000;
    opacity: 0;
    transition: all var(--transition-normal);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

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

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

.toast-message {
    font-size: 0.9rem;
    font-weight: 500;
}

/* ========================================
   Floating Action Button
   ======================================== */
.fab {
    display: none;
}

/* ========================================
   Attachments Section
   ======================================== */
.attachments-section {
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    padding: var(--space-md) var(--space-lg);
    max-height: 200px;
    overflow-y: auto;
}

.attachments-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-sm);
}

.attachments-header h4 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-add-attachment {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.btn-add-attachment:hover {
    background: var(--accent-glow);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
}

.btn-add-attachment svg {
    width: 14px;
    height: 14px;
}

.attachments-list {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-sm);
}

.no-attachments {
    color: var(--text-muted);
    font-size: 0.85rem;
    font-style: italic;
}

.attachment-item {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    max-width: 200px;
    transition: all var(--transition-fast);
}

.attachment-item:hover {
    border-color: var(--accent-primary);
    box-shadow: var(--shadow-sm);
}

.attachment-link {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    text-decoration: none;
    color: var(--text-primary);
    overflow: hidden;
}

.attachment-thumbnail {
    width: 32px;
    height: 32px;
    object-fit: cover;
    border-radius: var(--radius-sm);
}

.attachment-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-glow);
    border-radius: var(--radius-sm);
    color: var(--accent-primary);
}

.attachment-icon svg {
    width: 18px;
    height: 18px;
}

.attachment-name {
    font-size: 0.8rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
}

.attachment-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    transition: all var(--transition-fast);
    flex-shrink: 0;
}

.attachment-remove:hover {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
}

.attachment-remove svg {
    width: 14px;
    height: 14px;
}

/* ========================================
   Sidebar Overlay
   ======================================== */
.sidebar-overlay {
    display: none;
}

/* ========================================
   Responsive Styles
   ======================================== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        box-shadow: var(--shadow-lg);
    }

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

    .sidebar-overlay {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 99;
        opacity: 0;
        visibility: hidden;
        transition: all var(--transition-normal);
    }

    .sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }

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

    .mobile-title {
        font-weight: 600;
        color: var(--text-primary);
    }

    .main-content {
        padding-bottom: 80px;
    }

    .fab {
        display: flex;
        position: fixed;
        bottom: var(--space-xl);
        right: var(--space-lg);
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: var(--accent-primary);
        color: white;
        border: none;
        cursor: pointer;
        align-items: center;
        justify-content: center;
        box-shadow: var(--shadow-lg);
        z-index: 50;
        transition: all var(--transition-normal);
    }

    .fab:active {
        transform: scale(0.95);
    }

    .fab svg {
        width: 24px;
        height: 24px;
    }

    .editor-header {
        padding: var(--space-md);
    }

    .note-title-input {
        font-size: 1.25rem;
    }

    .note-content-input,
    .markdown-preview {
        padding: var(--space-md);
        font-size: 16px;
    }

    .toast {
        bottom: 100px;
    }
}

/* Safe Area Support */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
        .main-content {
            padding-bottom: calc(80px + env(safe-area-inset-bottom));
        }

        .fab {
            bottom: calc(var(--space-xl) + env(safe-area-inset-bottom));
        }

        .toast {
            bottom: calc(100px + env(safe-area-inset-bottom));
        }
    }
}