/* ============================================================
   USDA Summary Browser — "Modern Agrarian" Design System
   ============================================================ */

:root {
    /* Core palette */
    --green-900: #0f2318;
    --green-800: #1a3a2a;
    --green-700: #245238;
    --green-600: #2d6847;
    --green-500: #3a8259;
    --green-400: #4a9e6e;
    --green-100: #dceee4;

    /* Warm neutrals */
    --cream: #faf8f4;
    --cream-dark: #f2efe8;
    --sand: #e8e4db;
    --stone: #d4cfc5;
    --slate-100: #f1f0ee;
    --slate-200: #e2e0db;
    --slate-400: #9b9689;
    --slate-500: #736e63;
    --slate-600: #5a564d;
    --slate-700: #3d3a33;
    --slate-800: #2a2722;
    --slate-900: #1c1a17;

    /* Accents */
    --gold: #b8860b;
    --gold-light: #d4a837;
    --gold-muted: #c9a94f33;
    --rust: #b44d2d;
    --blue-link: #2563a8;

    /* Functional */
    --radius-sm: 0.25rem;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;
    --shadow-sm: 0 1px 3px rgba(28, 26, 23, 0.06);
    --shadow-md: 0 4px 12px rgba(28, 26, 23, 0.08);
    --shadow-lg: 0 8px 24px rgba(28, 26, 23, 0.12);

    /* Typography */
    --font-display: 'DM Serif Display', Georgia, serif;
    --font-body: 'IBM Plex Sans', -apple-system, sans-serif;
    --font-mono: 'IBM Plex Mono', 'Consolas', monospace;
}

/* ============================================================
   Base
   ============================================================ */

html, body {
    font-family: var(--font-body);
    font-size: 14px;
    color: var(--slate-800);
    background: var(--cream);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

a, .btn-link {
    color: var(--blue-link);
    text-decoration-color: transparent;
    transition: text-decoration-color 0.2s;
}

a:hover, .btn-link:hover {
    text-decoration-color: var(--blue-link);
}

.btn-primary {
    color: #fff;
    background-color: var(--green-600);
    border-color: var(--green-700);
    font-weight: 500;
}

.btn-primary:hover {
    background-color: var(--green-700);
    border-color: var(--green-800);
}

.btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
    box-shadow: 0 0 0 0.1rem var(--cream), 0 0 0 0.25rem var(--green-400);
}

.content {
    padding-top: 1.25rem;
}

h1, h2, h3 {
    font-family: var(--font-display);
    color: var(--slate-900);
}

h1:focus { outline: none; }

.valid.modified:not([type=checkbox]) { outline: 1px solid var(--green-500); }
.invalid { outline: 1px solid var(--rust); }
.validation-message { color: var(--rust); }

.blazor-error-boundary {
    background: var(--rust);
    padding: 1rem 1rem 1rem 1.5rem;
    color: white;
    border-radius: var(--radius-md);
}

.blazor-error-boundary::after { content: "An error has occurred."; }
.darker-border-checkbox.form-check-input { border-color: var(--slate-400); }

/* ============================================================
   Page header
   ============================================================ */

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--sand);
}

.page-header h1 {
    font-size: 1.6rem;
    margin: 0;
    letter-spacing: -0.01em;
}

.record-count {
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--slate-500);
    font-variant-numeric: tabular-nums;
    background: var(--cream-dark);
    padding: 0.2rem 0.6rem;
    border-radius: var(--radius-sm);
}

/* ============================================================
   Filter panel
   ============================================================ */

.filter-panel {
    background: var(--cream-dark);
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    padding: 0.6rem 0.75rem;
    margin-bottom: 0.85rem;
    position: relative;
    z-index: 10;
}

.filter-panel .row {
    row-gap: 0.35rem;
    flex-wrap: wrap;
}

.filter-panel .row > [class*="col-"] {
    flex: 0 0 auto;
    width: auto;
    min-width: 120px;
    max-width: 160px;
}

.filter-panel .row > .d-flex {
    min-width: 100px;
    max-width: 100px;
}

.filter-panel select, .filter-panel input {
    font-size: 0.82rem;
    font-family: var(--font-body);
    background: white;
    border-color: var(--stone);
    color: var(--slate-800);
}

.filter-panel select:focus, .filter-panel input:focus {
    border-color: var(--green-500);
    box-shadow: 0 0 0 0.15rem rgba(58, 130, 89, 0.2);
}

.filter-panel label {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--slate-500);
    margin-bottom: 0.2rem;
}

/* ============================================================
   Stats cards (Dashboard)
   ============================================================ */

.stat-card {
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    padding: 1.25rem;
    text-align: center;
    transition: box-shadow 0.2s, transform 0.2s;
    position: relative;
    overflow: hidden;
}

.stat-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--green-600), var(--gold-light));
}

.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}

.stat-card h3 {
    font-family: var(--font-body);
    font-size: 1.7rem;
    font-weight: 700;
    margin: 0;
    color: var(--green-800);
    font-variant-numeric: tabular-nums;
}

.stat-card p {
    margin: 0.3rem 0 0;
    color: var(--slate-500);
    font-size: 0.8rem;
    font-weight: 500;
    letter-spacing: 0.02em;
}

/* ============================================================
   Grid container
   ============================================================ */

.grid-container {
    position: relative;
    overflow-x: auto;
    overflow-y: auto;
    min-height: 400px;
    max-height: calc(100vh - 300px);
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    background: white;
    box-shadow: var(--shadow-sm);
}

.grid-loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.7);
    z-index: 10;
    border-radius: var(--radius-md);
}

/* ============================================================
   QuickGrid styling
   ============================================================ */

.quickgrid {
    width: 100%;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

.quickgrid th {
    background: var(--slate-100);
    position: sticky;
    top: 0;
    z-index: 1;
    white-space: nowrap;
    padding: 0.5rem 0.65rem;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--slate-600);
    border-bottom: 2px solid var(--sand);
}

.quickgrid th:hover {
    color: var(--green-700);
    background: var(--slate-200);
}

.quickgrid td {
    padding: 0.35rem 0.65rem;
    white-space: nowrap;
    border-bottom: 1px solid var(--slate-100);
    color: var(--slate-700);
}

.quickgrid tr:hover td {
    background: var(--green-100);
}

.quickgrid .col-numeric {
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-family: var(--font-mono);
    font-size: 0.78rem;
}

/* ============================================================
   Column filter inputs (header popups)
   ============================================================ */

.quickgrid .col-options-button {
    cursor: pointer;
}

.quickgrid .col-options {
    z-index: 100;
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 0.5rem;
    min-width: 180px;
}

.column-filter {
    width: 100%;
    padding: 0.3rem 0.45rem;
    font-size: 0.8rem;
    font-family: var(--font-body);
    border: 1px solid var(--stone);
    border-radius: var(--radius-sm);
    outline: none;
    background: white;
}

.column-filter:focus {
    border-color: var(--green-500);
    box-shadow: 0 0 0 0.15rem rgba(58, 130, 89, 0.2);
}

/* ============================================================
   Analytics toolbar
   ============================================================ */

.data-toolbar {
    display: flex;
    gap: 0.45rem;
    align-items: center;
    padding: 0.45rem 0.7rem;
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    margin-bottom: 0.85rem;
    flex-wrap: wrap;
    box-shadow: var(--shadow-sm);
}

.data-toolbar .btn-toolbar {
    font-size: 0.78rem;
    font-weight: 500;
    padding: 0.3rem 0.65rem;
    border: 1px solid var(--stone);
    background: var(--cream);
    border-radius: var(--radius-sm);
    cursor: pointer;
    white-space: nowrap;
    color: var(--slate-700);
    transition: all 0.15s;
}

.data-toolbar .btn-toolbar:hover {
    background: var(--sand);
    border-color: var(--slate-400);
}

.data-toolbar .btn-toolbar.active {
    background: var(--green-700);
    color: white;
    border-color: var(--green-800);
}

/* Dropdown panels */
.group-by-panel, .columns-panel {
    position: relative;
    display: inline-block;
}

.group-by-dropdown, .columns-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 10;
    min-width: 210px;
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 0.5rem;
    margin-top: 0.3rem;
}

.columns-dropdown {
    left: auto;
    right: 0;
    max-height: 300px;
    overflow-y: auto;
}

.group-by-dropdown label, .columns-dropdown label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.3rem 0.5rem;
    font-size: 0.8rem;
    cursor: pointer;
    border-radius: var(--radius-sm);
    color: var(--slate-700);
}

.group-by-dropdown label:hover, .columns-dropdown label:hover {
    background: var(--green-100);
}

/* Sort By */
.sort-by-panel {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}

.sort-by-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 10;
    min-width: 280px;
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    padding: 0.5rem;
    margin-top: 0.3rem;
}

.sort-add-row {
    display: flex;
    gap: 0.35rem;
    align-items: center;
}

.sort-add-row select {
    font-size: 0.8rem;
    padding: 0.2rem 0.4rem;
    border: 1px solid var(--stone);
    border-radius: var(--radius-sm);
    font-family: var(--font-body);
}

.sort-pills {
    display: inline-flex;
    gap: 0.3rem;
    align-items: center;
    flex-wrap: wrap;
}

.sort-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.75rem;
    font-weight: 500;
    background: var(--green-100);
    color: var(--green-800);
    border-radius: var(--radius-sm);
    padding: 0.15rem 0.5rem;
    white-space: nowrap;
}

.sort-pill-remove {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.7rem;
    color: var(--slate-400);
    padding: 0 0.15rem;
    line-height: 1;
}

.sort-pill-remove:hover { color: var(--rust); }

/* ============================================================
   Formula bar
   ============================================================ */

.formula-bar {
    background: var(--cream-dark);
    border: 1px solid var(--sand);
    border-radius: var(--radius-md);
    padding: 0.75rem;
    margin-bottom: 0.85rem;
}

.formula-bar .formula-inputs {
    display: flex;
    gap: 0.5rem;
    align-items: end;
}

.formula-bar .formula-inputs input {
    font-size: 0.82rem;
    padding: 0.3rem 0.5rem;
    font-family: var(--font-mono);
    border-color: var(--stone);
}

.formula-bar .formula-inputs .col-name { flex: 0 0 180px; font-family: var(--font-body); }
.formula-bar .formula-inputs .col-formula { flex: 1; }
.formula-bar .formula-error { color: var(--rust); font-size: 0.78rem; margin-top: 0.25rem; }

.formula-bar .active-formulas {
    margin-top: 0.5rem;
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.formula-bar .formula-tag {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.2rem 0.5rem;
    background: var(--gold-muted);
    border: 1px solid rgba(201, 169, 79, 0.3);
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    color: var(--slate-800);
}

.formula-bar .formula-tag strong {
    color: var(--gold);
}

.formula-bar .formula-tag button {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--slate-400);
    padding: 0;
    line-height: 1;
}

.formula-bar .formula-tag button:hover { color: var(--rust); }

.formula-bar .column-ref-guide {
    margin-top: 0.5rem;
    font-size: 0.78rem;
    color: var(--slate-500);
}

.formula-bar .column-ref-guide .column-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    margin-top: 0.25rem;
}

.formula-bar .column-ref-guide .column-list span {
    padding: 0.15rem 0.4rem;
    background: white;
    border: 1px solid var(--sand);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--slate-600);
}

/* ============================================================
   Groupable grid
   ============================================================ */

.groupable-grid {
    width: 100%;
    font-size: 0.8rem;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

.groupable-grid th {
    background: var(--slate-100);
    position: sticky;
    top: 0;
    z-index: 1;
    white-space: nowrap;
    padding: 0.5rem 0.65rem;
    cursor: pointer;
    border-bottom: 2px solid var(--sand);
    text-align: left;
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--slate-600);
}

.groupable-grid th.col-numeric { text-align: right; }
.groupable-grid th:hover { background: var(--slate-200); color: var(--green-700); }

.groupable-grid td {
    padding: 0.35rem 0.65rem;
    white-space: nowrap;
    border-bottom: 1px solid var(--slate-100);
    color: var(--slate-700);
}

.groupable-grid tr:hover td { background: var(--green-100); }

.groupable-grid .col-numeric {
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-family: var(--font-mono);
    font-size: 0.78rem;
}

/* Group rows */
.groupable-grid tr.group-row {
    background: var(--cream-dark);
    font-weight: 600;
    cursor: pointer;
}

.groupable-grid tr.group-row:hover td { background: var(--sand); }
.groupable-grid tr.group-row td {
    padding: 0.5rem 0.65rem;
    border-bottom: 1px solid var(--stone);
    color: var(--slate-800);
}

.groupable-grid .group-chevron {
    display: inline-block;
    width: 1rem;
    text-align: center;
    transition: transform 0.15s;
    margin-right: 0.3rem;
    color: var(--green-600);
}

.groupable-grid .group-chevron.expanded { transform: rotate(90deg); }
.groupable-grid .group-indent-1 td:first-child { padding-left: 1.6rem; }
.groupable-grid .group-indent-2 td:first-child { padding-left: 3.2rem; }

.groupable-grid .child-row-badge {
    display: inline-block;
    padding: 0.1rem 0.45rem;
    background: var(--green-700);
    color: white;
    border-radius: 1rem;
    font-size: 0.68rem;
    font-weight: 500;
    margin-left: 0.5rem;
}

.groupable-grid .detail-indent-1 td:first-child { padding-left: 2rem; }
.groupable-grid .detail-indent-2 td:first-child { padding-left: 3.6rem; }

.groupable-grid .load-more-row td { text-align: center; padding: 0.6rem; }
.groupable-grid .load-more-row a {
    color: var(--green-600);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
}

/* Footer totals row */
.groupable-grid tfoot tr {
    background: var(--cream-dark);
    color: var(--slate-900);
    font-weight: 700;
}

.groupable-grid tfoot td {
    padding: 0.6rem 0.65rem;
    border-top: 3px double var(--green-600);
    border-bottom: none;
    font-family: var(--font-mono);
    font-size: 0.82rem;
}

/* Calculated column header */
.groupable-grid th.calc-column {
    background: var(--gold-muted);
    border-bottom-color: var(--gold);
    color: var(--gold);
}

.groupable-grid th.calc-column .remove-calc {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--slate-400);
    font-size: 0.8rem;
    margin-left: 0.3rem;
    padding: 0;
}

.groupable-grid th.calc-column .remove-calc:hover { color: var(--rust); }

/* ============================================================
   Multi-select dropdown
   ============================================================ */

.multi-select-panel {
    position: relative;
    z-index: 1;
}

.multi-select-btn {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    width: 100%;
    padding: 0.25rem 0.5rem;
    font-size: 0.82rem;
    font-family: var(--font-body);
    text-align: left;
    background: white;
    border: 1px solid var(--stone);
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--slate-600);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: border-color 0.15s, background 0.15s;
}

.multi-select-btn:hover {
    border-color: var(--slate-400);
    background: var(--cream-dark);
}

.multi-select-btn.active {
    border-color: var(--green-500);
    background: var(--green-100);
    color: var(--green-800);
    font-weight: 500;
}

.filter-indicator {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--stone);
    flex-shrink: 0;
    transition: background 0.15s, box-shadow 0.15s;
}

.multi-select-btn.active .filter-indicator {
    background: var(--green-500);
    box-shadow: 0 0 4px var(--green-400);
}

.multi-select-panel .group-by-dropdown {
    max-height: 280px;
    overflow-y: auto;
    z-index: 30;
}

/* ============================================================
   Pagination override
   ============================================================ */

.pagination .page-link {
    color: var(--green-700);
    border-color: var(--sand);
    font-size: 0.8rem;
    font-weight: 500;
}

.pagination .page-item.active .page-link {
    background-color: var(--green-700);
    border-color: var(--green-700);
    color: white;
}

.pagination .page-link:hover {
    background-color: var(--green-100);
    color: var(--green-800);
}

/* ============================================================
   Totals summary bar (flat view)
   ============================================================ */

.totals-bar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0.6rem 1rem;
    margin-top: 0.5rem;
    background: var(--cream-dark);
    border: 1px solid var(--sand);
    border-top: 3px double var(--green-600);
    border-radius: var(--radius-md);
    font-family: var(--font-mono);
    font-size: 0.82rem;
    font-variant-numeric: tabular-nums;
    color: var(--slate-800);
    flex-wrap: wrap;
}

.totals-bar .totals-label {
    font-family: var(--font-body);
    font-weight: 700;
    color: var(--slate-900);
    margin-right: auto;
}

.totals-bar .totals-item {
    font-weight: 600;
    color: var(--green-800);
}

.totals-bar .totals-key {
    font-weight: 400;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--slate-500);
    margin-right: 0.3rem;
}

/* ============================================================
   Selection & scrollbar styling
   ============================================================ */

::selection {
    background: var(--green-400);
    color: white;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--cream); }
::-webkit-scrollbar-thumb { background: var(--stone); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--slate-400); }

/* ============================================================
   Sidebar collapse (global — targets #sidebar from MainLayout)
   ============================================================ */

#sidebar {
    transition: width 0.2s ease;
    overflow: hidden;
}

#sidebar .brand-short { display: none; }
#sidebar .brand-full { display: inline; }
#sidebar .chevron-expand { display: none; }
#sidebar .chevron-collapse { display: inline; }

#sidebar.collapsed { width: 64px !important; }
#sidebar.collapsed .brand-full { display: none; }
#sidebar.collapsed .brand-short { display: inline; }
#sidebar.collapsed .nav-label { display: none; }
#sidebar.collapsed .chevron-collapse { display: none; }
#sidebar.collapsed .chevron-expand { display: inline; }
#sidebar.collapsed .nav-item { padding-left: 0.5rem !important; padding-right: 0.5rem !important; }
#sidebar.collapsed .nav-icon { margin-right: 0; }
