/* ================================================================
   ICD-10-CM Explorer — Premium Dark Theme
   ================================================================ */

:root {
    /* Color palette */
    --bg-primary: #0a0e17;
    --bg-secondary: #111827;
    --bg-card: #1a1f2e;
    --bg-card-hover: #222940;
    --bg-glass: rgba(26, 31, 46, 0.85);
    --border: rgba(255, 255, 255, 0.06);
    --border-hover: rgba(255, 255, 255, 0.12);

    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;

    --accent: #6366f1;
    --accent-hover: #818cf8;
    --accent-glow: rgba(99, 102, 241, 0.15);

    --green: #10b981;
    --green-glow: rgba(16, 185, 129, 0.15);
    --amber: #f59e0b;
    --amber-glow: rgba(245, 158, 11, 0.15);
    --red: #ef4444;
    --cyan: #06b6d4;
    --purple: #a855f7;

    /* Hierarchy colors */
    --chapter-color: #6366f1;
    --section-color: #06b6d4;
    --category-color: #10b981;
    --subcategory-color: #f59e0b;
    --leaf-color: #a855f7;

    /* Typography */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    /* Spacing */
    --radius: 12px;
    --radius-sm: 8px;
    --radius-xs: 6px;
}

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

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

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow: hidden;
}

/* ================================================================
   Top Bar
   ================================================================ */

.top-bar {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(20px);
    z-index: 100;
    position: relative;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.logo-icon {
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, var(--accent), var(--purple));
    border-radius: var(--radius-sm);
}

.logo h1 {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* Search bar */
.header-search {
    flex: 1;
    max-width: 480px;
    position: relative;
}

.search-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 12px;
    width: 16px;
    height: 16px;
    color: var(--text-muted);
    pointer-events: none;
}

.search-shortcut {
    position: absolute;
    right: 10px;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-muted);
    background: var(--bg-primary);
    border: 1px solid var(--border);
    padding: 2px 6px;
    border-radius: 4px;
    pointer-events: none;
}

#globalSearch {
    width: 100%;
    padding: 10px 40px 10px 38px;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 13px;
    font-family: var(--font-sans);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

#globalSearch:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

#globalSearch::placeholder {
    color: var(--text-muted);
}

.search-dropdown {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    max-height: 400px;
    overflow-y: auto;
    z-index: 200;
    box-shadow: 0 20px 40px rgba(0,0,0,0.4);
}

.search-dropdown.hidden { display: none; }

.search-result-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 14px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}

.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--bg-card-hover); }

.search-result-code {
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: 600;
    color: var(--accent);
    background: var(--accent-glow);
    padding: 2px 8px;
    border-radius: var(--radius-xs);
    flex-shrink: 0;
    min-width: 65px;
    text-align: center;
}

.search-result-desc {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.4;
}

/* Mode buttons */
.header-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.btn-mode {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 13px;
    font-family: var(--font-sans);
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-mode svg { width: 16px; height: 16px; }
.btn-mode:hover { background: var(--bg-card); border-color: var(--border-hover); }
.btn-mode.active {
    background: var(--accent-glow);
    border-color: var(--accent);
    color: var(--accent-hover);
}

/* ================================================================
   Main Layout
   ================================================================ */

.app-container {
    display: flex;
    height: calc(100vh - 65px);
    overflow: hidden;
}

.panel {
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Tree Panel */
.tree-panel {
    flex: 1;
    min-width: 0;
}

.panel-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.panel-header h2 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.breadcrumb {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
}

.crumb {
    font-size: 12px;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.15s;
}

.crumb:hover { color: var(--text-primary); background: var(--bg-card); }
.crumb.active { color: var(--accent); font-weight: 500; }

.crumb + .crumb::before {
    content: '›';
    margin-right: 4px;
    color: var(--text-muted);
}

/* Tree Container */
.tree-container {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.tree-container::-webkit-scrollbar { width: 6px; }
.tree-container::-webkit-scrollbar-track { background: transparent; }
.tree-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

/* Tree Nodes */
.tree-node {
    padding: 0 4px;
    margin-bottom: 2px;
}

.tree-node-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.15s;
    border: 1px solid transparent;
}

.tree-node-header:hover {
    background: var(--bg-card-hover);
    border-color: var(--border);
}

.tree-node-header.selected {
    background: var(--accent-glow);
    border-color: rgba(99, 102, 241, 0.3);
}

.node-expand {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    color: var(--text-muted);
    font-size: 11px;
    transition: transform 0.2s, color 0.15s;
    flex-shrink: 0;
}

.node-expand.expanded { transform: rotate(90deg); }
.node-expand.leaf { visibility: hidden; }

.node-badge {
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: var(--radius-xs);
    flex-shrink: 0;
    min-width: 50px;
    text-align: center;
}

.node-badge.chapter { color: var(--chapter-color); background: rgba(99, 102, 241, 0.12); }
.node-badge.section { color: var(--section-color); background: rgba(6, 182, 212, 0.12); }
.node-badge.category { color: var(--category-color); background: rgba(16, 185, 129, 0.12); }
.node-badge.subcategory { color: var(--subcategory-color); background: rgba(245, 158, 11, 0.12); }
.node-badge.leaf { color: var(--leaf-color); background: rgba(168, 85, 247, 0.12); }

.node-desc {
    font-size: 13px;
    color: var(--text-primary);
    line-height: 1.4;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.node-child-count {
    font-size: 11px;
    color: var(--text-muted);
    flex-shrink: 0;
}

.tree-node-children {
    padding-left: 20px;
    border-left: 1px solid var(--border);
    margin-left: 22px;
}

/* ================================================================
   Detail Panel
   ================================================================ */

.detail-panel {
    width: 380px;
    flex-shrink: 0;
    overflow-y: auto;
    border-right: none;
}

.detail-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
    color: var(--text-muted);
}

.placeholder-icon { font-size: 48px; opacity: 0.3; }

.detail-content { padding: 20px; }
.detail-content.hidden { display: none; }

.detail-header {
    margin-bottom: 20px;
}

.detail-code {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
    background: var(--accent-glow);
    padding: 4px 12px;
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
}

#detailDesc {
    font-size: 15px;
    font-weight: 500;
    line-height: 1.5;
    color: var(--text-primary);
}

.detail-meta {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.detail-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px;
}

.detail-section-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

.detail-section-title.includes { color: var(--green); }
.detail-section-title.excludes { color: var(--red); }
.detail-section-title.hints { color: var(--amber); }

.detail-section ul {
    list-style: none;
    padding: 0;
}

.detail-section li {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
    padding: 3px 0;
    border-bottom: 1px solid var(--border);
}

.detail-section li:last-child { border-bottom: none; }

/* ================================================================
   RAG Panel
   ================================================================ */

.rag-panel {
    flex: 1;
    border-right: none;
    overflow-y: auto;
    padding: 24px;
}

.rag-panel.hidden { display: none; }

.rag-input-section h2 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 4px;
    background: linear-gradient(135deg, var(--accent), var(--purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.rag-subtitle {
    color: var(--text-muted);
    font-size: 13px;
    margin-bottom: 20px;
}

.rag-form textarea {
    width: 100%;
    padding: 16px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 13px;
    line-height: 1.6;
    resize: vertical;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.rag-form textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.rag-form textarea::placeholder { color: var(--text-muted); }

.rag-options {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-top: 12px;
    flex-wrap: wrap;
}

.option-group {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}

.option-group select {
    padding: 6px 8px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 12px;
    outline: none;
    cursor: pointer;
}

.option-group.checkbox input[type="checkbox"] {
    accent-color: var(--accent);
}

.btn-primary {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 20px;
    background: linear-gradient(135deg, var(--accent), #4f46e5);
    border: none;
    border-radius: var(--radius-sm);
    color: white;
    font-family: var(--font-sans);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    margin-left: auto;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3);
}

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

.btn-primary svg { width: 16px; height: 16px; }

/* Progress */
.rag-progress {
    margin-top: 24px;
}

.rag-progress.hidden { display: none; }

.progress-bar {
    height: 4px;
    background: var(--bg-card);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 12px;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--accent), var(--purple));
    border-radius: 2px;
    transition: width 0.5s ease;
}

.progress-log {
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 200px;
    overflow-y: auto;
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-muted);
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    padding: 12px;
}

.log-entry {
    display: flex;
    gap: 8px;
    padding: 2px 0;
}

.log-entry .log-time { color: var(--text-muted); flex-shrink: 0; }
.log-entry .log-msg { color: var(--text-secondary); }
.log-entry.stage { color: var(--accent); }
.log-entry.done { color: var(--green); }

/* Results */
.rag-results { margin-top: 24px; }
.rag-results.hidden { display: none; }

.rag-results h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.02em;
    margin-bottom: 12px;
}

.result-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 8px;
    transition: border-color 0.2s;
}

.result-card:hover { border-color: var(--border-hover); }

.result-card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.result-card-code {
    font-family: var(--font-mono);
    font-size: 16px;
    font-weight: 700;
    color: var(--accent);
    background: var(--accent-glow);
    padding: 4px 10px;
    border-radius: var(--radius-xs);
}

.result-card-score {
    font-size: 11px;
    color: var(--green);
    background: var(--green-glow);
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: 500;
}

.result-card-path {
    font-size: 12px;
    color: var(--text-muted);
    font-family: var(--font-mono);
    margin-bottom: 6px;
}

.result-card-path span {
    color: var(--text-secondary);
}

.result-card-context {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
    white-space: pre-line;
    background: var(--bg-primary);
    padding: 10px;
    border-radius: var(--radius-xs);
    margin-top: 8px;
    border-left: 3px solid var(--accent);
}

/* ================================================================
   Loading & Utility
   ================================================================ */

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    gap: 16px;
    color: var(--text-muted);
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.hidden { display: none !important; }

/* Smooth entry animation */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.tree-node { animation: fadeIn 0.2s ease-out; }

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

@media (max-width: 900px) {
    .detail-panel { display: none; }
    .header-search { max-width: 300px; }
}
