/* ══════════════════════════════════════════════════════════════
   Clinical Protocol Viewer — Design System
   Clean, professional, minimal medical UI
   ══════════════════════════════════════════════════════════════ */

:root {
    --bg:            #f1f5f9;
    --surface:       #ffffff;
    --surface-2:     #f8fafc;
    --border:        #e2e8f0;
    --border-soft:   #f1f5f9;
    --text:          #1e293b;
    --text-2:        #475569;
    --text-3:        #94a3b8;
    --primary:       #0f6d78;
    --primary-hover: #0a5560;
    --primary-light: #eef9fa;
    --primary-mid:   #cceef1;
    --danger:        #dc2626;
    --success:       #059669;
    --warn:          #d97706;
    --blue:          #2563eb;
    --blue-light:    #eff6ff;
    --radius-sm: 5px;
    --radius:    8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --shadow-xs: 0 1px 2px rgba(0,0,0,.06);
    --shadow-sm: 0 1px 4px rgba(0,0,0,.08), 0 0 0 1px rgba(0,0,0,.03);
    --shadow:    0 4px 16px rgba(0,0,0,.08), 0 0 0 1px rgba(0,0,0,.04);
    --topbar-h:  58px;
    --sidebar-w: 290px;
    --font:      'Inter', system-ui, -apple-system, sans-serif;
}

/* ── Reset & base ── */
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 14px; scroll-behavior: smooth; }
body {
    margin: 0;
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
p  { margin: 0; }
button, input { font: inherit; }
details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }

/* ══════════════════════════════════════════════════════════════
   TOP BAR
   ══════════════════════════════════════════════════════════════ */
.topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--topbar-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 18px;
    z-index: 200;
    box-shadow: var(--shadow-xs);
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    text-decoration: none;
}
.brand-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
    letter-spacing: -0.01em;
}

/* Search bar */
.topbar-search {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 0 4px 0 12px;
    transition: border-color .15s, box-shadow .15s;
    max-width: 680px;
}
.topbar-search:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(15,109,120,.12);
    background: var(--surface);
}
.search-icon { color: var(--text-3); flex-shrink: 0; }
.topbar-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    padding: 9px 10px;
    color: var(--text);
    font-size: 14px;
}
.topbar-input::placeholder { color: var(--text-3); }
.topbar-btn {
    flex-shrink: 0;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: 7px 16px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s;
    white-space: nowrap;
}
.topbar-btn:hover   { background: var(--primary-hover); }
.topbar-btn:disabled { opacity: .55; cursor: not-allowed; }

/* Stats + status dot */
.topbar-meta {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    margin-left: auto;
}
.topbar-stats {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.ts-chip {
    font-size: 12px;
    color: var(--text-2);
    white-space: nowrap;
}
.ts-chip b { color: var(--text); font-weight: 700; margin-right: 2px; }

.status-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.status-idle    { background: var(--text-3); }
.status-loading { background: var(--warn); animation: pulse 1s infinite; }
.status-ok      { background: var(--success); }
.status-error   { background: var(--danger); }

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

/* ══════════════════════════════════════════════════════════════
   APP BODY
   ══════════════════════════════════════════════════════════════ */
.app-body {
    display: flex;
    height: calc(100vh - var(--topbar-h));
    margin-top: var(--topbar-h);
    overflow: hidden;
}

/* ══════════════════════════════════════════════════════════════
   SIDEBAR
   ══════════════════════════════════════════════════════════════ */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-head {
    padding: 12px 12px 8px;
    border-bottom: 1px solid var(--border-soft);
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}

.sf-wrap {
    display: flex;
    align-items: center;
    gap: 7px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0 10px;
    color: var(--text-3);
    transition: border-color .15s;
}
.sf-wrap:focus-within {
    border-color: var(--primary);
    color: var(--primary);
}
.sidebar-filter {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    padding: 8px 0;
    font-size: 13px;
    color: var(--text);
}
.sidebar-filter::placeholder { color: var(--text-3); }

.sidebar-count {
    font-size: 11px;
    color: var(--text-3);
    font-weight: 500;
    padding-left: 2px;
}

.sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 6px 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.sidebar-list::-webkit-scrollbar       { width: 4px; }
.sidebar-list::-webkit-scrollbar-track { background: transparent; }
.sidebar-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.sidebar-empty {
    padding: 16px 8px;
    text-align: center;
    color: var(--text-3);
    font-size: 13px;
}

/* Protocol list item */
.proto-item {
    display: flex;
    flex-direction: column;
    gap: 3px;
    width: 100%;
    text-align: left;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius);
    padding: 9px 10px;
    cursor: pointer;
    transition: background .12s, border-color .12s;
    margin-bottom: 1px;
}
.proto-item:hover {
    background: var(--surface-2);
    border-color: var(--border);
}
.proto-item.active {
    background: var(--primary-light);
    border-color: var(--primary-mid);
}
.proto-item.active .pi-name { color: var(--primary); }

.pi-name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.pi-meta {
    font-size: 11.5px;
    color: var(--text-3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.pi-chips {
    display: flex;
    gap: 5px;
    flex-wrap: wrap;
    margin-top: 1px;
}
.pi-code, .pi-sec {
    font-size: 10.5px;
    padding: 1px 6px;
    border-radius: 4px;
    font-weight: 600;
}
.pi-code { background: var(--primary-light); color: var(--primary); }
.pi-sec  { background: var(--surface-2); color: var(--text-3); border: 1px solid var(--border); }

/* ══════════════════════════════════════════════════════════════
   MAIN AREA
   ══════════════════════════════════════════════════════════════ */
.main-area {
    flex: 1;
    overflow-y: auto;
    background: var(--bg);
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.main-area::-webkit-scrollbar       { width: 6px; }
.main-area::-webkit-scrollbar-track { background: transparent; }
.main-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Empty state ── */
.empty-state-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 180px;
    padding: 24px 20px 20px;
    text-align: center;
}
.es-icon {
    width: 60px; height: 60px;
    border-radius: 50%;
    background: var(--surface);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-3);
    margin-bottom: 14px;
    box-shadow: var(--shadow-sm);
}
.empty-state-page h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 6px;
}
.empty-state-page p {
    color: var(--text-2);
    font-size: 14px;
    max-width: 360px;
    margin-bottom: 18px;
}

.quick-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: center;
}
.qchip {
    padding: 6px 12px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: border-color .12s, color .12s, background .12s, transform .1s;
    box-shadow: var(--shadow-xs);
}
.qchip:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
    transform: translateY(-1px);
}

/* ── Search panel ── */
.search-panel {
    padding: 28px 32px 40px;
    max-width: 960px;
}
.sp-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}
.sp-header h2 { font-size: 18px; color: var(--text); margin-bottom: 4px; }
.sp-sub { color: var(--text-2); font-size: 13px; }

.btn-ghost {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text-2);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: border-color .12s, color .12s;
    white-space: nowrap;
}
.btn-ghost:hover { border-color: var(--text-2); color: var(--text); }

.btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: none;
    border-radius: var(--radius);
    background: var(--primary);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s;
    white-space: nowrap;
}
.btn-primary:hover { background: var(--primary-hover); }

.results-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.no-results {
    text-align: center;
    color: var(--text-2);
    padding: 40px 0;
    font-size: 14px;
}

.result-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    box-shadow: var(--shadow-xs);
    transition: border-color .12s, box-shadow .12s;
}
.result-item:hover {
    border-color: var(--primary-mid);
    box-shadow: var(--shadow-sm);
}
.ri-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}
.ri-left h3 {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 4px;
}
.ri-meta {
    font-size: 12.5px;
    color: var(--text-2);
}
.ri-mkb {
    display: inline-block;
    background: var(--primary-light);
    color: var(--primary);
    padding: 1px 7px;
    border-radius: 4px;
    font-weight: 600;
    font-size: 11.5px;
    margin-left: 4px;
}
.ri-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}
.score-badge {
    font-size: 11.5px;
    padding: 2px 8px;
    border-radius: 4px;
    background: #fef3c7;
    color: #92400e;
    font-weight: 600;
    white-space: nowrap;
}

.ri-matches {
    display: flex;
    flex-direction: column;
    gap: 6px;
    border-top: 1px solid var(--border-soft);
    padding-top: 10px;
}
.match-item {
    background: var(--surface-2);
    border-radius: var(--radius);
    padding: 8px 12px;
}
.match-label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-3);
    display: block;
    margin-bottom: 3px;
}
.match-text {
    font-size: 13px;
    color: var(--text-2);
    line-height: 1.5;
    white-space: pre-wrap;
    max-height: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

/* ══════════════════════════════════════════════════════════════
   PROTOCOL VIEWER
   ══════════════════════════════════════════════════════════════ */
.protocol-view {
    display: flex;
    flex-direction: column;
    min-height: 100%;
}

/* Protocol header */
.pv-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 22px 32px 18px;
    box-shadow: var(--shadow-xs);
}
.pv-title-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}
.pv-header h1 {
    font-size: clamp(17px, 2.2vw, 22px);
    font-weight: 700;
    color: var(--text);
    line-height: 1.3;
    max-width: 820px;
}
.pv-stats {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    flex-shrink: 0;
    align-items: center;
}
.stat-chip {
    font-size: 12px;
    padding: 3px 9px;
    border-radius: 5px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text-2);
    font-weight: 500;
    white-space: nowrap;
}

.pv-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}
.mkb-chip {
    padding: 4px 10px;
    border-radius: 5px;
    background: var(--primary-light);
    border: 1px solid var(--primary-mid);
    color: var(--primary);
    font-size: 12.5px;
    font-weight: 700;
    cursor: pointer;
    transition: background .12s, transform .1s;
    letter-spacing: .01em;
}
.mkb-chip:hover {
    background: var(--primary-mid);
    transform: translateY(-1px);
}

.pv-meta {
    font-size: 12.5px;
    color: var(--text-2);
    display: flex;
    flex-wrap: wrap;
    gap: 0 4px;
    align-items: center;
}
.pm-sep { color: var(--text-3); margin: 0 3px; }
.pm-file { color: var(--text-3); }

/* Protocol two-column body */
.pv-body {
    display: grid;
    grid-template-columns: 224px 1fr;
    gap: 0;
    align-items: start;
    flex: 1;
}

/* TOC panel */
.pv-toc-panel {
    border-right: 1px solid var(--border);
    padding: 16px 10px 40px;
    position: sticky;
    top: 0;       /* sticks below nothing — sticks inside main-area scroll */
    max-height: calc(100vh - var(--topbar-h));
    overflow-y: auto;
    background: var(--surface);
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.pv-toc-panel::-webkit-scrollbar       { width: 3px; }
.pv-toc-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.toc-head {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 10.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-3);
    padding: 0 6px 10px;
    border-bottom: 1px solid var(--border-soft);
    margin-bottom: 8px;
}

.toc-items {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.toc-item {
    display: block;
    width: 100%;
    text-align: left;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    padding: 5px 8px;
    font-size: 12.5px;
    color: var(--text-2);
    cursor: pointer;
    transition: background .1s, color .1s;
    line-height: 1.4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.toc-item:hover    { background: var(--surface-2); color: var(--text); }
.toc-item.toc-active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

.toc-lv1 { padding-left: 8px; font-weight: 600; font-size: 13px; color: var(--text); }
.toc-lv2 { padding-left: 16px; }
.toc-lv3 { padding-left: 24px; font-size: 12px; color: var(--text-3); }
.toc-lv4 { padding-left: 30px; font-size: 11.5px; color: var(--text-3); }

/* Content area */
.pv-content-area {
    padding: 20px 32px 40px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 0;
}

/* Sections */
.sections-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.section-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    box-shadow: var(--shadow-xs);
    scroll-margin-top: 12px;
    transition: border-color .15s;
}
.section-item:hover { border-color: var(--primary-mid); }

.si-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 6px;
}

.si-title {
    font-family: var(--font);
    color: var(--text);
    line-height: 1.3;
}
.si-title.lv1 { font-size: 16px; font-weight: 700; color: var(--primary); }
.si-title.lv2 { font-size: 14.5px; font-weight: 600; }
.si-title.lv3 { font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.si-title.lv4 { font-size: 13px; font-weight: 500; color: var(--text-2); }

.si-path {
    font-size: 11.5px;
    color: var(--text-3);
    margin-bottom: 8px;
}

.stype-badge {
    flex-shrink: 0;
    font-size: 10.5px;
    padding: 2px 7px;
    border-radius: 4px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    color: var(--text-3);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    white-space: nowrap;
}

.si-text {
    font-size: 13.5px;
    line-height: 1.7;
    color: var(--text-2);
    white-space: pre-wrap;
    word-break: break-word;
    padding: 10px 12px;
    background: var(--surface-2);
    border-radius: var(--radius);
    border: 1px solid var(--border-soft);
    margin-top: 6px;
    max-height: 420px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.si-sub {
    background: var(--bg);
    border-color: var(--border);
    margin-top: 8px;
    max-height: 320px;
}

.subtree-detail { margin-top: 8px; }
.subtree-detail summary {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--primary);
    cursor: pointer;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    background: var(--primary-light);
    border: 1px solid var(--primary-mid);
    display: inline-flex;
    align-items: center;
    gap: 5px;
    transition: background .12s;
}
.subtree-detail summary:hover { background: var(--primary-mid); }
.subtree-detail[open] summary { border-radius: var(--radius-sm) var(--radius-sm) 0 0; }

/* ── Metadata accordion ── */
.meta-accordion {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-xs);
}

.meta-accordion-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 18px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-2);
    user-select: none;
    transition: background .12s;
}
.meta-accordion-toggle:hover { background: var(--surface-2); }
details.meta-accordion[open] .meta-accordion-toggle { background: var(--surface-2); }
.acc-arrow {
    margin-left: auto;
    color: var(--text-3);
    transition: transform .2s;
}
details.meta-accordion[open] .acc-arrow { transform: rotate(180deg); }

.meta-accordion-body {
    padding: 16px 18px 20px;
    border-top: 1px solid var(--border-soft);
}
.meta-split {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}
.meta-section h4 {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-3);
    margin-bottom: 10px;
}

.meta-dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 5px 12px;
    margin: 0;
}
.meta-dl dt {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-3);
    white-space: nowrap;
}
.meta-dl dd {
    margin: 0;
    font-size: 12.5px;
    color: var(--text-2);
    word-break: break-word;
}

.toc-full { display: flex; flex-direction: column; gap: 3px; }
.toc-full-item {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    font-size: 12.5px;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    color: var(--text-2);
}
.toc-full-item:hover { background: var(--surface-2); }
.toc-full-item b { color: var(--text-3); flex-shrink: 0; }

.term-groups { display: flex; flex-direction: column; gap: 10px; }
.tg-group {}
.tg-label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-3);
    display: block;
    margin-bottom: 5px;
}
.tg-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.tg-chip {
    padding: 3px 9px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-2);
    font-size: 12px;
    cursor: pointer;
    transition: border-color .12s, color .12s, background .12s;
}
.tg-chip:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-light);
}

.empty-hint {
    font-size: 12.5px;
    color: var(--text-3);
    font-style: italic;
}

/* ══════════════════════════════════════════════════════════════
   RESPONSIVE
   ══════════════════════════════════════════════════════════════ */
@media (max-width: 1100px) {
    :root { --sidebar-w: 240px; }
    .pv-body { grid-template-columns: 190px 1fr; }
}

@media (max-width: 860px) {
    .sidebar { display: none; }
    .pv-body { grid-template-columns: 1fr; }
    .pv-toc-panel {
        position: static;
        max-height: none;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .pv-content-area { padding: 16px 16px 32px; }
    .pv-header { padding: 16px; }
    .search-panel { padding: 20px 16px; }
    .topbar { padding: 0 12px; gap: 10px; }
    .brand-name { display: none; }
    .topbar-meta .topbar-stats { display: none; }
}

@media (max-width: 600px) {
    :root { --topbar-h: 52px; }
    .pv-header { padding: 12px 14px; }
    .pv-header h1 { font-size: 15px; }
    .ri-right { flex-direction: column; align-items: flex-end; }
}
