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

:root {
    --bg: #f5f5f0;
    --surface: #fff;
    --border: #d4d4c8;
    --text: #2a2a28;
    --text-muted: #6b6b60;
    --accent: #3a6b35;
    --accent-light: #e8f0e6;
    --danger: #8b3a3a;
    --warning: #8b7a3a;
    --info: #3a5a8b;
    --radius: 4px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    font-size: 14px;
}

/* === Header === */
header {
    background: var(--surface);
    border-bottom: 2px solid var(--accent);
    padding: 1rem 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow);
}

header h1 {
    font-size: 1.4rem;
    color: var(--accent);
    margin-bottom: 0.2rem;
}

.subtitle {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
}

.controls {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

.controls label {
    font-size: 0.9rem;
    white-space: nowrap;
}

#pop-slider {
    width: 200px;
    accent-color: var(--accent);
}

.cost-summary {
    margin-left: auto;
    font-size: 0.95rem;
    color: var(--accent);
}

/* === Tab Navigation === */
#tab-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    background: var(--surface);
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
}

.tab-btn {
    background: none;
    border: 1px solid transparent;
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
    cursor: pointer;
    border-radius: var(--radius);
    white-space: nowrap;
    color: var(--text-muted);
    transition: all 0.15s;
}

.tab-btn:hover {
    background: var(--accent-light);
    color: var(--accent);
}

.tab-btn.active {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

/* === Layout === */
.layout {
    display: flex;
    max-width: 1400px;
    margin: 0 auto;
    min-height: calc(100vh - 200px);
}

#sidebar {
    width: 220px;
    min-width: 220px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem;
    position: sticky;
    top: 130px;
    height: fit-content;
    max-height: calc(100vh - 150px);
    overflow-y: auto;
}

#sidebar h3 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

#toc-list {
    list-style: none;
}

#toc-list li a {
    display: block;
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: var(--radius);
    transition: all 0.15s;
}

#toc-list li a:hover {
    background: var(--accent-light);
    color: var(--accent);
}

main {
    flex: 1;
    padding: 1.5rem;
    overflow-x: auto;
}

/* === Tab Panels === */
.tab-panel {
    display: none;
}
.tab-panel.active {
    display: block;
}

/* === Sections (subcategories) === */
.subcategory {
    margin-bottom: 1.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

.subcategory-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 1rem;
    background: var(--accent-light);
    cursor: pointer;
    user-select: none;
    border-radius: var(--radius) var(--radius) 0 0;
}

.subcategory-header h3 {
    font-size: 0.95rem;
    color: var(--accent);
}

.subcategory-header .toggle {
    font-size: 0.8rem;
    color: var(--text-muted);
    transition: transform 0.2s;
}

.subcategory.collapsed .toggle {
    transform: rotate(-90deg);
}

.subcategory.collapsed .subcategory-body {
    display: none;
}

.subcategory-subtotal {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-left: auto;
    margin-right: 1rem;
}

/* === Item Tables === */
.item-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}

.item-table th {
    background: #f0f0ea;
    text-align: left;
    padding: 0.4rem 0.6rem;
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
}

.item-table td {
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid #eee;
    vertical-align: top;
}

.item-table tr:hover td {
    background: #fafaf5;
}

/* Importance badges */
.badge {
    display: inline-block;
    padding: 0.1rem 0.4rem;
    border-radius: 3px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge.necessary { background: #d4edda; color: #155724; }
.badge.important { background: #d1ecf1; color: #0c5460; }
.badge.useful { background: #fff3cd; color: #856404; }
.badge.optional { background: #e2e3e5; color: #383d41; }
.badge.possible { background: #f8d7da; color: #721c24; }

/* === Details expand === */
.item-details {
    display: none;
    padding: 0.5rem 0.6rem;
    background: #fafaf5;
    font-size: 0.78rem;
    color: var(--text-muted);
}
.item-details.open {
    display: table-row;
}
.item-details td {
    padding: 0.5rem 0.6rem;
    border-bottom: 1px solid #eee;
}

.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 0.5rem;
}

.detail-grid dt {
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    color: var(--text-muted);
}

.detail-grid dd {
    margin-bottom: 0.3rem;
}

.ref-link {
    color: var(--info);
    text-decoration: none;
    font-size: 0.75rem;
}
.ref-link:hover {
    text-decoration: underline;
}

/* === Budget Overview === */
#budget-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

#budget-table th, #budget-table td {
    padding: 0.5rem 0.75rem;
    text-align: right;
    border-bottom: 1px solid var(--border);
    font-size: 0.85rem;
}

#budget-table th:first-child,
#budget-table td:first-child {
    text-align: left;
}

#budget-table th {
    background: #f0f0ea;
    font-weight: 600;
}

#budget-table tfoot td {
    font-weight: 700;
    border-top: 2px solid var(--accent);
    font-size: 0.9rem;
}

#budget-summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.summary-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    text-align: center;
}

.summary-card .label {
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.summary-card .value {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--accent);
}

.summary-card .sub {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Importance breakdown */
#importance-breakdown {
    margin-top: 2rem;
}

.importance-bar {
    display: flex;
    height: 28px;
    border-radius: var(--radius);
    overflow: hidden;
    margin: 0.5rem 0 1rem;
}

.importance-bar .segment {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 600;
    color: #fff;
    min-width: 2%;
}

.importance-bar .segment.necessary { background: #28a745; }
.importance-bar .segment.important { background: #17a2b8; }
.importance-bar .segment.useful { background: #ffc107; color: #333; }
.importance-bar .segment.optional { background: #6c757d; }
.importance-bar .segment.possible { background: #dc3545; }

/* Category bar chart */
.cat-bar-container {
    margin-top: 1.5rem;
}

.cat-bar-row {
    display: flex;
    align-items: center;
    margin-bottom: 0.3rem;
}

.cat-bar-label {
    width: 160px;
    font-size: 0.78rem;
    text-align: right;
    padding-right: 0.75rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.cat-bar {
    height: 20px;
    background: var(--accent);
    border-radius: 2px;
    position: relative;
    min-width: 2px;
    transition: width 0.3s;
}

.cat-bar-value {
    font-size: 0.72rem;
    margin-left: 0.5rem;
    color: var(--text-muted);
    white-space: nowrap;
}

/* === Scenario Tags === */
.tag-badge {
    display: inline-block;
    padding: 0.05rem 0.35rem;
    border-radius: 3px;
    font-size: 0.62rem;
    font-weight: 600;
    color: #fff;
    margin: 0 0.15rem;
    vertical-align: middle;
    letter-spacing: 0.02em;
}

.tag-filters {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
    flex-wrap: wrap;
}

.tag-filter-label {
    font-size: 0.78rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.tag-filter-btns {
    display: flex;
    flex-wrap: wrap;
    gap: 3px;
}

.tag-filter-btn {
    background: #e8e8e0;
    border: 2px solid transparent;
    padding: 0.15rem 0.5rem;
    font-size: 0.68rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: 3px;
    color: var(--text-muted);
    transition: all 0.15s;
}

.tag-filter-btn:hover {
    border-color: var(--tag-color, #888);
    color: var(--text);
}

.tag-filter-btn.active {
    background: var(--tag-color, #888);
    color: #fff;
    border-color: var(--tag-color, #888);
}

.active-filter-note {
    background: #fff8e1;
    border: 1px solid #ffe082;
    border-radius: var(--radius);
    padding: 0.5rem 1rem;
    font-size: 0.82rem;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.clear-filter-btn {
    background: none;
    border: 1px solid var(--border);
    padding: 0.15rem 0.5rem;
    font-size: 0.72rem;
    cursor: pointer;
    border-radius: 3px;
    margin-left: 0.5rem;
}
.clear-filter-btn:hover {
    background: #f0f0ea;
}

/* === Footer === */
footer {
    text-align: center;
    padding: 1.5rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    border-top: 1px solid var(--border);
    margin-top: 2rem;
}

/* === Responsive === */
@media (max-width: 900px) {
    #sidebar {
        display: none;
    }
    .layout {
        flex-direction: column;
    }
    #tab-nav {
        padding: 0.3rem;
    }
    .tab-btn {
        font-size: 0.72rem;
        padding: 0.3rem 0.5rem;
    }
    .controls {
        flex-direction: column;
        align-items: flex-start;
    }
    .cost-summary {
        margin-left: 0;
    }
}

/* === Print === */
@media print {
    header {
        position: static;
        border-bottom: 2px solid #000;
    }
    #tab-nav, #sidebar, .controls input[type="range"], footer, .tag-filters {
        display: none;
    }
    .tab-panel {
        display: block !important;
        page-break-before: always;
    }
    .tab-panel:first-child {
        page-break-before: avoid;
    }
    .subcategory {
        page-break-inside: avoid;
    }
    .subcategory.collapsed .subcategory-body {
        display: block !important;
    }
    body {
        font-size: 10pt;
        background: #fff;
    }
    .item-table th {
        background: #eee !important;
    }
    .badge {
        border: 1px solid #999;
    }
}
