/* Canvas dimmed backdrop */
.canvas-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 999;
    transition: opacity 0.25s ease;
}

.canvas-backdrop.hidden {
    display: none;
}

.sheet-builder-card {
    background: var(--bg-primary);
    border: none;
    border-radius: 20px;
    box-shadow: var(--neo-float);
    overflow: hidden;
    padding: 0.75rem 1rem;
    animation: fadeUp 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    height: 100%;
    box-sizing: border-box;
}

/* Floating Modal Styling */
.sheet-builder-card.floating-module {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 95vw;
    max-width: 1100px;
    height: auto;
    max-height: calc(100vh - 2 * (1.3rem + 80px));
    overflow: hidden;
    z-index: 1000;
    box-shadow: var(--neo-float);
    margin: 0;
    border-radius: 20px;
    animation: fadeUpCentered 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes fadeUpCentered {
    from {
        opacity: 0;
        transform: translate(-50%, calc(-50% + 24px));
    }
    to {
        opacity: 1;
        transform: translate(-50%, -50%);
    }
}

@keyframes fadeUpFull {
    from { opacity: 0; transform: translateX(-50%) translateY(16px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.sheet-builder-card.hidden {
    display: none !important;
}

.sheet-drag-handle {
    height: 10px;
    cursor: default;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
}

.drag-indicator {
    width: 28px;
    height: 3px;
    background: var(--text-muted);
    border-radius: 2px;
}

.sheet-header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-left: auto;
}

.sheet-close-btn {
    position: static;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--bg-primary);
    box-shadow: var(--neo-btn);
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    line-height: 1;
    color: color-mix(in srgb, var(--error), var(--text-muted) 55%);
    flex-shrink: 0;
    transition: box-shadow 0.15s ease, color 0.15s ease;
}

.sheet-close-btn:hover {
    color: var(--error);
    box-shadow: 0 0 9px var(--neo-dark);
}

.sheet-close-btn:active {
    box-shadow: var(--neo-pressed);
    color: var(--error);
}

.sheet-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: nowrap;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 0.1rem;
}

.sheet-title {
    font-family: 'Outfit', sans-serif;
    font-size: 0.9rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
    color: var(--text-secondary);
    margin: 0;
}

.sheet-title span {
    color: var(--brand-cyan);
}

.sheet-controls {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.sheet-presets {
    display: flex;
    gap: 0.35rem;
    align-items: center;
}

.sheet-preset-btn {
    background: var(--bg-primary) !important;
    border: none !important;
    box-shadow: var(--neo-btn) !important;
    height: 30px !important;
    padding: 0 0.8rem !important;
    font-size: 0.8rem !important;
    font-weight: 600 !important;
    border-radius: 8px !important;
    white-space: nowrap;
    box-sizing: border-box !important;
    transition: box-shadow 0.15s ease, transform 0.1s ease, color 0.15s ease;
}

/* Canvas header button accent colors (defined here, not inline, so they're overridable) */
#canvasEditBtn  { color: var(--brand-orange); }
#canvasDoneBtn  { color: var(--brand-cyan); }
#btnResetCanvas { color: color-mix(in srgb, var(--error), var(--text-muted) 55%); }

.sheet-preset-btn:hover {
    box-shadow: 0 0 14px var(--neo-dark) !important;
    transform: translateY(-1px);
}

.sheet-preset-btn:active {
    box-shadow: var(--neo-pressed) !important;
    transform: translateY(0);
}

/* Spreadsheet Toolbar */
.spreadsheet-toolbar {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    flex-wrap: wrap;
    padding: 0.1rem 0;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.toolbar-btn {
    background: var(--bg-primary);
    border: none;
    color: var(--text-secondary);
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
    transition: box-shadow 0.15s ease, color 0.15s ease, transform 0.1s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    box-shadow: 0 0 7px var(--neo-dark);
}

.toolbar-btn:hover {
    color: var(--text-primary);
    box-shadow: 0 0 10px var(--neo-dark);
    transform: translateY(-1px);
}

.toolbar-btn:active {
    transform: translateY(0);
    box-shadow: var(--neo-pressed);
    color: var(--text-primary);
}

.toolbar-btn.active {
    background: var(--bg-primary);
    color: var(--brand-cyan);
    box-shadow: var(--neo-pressed), 0 0 8px rgba(0, 212, 255, 0.2);
}

.toolbar-btn.danger-btn:hover {
    color: var(--error);
    box-shadow: 0 0 10px var(--neo-dark);
}

.toolbar-btn.danger-btn:active {
    box-shadow: var(--neo-pressed);
}

.toolbar-select {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: none;
    box-shadow: var(--neo-btn);
    padding: 0.22rem 1.8rem 0.22rem 0.5rem;
    border-radius: 6px;
    font-size: 0.78rem;
    outline: none;
    cursor: pointer;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='11' height='11' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.45rem center;
    background-size: 11px;
    transition: border-color 0.15s;
}

.toolbar-select:hover {
    border-color: rgba(255, 255, 255, 0.15);
    color: var(--text-primary);
}

.toolbar-select:focus {
    box-shadow: var(--neo-pressed), 0 0 0 2px rgba(0, 212, 255, 0.15);
    outline: none;
}

.toolbar-select option {
    background-color: var(--bg-primary);
    color: var(--text-primary);
}

.toolbar-divider {
    width: 1px;
    height: 14px;
    background: var(--border-color);
    margin: 0 0.2rem;
}

.toolbar-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
}

/* Canvas guide (?) button */
.canvas-guide-btn {
    font-size: 0.95rem;
    font-weight: 700;
    font-family: 'Space Mono', monospace;
}

.canvas-guide-btn.active {
    box-shadow: var(--neo-pressed);
    color: var(--brand-cyan);
}

/* Collapsible tip panel */
.canvas-tip-panel {
    background: var(--bg-deep);
    box-shadow: var(--neo-inset);
    border-radius: 10px;
    padding: 0.6rem 0.9rem;
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

.canvas-tip-panel code {
    font-family: 'Space Mono', monospace;
    color: var(--brand-cyan);
    font-size: 0.78rem;
}

/* Canvas template name input */
.canvas-tmpl-input {
    flex: 1;
    min-width: 0;
    max-width: 180px;
    background: var(--bg-deep);
    box-shadow: var(--neo-inset);
    border: none;
    border-radius: 8px;
    padding: 0.28rem 0.65rem;
    font-size: 0.8rem;
    color: var(--text-primary);
    font-family: inherit;
    outline: none;
}

.canvas-tmpl-input::placeholder { color: var(--text-muted); }
.canvas-tmpl-input:focus { box-shadow: var(--neo-inset), 0 0 0 2px rgba(0, 212, 255, 0.15); }

/* Spreadsheet Table Styles */
.sheet-grid-container {
    width: 100%;
    overflow-x: auto;
    overflow-y: auto;
    border-radius: 10px;
    border: none;
    background: var(--bg-deep);
    position: relative;
    box-shadow: var(--neo-inset);
}

/* ── Preview mode — scales to fit any viewport, zero scrollbars ──────────────
   Scoped to .preview-mode so the editor keeps its fixed column widths.
   Specificity (0,3,1) beats all media-query th/td overrides in responsive.css. */
.sheet-builder-card.preview-mode .sheet-grid-container {
    overflow-x: hidden;
}
.sheet-builder-card.preview-mode #spreadsheetTable {
    table-layout: auto;
    font-size: clamp(0.52rem, 1.6vw, 0.75rem);
    width: 100%;
}
.sheet-builder-card.preview-mode #spreadsheetTable col             { width: auto !important; }
.sheet-builder-card.preview-mode #spreadsheetTable col:first-child { width: 32px !important; }
.sheet-builder-card.preview-mode #spreadsheetTable col:nth-child(3){ width: 30% !important; }
.sheet-builder-card.preview-mode .spreadsheet-table td,
.sheet-builder-card.preview-mode .spreadsheet-table th {
    padding: clamp(0.15rem, 0.4vw, 0.5rem) clamp(0.2rem, 0.5vw, 0.7rem);
    word-break: break-word;
}

.sheet-grid-container.drag-over {
    border-color: var(--brand-cyan);
    background: rgba(0, 212, 255, 0.04);
}

.spreadsheet-table td.drop-target {
    outline: 2px solid var(--brand-cyan) !important;
    background: rgba(0, 212, 255, 0.12) !important;
}

.spreadsheet-table tr.drop-row-target td {
    background: rgba(255, 122, 42, 0.12) !important;
    outline: none !important;
}

.spreadsheet-table tr.drop-row-target td:first-child {
    outline: 2px solid var(--brand-orange) !important;
}

.spreadsheet-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    table-layout: fixed;
}

.spreadsheet-table th {
    background: var(--bg-deep);
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 0.6rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    border-right: 1px solid var(--border-color);
    text-align: center;
    position: sticky;
    top: 0;
    z-index: 10;
}

.spreadsheet-table th.row-num-col, .spreadsheet-table td.row-num-col {
    width: 62px;
    text-align: center;
    vertical-align: middle;
    background: var(--bg-deep);
    color: var(--text-muted);
    border-right: 1px solid rgba(255, 255, 255, 0.08);
    user-select: none;
    position: sticky;
    left: 0;
    z-index: 8;
    cursor: grab;
    transition: color 0.15s;
}

.row-num-inner {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.row-num-label {
    font-family: 'Space Mono', monospace;
    font-size: 0.7rem;
    font-weight: 700;
}

.spreadsheet-table td.row-num-col:hover {
    color: var(--brand-orange);
}

.spreadsheet-table td.row-num-col:active {
    cursor: grabbing;
    color: var(--brand-orange);
}

.spreadsheet-table tr.dragging-row td {
    opacity: 0.35;
}

.spreadsheet-table tr.drop-row-target td {
    background: rgba(255, 122, 42, 0.14) !important;
}

.spreadsheet-table tr.drop-row-target td.row-num-col {
    border-top: 3px solid var(--brand-orange);
    color: var(--brand-orange);
}

.spreadsheet-table td {
    background: var(--bg-primary);
    padding: 0.5rem 0.7rem;
    border-bottom: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    min-height: 32px;
    vertical-align: middle;
    outline: none;
    word-break: break-all;
    user-select: none;
}

/* Draggable cells show grab cursor */
.spreadsheet-table td[draggable="true"] { cursor: grab; }
.spreadsheet-table td[draggable="true"]:active { cursor: grabbing; }

/* Edit State outline */
.spreadsheet-table td[contenteditable="true"] {
    cursor: cell;
}

.spreadsheet-table td[contenteditable="true"]:focus {
    background: rgba(255, 255, 255, 0.03);
    box-shadow: inset 0 0 0 1px var(--brand-cyan);
    user-select: text;
}

.spreadsheet-table td.selected {
    background: rgba(0, 212, 255, 0.1) !important;
    box-shadow: inset 0 0 0 1px rgba(0, 212, 255, 0.4);
}

/* Color Presets */
.cell-bg-cyan {
    background: rgba(0, 212, 255, 0.15) !important;
    color: var(--brand-cyan) !important;
}

.cell-bg-orange {
    background: rgba(255, 122, 42, 0.12) !important;
    color: var(--brand-orange) !important;
}

.cell-bg-gray {
    background: rgba(255, 255, 255, 0.08) !important;
    color: var(--text-primary) !important;
}

.cell-bg-green {
    background: rgba(16, 185, 129, 0.15) !important;
    color: #10b981 !important;
}

/* Item identifier header row — distinct from Labour (orange) and Material (cyan) */
.cell-bg-indigo {
    background: rgba(99, 102, 241, 0.2) !important;
    color: #a5b4fc !important;
}

/* % Charge inline form in canvas */
.charge-row-form {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.55rem 0.75rem;
    background: rgba(0, 212, 255, 0.05);
    border: 1px solid rgba(0, 212, 255, 0.15);
    border-radius: 10px;
    flex-wrap: wrap;
}

.charge-form-label { font-size: 0.75rem; color: var(--text-muted); white-space: nowrap; }

.charge-form-input {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 0.82rem;
    padding: 0.3rem 0.5rem;
    outline: none;
    font-family: inherit;
}

.charge-form-input:focus { border-color: var(--brand-cyan); }
.charge-pct-input { width: 64px; }

.charge-pct-input::-webkit-inner-spin-button,
.charge-pct-input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.charge-pct-input[type=number] { -moz-appearance: textfield; }

/* ── Canvas footer (Copy Grid button) ── */
.canvas-footer {
    padding: 0.85rem 1.25rem;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    display: flex;
    justify-content: flex-end;
}

.copy-grid-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1.25rem;
    border-radius: 10px;
    font-size: 0.85rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: var(--bg-deep);
    color: var(--brand-cyan);
    box-shadow: var(--neo-btn);
    transition: box-shadow 0.16s, transform 0.12s, color 0.15s;
}

.copy-grid-btn:hover { box-shadow: 0 0 10px var(--neo-dark); transform: translateY(-1px); }
.copy-grid-btn:active { transform: translateY(0); box-shadow: var(--neo-pressed); }
.copy-grid-btn.copied { color: var(--brand-orange); }

/* ── Template preview mode ───────────────────────────────────────────────────
   The canvas opens read-only by default (a preview of the selected template);
   the "✎ Edit" button removes .preview-mode to unlock the full builder. */
.sheet-builder-card.preview-mode .spreadsheet-toolbar { display: none; }
.sheet-builder-card.preview-mode .canvas-edit-only     { display: none; }
.sheet-builder-card:not(.preview-mode) .canvas-edit-btn { display: none; }

/* In preview mode the grid is read-only — no cell editing, no drag/drop, no
   row reordering, no range selection. The wrapper still scrolls (wheel), so
   long RA tables stay viewable. The "✎ Edit" button restores interaction. */
.sheet-builder-card.preview-mode .spreadsheet-table td,
.sheet-builder-card.preview-mode .spreadsheet-table th { pointer-events: none; }
.sheet-builder-card.preview-mode .spreadsheet-table td[draggable="true"] { cursor: default; }

/* Template selector in the canvas header — same height as preset buttons (30px) */
.canvas-tmpl-select.format-select {
    height: 30px;
    padding: 0 1.9rem 0 0.7rem;
    font-size: 0.8rem;
    border-radius: 8px;
    border: 1px solid rgba(255, 122, 42, 0.4); /* orange accent at rest — matches card dropdown */
    box-shadow: var(--neo-btn);
    background-position: right 0.55rem center;
    background-size: 12px;
    box-sizing: border-box;
    color: var(--text-secondary);
}

.canvas-tmpl-select.format-select:hover {
    border-color: rgba(255, 122, 42, 0.6);
}

.canvas-tmpl-select.format-select:focus {
    border-color: rgba(255, 122, 42, 0.6);
    box-shadow: var(--neo-btn), 0 0 0 2px rgba(255, 122, 42, 0.4);
    outline: none;
}

.canvas-tmpl-select.format-select option {
    background-color: #111111;
    color: #e2e2ea;
}
