/* AG Grid Compact Scale */
[class^="ag-theme-"] {
    --ag-font-size: 12px;
    --ag-row-height: 32px;
    --ag-header-height: 36px;
    --ag-cell-horizontal-padding: 8px;
}

.ag-cell-error {
    background-color: #FFC7CE !important;
    color: #9C0006 !important;
}

/* Editable Cells */
.ag-theme-quartz .ag-cell.ag-cell-editable {
    background-color: rgba(59, 130, 246, 0.03);
    border: 1px dashed transparent;
    transition: border-color 0.2s;
}

.ag-theme-quartz .ag-cell.ag-cell-editable:hover {
    border-color: #93c5fd;
    cursor: text;
}

/* Empty Append Row */
.ag-theme-quartz .empty-append-row {
    background-color: #f8fafc !important;
}

.ag-theme-quartz .empty-append-row .ag-cell {
    border-top: 2px dashed #cbd5e1 !important;
    border-bottom: 2px dashed #cbd5e1 !important;
    opacity: 0.7;
}

/* Detail Drawer */
.detail-drawer {
    width: 0;
    background: #f8fafc;
    border-left: 1px solid transparent;
    transition: width 0.3s ease, border-color 0.3s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
    position: relative;
}
.detail-drawer.open {
    width: 500px;
    border-left-color: #cbd5e1;
}
.detail-drawer.is-resizing {
    transition: none;
}
.resize-handle {
    position: absolute;
    top: 0;
    left: 0;
    width: 4px;
    height: 100%;
    cursor: col-resize;
    background: transparent;
    z-index: 10;
    transition: background 0.2s;
}
.resize-handle:hover, .detail-drawer.is-resizing .resize-handle {
    background: #3b82f6;
}

/* Layout: every level flexes into the height the page actually has —
   a fixed height here disagrees with the real header/statusbar height
   and clips the grid (and the pager below it) mid-row. */
.tab-layout {
    display: flex;
    flex: 1;
    min-height: 0;
    width: 100%;
    overflow: hidden;
}
.sidebar-container {
    width: 300px;
    background: #f8fafc;
    border-right: 1px solid #cbd5e1;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    overflow-y: auto;
    transition: width 0.3s ease;
}
.sidebar-container.collapsed {
    display: none;
}
.grid-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    padding: 1rem;
    overflow-y: auto;
}
.grid-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}
.toolbar-left, .toolbar-right {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.toggle-sidebar-btn {
    background: #f1f5f9;
    color: #334155;
    border: 1px solid #cbd5e1;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.btn-secondary {
    background: #f1f5f9;
    color: #334155;
    border: 1px solid #cbd5e1;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: all 0.2s;
}
.btn-secondary:hover {
    background: #e2e8f0;
    color: #0f172a;
}
.toggle-sidebar-btn:hover {
    background: #e2e8f0;
}
.view-item {
    padding: 0.4rem 0.5rem;
    border: 1px solid #cbd5e1;
    margin-bottom: 0.25rem;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    height: 34px;
    background: #ffffff;
}
.view-item:hover {
    background: #f8fafc;
}
.view-item.active {
    border-color: #3b82f6;
    background: #eff6ff;
}
.view-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 0.25rem;
}
.drag-handle {
    width: 14px;
    height: 14px;
    color: #cbd5e1;
    cursor: grab;
    flex-shrink: 0;
}
/* Marks a view as the system's: everyone sees it, nobody renames or
   deletes it, and saving on it keeps the change as a copy. */
.view-shared-icon {
    width: 13px;
    height: 13px;
    color: #94a3b8;
    flex-shrink: 0;
}
.view-item.shared .view-title {
    cursor: default;
}
.view-title {
    flex-grow: 1;
    outline: none;
    border-bottom: 1px solid transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.85rem;
    font-weight: 500;
    color: #334155;
}
.view-title[contenteditable="true"]:focus {
    border-bottom-color: #3b82f6;
    background: #fff;
}
.row-badge {
    background: #3b82f6;
    color: #ffffff;
    padding: 0.1rem 0.4rem;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: 600;
    flex-shrink: 0;
}
.row-badge.inactive {
    background: #e2e8f0;
    color: #64748b;
}
.view-actions {
    display: none;
    gap: 0.1rem;
    margin-right: 0.5rem;
}
/* Show the action buttons on hover */
.view-item:hover .view-actions {
    display: flex;
}
.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.1rem 0.2rem;
    border-radius: 3px;
    color: #64748b;
    display: flex;
    align-items: center;
    justify-content: center;
}
.icon-btn:hover {
    background: #e2e8f0;
    color: #0f172a;
}
.icon-btn.delete:hover {
    color: #ef4444;
    background: #fee2e2;
}
.save-indicator {
    font-size: 0.75rem;
    color: #10b981;
    opacity: 0;
    transition: opacity 0.3s;
    font-weight: bold;
}
.save-indicator.show {
    opacity: 1;
}
.view-item[draggable="true"] {
    cursor: grab;
}
.view-item[draggable="true"]:active {
    cursor: grabbing;
}
.view-item.dragging {
    opacity: 0.5;
    border: 2px dashed #3b82f6;
    background: #f8fafc;
}

/* Drag and Drop Upload */
.grid-dropzone {
    position: relative;
    /* The grid takes whatever height the column has left; its own
       viewport scrolls the rows. The floor keeps a usable grid on very
       short windows (the surrounding .grid-content then scrolls). */
    flex: 1 1 auto;
    min-height: 240px;
}
.grid-dropzone-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(59, 130, 246, 0.1);
    border: 2px dashed #3b82f6;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: #3b82f6;
    font-weight: bold;
    z-index: 10;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    border-radius: 4px;
}
.grid-dropzone.drag-over .grid-dropzone-overlay {
    opacity: 1;
    pointer-events: auto;
}

/* --- Master-detail (expandable group rows) ---
   AG Grid's native master-detail is Enterprise-only, so the hierarchy is
   drawn onto ordinary rows: an expander column, an indented child row,
   and a tint that separates a group's lines from the next group. */
.cell-expander {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
}
.tree-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    cursor: pointer;
    user-select: none;
    padding: 0 0.35rem;
    border-radius: 3px;
    color: #475569;
    line-height: 1.6;
}
.tree-toggle:hover {
    background: #e2e8f0;
    color: #0f172a;
}
.tree-chevron {
    font-size: 0.85rem;
    width: 0.7rem;
    display: inline-block;
    text-align: center;
}
.tree-count {
    font-size: 0.7rem;
    font-variant-numeric: tabular-nums;
    background: #e2e8f0;
    border-radius: 8px;
    padding: 0 0.3rem;
    min-width: 1.1rem;
    text-align: center;
}
.tree-toggle.open .tree-count {
    background: #cbd5e1;
}
.tree-child-guide {
    display: inline-block;
    width: 100%;
    border-left: 2px solid #cbd5e1;
    height: 100%;
    margin-left: 45%;
}

/* Child rows sit visually inside their parent. */
.ag-row.row-detail {
    background: #f8fafc;
    font-size: 0.92em;
}
.ag-row.row-detail .ag-cell {
    color: #475569;
}
.ag-row.row-group-parent {
    font-weight: 600;
}

.md-toolbar {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}
.md-count {
    font-size: 0.8rem;
    color: #64748b;
    font-variant-numeric: tabular-nums;
}

/* --- Provenance and editability, as cell styling ---
   Replaces what the "Legacy Info" super-header used to imply. Groups only
   span adjacent columns, so they fracture into repeated spans as soon as
   a view interleaves them; a per-cell cue survives any column order. */
.ag-cell.cell-legacy {
    background: #fbfaf7;
    color: #44403c;
}
.ag-cell.cell-editable {
    background: #ffffff;
    box-shadow: inset 2px 0 0 #93c5fd;
}
.ag-row.row-detail .ag-cell.cell-legacy {
    background: #f6f5f1;
}

.grid-legend {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.35rem 0.1rem 0.6rem;
    font-size: 0.75rem;
    color: #64748b;
    flex-wrap: wrap;
}
.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}
.legend-swatch {
    width: 0.75rem;
    height: 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 2px;
    display: inline-block;
}
.swatch-legacy { background: #fbfaf7; }
.swatch-editable { background: #ffffff; box-shadow: inset 2px 0 0 #93c5fd; }
.swatch-detail { background: #f8fafc; }
.legend-hint {
    margin-left: auto;
    font-style: italic;
}

/* --- Record inspector (the drawer's new job) --- */
.record-inspector {
    margin: 0;
    padding: 1rem;
    display: grid;
    grid-template-columns: minmax(8rem, 40%) 1fr;
    gap: 0.35rem 1rem;
    overflow-y: auto;
    font-size: 0.85rem;
}
.record-inspector dt {
    color: #64748b;
    font-weight: 500;
}
.record-inspector dd {
    margin: 0;
    color: #0f172a;
    overflow-wrap: anywhere;
}
.record-inspector-empty {
    padding: 1rem;
    color: #64748b;
}

/* --- Record inspector header ---
   The close control is a labelled button, not a bare glyph: the pane
   covers half the screen, so the way out has to be obvious. */
.drawer-header {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #cbd5e1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    background: #f8fafc;
}
.drawer-header h2 {
    margin: 0;
    font-size: 1.1rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.drawer-close-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    flex-shrink: 0;
    background: #ffffff;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    padding: 0.35rem 0.7rem;
    font-size: 0.85rem;
    color: #334155;
    cursor: pointer;
    line-height: 1.2;
}
.drawer-close-btn:hover {
    background: #e2e8f0;
    border-color: #94a3b8;
    color: #0f172a;
}
.drawer-close-btn:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 1px;
}
.drawer-close-glyph {
    font-size: 1.1rem;
    line-height: 1;
}

/* --- Unsupported filter/sort notice (server could not push it down) --- */
.grid-unsupported-banner {
    background: #fffbeb;
    border: 1px solid #fcd34d;
    color: #92400e;
    border-radius: 4px;
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
}

/* --- Pager: the grid holds a page, the server holds the population --- */
.grid-pager {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 0.5rem;
    font-size: 0.85rem;
    color: #374151;
    flex-shrink: 0; /* never sacrificed to the grid above it */
}
.grid-pager .pager-status {
    flex: 1;
    text-align: center;
}
.grid-pager button[disabled] {
    opacity: 0.45;
    cursor: default;
}

/* --- View settings modal ---
   Markup is built in views.js (openViewSettings); every visual rule
   lives here, per the styling pattern — no inline styles or <style>
   blocks travelling with the HTML string. */
.modal-backdrop {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
}
.modal-content {
    background: white;
    padding: 20px;
    border-radius: 8px;
    width: 700px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}
.modal-header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 15px;
}
.modal-title-row { display: flex; align-items: center; gap: 8px; }
.modal-view-title { margin: 0; padding-bottom: 2px; outline: none; }
.modal-view-title[contenteditable="true"] {
    background-color: #f8f9fa;
    border-bottom: 1px dashed #666;
    outline: 2px solid #007bff;
    border-radius: 3px;
    padding: 0 4px;
}
.modal-shared-tag {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    color: #64748b;
}
.modal-delete-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #dc3545;
}
/* Header and rows share one grid template so the columns line up. */
.col-list-header, .modal-col-item {
    display: grid;
    grid-template-columns: 30px 40px 1fr 80px 100px;
    gap: 10px;
    align-items: center;
}
.col-list-header {
    font-weight: bold;
    font-size: 0.85rem;
    color: #666;
    padding: 8px;
    border-bottom: 2px solid #eee;
    margin-bottom: 8px;
}
.modal-col-item {
    padding: 6px 8px;
    background: white;
    border: 1px solid #ddd;
    margin-bottom: 4px;
    border-radius: 4px;
    cursor: grab;
}
#modalColumnList { overflow-y: auto; flex: 1; padding-right: 5px; }
.modal-col-grip {
    width: 16px;
    height: 16px;
    color: #ccc;
    pointer-events: none;
    justify-self: center;
}
.modal-col-check { cursor: pointer; justify-self: center; }
.modal-col-name {
    pointer-events: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.modal-col-na { color: #ccc; display: block; }
.col-centered { text-align: center; justify-self: center; }
.agg-method-select {
    width: 100%;
    font-size: 0.8rem;
    padding: 2px;
    cursor: pointer;
}
.modal-footer {
    margin-top: 15px;
    font-size: 0.8rem;
    color: #666;
}
