/* ========================================
   NoteKeeper - CSS Variables & Reset
   ======================================== */

:root {
    /* Colors - Dark theme inspired by Nord but warmer */
    --bg-primary: #0f0f0f;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #242424;
    --bg-hover: #2a2a2a;
    --bg-active: #333333;
    
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0a0;
    --text-muted: #666666;
    
    --accent: #e8a654;
    --accent-hover: #f0b86a;
    --accent-muted: rgba(232, 166, 84, 0.15);
    
    --success: #7ec699;
    --warning: #e8c547;
    --danger: #e85454;
    
    --border: #2a2a2a;
    --border-light: #333333;
    
    /* Typography */
    --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'IBM Plex Mono', 'SF Mono', Consolas, monospace;
    
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.25rem;
    --text-2xl: 1.5rem;
    --text-3xl: 2rem;
    
    /* Spacing */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-8: 2rem;
    --space-10: 2.5rem;
    --space-12: 3rem;
    
    /* Sizing */
    --sidebar-width: 280px;
    --header-height: 56px;
    
    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px 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);
    
    /* Border radius */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-full: 9999px;
}

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

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-primary);
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
}

/* ========================================
   Utility Classes
   ======================================== */

.hidden { display: none !important; }
.mobile-only { display: none; }

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

/* ========================================
   Screen Container
   ======================================== */

.screen {
    height: 100vh;
    height: 100dvh;
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

/* ========================================
   Login Screen
   ======================================== */

#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-primary);
    background-image: 
        radial-gradient(ellipse at 20% 50%, rgba(232, 166, 84, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 80%, rgba(232, 166, 84, 0.05) 0%, transparent 50%);
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: var(--space-6);
}

.login-brand {
    text-align: center;
    margin-bottom: var(--space-8);
}

.login-brand .logo {
    font-size: 4rem;
    display: block;
    margin-bottom: var(--space-4);
}

.login-brand h1 {
    font-size: var(--text-3xl);
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--space-2);
}

.login-brand p {
    color: var(--text-secondary);
}

.login-form {
    background: var(--bg-secondary);
    padding: var(--space-6);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
}

.form-group {
    margin-bottom: var(--space-5);
}

.form-group label {
    display: block;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: var(--space-2);
}

.form-group input {
    width: 100%;
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-base);
    font-family: var(--font-sans);
    color: var(--text-primary);
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

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

.error-message {
    color: var(--danger);
    font-size: var(--text-sm);
    margin-top: var(--space-4);
    text-align: left;
    background: rgba(232, 84, 84, 0.1);
    padding: var(--space-3);
    border-radius: var(--radius-sm);
    display: none;
}

.error-message:not(:empty) {
    display: block;
}

.error-message code {
    background: var(--bg-primary);
    padding: 2px 4px;
    border-radius: 2px;
    display: block;
    margin-top: var(--space-2);
    max-height: 150px;
    overflow: auto;
}

/* ========================================
   Buttons
   ======================================== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-5);
    font-size: var(--text-sm);
    font-weight: 500;
    font-family: var(--font-sans);
    border-radius: var(--radius-md);
    border: none;
    cursor: pointer;
    transition: all var(--transition-fast);
    text-decoration: none;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

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

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

.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--bg-hover);
    border-color: var(--border-light);
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-danger:hover:not(:disabled) {
    color: var(--danger);
}

.btn-block {
    width: 100%;
}

.icon-btn {
    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);
}

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

/* ========================================
   App Layout
   ======================================== */

#app-screen {
    display: flex;
    flex-direction: column;
}

/* Mobile Header */
.mobile-header {
    display: none;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 var(--space-4);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
}

.mobile-header h1 {
    font-size: var(--text-lg);
    font-weight: 600;
}

@media (max-width: 768px) {
    .mobile-header {
        display: flex;
    }
}

/* ========================================
   Sidebar
   ======================================== */

.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100%;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 200;
}

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

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

.brand .logo {
    font-size: 1.75rem;
}

.brand-name {
    font-size: var(--text-lg);
    font-weight: 600;
}

.sidebar-actions {
    padding: var(--space-4) var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.sidebar-search {
    padding: 0 var(--space-4) var(--space-4);
    position: relative;
}

.sidebar-search svg {
    position: absolute;
    left: calc(var(--space-4) + var(--space-3));
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
}

.sidebar-search input {
    width: 100%;
    padding: var(--space-2) var(--space-3) var(--space-2) calc(var(--space-3) + 24px);
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    color: var(--text-primary);
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    transition: all var(--transition-fast);
}

.sidebar-search input:focus {
    outline: none;
    border-color: var(--accent);
    background: var(--bg-primary);
}

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

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-2) 0;
}

.nav-section {
    padding: var(--space-2) var(--space-4);
}

.nav-section h3 {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: var(--space-2);
    padding: 0 var(--space-2);
}

.notes-list,
.tags-list {
    list-style: none;
}

.notes-list li,
.tags-list li {
    margin-bottom: 1px;
}

.notes-list a,
.tags-list a {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.notes-list a:hover,
.tags-list a:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.notes-list a.active,
.tags-list a.active {
    background: var(--accent-muted);
    color: var(--accent);
}

.notes-list .note-date {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-left: auto;
}

.tags-list .tag-count {
    font-size: var(--text-xs);
    color: var(--text-muted);
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: var(--radius-full);
    margin-left: auto;
}

.sidebar-footer {
    padding: var(--space-4);
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.sync-status {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-xs);
    color: var(--text-muted);
}

.sync-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
}

.sync-status.syncing .sync-indicator {
    background: var(--warning);
    animation: pulse 1s infinite;
}

.sync-status.offline .sync-indicator {
    background: var(--text-muted);
}

.sync-status.error .sync-indicator {
    background: var(--danger);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Mobile sidebar */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform var(--transition-normal);
    }
    
    .sidebar.open {
        transform: translateX(0);
    }
    
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 150;
        opacity: 0;
        visibility: hidden;
        transition: all var(--transition-normal);
    }
    
    .sidebar-overlay.visible {
        opacity: 1;
        visibility: visible;
    }
}

/* ========================================
   Main Content
   ======================================== */

.main-content {
    margin-left: var(--sidebar-width);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

@media (max-width: 768px) {
    .main-content {
        margin-left: 0;
        margin-top: var(--header-height);
        height: calc(100vh - var(--header-height));
        height: calc(100dvh - var(--header-height));
    }
}

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

.empty-icon {
    font-size: 5rem;
    margin-bottom: var(--space-6);
    opacity: 0.5;
}

.empty-state h2 {
    font-size: var(--text-2xl);
    font-weight: 600;
    margin-bottom: var(--space-3);
}

.empty-state p {
    color: var(--text-secondary);
    margin-bottom: var(--space-6);
    max-width: 400px;
}

/* ========================================
   Editor
   ======================================== */

.editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

.note-title-input {
    flex: 1;
    font-size: var(--text-xl);
    font-weight: 600;
    font-family: var(--font-sans);
    color: var(--text-primary);
    background: transparent;
    border: none;
    outline: none;
}

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

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

.editor-body {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Edit Mode - CodeMirror */
.edit-mode {
    flex: 1;
    overflow: hidden;
}

.edit-mode .CodeMirror {
    height: 100%;
    font-family: var(--font-mono);
    font-size: var(--text-base);
    line-height: 1.7;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.edit-mode .CodeMirror-gutters {
    background: var(--bg-primary);
    border-right: 1px solid var(--border);
}

.edit-mode .CodeMirror-linenumber {
    color: var(--text-muted);
}

.edit-mode .CodeMirror-cursor {
    border-left: 2px solid var(--accent);
}

.edit-mode .CodeMirror-selected {
    background: var(--accent-muted) !important;
}

.edit-mode .CodeMirror-focused .CodeMirror-selected {
    background: var(--accent-muted) !important;
}

/* Markdown syntax highlighting */
.cm-header { color: var(--accent); font-weight: 600; }
.cm-header-1 { font-size: 1.5em; }
.cm-header-2 { font-size: 1.3em; }
.cm-header-3 { font-size: 1.1em; }
.cm-link { color: var(--accent); }
.cm-url { color: var(--text-muted); }
.cm-strong { font-weight: 600; }
.cm-em { font-style: italic; }
.cm-comment { color: var(--text-muted); }
.cm-quote { color: var(--text-secondary); font-style: italic; }
.cm-tag { color: var(--success); }

/* View Mode */
.view-mode {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-6) var(--space-8);
}

.rendered-content {
    max-width: 720px;
    margin: 0 auto;
}

.rendered-content h1,
.rendered-content h2,
.rendered-content h3,
.rendered-content h4,
.rendered-content h5,
.rendered-content h6 {
    color: var(--text-primary);
    font-weight: 600;
    line-height: 1.3;
    margin-top: 1.5em;
    margin-bottom: 0.5em;
}

.rendered-content h1 { font-size: var(--text-3xl); }
.rendered-content h2 { font-size: var(--text-2xl); }
.rendered-content h3 { font-size: var(--text-xl); }

.rendered-content p {
    margin-bottom: 1em;
}

.rendered-content a {
    color: var(--accent);
    text-decoration: none;
}

.rendered-content a:hover {
    text-decoration: underline;
}

.rendered-content a.wiki-link {
    background: var(--accent-muted);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
}

.rendered-content a.wiki-link.broken {
    color: var(--text-muted);
    background: var(--bg-tertiary);
}

.rendered-content code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
}

.rendered-content pre {
    background: var(--bg-tertiary);
    padding: var(--space-4);
    border-radius: var(--radius-md);
    overflow-x: auto;
    margin-bottom: 1em;
}

.rendered-content pre code {
    background: none;
    padding: 0;
}

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

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

.rendered-content li {
    margin-bottom: 0.25em;
}

.rendered-content img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    margin: 1em 0;
}

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

.rendered-content table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1em;
}

.rendered-content th,
.rendered-content td {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    text-align: left;
}

.rendered-content th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

/* Task checkboxes */
.rendered-content input[type="checkbox"] {
    margin-right: var(--space-2);
    accent-color: var(--accent);
}

/* Tags in rendered content */
.rendered-content .tag {
    display: inline-block;
    font-size: var(--text-sm);
    color: var(--success);
    background: rgba(126, 198, 153, 0.15);
    padding: 2px 8px;
    border-radius: var(--radius-full);
    text-decoration: none;
}

/* ========================================
   Backlinks Panel
   ======================================== */

.backlinks-panel {
    padding: var(--space-4) var(--space-6);
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
}

.backlinks-panel h3 {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: var(--space-3);
}

.backlinks-list {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.backlinks-list:empty::after {
    content: 'No backlinks yet';
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.backlinks-list a {
    display: inline-flex;
    align-items: center;
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-sm);
    color: var(--text-secondary);
    background: var(--bg-tertiary);
    border-radius: var(--radius-sm);
    text-decoration: none;
    transition: all var(--transition-fast);
}

.backlinks-list a:hover {
    background: var(--bg-hover);
    color: var(--accent);
}

/* ========================================
   Toast Notifications
   ======================================== */

.toast-container {
    position: fixed;
    bottom: var(--space-6);
    right: var(--space-6);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

@media (max-width: 768px) {
    .toast-container {
        left: var(--space-4);
        right: var(--space-4);
        bottom: var(--space-4);
    }
}

.toast {
    padding: var(--space-3) var(--space-4);
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    animation: slideIn 0.3s ease;
}

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

@keyframes slideIn {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* ========================================
   Scrollbar Styling
   ======================================== */

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

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

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

::-webkit-scrollbar-thumb:hover {
    background: var(--border-light);
}

/* ========================================
   Mobile Adjustments
   ======================================== */

@media (max-width: 768px) {
    .editor-header {
        padding: var(--space-3) var(--space-4);
    }
    
    .note-title-input {
        font-size: var(--text-lg);
    }
    
    .view-mode {
        padding: var(--space-4);
    }
    
    .backlinks-panel {
        padding: var(--space-3) var(--space-4);
    }
}

/* ========================================
   Print Styles
   ======================================== */

@media print {
    .sidebar,
    .mobile-header,
    .editor-header,
    .backlinks-panel {
        display: none !important;
    }
    
    .main-content {
        margin: 0;
    }
    
    .rendered-content {
        max-width: none;
    }
}
