/* === Theme Variables === */
:root,
[data-theme="light"] {
    --bg-primary: #fafafa;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-hover: #f3f4f6;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border: #e5e7eb;
    --border-light: #f3f4f6;
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --accent-light: #eef2ff;
    --accent-muted: rgba(99, 102, 241, 0.08);
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.06);
    --code-bg: #f1f5f9;
}

[data-theme="dark"] {
    --bg-primary: #0f0f14;
    --bg-secondary: #16161d;
    --bg-card: #1c1c27;
    --bg-hover: #232330;
    --text-primary: #e4e4ed;
    --text-secondary: #8b8ba0;
    --text-muted: #5c5c72;
    --border: #2a2a3a;
    --border-light: #1f1f2c;
    --accent: #818cf8;
    --accent-hover: #a5b4fc;
    --accent-light: rgba(129, 140, 248, 0.1);
    --accent-muted: rgba(129, 140, 248, 0.06);
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.3);
    --code-bg: #1a1a28;
}

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

html {
    scroll-behavior: smooth;
    scroll-padding-top: 2rem;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.65;
    -webkit-font-smoothing: antialiased;
    transition: background 0.2s, color 0.2s;
}

a { color: var(--accent); text-decoration: none; transition: color 0.15s; }
a:hover { color: var(--accent-hover); }

/* === Navbar === */
.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2rem;
    height: 56px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    z-index: 100;
}

.navbar-brand {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
}
.navbar-brand:hover { color: var(--accent); }
.brand-icon { color: var(--accent); }

.navbar-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.user-name {
    color: var(--text-secondary);
    font-size: 0.85rem;
    font-weight: 500;
}

.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: var(--accent-muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.15s;
}
.btn-icon:hover { background: var(--accent-light); color: var(--accent); border-color: var(--accent); }

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

.btn {
    display: inline-flex;
    align-items: center;
    padding: 0.45rem 1rem;
    font-size: 0.82rem;
    font-weight: 500;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: all 0.15s;
}
.btn-sm { padding: 0.3rem 0.75rem; font-size: 0.78rem; }
.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-secondary);
}
.btn-outline:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-muted); }

/* === Container === */
.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem;
}

/* === Login === */
.login-container {
    text-align: center;
    padding: 5rem 1rem;
}
.login-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 80px;
    height: 80px;
    border-radius: 20px;
    background: var(--accent-light);
    color: var(--accent);
    margin-bottom: 1.5rem;
}
.login-container h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    letter-spacing: -0.02em;
}
.login-container p { color: var(--text-secondary); margin-bottom: 2rem; font-size: 0.95rem; }
.tg-login-widget { display: flex; justify-content: center; }
.error { color: #ef4444; margin-top: 1rem; font-size: 0.9rem; }

/* === Notes Grid === */
.notes-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 1.5rem;
}
.notes-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}
.notes-count {
    font-size: 0.8rem;
    color: var(--text-muted);
    background: var(--accent-light);
    padding: 0.2rem 0.6rem;
    border-radius: 10px;
    font-weight: 500;
}

.search-box {
    position: relative;
    margin-bottom: 1.5rem;
}
.search-icon {
    position: absolute;
    left: 0.9rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
}
.search-box input {
    width: 100%;
    padding: 0.7rem 1rem 0.7rem 2.6rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-card);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.search-box input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-muted);
}
.search-box input::placeholder { color: var(--text-muted); }

.notes-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 0.75rem;
}

.note-card {
    display: block;
    padding: 1.1rem 1.2rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    color: var(--text-primary);
    transition: all 0.2s ease;
}
.note-card:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}
.note-card h3 {
    margin-bottom: 0.5rem;
    font-size: 0.92rem;
    font-weight: 600;
}

/* === Tags === */
.tags { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.tag {
    font-size: 0.72rem;
    font-weight: 500;
    padding: 0.12rem 0.5rem;
    background: var(--accent-muted);
    color: var(--accent);
    border-radius: 5px;
}

.badge { font-size: 0.68rem; padding: 0.1rem 0.45rem; border-radius: 5px; font-weight: 500; }
.badge-public { background: rgba(34,197,94,0.1); color: #22c55e; }

/* === Note Content (centered as before) === */
.note-content {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
}

.back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    margin-bottom: 1.2rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 500;
}
.back-link:hover { color: var(--accent); }

.note-header {
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-light);
}
.note-header h1 {
    font-size: 1.75rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    line-height: 1.3;
    margin-bottom: 0.6rem;
}

/* === TOC Floating (desktop only) === */
.toc-float {
    display: none;
    position: fixed;
    left: 2rem;
    top: 80px;
    width: 200px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
    opacity: 0.35;
    transition: opacity 0.3s ease;
    z-index: 50;
}
.toc-float:hover {
    opacity: 1;
}

.toc-float-nav {
    display: flex;
    flex-direction: column;
    border-left: 1.5px solid var(--border);
    padding-left: 0;
    position: relative;
}

.toc-float-link {
    display: block;
    font-size: 0.75rem;
    line-height: 1.4;
    color: var(--text-muted);
    font-weight: 400;
    padding: 0.25rem 0 0.25rem 0.75rem;
    margin-left: -1.5px;
    border-left: 1.5px solid transparent;
    transition: all 0.15s;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}
.toc-float-link:hover {
    color: var(--text-secondary);
}
.toc-float-link.active {
    color: var(--accent);
    border-left-color: var(--accent);
    opacity: 1;
    filter: brightness(1.3);
}

/* Indentation by heading level */
.toc-fl-2 { padding-left: 0.75rem; }
.toc-fl-3 { padding-left: 1.5rem; }
.toc-fl-4 { padding-left: 2.25rem; }

.toc-float::-webkit-scrollbar { width: 2px; }
.toc-float::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* === TOC Mobile (inline, collapsible) === */
.toc-mobile {
    margin-bottom: 1.5rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
}

.toc-mobile-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    list-style: none;
    background: var(--bg-card);
}
.toc-mobile-toggle:hover { background: var(--bg-hover); }
.toc-mobile-toggle::-webkit-details-marker { display: none; }
.toc-mobile-toggle svg { color: var(--text-muted); }

.toc-mobile-nav {
    display: flex;
    flex-direction: column;
    padding: 0.5rem 0;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
}
.toc-mobile-nav .toc-link {
    display: block;
    font-size: 0.82rem;
    color: var(--text-muted);
    font-weight: 400;
    padding: 0.3rem 1rem;
    transition: color 0.15s;
}
.toc-mobile-nav .toc-link:hover { color: var(--text-primary); }
.toc-mobile-nav .toc-link.active {
    color: var(--accent);
    font-weight: 500;
    background: var(--accent-muted);
    border-radius: 6px;
}
.toc-mobile-nav .toc-level-2 { padding-left: 1rem; }
.toc-mobile-nav .toc-level-3 { padding-left: 1.75rem; }
.toc-mobile-nav .toc-level-4 { padding-left: 2.5rem; }

/* === Scroll to TOC button (mobile) === */
.scroll-top-btn {
    display: none;
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-muted);
    cursor: pointer;
    opacity: 0;
    transform: translateY(10px);
    transition: opacity 0.3s, transform 0.3s, color 0.15s;
    z-index: 90;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
}
.scroll-top-btn.visible {
    opacity: 0.5;
    transform: translateY(0);
}
.scroll-top-btn:hover {
    opacity: 1;
    color: var(--accent);
    border-color: var(--accent);
}
@media (max-width: 1199px) {
    .scroll-top-btn { display: flex; }
}

/* Show floating TOC on wide screens, hide mobile TOC */
@media (min-width: 1200px) {
    .toc-float { display: block; }
    .toc-mobile { display: none; }
}

/* === Note Body === */
.note-body {
    font-size: 0.95rem;
    line-height: 1.8;
}
.note-body h2 {
    font-size: 1.35rem;
    font-weight: 700;
    margin-top: 2.5em;
    margin-bottom: 0.6em;
    letter-spacing: -0.02em;
}
.note-body h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-top: 2em;
    margin-bottom: 0.5em;
}
.note-body h4 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-top: 1.5em;
    margin-bottom: 0.4em;
}
.note-body p { margin-bottom: 1.1em; }
.note-body ul, .note-body ol {
    margin-bottom: 1.1em;
    padding-left: 1.5em;
}
.note-body li { margin-bottom: 0.3em; }
.note-body img {
    max-width: 100%;
    border-radius: 10px;
    margin: 1.2rem 0;
}
.note-body code {
    background: var(--code-bg);
    padding: 0.15em 0.4em;
    border-radius: 5px;
    font-size: 0.85em;
    font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
}
.note-body pre {
    background: var(--code-bg);
    padding: 1.1rem 1.2rem;
    border-radius: 10px;
    overflow-x: auto;
    margin-bottom: 1.2em;
    border: 1px solid var(--border-light);
}
.note-body pre code {
    background: none;
    padding: 0;
    font-size: 0.83rem;
    line-height: 1.6;
}
.note-body blockquote {
    border-left: 3px solid var(--accent);
    padding: 0.5rem 1rem;
    color: var(--text-secondary);
    margin-bottom: 1.1em;
    background: var(--accent-muted);
    border-radius: 0 8px 8px 0;
}
.note-body table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1.2em;
    font-size: 0.9rem;
}
.note-body th, .note-body td {
    border: 1px solid var(--border);
    padding: 0.55rem 0.85rem;
    text-align: left;
}
.note-body th {
    background: var(--bg-secondary);
    font-weight: 600;
    font-size: 0.85rem;
}
.note-body hr {
    border: none;
    height: 1px;
    background: var(--border);
    margin: 2rem 0;
}

/* === Callouts === */
.callout {
    border-radius: 10px;
    padding: 1rem 1.1rem;
    margin-bottom: 1.2em;
    border-left: 3px solid;
}
.callout-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.88rem;
    margin-bottom: 0.35rem;
}
.callout-title svg { flex-shrink: 0; }
.callout-body {
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

.callout-info { border-color: #6366f1; background: rgba(99,102,241,0.06); }
.callout-info .callout-title { color: #6366f1; }
.callout-tip { border-color: #22c55e; background: rgba(34,197,94,0.06); }
.callout-tip .callout-title { color: #22c55e; }
.callout-warning { border-color: #f59e0b; background: rgba(245,158,11,0.06); }
.callout-warning .callout-title { color: #f59e0b; }
.callout-danger { border-color: #ef4444; background: rgba(239,68,68,0.06); }
.callout-danger .callout-title { color: #ef4444; }
.callout-note { border-color: #8b5cf6; background: rgba(139,92,246,0.06); }
.callout-note .callout-title { color: #8b5cf6; }
.callout-example { border-color: #6366f1; background: rgba(99,102,241,0.06); }
.callout-example .callout-title { color: #6366f1; }
.callout-quote { border-color: #94a3b8; background: rgba(148,163,184,0.06); }
.callout-quote .callout-title { color: #94a3b8; }
.callout-success { border-color: #22c55e; background: rgba(34,197,94,0.06); }
.callout-success .callout-title { color: #22c55e; }
.callout-question { border-color: #f59e0b; background: rgba(245,158,11,0.06); }
.callout-question .callout-title { color: #f59e0b; }
.callout-bug { border-color: #ef4444; background: rgba(239,68,68,0.06); }
.callout-bug .callout-title { color: #ef4444; }

/* === Backlinks === */
.backlinks {
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border);
}
.backlinks-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.9rem;
    margin-bottom: 0.8rem;
    color: var(--text-secondary);
}
.backlinks ul { list-style: none; padding: 0; }
.backlinks li { margin-bottom: 0.35rem; }
.backlinks a { font-size: 0.9rem; }

/* === Error page === */
.error-page {
    text-align: center;
    padding: 5rem 1rem;
}
.error-page h1 { font-size: 5rem; font-weight: 700; color: var(--text-muted); }
.error-page p { margin: 1rem 0 2rem; color: var(--text-secondary); }

/* === Empty state === */
.empty-state {
    grid-column: 1 / -1;
    text-align: center;
    padding: 3rem;
    color: var(--text-muted);
}
.empty-state svg { margin-bottom: 1rem; }
.empty-state p { font-size: 0.9rem; }

/* === Responsive === */
@media (max-width: 768px) {
    .navbar { padding: 0 1rem; height: 50px; }
    .container { padding: 1.25rem; }
    .note-content { padding: 1.25rem; }
    .notes-grid { grid-template-columns: 1fr; }
    .notes-header h1 { font-size: 1.25rem; }
    .note-header h1 { font-size: 1.35rem; }
}
