/* =====================================================
   DATA GRID — High-Density Table System
   Use for: large datasets (boxes, loans, reports,
   inventory lists, audit logs). Replaces card-based
   layouts when row counts exceed ~50.
   ===================================================== */

/* ── Density scale ──────────────────────────────────── */
:root {
    --grid-row-py: 5px;
    --grid-row-px: 10px;
    --grid-font:   12.5px;
    --grid-lh:     1.35;
    --grid-th-py:  6px;
    --grid-th-px:  10px;
    --grid-th-font: 11px;
    --grid-badge-py: 2px;
    --grid-badge-px: 6px;
    --grid-badge-font: 10.5px;
    --grid-action-size: 24px;
}

/* Comfortable density (default for medium lists ~50–200 rows) */
.grid-density-comfortable {
    --grid-row-py: 8px;
    --grid-row-px: 12px;
    --grid-font:   13px;
    --grid-lh:     1.4;
    --grid-th-py:  8px;
    --grid-th-px:  12px;
    --grid-th-font: 11.5px;
    --grid-badge-py: 3px;
    --grid-badge-px: 7px;
    --grid-badge-font: 11px;
    --grid-action-size: 26px;
}

/* ── Wrapper ────────────────────────────────────────── */
.data-grid-wrap {
    display: flex;
    flex-direction: column;
    gap: 0;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 2px var(--shadow);
}

/* ── Toolbar ────────────────────────────────────────── */
.grid-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.grid-toolbar-left  { display: flex; align-items: center; gap: 6px; flex: 1; flex-wrap: wrap; }
.grid-toolbar-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

/* Inline search */
.grid-search {
    position: relative;
    min-width: 180px;
    max-width: 260px;
}
.grid-search input {
    width: 100%;
    height: 28px;
    padding: 0 8px 0 28px;
    font-size: 12px;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    background: var(--bg-primary);
    color: var(--text-primary);
    outline: none;
    transition: border-color .15s;
}
.grid-search input:focus { border-color: var(--accent-primary); }
.grid-search .grid-search-icon {
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 11px;
    color: var(--text-secondary);
    pointer-events: none;
}

/* Quick filter chips */
.grid-filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    font-size: 11.5px;
    font-weight: 500;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-primary);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all .15s;
    white-space: nowrap;
    user-select: none;
}
.grid-filter-chip:hover        { border-color: var(--accent-primary); color: var(--accent-primary); }
.grid-filter-chip.active       { background: var(--accent-primary); border-color: var(--accent-primary); color: #fff; }
.grid-filter-chip .chip-count  { font-size: 10px; opacity: .75; }

/* Toolbar icon buttons */
.grid-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    height: 28px;
    padding: 0 8px;
    font-size: 12px;
    font-weight: 500;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    background: var(--bg-primary);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all .15s;
    white-space: nowrap;
    text-decoration: none;
}
.grid-btn:hover         { border-color: var(--accent-primary); color: var(--accent-primary); background: var(--bg-primary); }
.grid-btn.grid-btn-primary { background: var(--accent-primary); border-color: var(--accent-primary); color: #fff; }
.grid-btn.grid-btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: #fff; }
.grid-btn.grid-btn-danger  { color: var(--status-danger); border-color: var(--status-danger); }
.grid-btn.grid-btn-danger:hover { background: var(--status-danger); color: #fff; }
.grid-btn i { font-size: 11px; }

.grid-divider { width: 1px; height: 18px; background: var(--border-color); margin: 0 2px; }

/* Row count label */
.grid-count {
    font-size: 11.5px;
    color: var(--text-secondary);
    white-space: nowrap;
}
.grid-count strong { color: var(--text-primary); }

/* Density toggle */
.grid-density-toggle {
    display: inline-flex;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    overflow: hidden;
}
.grid-density-toggle button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border: none;
    background: var(--bg-primary);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 12px;
    transition: all .15s;
    border-right: 1px solid var(--border-color);
}
.grid-density-toggle button:last-child { border-right: none; }
.grid-density-toggle button.active     { background: var(--accent-primary); color: #fff; }
.grid-density-toggle button:hover:not(.active) { background: var(--bg-secondary); color: var(--text-primary); }

/* ── Filter row (collapsible) ───────────────────────── */
.grid-filters {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
}
.grid-filters.collapsed { display: none; }

.grid-filter-group {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 120px;
}
.grid-filter-group label {
    font-size: 10.5px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .4px;
}
.grid-filter-group select,
.grid-filter-group input[type="text"],
.grid-filter-group input[type="date"] {
    height: 28px;
    padding: 0 8px;
    font-size: 12px;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    background: var(--bg-primary);
    color: var(--text-primary);
    outline: none;
    transition: border-color .15s;
}
.grid-filter-group select:focus,
.grid-filter-group input:focus { border-color: var(--accent-primary); }

.grid-filter-actions {
    display: flex;
    gap: 6px;
    align-items: flex-end;
    padding-bottom: 0;
    margin-left: auto;
}

/* ── Scroll container ───────────────────────────────── */
.grid-scroll {
    overflow-x: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: var(--grid-max-height, calc(100vh - 280px));
}

/* ── Table ──────────────────────────────────────────── */
.data-grid {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--grid-font);
    line-height: var(--grid-lh);
    color: var(--text-primary);
    table-layout: fixed;
}

/* Sticky header */
.data-grid thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: var(--grid-th-font);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .4px;
    padding: var(--grid-th-py) var(--grid-th-px);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
    user-select: none;
    text-align: left;
}

/* Sticky first column (ID / checkbox) */
.data-grid th.col-sticky,
.data-grid td.col-sticky {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--bg-primary);
}
.data-grid thead th.col-sticky { z-index: 4; background: var(--bg-secondary); }

/* Cells */
.data-grid tbody td {
    padding: var(--grid-row-py) var(--grid-row-px);
    border-bottom: 1px solid var(--border-light);
    vertical-align: middle;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.data-grid tbody td.wrap { white-space: normal; }

.data-grid tbody tr:last-child td { border-bottom: none; }

.data-grid tbody tr:hover td {
    background: var(--bg-secondary);
}
.data-grid tbody tr:hover td.col-sticky {
    background: var(--bg-secondary);
}

/* Zebra striping (optional, add .grid-striped to table) */
.data-grid.grid-striped tbody tr:nth-child(even) td {
    background: color-mix(in srgb, var(--bg-secondary) 50%, transparent);
}

/* Selected row */
.data-grid tbody tr.selected td {
    background: color-mix(in srgb, var(--accent-primary) 8%, var(--bg-primary));
}

/* ── Column types ───────────────────────────────────── */
.col-id    { width: 110px; font-family: 'Courier New', monospace; font-weight: 600; color: var(--accent-primary); }
.col-check { width: 36px; text-align: center; }
.col-status { width: 90px; }
.col-date  { width: 88px; color: var(--text-secondary); }
.col-num   { text-align: right; font-variant-numeric: tabular-nums; }
.col-mono  { font-family: 'Courier New', monospace; }
.col-actions { width: 72px; text-align: right; white-space: nowrap; }
.col-wide  { min-width: 200px; }
.col-narrow { width: 80px; }

/* Sortable header */
.data-grid th.sortable { cursor: pointer; }
.data-grid th.sortable:hover { color: var(--text-primary); }
.data-grid th.sortable::after { content: ' ↕'; opacity: .35; font-size: 9px; }
.data-grid th.sort-asc::after  { content: ' ↑'; opacity: 1; color: var(--accent-primary); }
.data-grid th.sort-desc::after { content: ' ↓'; opacity: 1; color: var(--accent-primary); }

/* ── Bulk select checkbox ───────────────────────────── */
.grid-check {
    width: 14px;
    height: 14px;
    cursor: pointer;
    accent-color: var(--accent-primary);
}

/* Bulk action bar (appears when rows selected) */
.grid-bulk-bar {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    background: color-mix(in srgb, var(--accent-primary) 10%, var(--bg-primary));
    border-bottom: 1px solid color-mix(in srgb, var(--accent-primary) 25%, var(--border-color));
    font-size: 12.5px;
    color: var(--text-primary);
}
.grid-bulk-bar.visible { display: flex; }
.grid-bulk-bar strong  { color: var(--accent-primary); }

/* ── Status badges (dense) ──────────────────────────── */
.grid-badge {
    display: inline-block;
    padding: var(--grid-badge-py) var(--grid-badge-px);
    font-size: var(--grid-badge-font);
    font-weight: 600;
    border-radius: 3px;
    white-space: nowrap;
    letter-spacing: .2px;
}
.grid-badge-success  { background: rgba(16,185,129,.12); color: #059669; }
.grid-badge-warning  { background: rgba(245,158,11,.12); color: #d97706; }
.grid-badge-danger   { background: rgba(239,68,68,.12);  color: #dc2626; }
.grid-badge-info     { background: rgba(59,130,246,.12); color: #2563eb; }
.grid-badge-neutral  { background: var(--bg-secondary);  color: var(--text-secondary); }
.grid-badge-purple   { background: rgba(139,92,246,.12); color: #7c3aed; }
.grid-badge-orange   { background: rgba(249,115,22,.12); color: #ea580c; }

/* Dot-style status (even denser) */
.grid-dot {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: var(--grid-badge-font);
    color: var(--text-secondary);
    white-space: nowrap;
}
.grid-dot::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}
.grid-dot.dot-success::before { background: #10b981; }
.grid-dot.dot-warning::before { background: #f59e0b; }
.grid-dot.dot-danger::before  { background: #ef4444; }
.grid-dot.dot-info::before    { background: #3b82f6; }
.grid-dot.dot-neutral::before { background: #94a3b8; }

/* ── Row action buttons ─────────────────────────────── */
.grid-row-actions {
    display: inline-flex;
    gap: 2px;
    align-items: center;
    justify-content: flex-end;
}
.grid-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--grid-action-size);
    height: var(--grid-action-size);
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 11px;
    cursor: pointer;
    text-decoration: none;
    transition: all .12s;
}
.grid-action-btn:hover           { background: var(--bg-secondary); border-color: var(--border-color); color: var(--text-primary); }
.grid-action-btn.action-primary  { color: var(--accent-primary); }
.grid-action-btn.action-primary:hover { background: rgba(59,130,246,.1); border-color: rgba(59,130,246,.3); }
.grid-action-btn.action-danger   { color: var(--status-danger); }
.grid-action-btn.action-danger:hover  { background: rgba(239,68,68,.1); border-color: rgba(239,68,68,.3); }
.grid-action-btn.action-success  { color: var(--status-success); }
.grid-action-btn.action-success:hover { background: rgba(16,185,129,.1); border-color: rgba(16,185,129,.3); }

/* ── Footer / Pagination ────────────────────────────── */
.grid-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 7px 12px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    font-size: 12px;
    color: var(--text-secondary);
    flex-wrap: wrap;
}

.grid-page-size {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
}
.grid-page-size select {
    height: 26px;
    padding: 0 6px;
    font-size: 12px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--bg-primary);
    color: var(--text-primary);
}

.grid-pagination {
    display: flex;
    align-items: center;
    gap: 2px;
}
.grid-page-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 26px;
    padding: 0 6px;
    font-size: 12px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--bg-primary);
    color: var(--text-secondary);
    cursor: pointer;
    text-decoration: none;
    transition: all .12s;
}
.grid-page-btn:hover          { border-color: var(--accent-primary); color: var(--accent-primary); }
.grid-page-btn.active         { background: var(--accent-primary); border-color: var(--accent-primary); color: #fff; }
.grid-page-btn[disabled]      { opacity: .4; pointer-events: none; }
.grid-page-btn.page-ellipsis  { border: none; pointer-events: none; color: var(--text-secondary); }

/* ── Empty & loading states ─────────────────────────── */
.grid-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}
.grid-empty i        { font-size: 32px; opacity: .3; margin-bottom: 10px; display: block; }
.grid-empty p        { font-size: 13px; margin: 0; }
.grid-empty .grid-empty-action { margin-top: 12px; }

.grid-loading tbody::after {
    content: '';
    display: block;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--accent-primary), transparent);
    animation: grid-shimmer 1.2s ease infinite;
}
@keyframes grid-shimmer {
    0%   { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

/* ── Compact page header variant ───────────────────────
   Use .page-header-sm on pages where the grid is the
   primary content and the header is just a label.
   ─────────────────────────────────────────────────── */
.page-header-sm {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}
.page-header-sm h1,
.page-header-sm .page-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}
.page-header-sm h1 i,
.page-header-sm .page-title i { font-size: 18px; color: var(--accent-primary); }
.page-header-sm .page-header-actions {
    margin-left: auto;
    display: flex;
    gap: 6px;
    align-items: center;
}

/* ── Column visibility dropdown ─────────────────────── */
.grid-col-toggle {
    position: relative;
}
.grid-col-menu {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 4px);
    min-width: 160px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    box-shadow: 0 4px 12px var(--shadow);
    padding: 6px 0;
    z-index: 100;
}
.grid-col-menu.open { display: block; }
.grid-col-menu label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 12px;
    font-size: 12px;
    color: var(--text-primary);
    cursor: pointer;
    transition: background .1s;
}
.grid-col-menu label:hover { background: var(--bg-secondary); }
.grid-col-menu input[type=checkbox] { accent-color: var(--accent-primary); }

/* ── Responsive collapse ────────────────────────────── */
@media (max-width: 768px) {
    .grid-toolbar         { gap: 6px; }
    .grid-search          { min-width: 140px; max-width: 100%; }
    .grid-filter-group    { min-width: 100px; }
    .grid-footer          { flex-direction: column; gap: 8px; align-items: flex-start; }
    .page-header-sm h1    { font-size: 17px; }
    .data-grid thead th,
    .data-grid tbody td   { font-size: 12px; }
}
