/* ===== CSS Variables - Light Theme ===== */
:root {
    /* Colors */
    --bg-primary: #f0f4f8;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f8fafc;
    --bg-hover: #e2e8f0;
    --bg-accent: rgba(99, 102, 241, 0.08);

    --text-primary: #1e293b;
    --text-secondary: #475569;
    --text-muted: #94a3b8;

    --border: #e2e8f0;
    --border-hover: #cbd5e1;

    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --accent-light: rgba(99, 102, 241, 0.15);
    --accent-2: #10b981;
    --accent-2-light: rgba(16, 185, 129, 0.15);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.05);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
    --shadow-glow: 0 0 40px rgba(99, 102, 241, 0.15);

    --gradient-accent: linear-gradient(135deg, var(--accent), var(--accent-2));
    --gradient-bg: radial-gradient(ellipse at 20% 0%, rgba(99, 102, 241, 0.12) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 100%, rgba(16, 185, 129, 0.08) 0%, transparent 50%);

    /* Layout */
    --topbar-h: 72px;
    --sidebar-w: 320px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;

    /* Typography */
    --font: "Inter", "Segoe UI", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
    --font-mono: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Consolas, monospace;

    /* Transitions */
    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 250ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== Dark Theme ===== */
[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --bg-hover: #475569;
    --bg-accent: rgba(129, 140, 248, 0.12);

    --text-primary: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-muted: #64748b;

    --border: #4b5563;
    --border-hover: #6b7280;

    --accent: #818cf8;
    --accent-hover: #a5b4fc;
    --accent-light: rgba(129, 140, 248, 0.2);
    --accent-2: #34d399;
    --accent-2-light: rgba(52, 211, 153, 0.2);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -4px rgba(0, 0, 0, 0.4);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.6), 0 8px 10px -6px rgba(0, 0, 0, 0.4);
    --shadow-glow: 0 0 40px rgba(129, 140, 248, 0.2);

    --gradient-bg: radial-gradient(ellipse at 20% 0%, rgba(129, 140, 248, 0.1) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 100%, rgba(52, 211, 153, 0.06) 0%, transparent 50%);
}

/* ===== Base Styles ===== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

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

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

*::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 999px;
    transition: background var(--transition-fast);
}

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

html {
    overflow: hidden;
    height: 100%;
    scroll-behavior: smooth;
}

body {
    background: var(--gradient-bg), var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.6;
    min-height: 100vh;
    height: 100%;
    overflow: hidden;
    transition: background var(--transition-slow), color var(--transition-slow);
}

/* ===== Top Bar ===== */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    height: var(--topbar-h);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(12px);
    box-shadow: var(--shadow-md);
    transition: all var(--transition-slow);
    position: relative;
    z-index: 100;
}

.brand {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-md);
    background: var(--gradient-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md), var(--shadow-glow);
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.logo:hover {
    transform: scale(1.05) rotate(-2deg);
}

.logo-icon {
    font-size: 22px;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

.brand h1 {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    background: var(--gradient-accent);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.eyebrow {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 2px;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.status {
    padding: 8px 16px;
    border-radius: var(--radius-md);
    background: var(--bg-accent);
    border: 1px solid var(--border);
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all var(--transition-fast);
}

/* ===== Theme Toggle Button ===== */
.theme-toggle {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    transition: all var(--transition-fast);
}

.theme-toggle:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
    transform: scale(1.05);
}

.theme-icon {
    font-size: 20px;
    position: absolute;
    transition: all var(--transition-normal);
}

.light-icon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

.dark-icon {
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
}

[data-theme="dark"] .light-icon {
    opacity: 0;
    transform: scale(0.5) rotate(-90deg);
}

[data-theme="dark"] .dark-icon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

/* ===== Main Layout ===== */
.layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    gap: 24px;
    padding: 24px;
    height: calc(100vh - var(--topbar-h));
    overflow: hidden;
    max-width: 100vw;
}

/* ===== Sidebar ===== */
.sidebar {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 20px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: all var(--transition-slow);
}

.sidebar__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.sidebar__head h2 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* ===== Buttons ===== */
button {
    cursor: pointer;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 14px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-weight: 600;
    font-size: 13px;
    transition: all var(--transition-fast);
}

button:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
    color: var(--accent);
}

.ghost {
    background: transparent;
    border-color: transparent;
}

.ghost:hover {
    background: var(--accent-light);
}

/* ===== Search ===== */
.search {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
}

.search input[type="search"] {
    width: 100%;
    padding: 12px 16px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: 14px;
    transition: all var(--transition-fast);
}

.search input[type="search"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
    background: var(--bg-secondary);
}

.search input[type="search"]::placeholder {
    color: var(--text-muted);
}

.search-results {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 50vh;
    overflow-y: auto;
    padding-right: 4px;
}

.search-result {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 12px 14px;
    background: var(--bg-tertiary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.search-result:hover {
    border-color: var(--accent);
    background: var(--accent-light);
    transform: translateX(4px);
}

.search-result__title {
    margin: 0 0 4px;
    font-weight: 700;
    color: var(--text-primary);
}

.search-result__meta {
    margin: 0 0 6px;
    color: var(--text-muted);
    font-size: 12px;
}

.search-result__excerpt {
    margin: 0;
    color: var(--text-secondary);
    line-height: 1.5;
    font-size: 13px;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ===== Document List ===== */
.doc-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow-y: auto;
    flex: 1;
    padding-right: 4px;
}

.doc-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-tertiary);
    cursor: pointer;
    transition: all var(--transition-fast);
    position: relative;
    overflow: visible;
    min-height: auto;
}

.doc-row::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--gradient-accent);
    opacity: 0;
    transition: opacity var(--transition-fast);
}

.doc-row:hover {
    border-color: var(--accent);
    background: var(--accent-light);
    transform: translateX(4px);
}

.doc-row:hover::before {
    opacity: 1;
}

.doc-row.dir {
    cursor: pointer;
    background: var(--bg-secondary);
    border-style: solid;
    border-left: 3px solid var(--accent-2);
    overflow: visible;
}

.doc-row.dir:hover {
    background: var(--accent-2-light);
    border-color: var(--accent-2);
}

.doc-row .title {
    margin: 0;
    font-weight: 600;
    font-size: 14px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
    line-height: 1.4;
    white-space: normal;
    word-break: break-word;
}

.doc-row .slug {
    margin: 0;
    color: var(--text-muted);
    font-size: 12px;
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.caret {
    font-size: 10px;
    color: var(--text-muted);
    transition: transform var(--transition-fast);
}

.caret.open {
    transform: rotate(0deg);
}

.doc-children {
    margin-left: 8px;
    border-left: 2px solid var(--border);
    padding-left: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 6px;
}

.doc-children .doc-row {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    padding: 12px 14px;
    overflow: visible;
}

.doc-children .doc-row .title {
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.4;
}

.doc-children .doc-row:hover {
    background: var(--accent-light);
    border-color: var(--accent);
}

/* ===== Content Area ===== */
.content {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 24px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: all var(--transition-slow);
}

.doc {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.doc__header {
    margin-bottom: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
    flex: 0 0 auto;
}

.doc__header h2 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.doc__body {
    flex: 1 1 auto;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 24px;
    background: var(--bg-tertiary);
    min-height: 300px;
    overflow-wrap: anywhere;
    transition: all var(--transition-slow);
}

.doc-updated {
    margin-bottom: 16px;
    padding: 8px 12px;
    background: var(--accent-light);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 12px;
    display: inline-block;
}

/* ===== Document Body Typography ===== */
.doc__body h1,
.doc__body h2,
.doc__body h3,
.doc__body h4 {
    margin-top: 24px;
    margin-bottom: 12px;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.doc__body h1 {
    font-size: 1.875rem;
}

.doc__body h2 {
    font-size: 1.5rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
}

.doc__body h3 {
    font-size: 1.25rem;
}

.doc__body h4 {
    font-size: 1.1rem;
}

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

.doc__body ul,
.doc__body ol {
    margin-bottom: 16px;
    padding-left: 24px;
    color: var(--text-secondary);
}

.doc__body li {
    margin-bottom: 6px;
}

.doc__body pre {
    background: var(--bg-secondary);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    overflow: auto;
    border: 1px solid var(--border);
    margin: 16px 0;
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.6;
}

.doc__body code {
    background: var(--accent-light);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: 0.9em;
    color: var(--accent);
}

.doc__body pre code {
    background: transparent;
    padding: 0;
    color: var(--text-primary);
}

.doc__body blockquote {
    border-left: 4px solid var(--accent);
    padding: 12px 16px;
    margin: 16px 0;
    background: var(--accent-light);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    color: var(--text-secondary);
}

.doc__body blockquote p {
    margin: 4px 0;
}

.doc__body blockquote ul {
    margin: 8px 0;
}

/* ===== Tables ===== */
.doc__body table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    border: 2px solid var(--border);
}

.doc__body th,
.doc__body td {
    padding: 12px 16px;
    text-align: left;
    border: 1px solid var(--border);
}

.doc__body th {
    background: var(--accent-light);
    color: var(--text-primary);
    font-weight: 700;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.doc__body td {
    color: var(--text-secondary);
}

.doc__body td:first-child {
    font-weight: 600;
    color: var(--text-primary);
}

.doc__body tbody tr {
    transition: background var(--transition-fast);
}

.doc__body tbody tr:hover {
    background: var(--bg-hover);
}

.doc__body a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    transition: color var(--transition-fast);
}

.doc__body a:hover {
    color: var(--accent-hover);
    text-decoration: underline;
}

/* ===== Edit Log ===== */
.edit-log {
    margin-top: 24px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}

.edit-log h3 {
    margin: 0 0 12px;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-muted);
}

.edit-log ul {
    list-style: none;
    padding-left: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.edit-log li {
    font-size: 13px;
    color: var(--text-secondary);
}

.commit-head {
    margin-bottom: 8px;
}

.commit-diff {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-tertiary);
    padding: 10px 14px;
}

.commit-diff summary {
    cursor: pointer;
    color: var(--accent);
    font-weight: 600;
    outline: none;
}

.diff-block {
    margin-top: 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-secondary);
    overflow: auto;
    font-family: var(--font-mono);
    font-size: 12px;
}

.diff-line {
    padding: 3px 12px;
    white-space: pre;
}

.diff-line.add {
    background: var(--accent-2-light);
    color: var(--accent-2);
}

.diff-line.del {
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
}

[data-theme="dark"] .diff-line.del {
    background: rgba(239, 68, 68, 0.2);
    color: #f87171;
}

.diff-line.hunk {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 600;
}

.diff-line.file {
    color: var(--text-secondary);
    font-weight: 600;
}

.mark-hit {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.5), rgba(251, 146, 60, 0.4));
    color: var(--text-primary);
    padding: 2px 5px;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(251, 191, 36, 0.3);
    font-weight: 500;
    animation: highlightPulse 1.5s ease-in-out;
}

@keyframes highlightPulse {
    0% {
        background: rgba(251, 191, 36, 0.8);
    }

    50% {
        background: rgba(251, 191, 36, 0.6);
    }

    100% {
        background: linear-gradient(135deg, rgba(251, 191, 36, 0.5), rgba(251, 146, 60, 0.4));
    }
}

[data-theme="dark"] .mark-hit {
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.4), rgba(251, 146, 60, 0.35));
    box-shadow: 0 1px 4px rgba(251, 191, 36, 0.4);
}

/* ===== Images ===== */
.doc__body img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    margin: 16px 0;
}

/* ===== Responsive ===== */

/* Menu Toggle Button - hidden on desktop */
.menu-toggle {
    display: none;
    width: 44px;
    height: 44px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    transition: all var(--transition-fast);
}

.menu-toggle:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

.menu-icon,
.close-icon {
    font-size: 20px;
    position: absolute;
    transition: all var(--transition-normal);
}

.menu-icon {
    opacity: 1;
    transform: scale(1);
}

.close-icon {
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
}

body.menu-open .menu-icon {
    opacity: 0;
    transform: scale(0.5) rotate(-90deg);
}

body.menu-open .close-icon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

/* Sidebar Overlay */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 199;
    opacity: 0;
    transition: opacity var(--transition-normal);
}

@media (max-width: 1024px) {
    .layout {
        grid-template-columns: 280px 1fr;
        gap: 16px;
        padding: 16px;
    }
}

@media (max-width: 768px) {
    :root {
        --topbar-h: 64px;
    }

    .topbar {
        padding: 0 16px;
    }

    .brand h1 {
        font-size: 1rem;
    }

    .logo {
        width: 36px;
        height: 36px;
    }

    .logo-icon {
        font-size: 18px;
    }

    .status {
        display: none;
    }

    /* Show menu toggle on mobile */
    .menu-toggle {
        display: flex;
    }

    .layout {
        grid-template-columns: 1fr;
        gap: 16px;
        padding: 16px;
        height: calc(100vh - var(--topbar-h));
        overflow: hidden;
    }

    /* Sidebar as slide-in panel */
    .sidebar {
        position: fixed;
        top: var(--topbar-h);
        right: -320px;
        width: 300px;
        max-width: 85vw;
        height: calc(100vh - var(--topbar-h));
        max-height: none;
        z-index: 200;
        border-radius: var(--radius-xl) 0 0 var(--radius-xl);
        transition: right var(--transition-normal), transform var(--transition-normal);
        box-shadow: var(--shadow-xl);
    }

    body.menu-open .sidebar {
        right: 0;
    }

    body.menu-open .sidebar-overlay {
        display: block;
        opacity: 1;
    }

    /* Content takes full width and scrolls */
    .content {
        min-height: auto;
        height: 100%;
        overflow: hidden;
    }

    .doc {
        height: 100%;
    }

    .doc__body {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.doc-list>.doc-row,
.doc-list>.doc-children {
    animation: fadeIn 0.3s ease both;
}

.doc-list>.doc-row:nth-child(1),
.doc-list>.doc-children:nth-child(1) {
    animation-delay: 0.02s;
}

.doc-list>.doc-row:nth-child(2),
.doc-list>.doc-children:nth-child(2) {
    animation-delay: 0.04s;
}

.doc-list>.doc-row:nth-child(3),
.doc-list>.doc-children:nth-child(3) {
    animation-delay: 0.06s;
}

.doc-list>.doc-row:nth-child(4),
.doc-list>.doc-children:nth-child(4) {
    animation-delay: 0.08s;
}

.doc-list>.doc-row:nth-child(5),
.doc-list>.doc-children:nth-child(5) {
    animation-delay: 0.1s;
}

/* Ensure children items are visible immediately */
.doc-children .doc-row {
    animation: none;
    opacity: 1;
}