/* ==========================================================================
   SPREADSHEET VIEWS (grid/sheet.js): view tabs, toolbar menus, column
   resize and move, groups and totals, dropdown picker, record panel and the
   calculated-field editor. The table itself is styled in grid.css.
   ========================================================================== */

/* Saved-view tabs: the same rounded chip-track look as .seg-tabs (chain-section.js),
   so every tab/segmented switcher in the app reads as one family. */
.sheet-views {
    display: flex; align-items: center; justify-content: space-between; gap: var(--space-5); flex-wrap: wrap;
    margin-bottom: var(--space-5);
}
.vt-tabs {
    display: flex; align-items: center; gap: 2px; padding: 3px; background: var(--surface-2);
    border: 1px solid var(--line); border-radius: var(--radius-md); overflow-x: auto; scrollbar-width: thin; min-width: 0;
}
.vt-tab {
    background: none; border: 0; border-radius: var(--radius-sm); padding: var(--space-3) var(--space-5);
    font: inherit; font-size: 12.5px; font-weight: 500; color: var(--ink-2); cursor: pointer;
    display: inline-flex; align-items: center; gap: var(--space-2); white-space: nowrap; flex-shrink: 0;
}
.vt-tab i { font-size: 11px; color: var(--ink-3); }
.vt-tab:hover { color: var(--ink); }
.vt-tab.is-active { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: var(--shadow-sm); }
.vt-icon { font-size: 10px !important; }
.vt-add {
    background: none; border: 0; color: var(--ink-3); cursor: pointer; padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-sm); font-size: 12px; flex-shrink: 0;
}
.vt-add:hover { color: var(--ink); background: var(--surface-3); }
.vt-note { font-size: 12px; color: var(--ink-3); padding: 0 var(--space-3); white-space: nowrap; }
.vt-state { display: flex; align-items: center; gap: var(--space-3); }
.vt-dirty { font-size: 12px; color: var(--warn-strong); font-weight: 500; }
.vt-btn { padding: var(--space-2) var(--space-4); font-size: 12px; }

/* Toolbar */
.sheet-toolbar .btn-toolbar-action.is-active { color: var(--ink); border-color: var(--focus); background: var(--accent-soft); }
:root[data-theme="dark"] .sheet-toolbar .btn-toolbar-action.is-active { background: var(--surface-3); }
.sheet-lead:empty { display: none; }
.sheet-count { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.sheet-note {
    font-size: 12.5px; color: var(--warn-strong); background: var(--warn-soft);
    padding: 8px 12px; border-radius: 8px; margin: -4px 0 12px;
}

/* Table: fixed layout so the saved widths apply exactly. The filler column
   (no width) takes any spare room, so columns never stretch. */
.sheet-table { table-layout: fixed; width: auto; min-width: 100%; }
.sheet-table td { max-width: none; }
.sheet-table .filler-cell { padding: 0 !important; border-right: 0; }
.sheet-table th.filler-cell { background: var(--surface-2); }
.sheet-table tbody tr { background: var(--surface); }
.sheet-table tbody tr:hover { background: var(--surface-2); }
/* Row actions stay at the right edge while the table scrolls sideways, and
   show on hover (always on touch screens) */
.sheet-table .action-cell {
    position: sticky; right: 0; z-index: 1; background: inherit;
    border-left: 1px solid var(--line); border-right: 0;
}
.sheet-table th.action-cell { z-index: 11; background: var(--surface-2); }
.sheet-table tfoot .action-cell { background: var(--surface); }
.sheet-table .btn-cell-delete { opacity: 0; }
.sheet-table tbody tr:hover .btn-cell-delete, .sheet-table tr.is-selected .btn-cell-delete,
.sheet-table .btn-cell-delete:focus-visible { opacity: 1; }
@media (hover: none) { .sheet-table .btn-cell-delete { opacity: 1; } }
.sheet-table .checkbox-cell .row-selector { opacity: 0.55; }
.sheet-table tbody tr:hover .row-selector, .sheet-table .row-selector:checked, .sheet-table .row-selector:focus-visible { opacity: 1; }
.sheet-table th.sheet-th { position: sticky; cursor: pointer; overflow: hidden; }
.sheet-table th.sheet-th:hover { background: var(--surface-3); }
.sheet-table th.sheet-th:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.sheet-table th.is-dragging { opacity: 0.5; }
.sheet-table .th-content { min-width: 0; }
.th-name { overflow: hidden; text-overflow: ellipsis; }
.th-content .th-mark { color: var(--focus); }
.th-content .th-error, .th-error { color: var(--warn-strong); }
.th-content sup { font-size: 9px; color: var(--ink-3); }
.col-resize {
    position: absolute; top: 0; right: 0; width: 8px; height: 100%; cursor: col-resize; z-index: 2; touch-action: none;
}
.col-resize::after {
    content: ""; position: absolute; top: 6px; bottom: 6px; right: 0; width: 2px; border-radius: 1px;
    background: var(--focus); opacity: 0; transition: opacity 0.12s ease;
}
.col-resize:hover::after, th.is-resizing .col-resize::after { opacity: 1; }
body.is-col-resizing, body.is-col-resizing * { cursor: col-resize !important; user-select: none; }
.notion-grid-inner { position: relative; }
.col-drop { position: absolute; top: 0; width: 2px; background: var(--focus); z-index: 20; pointer-events: none; }

.grid-cell-value { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; line-height: 22px; }
.grid-cell-value.num { text-align: right; font-variant-numeric: tabular-nums; }
.cell-bool { color: var(--ink-2); }
.notion-style-table tbody tr.is-selected { background: var(--accent-soft); }
:root[data-theme="dark"] .notion-style-table tbody tr.is-selected { background: var(--surface-3); }

/* A link in a cell: opens the linked record */
.rel-chip {
    display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
    background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px;
    padding: 1px 7px; font: inherit; font-size: 12.5px; color: var(--link); cursor: pointer;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.rel-chip i { font-size: 9px; color: var(--ink-3); }
.rel-chip:hover { border-color: var(--link-line); text-decoration: underline; text-decoration-color: var(--link-line); }
.rel-chip.is-missing { color: var(--ink-3); cursor: default; text-decoration: none; }

/* Groups and totals */
.notion-style-table tr.group-row td { background: var(--surface-2); padding: 6px 10px; height: auto; }
.group-toggle {
    background: none; border: 0; font: inherit; font-size: 12.5px; font-weight: 600; color: var(--ink);
    display: inline-flex; align-items: center; gap: 8px; cursor: pointer; padding: 2px 4px;
}
.group-toggle i { font-size: 10px; color: var(--ink-3); width: 10px; }
.group-count { font-weight: 500; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.total-cell { font-size: 12px; color: var(--ink-3); cursor: pointer; height: 32px !important; }
.total-cell:hover { background: var(--surface-2); }
.total-label { text-transform: uppercase; font-size: 10px; letter-spacing: 0.04em; }
.total-value { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.total-add { visibility: hidden; }
.total-cell:hover .total-add { visibility: visible; }
.sheet-total td { border-top: 1px solid var(--line-strong); position: sticky; bottom: 0; background: var(--surface); }
.sheet-more { padding: 10px; text-align: center; border-top: 1px solid var(--line); }

/* Floating panels (core/popover.js) */
.popover {
    position: fixed; z-index: 1000; background: var(--surface); color: var(--ink);
    border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-md);
    max-height: calc(100vh - 16px); max-width: calc(100vw - 16px); overflow: auto; padding: 6px;
}
.menu { min-width: 220px; font-size: 13px; }
.menu-head { padding: 6px 10px 8px; font-weight: 600; display: flex; flex-direction: column; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-head span { font-weight: 400; font-size: 11.5px; color: var(--ink-3); }
.menu-title { padding: 6px 8px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-3); }
.menu-item {
    width: 100%; background: none; border: 0; text-align: left; font: inherit; color: var(--ink);
    padding: 7px 10px; border-radius: 6px; cursor: pointer; display: flex; align-items: center; gap: 10px;
}
.menu-item i { width: 14px; font-size: 12px; color: var(--ink-3); }
.menu-item:hover, .menu-item.is-active { background: var(--surface-2); }
.menu-item.danger, .menu-item.danger i { color: var(--danger-strong); }
.menu-sep { height: 1px; background: var(--line); margin: 4px 0; }
.menu-empty, .menu-hint { padding: 6px 8px; color: var(--ink-3); font-size: 12.5px; }
.menu-hint { font-size: 11.5px; display: flex; gap: 6px; }
.menu-actions { display: flex; gap: 12px; padding: 6px 8px; }
.menu-row { display: flex; align-items: center; gap: 10px; padding: 6px 8px; color: var(--ink-2); font-size: 12.5px; }
.menu-wide { width: calc(100% - 16px); margin: 4px 8px; justify-content: center; }
.sheet-menu select, .sheet-menu input, .prop-search, .picker-input {
    font: inherit; font-size: 12.5px; color: var(--ink); background: var(--surface);
    border: 1px solid var(--line-strong); border-radius: 6px; padding: 5px 8px; outline: none; min-width: 0;
}
.sheet-menu select:focus, .sheet-menu input:focus, .prop-search:focus, .picker-input:focus { border-color: var(--focus); }

.btn-link {
    background: none; border: 0; padding: 2px 0; font: inherit; font-size: 12.5px; font-weight: 500;
    color: var(--ink-2); cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.btn-link:hover { color: var(--ink); text-decoration: underline; text-decoration-color: var(--link-line); }
.btn-link.danger { color: var(--danger-strong); }
.icon-btn {
    background: none; border: 0; color: var(--ink-3); cursor: pointer; padding: 5px 7px; border-radius: 6px; font-size: 12px;
}
.icon-btn:hover:not(:disabled) { color: var(--ink); background: var(--surface-2); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }

/* Filter and sort rules */
.menu-filter, .menu-sort { width: min(620px, calc(100vw - 16px)); }
.filter-rule, .sort-rule {
    display: grid; grid-template-columns: 64px minmax(0, 1.2fr) minmax(0, 1fr) minmax(0, 1.2fr) auto;
    gap: 6px; align-items: start; padding: 4px 8px;
}
.sort-rule { grid-template-columns: 64px minmax(0, 1.4fr) minmax(0, 1fr) auto auto; }
.fr-conj { font-size: 12px; color: var(--ink-3); padding-top: 6px; }
.fr-conj select { width: 100%; }
.fr-options { display: flex; flex-direction: column; gap: 4px; }
.fr-option-list {
    max-height: 180px; overflow-y: auto; border: 1px solid var(--line); border-radius: 6px; padding: 4px;
}
.fr-option { display: flex; align-items: center; gap: 6px; font-size: 12.5px; padding: 3px 4px; cursor: pointer; }
.fr-option span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Properties */
.menu-properties { width: 300px; }
.prop-search { width: calc(100% - 16px); margin: 0 8px 6px; }
.prop-list { max-height: 320px; overflow-y: auto; }
.prop-row { display: flex; align-items: center; gap: 8px; padding: 2px 4px 2px 10px; border-radius: 6px; }
.prop-row:hover { background: var(--surface-2); }
.prop-row.is-hidden .prop-name, .prop-row.is-hidden .prop-icon { color: var(--ink-3); }
.prop-icon { width: 14px; font-size: 11px; color: var(--ink-3); }
.prop-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }

/* Typeable dropdown */
.picker { width: 300px; padding: 6px; }
.picker-input { width: 100%; margin-bottom: 6px; }
.picker-list { max-height: 280px; overflow-y: auto; }
.picker-item {
    display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.picker-item > span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker-item.is-active { background: var(--surface-3); }
.picker-item.is-current > span { font-weight: 600; }
.picker-none { color: var(--ink-3); }
.picker-create { color: var(--ink); border-top: 1px solid var(--line); margin-top: 4px; }
.picker-create i { color: var(--focus); font-size: 11px; }
.picker-peek { background: none; border: 0; color: var(--ink-3); cursor: pointer; padding: 2px 4px; border-radius: 4px; font-size: 11px; }
.picker-peek:hover { color: var(--ink); background: var(--surface); }
.picker-more { padding: 6px 8px; font-size: 12px; color: var(--ink-3); }
.picker-error { font-size: 12px; color: var(--danger-strong); background: var(--danger-soft); padding: 6px 8px; border-radius: 6px; margin-top: 6px; }

/* "Name this" prompt */
.popover-prompt { width: 280px; padding: 12px; }
.pp-form { display: flex; flex-direction: column; gap: 8px; }
.pp-label { font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.pp-input { font: inherit; font-size: 13px; color: var(--ink); background: var(--surface); border: 1px solid var(--line-strong); border-radius: 6px; padding: 6px 8px; outline: none; }
.pp-input:focus { border-color: var(--focus); }
.pp-actions { display: flex; justify-content: flex-end; gap: 8px; }
.pp-actions .btn, .pp-actions .btn-primary { padding: 5px 12px; font-size: 12.5px; }

/* Record panel */
.record-peek {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(400px, 100vw); z-index: 900;
    background: var(--surface); color: var(--ink); border-left: 1px solid var(--line); box-shadow: var(--shadow-md);
    display: flex; flex-direction: column; overflow-y: auto; animation: fadeIn 0.15s ease-out;
}
.rp-head { display: flex; align-items: flex-start; gap: 8px; padding: 16px 16px 12px; border-bottom: 1px solid var(--line); }
.rp-title { flex: 1; min-width: 0; }
.rp-title h3 { font-size: 17px; font-weight: 600; overflow-wrap: anywhere; }
.rp-table { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-3); }
.rp-fields { display: grid; grid-template-columns: 130px minmax(0, 1fr); gap: 8px 12px; padding: 16px; font-size: 13px; }
.rp-fields dt { color: var(--ink-3); }
.rp-fields dd { overflow-wrap: anywhere; }
.rp-fields .uuid { font-family: var(--font-mono); font-size: 11.5px; color: var(--ink-3); }
.rp-links { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 10px; }
.rp-links h4 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-3); font-weight: 600; }
.rp-link-group { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.rp-link-title { flex-basis: 100%; font-size: 12.5px; color: var(--ink-2); }
.rp-link-title em { font-style: normal; color: var(--ink-3); }
.rp-actions { margin-top: auto; display: flex; gap: 8px; flex-wrap: wrap; padding: 14px 16px; border-top: 1px solid var(--line); }
.rp-loading { padding: 16px; color: var(--ink-3); font-size: 13px; }

/* The modal dialog shell: every "add/edit a field, KPI or formula" editor
   (grid, testing, admin) is built on this one shape, so they read as one system. */
.dlg {
    border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 0; width: min(520px, calc(100vw - 32px));
    background: var(--surface); color: var(--ink); box-shadow: var(--shadow-md); margin: auto;
}
.dlg::backdrop { background: rgba(14, 21, 48, 0.45); }
.dlg form { padding: var(--space-8); display: flex; flex-direction: column; gap: var(--space-5); }
.dlg h3 { font-size: 17px; font-weight: 600; }
.dlg label { display: flex; flex-direction: column; gap: var(--space-2); font-size: 12.5px; color: var(--ink-2); font-weight: 500; }
.dlg label .opt { font-weight: 400; color: var(--ink-3); }
.dlg-row { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: var(--space-5); }
.dlg-note { font-size: 12px; color: var(--ink-3); display: flex; gap: var(--space-3); }
.dlg-error { font-size: 12.5px; color: var(--danger-strong); background: var(--danger-soft); padding: var(--space-3) var(--space-4); border-radius: var(--radius-sm); }
.dlg-actions { display: flex; justify-content: flex-end; gap: var(--space-4); margin-top: var(--space-1); }
.dlg input, .dlg select, .dlg textarea {
    width: 100%; font: inherit; font-size: 13px; color: var(--ink); background: var(--surface);
    border: 1px solid var(--line-strong); border-radius: var(--radius-md); padding: var(--space-3) var(--space-4); outline: none;
}
.dlg input:focus, .dlg select:focus, .dlg textarea:focus { border-color: var(--focus); }
.calc-editor { width: min(680px, calc(100vw - 32px)); }
.ce-formula { font-family: var(--font-mono) !important; font-size: 12.5px !important; resize: vertical; }
.ce-suggest { display: flex; flex-wrap: wrap; gap: 4px; max-height: 92px; overflow-y: auto; }
.ce-chip {
    background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; padding: 2px 7px;
    font-family: var(--font-mono); font-size: 11.5px; color: var(--ink-2); cursor: pointer;
}
.ce-chip:hover { border-color: var(--focus); color: var(--ink); }
.ce-error { font-size: 12.5px; color: var(--danger-strong); background: var(--danger-soft); padding: var(--space-3) var(--space-4); border-radius: var(--radius-sm); }
.ce-preview { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.ce-preview:empty { display: none; }
.ce-preview-head { display: flex; justify-content: space-between; padding: 6px 10px; background: var(--surface-2); font-size: 12px; font-weight: 600; }
.ce-preview-head span { font-weight: 400; color: var(--ink-3); }
.ce-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.ce-table td { padding: 5px 10px; border-top: 1px solid var(--line); }
.ce-table td:last-child { font-family: var(--font-mono); text-align: right; }
.dlg label.ce-share { flex-direction: row; align-items: center; gap: 8px; }
.dlg label.ce-share input { width: auto; }

/* A module's Home / Spreadsheet switch (Drawings, Supply Chain) */
.mode-switch { margin-left: auto; }

@media (max-width: 768px) {
    .filter-rule, .sort-rule { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
    .fr-conj { grid-column: 1 / -1; padding-top: 0; }
    .sheet-views { flex-direction: column; align-items: stretch; }
    .vt-state { flex-wrap: wrap; }
    .rp-fields { grid-template-columns: minmax(0, 1fr); gap: 2px; }
    .rp-fields dd { margin-bottom: 8px; }
    .mode-switch { margin-left: 0; }
}

/* Custom field editor */
.custom-field-editor { width: min(480px, calc(100vw - 32px)); }
.cfe-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.cfe-row .hide { display: none; }
.menu-buttons { display: flex; gap: 6px; padding: 4px 8px; }
.menu-buttons .btn { flex: 1; justify-content: center; padding: 6px 8px; font-size: 12.5px; }

/* "+" after the last column: add a property (new-property.js) */
.sheet-table .add-col-cell { padding: 0 !important; text-align: center; border-right: 0; }
.sheet-table th.add-col-cell { background: var(--surface-2); }
.add-col-btn {
    width: 28px; height: 28px; border: 0; border-radius: 6px; background: none; cursor: pointer;
    color: var(--ink-3); font-size: 12px; transition: var(--transition-fast);
}
.add-col-btn:hover, .add-col-btn:focus-visible { color: var(--ink); background: var(--surface-3); outline: none; }
.new-prop { width: 300px; }
.np-list { max-height: 360px; overflow-y: auto; }
.np-type span { display: flex; flex-direction: column; min-width: 0; }
.np-type small { font-size: 11.5px; color: var(--ink-3); }
.np-form { display: flex; flex-direction: column; gap: var(--space-4); padding: var(--space-1) var(--space-2) var(--space-2); }
.np-head { display: flex; align-items: center; gap: var(--space-3); font-size: 13px; padding-bottom: var(--space-2); border-bottom: 1px solid var(--line); }
.np-head > i { color: var(--ink-3); font-size: 12px; }
.np-form label { display: flex; flex-direction: column; gap: var(--space-1); font-size: 12px; font-weight: 500; color: var(--ink-2); }
.np-form label.np-check { flex-direction: row; align-items: center; gap: var(--space-3); font-weight: 400; }
.np-form input:not([type=checkbox]), .np-form select, .np-form textarea {
    font: inherit; font-size: 12.5px; color: var(--ink); background: var(--surface);
    border: 1px solid var(--line-strong); border-radius: 6px; padding: 6px 8px; outline: none; min-width: 0;
}
.np-form input:focus, .np-form select:focus, .np-form textarea:focus { border-color: var(--focus); }
.np-add { justify-content: center; }
