/* ============================================================
   CSS Variables (Cloudflare Developers inspired)
============================================================ */
:root {
    --sidebar-width: 260px;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #eff6ff;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --border: #e5e7eb;
    --border-light: #f3f4f6;
    --bg-white: #ffffff;
    --bg-sidebar: #f9fafb;
    --bg-code: #f6f8fa;
    --accent-orange: #f97316;
    --sidebar-active-bg: #eff6ff;
    --sidebar-active-color: #2563eb;
    --toc-width: 220px;
    --font-mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    --radius: 6px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.12);
}

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

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans KR', sans-serif;
    color: var(--text-primary);
    background: var(--bg-white);
    line-height: 1.6;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ============================================================
   Layout
============================================================ */
.layout {
    display: flex;
    min-height: 100vh;
}

/* ============================================================
   Sidebar
============================================================ */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    overflow-y: auto;
    z-index: 100;
    display: flex;
    flex-direction: column;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.sidebar-header {
    padding: 20px 16px 16px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg-sidebar);
    z-index: 1;
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text-primary);
    font-weight: 700;
    font-size: 15px;
    text-decoration: none;
}

.sidebar-logo:hover { text-decoration: none; opacity: 0.8; }
.sidebar-logo-icon { font-size: 20px; }
.sidebar-logo-text { letter-spacing: -0.3px; }

/* Sidebar Search */
.sidebar-search-wrap {
    position: relative;
    margin-top: 12px;
}

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

.sidebar-search {
    width: 100%;
    padding: 7px 28px 7px 30px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: inherit;
    background: var(--bg-white);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.15s;
}

.sidebar-search:focus { border-color: var(--primary); }
.sidebar-search::placeholder { color: var(--text-muted); }

.sidebar-search-clear {
    position: absolute;
    right: 7px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 16px;
    cursor: pointer;
    padding: 0 2px;
    line-height: 1;
    display: none;
}

.sidebar-search-clear.visible { display: block; }
.sidebar-search-clear:hover { color: var(--text-secondary); }

.sidebar-no-results {
    padding: 12px 16px;
    font-size: 13px;
    color: var(--text-muted);
    display: none;
}

.sidebar-no-results.visible { display: block; }

.sidebar-nav { padding: 12px 0 80px; }

.sidebar-section { margin-bottom: 4px; }

.sidebar-section-header {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    border-radius: 0;
    transition: color 0.15s;
}

.sidebar-section-header:hover { color: var(--primary); }

.sidebar-section-header .chevron {
    width: 14px;
    height: 14px;
    transition: transform 0.2s;
    flex-shrink: 0;
}

.sidebar-section-header.open .chevron { transform: rotate(180deg); }

.sidebar-items {
    list-style: none;
    display: none;
    padding: 2px 0;
}

.sidebar-items.open { display: block; }

.sidebar-link {
    display: block;
    padding: 6px 16px 6px 28px;
    font-size: 13.5px;
    color: var(--text-primary);
    border-radius: 0;
    transition: background 0.12s, color 0.12s;
    border-left: 3px solid transparent;
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-link:hover {
    background: var(--border-light);
    color: var(--primary);
    text-decoration: none;
}

.sidebar-link.active {
    background: var(--sidebar-active-bg);
    color: var(--sidebar-active-color);
    border-left-color: var(--primary);
    font-weight: 600;
}

/* ============================================================
   Main Content
============================================================ */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    min-width: 0;
}

.content-wrapper {
    max-width: 900px;
    padding: 32px 40px 80px;
    margin: 0 auto;
}

/* ============================================================
   Breadcrumb
============================================================ */
.breadcrumb {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 20px;
}

.breadcrumb-home { color: var(--primary); }
.breadcrumb-sep { color: var(--text-muted); }
.breadcrumb-section { text-transform: capitalize; }
.breadcrumb-current { color: var(--text-secondary); }

/* ============================================================
   Document Header
============================================================ */
.doc-header {
    margin-bottom: 32px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--border);
}

.doc-title {
    font-size: 28px;
    font-weight: 700;
    line-height: 1.3;
    color: var(--text-primary);
    letter-spacing: -0.5px;
    margin-bottom: 8px;
}

.doc-description {
    font-size: 15px;
    color: var(--text-secondary);
    margin-bottom: 12px;
    line-height: 1.5;
}

.doc-meta {
    font-size: 12px;
    color: var(--text-muted);
}

.doc-meta-label { margin-right: 4px; }

/* ============================================================
   Document Layout (body + TOC)
============================================================ */
.doc-layout {
    display: flex;
    gap: 40px;
    align-items: flex-start;
}

.doc-body { flex: 1; min-width: 0; }

/* ============================================================
   Document Content (Markdown rendered)
============================================================ */
.doc-content h1,
.doc-content h2,
.doc-content h3,
.doc-content h4 {
    font-weight: 700;
    line-height: 1.35;
    margin-top: 32px;
    margin-bottom: 12px;
    color: var(--text-primary);
    letter-spacing: -0.3px;
}

.doc-content h1 { font-size: 24px; margin-top: 0; }
.doc-content h2 {
    font-size: 20px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}
.doc-content h3 { font-size: 17px; }
.doc-content h4 { font-size: 15px; }

.doc-content p {
    margin-bottom: 16px;
    line-height: 1.7;
    color: var(--text-primary);
}

.doc-content ul,
.doc-content ol {
    margin: 0 0 16px 24px;
    line-height: 1.7;
}

.doc-content li { margin-bottom: 4px; }
.doc-content li > ul,
.doc-content li > ol { margin-top: 4px; margin-bottom: 4px; }

.doc-content strong { font-weight: 600; color: var(--text-primary); }
.doc-content em { font-style: italic; }

.doc-content hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 28px 0;
}

/* Code blocks */
.doc-content pre {
    background: var(--bg-code);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    overflow-x: auto;
    margin-bottom: 16px;
    position: relative;
    font-size: 13.5px;
    line-height: 1.6;
}

.doc-content code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    background: var(--bg-code);
    padding: 2px 5px;
    border-radius: 4px;
    border: 1px solid var(--border);
    color: var(--text-primary);
}

.doc-content pre code {
    background: none;
    padding: 0;
    border: none;
    font-size: inherit;
    color: inherit;
}

/* Copy button */
.copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 4px 10px;
    font-size: 11px;
    font-family: var(--font-mono);
    background: var(--bg-white);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s;
    opacity: 0;
}

.doc-content pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--primary); color: white; border-color: var(--primary); }
.copy-btn.copied { background: #10b981; color: white; border-color: #10b981; }

/* Tables */
.doc-content table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 16px;
    font-size: 14px;
    overflow: hidden;
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.doc-content th,
.doc-content td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.doc-content th {
    background: var(--border-light);
    font-weight: 600;
    font-size: 13px;
    color: var(--text-primary);
}

.doc-content tr:last-child td { border-bottom: none; }
.doc-content tr:nth-child(even) td { background: #fafafa; }

/* Blockquote */
.doc-content blockquote {
    border-left: 4px solid var(--primary);
    padding: 12px 16px;
    margin: 0 0 16px;
    background: var(--primary-light);
    border-radius: 0 var(--radius) var(--radius) 0;
    color: var(--text-secondary);
    font-style: italic;
}

/* Checkbox list */
.doc-content input[type="checkbox"] {
    margin-right: 8px;
    accent-color: var(--primary);
}

/* ============================================================
   TOC Sidebar
============================================================ */
.toc-sidebar {
    width: var(--toc-width);
    flex-shrink: 0;
}

.toc-container {
    position: sticky;
    top: 24px;
    font-size: 13px;
}

.toc-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 12px;
}

.toc-nav { display: flex; flex-direction: column; gap: 4px; }

.toc-link {
    color: var(--text-secondary);
    text-decoration: none;
    line-height: 1.4;
    transition: color 0.15s;
    display: block;
    padding: 2px 0;
    border-left: 2px solid transparent;
    padding-left: 8px;
}

.toc-link:hover { color: var(--primary); text-decoration: none; }
.toc-link.active { color: var(--primary); border-left-color: var(--primary); font-weight: 500; }
.toc-link.level-3 { padding-left: 20px; font-size: 12px; }

/* ============================================================
   FAB Button
============================================================ */
.fab-btn {
    position: fixed;
    bottom: 28px;
    right: 28px;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(37, 99, 235, 0.4);
    z-index: 200;
    transition: all 0.2s;
}

.fab-btn:hover {
    background: var(--primary-hover);
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(37, 99, 235, 0.5);
}

.fab-btn svg { width: 22px; height: 22px; }

/* ============================================================
   AI Panel
============================================================ */
.ai-panel {
    position: fixed;
    bottom: 88px;
    right: 28px;
    width: 600px;
    max-height: 800px;
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    z-index: 200;
    opacity: 0;
    transform: translateY(16px) scale(0.95);
    pointer-events: none;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
}

.ai-panel.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

.ai-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--primary);
    color: white;
    flex-shrink: 0;
}

.ai-panel-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 14px;
}

.ai-panel-close {
    background: none;
    border: none;
    color: rgba(255,255,255,0.8);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    transition: color 0.15s;
}

.ai-panel-close:hover { color: white; }

.ai-chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 200px;
}

.ai-welcome {
    color: var(--text-secondary);
    font-size: 13.5px;
    text-align: center;
    padding: 20px 16px;
}

/* Chat messages */
.chat-msg {
    display: flex;
    gap: 8px;
    align-items: flex-start;
}

.chat-msg-user { flex-direction: row-reverse; }

.chat-bubble {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 13.5px;
    line-height: 1.5;
}

.chat-msg-user .chat-bubble {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-msg-assistant .chat-bubble {
    background: var(--border-light);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.chat-bubble pre {
    background: rgba(0,0,0,0.08);
    border-radius: 4px;
    padding: 8px;
    margin: 8px 0 0;
    font-size: 12px;
    overflow-x: auto;
    font-family: var(--font-mono);
}

.chat-bubble code {
    background: rgba(0,0,0,0.06);
    padding: 1px 4px;
    border-radius: 3px;
    font-family: var(--font-mono);
    font-size: 12px;
}

/* Loading */
.chat-loading {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
    background: var(--border-light);
    border-radius: 12px;
    border-bottom-left-radius: 4px;
    color: var(--text-muted);
    font-size: 13px;
    width: fit-content;
}

.dot-flashing {
    display: flex;
    gap: 4px;
}

.dot-flashing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: dotFlash 1.2s ease-in-out infinite;
}

.dot-flashing span:nth-child(2) { animation-delay: 0.2s; }
.dot-flashing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotFlash {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1); }
}

/* Input area */
.ai-input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-input-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.ai-input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13.5px;
    font-family: inherit;
    resize: none;
    outline: none;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color 0.15s;
}

.ai-input:focus { border-color: var(--primary); }

.ai-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

.ai-send-btn:hover { background: var(--primary-hover); }
.ai-send-btn:disabled { background: var(--text-muted); cursor: not-allowed; }

.ai-clear-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 12px;
    cursor: pointer;
    text-align: left;
    padding: 0;
    transition: color 0.15s;
}

.ai-clear-btn:hover { color: var(--text-secondary); }

/* ============================================================
   Hamburger (Mobile)
============================================================ */
.hamburger {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 300;
    background: var(--bg-white);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    cursor: pointer;
    flex-direction: column;
    gap: 5px;
    box-shadow: var(--shadow-sm);
}

.hamburger span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--text-primary);
    border-radius: 2px;
    transition: all 0.2s;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 99;
}

/* ============================================================
   Responsive
============================================================ */
@media (max-width: 1100px) {
    .toc-sidebar { display: none; }
}

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

    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
    }

    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay.open { display: block; }

    .main-content { margin-left: 0; }

    .content-wrapper { padding: 20px 20px 60px; }

    .doc-title { font-size: 22px; }

    .ai-panel {
        width: calc(100vw - 32px);
        right: 16px;
        bottom: 80px;
    }

    .fab-btn { right: 16px; bottom: 16px; }
}

/* ============================================================
   Scrollbar (global)
============================================================ */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
