/* ===========================
   TIFANY UI; 3-Panel Layout
   =========================== */

/* CSS Custom Properties; Light (default) */
:root {
    --t-primary: #1a73e8;
    --t-primary-dark: #1557b0;
    --t-bg-surface: #ffffff;
    --t-bg-workspace: #f8f9fa;
    --t-bg-input: #ffffff;
    --t-border: #e5e7eb;
    --t-text: #111827;
    --t-text-muted: #6b7280;
    --t-text-light: #9ca3af;
    --t-section-header: #1a73e8;
    --t-hover-bg: rgba(26, 115, 232, 0.07);
    --t-active-bg: rgba(26, 115, 232, 0.12);
    --t-toolbar-bg: #f0f4ff;
    --t-toolbar-border: rgba(79, 172, 254, 0.4);
    --t-header-height: 52px;
}

/* Dark Theme */
[data-theme="dark"] {
    --t-bg-surface: #252526;
    --t-bg-workspace: #1e1e1e;
    --t-bg-input: #2d2d30;
    --t-border: #3e3e42;
    --t-text: #d4d4d4;
    --t-text-muted: #9d9d9d;
    --t-text-light: #6a6a6a;
    --t-hover-bg: rgba(255, 255, 255, 0.06);
    --t-active-bg: rgba(255, 255, 255, 0.1);
    --t-toolbar-bg: rgba(79, 172, 254, 0.15);
    --t-toolbar-border: rgba(79, 172, 254, 0.3);
}

/* ===========================
   RESET (scoped to TIFANY)
   =========================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    overflow-x: hidden;
}

body {
    background-color: var(--t-bg-workspace);
    color: var(--t-text);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 13px;
    line-height: 1.5;
}

/* ===========================
   TIFANY HEADER
   =========================== */
.tifany-header {
    height: var(--t-header-height);
    background: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
    position: sticky;
    top: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 16px;
}

.tifany-header .logo {
    flex-shrink: 0;
}

.tifany-header .logo h1 {
    font-size: 16px;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.3px;
}

.tifany-header .nav {
    flex: 1;
    display: flex;
    gap: 2px;
    align-items: center;
}

.tifany-header .nav-link {
    font-size: 13px;
    color: var(--t-text-muted);
    padding: 5px 10px;
    border-radius: 6px;
    transition: all 150ms ease;
}

.tifany-header .nav-link:hover {
    color: var(--t-text);
    background: var(--t-hover-bg);
}

/* Theme toggle button */
.theme-toggle-btn {
    background: none;
    border: 1px solid var(--t-border);
    border-radius: 6px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--t-text-muted);
    font-size: 14px;
    transition: all 150ms ease;
    flex-shrink: 0;
}

.theme-toggle-btn.active {
    background: var(--t-primary);
    border-color: var(--t-primary);
    color: #fff;
}

.theme-toggle-btn:hover {
    background: var(--t-hover-bg);
    color: var(--t-text);
}

/* ===========================
   3-PANEL APP LAYOUT
   =========================== */
.tifany-app {
    display: flex;
    flex-direction: row;
    height: calc(100vh - var(--t-header-height));
    overflow: hidden;
}

/* --- Icon nav rail --- */
.sidebar-nav-rail {
    width: 48px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-right: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    padding: 8px 0;
    gap: 4px;
}

.sidebar-nav-rail a,
.sidebar-nav-rail button {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    color: var(--t-text-muted);
    text-decoration: none;
    border: none;
    background: none;
    cursor: pointer;
    transition: all 150ms ease;
}

.sidebar-nav-rail a:hover,
.sidebar-nav-rail button:hover {
    background: var(--t-hover-bg);
    color: var(--t-primary);
}

.sidebar-nav-rail a.active {
    color: var(--t-primary);
    background: var(--t-active-bg);
}

/* --- Left panel --- */
.tifany-left-panel {
    width: 264px;
    min-width: 200px;
    overflow-y: auto;
    border-right: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: calc(100vh - var(--t-header-height));
    align-self: flex-start;
}

.tifany-left-panel.panel-hidden {
    display: none;
}

/* --- Center panel --- */
.tifany-center-panel {
    flex: 1;
    min-width: 0;
    overflow: auto;
    background: var(--t-bg-workspace);
    display: flex;
    flex-direction: column;
}

/* --- Right panel --- */
.tifany-right-panel {
    width: 300px;
    min-width: 220px;
    max-width: 600px;
    overflow-y: auto;
    border-left: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.right-panel-resize-handle {
    position: absolute;
    top: 0;
    left: -3px;
    width: 10px;
    height: 100%;
    cursor: col-resize;
    z-index: 10;
}

.right-panel-resize-handle:hover,
.right-panel-resize-handle.dragging {
    background: var(--t-accent, #4a90d9);
    opacity: 0.5;
}

/* ===========================
   LEFT PANEL SECTIONS
   =========================== */
.left-section-header {
    /* color: var(--t-section-header);
    font-weight: 600;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    padding: 10px 12px 6px;
    border-bottom: 1px solid var(--t-border);
    user-select: none; */

    display: block;
    position: sticky;
    z-index: 100;
    top: 0;
    text-align: left;
    border: none;
    border-bottom: 1px solid var(--t-border);
    width: 100%;
    cursor: pointer;
    padding: 10px 12px 6px;
    color: var(--t-section-header);
    background: var(--t-bg-input);
    font-weight: 600;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
}

.left-section-body {
    padding: 8px 12px 10px;
    border-bottom: 1px solid var(--t-border);
}

/* ===========================
   RIGHT PANEL SECTIONS (collapsible)
   =========================== */
.right-section {
    border-bottom: 1px solid var(--t-border);
}

.right-section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    color: var(--t-section-header);
    font-weight: 600;
    font-size: 13px;
    cursor: pointer;
    user-select: none;
    background: var(--t-bg-surface);
    transition: background 150ms ease;
}

.right-section-header:hover {
    background: var(--t-hover-bg);
}

.right-section-chevron {
    font-size: 10px;
    color: var(--t-text-muted);
    transition: transform 150ms ease;
}

.right-section-body {
    padding: 10px 14px 12px;
    background: var(--t-bg-surface);
}

.right-section-body.collapsed {
    display: none;
}

/* Monaco editor container */
#monaco-editor-container {
    height: 480px;
    min-height: 180px;
    border: 1px solid var(--t-border);
    border-radius: 4px;
    overflow: hidden;
    margin-top: 8px;
}

/* Generate actions row */
.generate-actions {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
    overflow: auto;
}

/* ===========================
   CENTER; TABLE WRAPPER
   =========================== */
.center-header {
    font-size: 12px;
    font-weight: 600;
    color: var(--t-text-muted);
    padding: 8px 14px;
    border-bottom: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    letter-spacing: 0.3px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.center-pos {
    font-size: 11px;
    color: var(--t-text-light);
}

.table-wrapper {
    background-color: var(--t-bg-workspace);
    flex: 1;
    text-align: center;
    cursor: pointer;
    position: relative;
    overflow: auto;
    padding: 16px;
    min-height: 200px;
}

/* ===========================
   MOBILE TOOLBAR (shown only on mobile)
   =========================== */
#tableViewer-mb {
    display: none;
}

.toolbar-container {
    display: flex;
    background-color: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
    padding: 6px 8px;
    overflow-x: auto;
    white-space: nowrap;
    gap: 4px;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 4px;
    padding-right: 8px;
    margin-right: 4px;
    border-right: 1px solid var(--t-border);
}

.toolbar-group:last-child {
    border-right: none;
}

.toolbar-btn {
    background: var(--t-toolbar-bg);
    border: 1px solid var(--t-toolbar-border);
    color: var(--t-text);
    padding: 5px 10px;
    border-radius: 16px;
    font-size: 11px;
    cursor: pointer;
    transition: all 200ms ease;
    white-space: nowrap;
    font-family: inherit;
}

.toolbar-btn:hover {
    background: var(--t-hover-bg);
    border-color: var(--t-primary);
    color: var(--t-primary);
}

.toolbar-btn.active {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

/* ===========================
   LEGACY / COMPAT (Bootstrap accordion panels still used inside left sidebar)
   =========================== */
.panel {
    display: none;
    border-radius: 10px;
    overflow: auto;
    
}

#tableContainer .panel {
    border: 1px solid #d1d5db;
    border-top: none;
    border-radius: 0 0 14px 14px;
    box-shadow:
        0 1px 3px rgba(0,0,0,0.06),
        0 4px 16px rgba(0,0,0,0.04);
    background: var(--t-bg-surface);
    padding: 0;
    margin-bottom: 16px;
    max-width: 960px;
}

/* Left panel sections start expanded and stay persistent (not reset on table reload) */
.tifany-left-panel .panel {
    display: block;
}

.accordion {
    background-color: #eee;
    color: #444;
    cursor: pointer;
    padding: 10px 14px;
    width: 100%;
    border: 1px solid var(--t-border);
    text-align: left;
    outline: none;
    font-size: 12px;
    transition: 0.3s;
    border-radius: 5px;
    font-family: inherit;
}

.accordion.active {
    background-color: #ddd;
}

.accordion::after {
    content: " \25BD";
    float: right;
}

.accordion.active::after {
    content: " \25B3";
}

.side-accordion {
    writing-mode: sideways-rl;
    align-content: space-around;
}

.ha-100.active {
    height: 100%;
}

.input-container {
    display: block;
    padding: 8px 0;
}

.sidebar-container {
    display: none;
    /* hidden on desktop; replaced by .tifany-left-panel */
}

.sidebar {
    width: 100%;
    padding: 5px;
    background: var(--t-bg-surface);
    overflow-y: auto;
}

.instructions {
    background: var(--t-bg-surface);
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 12px;
    font-size: 11px;
    line-height: 1.6;
    color: var(--t-text-muted);
}

.control-group {
    margin-bottom: 12px;
    padding: 8px;
    border-radius: 8px;
}

.control-group h3 {
    margin-bottom: 8px;
    color: var(--t-text);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.panel-style {
    border: 1px solid var(--t-border);
    border-radius: 8px;
    margin-top: 5px;
    padding: 12px;
}

.style-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin: 8px 0;
}

.button-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    justify-content: space-between;
    min-width: 100%;
}

.sp-class:hover {
    transition: all 200ms ease;
    transform: scale(1.05);
}

.sp-class.sp-active {
    background-color: #337ab7;
    color: #fff;
    border-color: #2e6da4;
}

/* ===========================
   DARK MODE; Bootstrap 4 overrides
   =========================== */
[data-theme="dark"] body {
    background-color: var(--t-bg-workspace);
    color: var(--t-text);
}

[data-theme="dark"] .tifany-header {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .tifany-left-panel,
[data-theme="dark"] .tifany-right-panel {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .left-section-header,
[data-theme="dark"] .right-section-header {
    border-color: var(--t-border);
}

[data-theme="dark"] .left-section-body,
[data-theme="dark"] .right-section-body {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .right-section {
    border-color: var(--t-border);
}

[data-theme="dark"] .accordion {
    background: #333;
    color: #ccc;
    border-color: var(--t-border);
}

[data-theme="dark"] .accordion.active {
    background: #3a3a3a;
}

[data-theme="dark"] .panel {
    background: var(--t-bg-surface);
}

[data-theme="dark"] .form-control,
[data-theme="dark"] select,
[data-theme="dark"] input[type="text"],
[data-theme="dark"] input[type="number"],
[data-theme="dark"] textarea {
    background-color: var(--t-bg-input) !important;
    color: var(--t-text) !important;
    border-color: var(--t-border) !important;
}

[data-theme="dark"] .btn-outline-primary {
    color: #6ab0f5;
    border-color: #6ab0f5;
}

[data-theme="dark"] .btn-outline-secondary {
    color: #ccc;
    border-color: #555;
}

[data-theme="dark"] .btn-outline-danger {
    color: #f87171;
    border-color: #f87171;
}

[data-theme="dark"] .modal-content {
    background-color: var(--t-bg-surface);
    color: var(--t-text);
    border-color: var(--t-border);
}

[data-theme="dark"] .modal-header,
[data-theme="dark"] .modal-footer {
    border-color: var(--t-border);
}

[data-theme="dark"] .close {
    color: var(--t-text);
}

[data-theme="dark"] label {
    color: var(--t-text-muted);
}

[data-theme="dark"] .cell-controls {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
    color: var(--t-text);
}

[data-theme="dark"] .toolbar-container {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .center-header {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .sidebar-nav-rail {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

/* ===========================
   INPUT TOOLBAR
   =========================== */
.input-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
    flex-shrink: 0;
    flex-wrap: wrap;
}

.input-toolbar-divider {
    width: 1px;
    height: 20px;
    background: var(--t-border);
    flex-shrink: 0;
}

/* Drag & Drop toggle switch */
.toolbar-switch-label {
    display: flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    user-select: none;
    font-size: 11px;
    color: var(--t-text-muted);
}

.toolbar-switch-label input[type="checkbox"] {
    display: none;
}

.toolbar-switch-track {
    display: inline-block;
    width: 28px;
    height: 15px;
    background: var(--t-border);
    border-radius: 8px;
    position: relative;
    transition: background 200ms ease;
    flex-shrink: 0;
}

.toolbar-switch-track::after {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 11px;
    height: 11px;
    background: #fff;
    border-radius: 50%;
    transition: transform 200ms ease;
}

.toolbar-switch-label input:checked+.toolbar-switch-track {
    background: var(--t-primary);
}

.toolbar-switch-label input:checked+.toolbar-switch-track::after {
    transform: translateX(13px);
}

/* Tab count mini-input */
.toolbar-tab-label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    color: var(--t-text-muted);
}

.toolbar-tab-input {
    border: 1px solid var(--t-border);
    border-radius: 4px;
    padding: 2px 4px;
    font-size: 12px;
    background: var(--t-bg-input);
    color: var(--t-text);
    text-align: center;
}

.toolbar-tab-input:focus {
    outline: none;
    border-color: var(--t-primary);
}

/* ===========================
   SHEET TAB BAR
   =========================== */
.sheet-tab-bar {
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px 12px;
    background: var(--t-bg-surface);
    border-top: 1px solid var(--t-border);
    flex-shrink: 0;
    overflow-x: auto;
    white-space: nowrap;
}

.sheet-tab {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px 3px 12px;
    border: 1px solid var(--t-border);
    border-radius: 4px 4px 0 0;
    background: var(--t-bg-workspace);
    font-size: 12px;
    color: var(--t-text-muted);
    cursor: pointer;
    user-select: none;
    transition: all 150ms ease;
    flex-shrink: 0;
}

.sheet-tab:hover {
    background: var(--t-hover-bg);
    color: var(--t-text);
}

.sheet-tab.active {
    background: var(--t-bg-surface);
    border-bottom-color: var(--t-bg-surface);
    color: var(--t-primary);
    font-weight: 600;
}

.sheet-tab-label {
    min-width: 40px;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    outline: none;
}

.sheet-tab-close {
    background: none;
    border: none;
    color: var(--t-text-light);
    font-size: 13px;
    line-height: 1;
    padding: 0 2px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 150ms ease;
}

.sheet-tab:hover .sheet-tab-close,
.sheet-tab.active .sheet-tab-close {
    opacity: 1;
}

.sheet-tab-close:hover {
    color: var(--t-text);
}

.sheet-tab-add {
    background: none;
    border: 1px solid var(--t-border);
    border-radius: 4px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    line-height: 1;
    color: var(--t-text-muted);
    cursor: pointer;
    flex-shrink: 0;
    transition: all 150ms ease;
}

.sheet-tab-add:hover {
    background: var(--t-hover-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

/* ===========================
   BOOTSTRAP MODAL; z-index safety
   Ensures modals always render above sticky header (z-200) and all panels.
   =========================== */
.modal {
    z-index: 1055 !important;
}

.modal-backdrop {
    z-index: 1050 !important;
}

/* Ensure the modal dialog is properly centered even with our CSS reset */
.modal-dialog {
    margin: 1.75rem auto !important;
}

/* ===========================
   DRAW CANVAS
   =========================== */
/* When active, Draw Mode hides .table-wrapper and fills the center panel */
body.draw-mode-active .tifany-center-panel {
    display: flex;
    flex-direction: column;
}

.draw-canvas {
    display: none;
    flex-direction: column;
    background: var(--t-bg-surface);
    flex: 1;
    /* fill the center panel */
    min-height: 0;
    /* allow flex child to shrink */
    overflow: hidden;
}

.draw-canvas-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    background: var(--t-active-bg);
    border-bottom: 1px solid var(--t-border);
    font-size: 12px;
    font-weight: 500;
    color: var(--t-primary);
    flex-shrink: 0;
}

.draw-canvas-header-actions {
    display: flex;
    gap: 6px;
    align-items: center;
}

.draw-canvas-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* Monaco container inside draw canvas */
.draw-monaco-container {
    flex: 1;
    min-height: 0;
    border-bottom: none;
    /* border replaced by the resize handle */
}

/* ── Vertical drag-resize handle ── */
.draw-resize-handle {
    flex-shrink: 0;
    height: 6px;
    background: var(--t-border);
    cursor: ns-resize;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 120ms ease;
    z-index: 2;
}

/* Three-dot grip indicator */
.draw-resize-handle::after {
    content: '';
    display: block;
    width: 28px;
    height: 3px;
    border-radius: 2px;
    background: var(--t-text-muted);
    opacity: 0.35;
    transition: opacity 120ms ease, background 120ms ease;
    /* Dotted grip pattern via box-shadow */
    box-shadow:
        -8px 0 0 var(--t-text-muted),
        8px 0 0 var(--t-text-muted);
}

.draw-resize-handle:hover,
.draw-resize-handle.dragging {
    background: color-mix(in srgb, var(--t-primary) 20%, transparent);
}

.draw-resize-handle:hover::after,
.draw-resize-handle.dragging::after {
    background: var(--t-primary);
    opacity: 0.8;
    box-shadow:
        -8px 0 0 var(--t-primary),
        8px 0 0 var(--t-primary);
}


.draw-input {
    flex: 1;
    width: 100%;
    min-height: 100px;
    max-height: 160px;
    resize: vertical;
    border: none;
    border-bottom: 1px solid var(--t-border);
    padding: 10px 14px;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    background: var(--t-bg-input);
    color: var(--t-text);
    outline: none;
}

.draw-input:focus {
    border-bottom-color: var(--t-primary);
}

.draw-canvas-toolbar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
    flex-shrink: 0;
}

.draw-toolbar-hint {
    font-size: 11px;
    color: var(--t-text-muted);
    margin-right: 4px;
}

.draw-preview {
    flex: 1;
    overflow-y: auto;
    padding: 8px 14px;
    background: var(--t-bg-workspace);
    min-height: 60px;
}

.draw-preview-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--t-text-muted);
    margin-bottom: 6px;
    display: block;
}

.draw-preview-grid {
    border-collapse: collapse;
    margin-top: 5px;
}

.draw-preview-grid td {
    border: 1px solid var(--t-border);
    padding: 2px 6px;
    min-width: 60px;
    height: 28px;
    font-size: 11px;
    color: var(--t-text);
    background: var(--t-bg-surface);
    cursor: pointer;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
    transition: all 150ms ease;
    user-select: none;
}

.draw-preview-grid td:hover {
    background: var(--t-hover-bg);
}

.draw-preview-grid td.active-cell {
    border: 2px solid var(--t-primary);
    background: var(--t-active-bg);
    padding: 1px 5px;
    /* Offset for 2px border */
}

/* Header cells show with a distinct tint */
.draw-preview-grid td.header-cell {
    background: rgba(var(--t-primary-rgb, 99, 102, 241), 0.12);
    color: var(--t-primary);
    font-weight: 600;
}

/* Draw mode indicator on toolbar button */
body.draw-mode-active #drawModeToggle {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

/* Inline cell editor (double-click to edit) */
.draw-cell-input {
    width: 100%;
    min-width: 56px;
    box-sizing: border-box;
    border: none;
    background: transparent;
    outline: none;
    font: inherit;
    font-size: 11px;
    color: var(--t-text);
    padding: 0;
    margin: 0;
    /* Remove default input chrome */
    -webkit-appearance: none;
    appearance: none;
}

.draw-cell-input:focus {
    outline: 2px solid var(--t-primary);
    outline-offset: -2px;
    border-radius: 2px;
}

/* Allow text selection inside editing cells */
.draw-preview-grid td:has(.draw-cell-input) {
    user-select: text;
    overflow: visible;
}

/* Empty cell placeholder */
.draw-cell-empty {
    color: var(--t-text-muted);
    font-style: italic;
    opacity: 0.55;
    font-size: 10px;
    pointer-events: none;
}

/* ===========================
   DRAW MODE (legacy cursor only)
   =========================== */
body.draw-mode-active .table-wrapper {
    cursor: default;
}

/* ===========================
   LAB MODE
   =========================== */

/* Show center panel as flex column when Lab is active */
body.lab-mode-active .tifany-center-panel {
    display: flex;
    flex-direction: column;
}

/* Toolbar button active state */
body.lab-mode-active #labModeToggle {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

/* ── Top pane: step builder ─────────────────────────────────────────────── */

.lab-step-pane {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--t-bg-surface);
}

/* Tab bar row */
.lab-tab-bar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 10px;
    border-bottom: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    flex-shrink: 0;
}

.lab-tab {
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 500;
    border: 1px solid var(--t-border);
    border-radius: 4px;
    background: transparent;
    color: var(--t-text-muted);
    cursor: pointer;
    transition: all 140ms ease;
    text-transform: capitalize;
}

.lab-tab:hover {
    background: var(--t-hover-bg);
    color: var(--t-text);
}

.lab-tab.active {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

.lab-tab-spacer { flex: 1; }

.lab-recipe-btn {
    font-size: 11px;
    padding: 2px 8px;
    height: 24px;
    line-height: 1;
}

.lab-run-btn {
    font-size: 11px;
    padding: 2px 10px;
    height: 24px;
    line-height: 1;
}

/* ── Function picker ────────────────────────────────────────────────────── */

.lab-fn-picker {
    background: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
    padding: 8px 10px;
    max-height: 220px;
    overflow-y: auto;
    flex-shrink: 0;
}

.lab-picker-inner {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.lab-picker-group {
    min-width: 140px;
}

.lab-picker-group-label {
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--t-text-muted);
    margin-bottom: 4px;
    padding: 0 2px;
}

.lab-picker-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 4px 8px;
    font-size: 11px;
    background: transparent;
    border: 1px solid var(--t-border);
    border-radius: 4px;
    color: var(--t-text);
    cursor: pointer;
    margin-bottom: 2px;
    transition: background 120ms ease;
}

.lab-picker-item:hover:not([disabled]) {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

.lab-picker-validate .lab-picker-item { border-left: 3px solid #dc3545; }
.lab-picker-transform .lab-picker-item { border-left: 3px solid #0d6efd; }
.lab-picker-analyze   .lab-picker-item { border-left: 3px solid #198754; }

.lab-picker-cloud .lab-picker-item,
.lab-picker-item[disabled] {
    opacity: 0.45;
    cursor: not-allowed;
    font-style: italic;
}

/* ── Step list ──────────────────────────────────────────────────────────── */

.lab-step-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 6px 10px;
}

.lab-step-empty {
    font-size: 11px;
    color: var(--t-text-muted);
    font-style: italic;
    padding: 10px 0;
}

.lab-step-card {
    border: 1px solid var(--t-border);
    border-radius: 6px;
    margin-bottom: 6px;
    background: var(--t-bg-workspace);
    overflow: hidden;
}

.lab-step-card-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 8px;
    background: var(--t-bg-surface);
    border-bottom: 1px solid var(--t-border);
}

.lab-step-num {
    font-size: 10px;
    font-weight: 700;
    color: var(--t-text-muted);
    min-width: 14px;
}

.lab-step-badge {
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 1px 5px;
    border-radius: 3px;
    white-space: nowrap;
}

.lab-badge-validate { background: rgba(220,53,69,0.12); color: #dc3545; }
.lab-badge-transform { background: rgba(13,110,253,0.12); color: #0d6efd; }
.lab-badge-analyze  { background: rgba(25,135,84,0.12);  color: #198754; }

.lab-step-label {
    font-size: 11px;
    color: var(--t-text);
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.lab-step-actions {
    display: flex;
    gap: 3px;
    flex-shrink: 0;
}

.lab-step-btn {
    background: transparent;
    border: 1px solid var(--t-border);
    border-radius: 3px;
    font-size: 11px;
    padding: 1px 5px;
    color: var(--t-text-muted);
    cursor: pointer;
    line-height: 1.4;
    transition: all 120ms ease;
}

.lab-step-btn:hover:not([disabled]) {
    background: var(--t-hover-bg);
    color: var(--t-text);
}

.lab-step-btn[disabled] { opacity: 0.3; cursor: default; }

.lab-step-remove:hover:not([disabled]) {
    background: rgba(220,53,69,0.1);
    border-color: #dc3545;
    color: #dc3545;
}

/* ── Param forms ────────────────────────────────────────────────────────── */

.lab-step-params {
    padding: 8px 10px;
}

.lab-param-grid {
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 4px 8px;
    align-items: center;
}

.lab-param-label {
    font-size: 10px;
    color: var(--t-text-muted);
    font-weight: 500;
    text-align: right;
    padding-right: 4px;
    white-space: nowrap;
}

.lab-sublabel {
    font-size: 9px;
    color: var(--t-text-muted);
    margin-top: 4px;
}

.lab-param-input {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.lab-input {
    font-size: 11px;
    padding: 3px 6px;
    border: 1px solid var(--t-border);
    border-radius: 4px;
    background: var(--t-bg-input);
    color: var(--t-text);
    width: 100%;
    box-sizing: border-box;
    height: 26px;
}

.lab-input:focus {
    outline: none;
    border-color: var(--t-primary);
    box-shadow: 0 0 0 2px rgba(99,102,241,0.15);
}

.lab-input-num { width: 90px; }
.lab-input-xs  { height: 22px; font-size: 10px; padding: 1px 4px; }

.lab-custom-term { margin-top: 4px; }

.lab-expr-sub {
    display: contents; /* slots sub-labels/inputs into the parent grid */
}

.lab-hint {
    font-size: 10px;
    color: var(--t-text-muted);
    font-style: italic;
}

/* expr sub-fields: compact vertical rows below the type select */
.lab-expr-sub {
    margin-top: 5px;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.lab-expr-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.lab-expr-label {
    font-size: 10px;
    color: var(--t-text-muted);
    white-space: nowrap;
    width: 50px;
    flex-shrink: 0;
    text-align: right;
}

/* mergeBy rules */
.lab-rule-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
}
.lab-rule-col {
    font-size: 10px;
    color: var(--t-text);
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* multiAggregate measures */
.lab-measures-rows { margin-bottom: 4px; }
.lab-measure-row { display: flex; gap: 4px; margin-bottom: 3px; }
.lab-add-measure { font-size: 10px; padding: 2px 6px; }

/* ── Add Step bar ───────────────────────────────────────────────────────── */

.lab-add-step-bar {
    flex-shrink: 0;
    padding: 6px 10px;
    border-top: 1px solid var(--t-border);
    background: var(--t-bg-surface);
}

/* ── Recipe section ─────────────────────────────────────────────────────── */

.lab-recipe-section {
    flex-shrink: 0;
    border-top: 1px solid var(--t-border);
}

.lab-recipe-monaco {
    height: 110px !important;
    flex: none !important;
}

/* ── Result pane ────────────────────────────────────────────────────────── */

.lab-result-badge {
    font-size: 10px;
    font-weight: 600;
    margin-left: 8px;
    display: inline;
}

/* Result table */
.lab-result-table {
    border-collapse: collapse;
    font-size: 11px;
    width: 100%;
    table-layout: auto;
}

.lab-result-table th {
    background: var(--t-bg-surface);
    border: 1px solid var(--t-border);
    padding: 3px 8px;
    font-weight: 600;
    color: var(--t-text);
    text-align: left;
    white-space: nowrap;
    position: sticky;
    top: 0;
    z-index: 1;
}

.lab-result-table td {
    border: 1px solid var(--t-border);
    padding: 2px 8px;
    color: var(--t-text);
    background: var(--t-bg-workspace);
    white-space: nowrap;
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Flagged row styles */
.lab-row-error > td:first-child { border-left: 3px solid #dc3545; }
.lab-row-warn  > td:first-child { border-left: 3px solid #ffc107; }
.lab-row-error { background: rgba(220,53,69,0.04) !important; }
.lab-row-warn  { background: rgba(255,193,7,0.06) !important; }

.lab-flag-col { min-width: 120px; max-width: 300px; }
.lab-flag-badge {
    display: inline-block;
    font-size: 9px;
    padding: 1px 5px;
    border-radius: 3px;
    margin: 1px;
    white-space: normal;
    word-break: break-word;
}
.lab-flag-error { background: rgba(220,53,69,0.12); color: #dc3545; }
.lab-flag-warn  { background: rgba(255,193,7,0.15);  color: #856404; }

/* Stat cards */
.lab-stat-row {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    padding: 12px 0;
}

.lab-stat-card {
    background: var(--t-bg-surface);
    border: 1px solid var(--t-border);
    border-radius: 8px;
    padding: 16px 24px;
    text-align: center;
    min-width: 120px;
}

.lab-stat-value {
    font-size: 26px;
    font-weight: 700;
    color: var(--t-primary);
    line-height: 1.2;
}

.lab-stat-label {
    font-size: 11px;
    color: var(--t-text-muted);
    margin-top: 4px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ── Submit row ─────────────────────────────────────────────────────────── */

.lab-submit-row {
    padding: 8px 10px;
    border-top: 1px solid var(--t-border);
    background: var(--t-bg-surface);
    flex-shrink: 0;
}

/* ===========================
   NODE EDITOR MODE
   =========================== */

/* Toolbar button active state */
body.node-editor-active #nodeEditorToggle {
    background: var(--t-active-bg);
    color: var(--t-primary);
    border-color: var(--t-primary);
}

/* Hide table view and sheet tabs while node editor is active */
body.node-editor-active .table-wrapper,
body.node-editor-active #sheetTabBar {
    display: none !important;
}

/* Center panel needs relative positioning for the absolute overlay */
.tifany-center-panel {
    position: relative;
}

/* Dark mode overrides for new components */
[data-theme="dark"] .input-toolbar {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .sheet-tab-bar {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .sheet-tab {
    background: var(--t-bg-workspace);
    border-color: var(--t-border);
    color: var(--t-text-muted);
}

[data-theme="dark"] .sheet-tab.active {
    background: var(--t-bg-surface);
    border-bottom-color: var(--t-bg-surface);
}

[data-theme="dark"] .toolbar-tab-input {
    background-color: var(--t-bg-input) !important;
    color: var(--t-text) !important;
    border-color: var(--t-border) !important;
}

/* ===========================
   MOBILE BOTTOM NAV (hidden on desktop)
   =========================== */
.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--t-bg-surface);
    border-top: 1px solid var(--t-border);
    z-index: 600;
    align-items: stretch;
    justify-content: space-around;
}

.mobile-nav-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    background: none;
    border: none;
    color: var(--t-text-muted);
    font-size: 10px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    padding: 4px 0;
    transition: color 150ms ease, background 150ms ease;
    letter-spacing: 0.3px;
}

.mobile-nav-tab i {
    font-size: 17px;
    line-height: 1;
}

.mobile-nav-tab.active {
    color: var(--t-primary);
    background: var(--t-active-bg);
}

.mobile-nav-tab:not(.active):hover {
    color: var(--t-text);
    background: var(--t-hover-bg);
}

/* Sheet backdrop (hidden on desktop) */
.mobile-sheet-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 490;
    opacity: 0;
    transition: opacity 250ms ease;
}

.mobile-sheet-backdrop.visible {
    display: block;
    opacity: 1;
}

/* ===========================
   MOBILE LAYOUT (≤767px)
   =========================== */
@media (max-width: 767px) {

    /* Icon rail: vertical → horizontal strip at top of app */
    #sidebar-nav-mount {
        flex-direction: row !important;
        width: 100%;
        height: auto;
        min-height: 44px;
        border-right: none !important;
        border-bottom: 1px solid var(--t-border);
        background: var(--t-bg-surface);
        padding: 4px 8px !important;
        overflow-x: auto;
        gap: 4px !important;
        flex-shrink: 0;
    }

    /* App becomes a column; bottom 56px reserved for nav */
    .tifany-app {
        flex-direction: column;
        height: calc(100dvh - 56px);
        overflow: hidden;
    }

    /* Center fills all remaining height */
    .tifany-center-panel {
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }

    .table-wrapper {
        padding: 8px;
    }

    /* Show the existing horizontal mobile toolbar */
    #tableViewer-mb {
        display: block;
        flex-shrink: 0;
    }

    /* Left panel: always in DOM but translated off-screen; slides up on .mobile-open */
    .tifany-left-panel,
    .tifany-left-panel.panel-hidden {
        display: block !important;
        position: fixed !important;
        bottom: 56px;
        left: 0;
        right: 0;
        top: auto !important;
        height: auto;
        max-height: 72vh;
        min-width: 0;
        width: 100% !important;
        border-right: none;
        border-top: 1px solid var(--t-border);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
        z-index: 500;
        overflow-y: auto;
        transform: translateY(102%);
        transition: transform 300ms cubic-bezier(0.32, 0.72, 0, 1);
    }

    /* Drag handle via pseudo on panel top */
    .tifany-left-panel::before {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--t-border);
        margin: 10px auto 6px;
    }

    .tifany-left-panel.mobile-open {
        transform: translateY(0);
    }

    /* Right panel: same treatment */
    .tifany-right-panel {
        display: flex !important;
        position: fixed !important;
        bottom: 56px;
        left: 0;
        right: 0;
        top: auto !important;
        height: auto;
        max-height: 72vh;
        min-width: 0;
        max-width: 100%;
        width: 100% !important;
        border-left: none;
        border-top: 1px solid var(--t-border);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.15);
        z-index: 500;
        overflow-y: auto;
        transform: translateY(102%);
        transition: transform 300ms cubic-bezier(0.32, 0.72, 0, 1);
        flex-direction: column;
    }

    .tifany-right-panel::before {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--t-border);
        margin: 10px auto 6px;
        flex-shrink: 0;
    }

    .tifany-right-panel.mobile-open {
        transform: translateY(0);
    }

    /* Monaco editor: compact height on mobile */
    #monaco-editor-container {
        height: 260px;
        min-height: 160px;
    }

    /* Show mobile bottom nav */
    .mobile-bottom-nav {
        display: flex;
    }

    /* Right panel resize handle: disable on mobile */
    .right-panel-resize-handle {
        display: none;
    }

    /* Sheet tab bar: move just below center-header so it's not hidden under mobileBottomNav */
    .center-header   { order: 1; }
    .sheet-tab-bar   { order: 2; border-top: none; border-bottom: 1px solid var(--t-border); }
    .table-wrapper   { order: 3; }

    /* Context menu: better spacing and touch targets on mobile */
    .cell-controls {
        width: 260px !important;
        border-radius: 12px !important;
        padding: 14px !important;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2) !important;
    }

    .cell-controls h6 {
        font-size: 10px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin: 12px 0 6px !important;
        color: var(--t-text-muted);
    }

    .cell-controls h6:first-child {
        margin-top: 0 !important;
    }

    /* .cell-op {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        gap: 6px !important;
    } */

    /* .cell-op button {
        padding: 8px 6px !important;
        font-size: 12px !important;
        border-radius: 6px !important;
        text-align: center;
        width: 100% !important;
    }

    .cell-op button#deleteCell,
    .cell-op button#deleteRow,
    .cell-op button#deleteColumn {
        flex: none;
    } */
}


[data-theme="dark"] .mobile-bottom-nav {
    background: var(--t-bg-surface);
    border-color: var(--t-border);
}

[data-theme="dark"] .mobile-sheet-backdrop {
    background: rgba(0, 0, 0, 0.65);
}