@import url('https://fonts.googleapis.com/css2?family=Fustat:wght@400;600;700&family=Nunito:wght@400;700&display=swap');

:root {
    --bg-app: #e2e2e7;
    --border: #d1d1d6;
    --border-subtle: #e8e8ed;
    --primary: #0066ff;
    --primary-dim: rgba(0,102,255,0.09);
    --orange: #f18701;
    --text-main: #1d1d1f;
    --text-muted: #86868b;
    --panel-bg: rgba(255,255,255,0.94);
    --topbar-h: 52px;
    --shadow-panel: 0 4px 6px rgba(0,0,0,0.04), 0 16px 48px rgba(0,0,0,0.13);
    --radius-panel: 14px;
}

/* ── RESET ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; line-height: inherit; }

body { font-family: 'Fustat', sans-serif; background: var(--bg-app); color: var(--text-main); height: 100vh; overflow: hidden; user-select: none; transition: background 0.2s; }
body.editing-popup { background: #1a1a1c; } /* Dim background when building popups */

/* ── TOPBAR ── */
.topbar {
    position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
    background: rgba(255,255,255,0.88); backdrop-filter: blur(16px) saturate(1.4); -webkit-backdrop-filter: blur(16px) saturate(1.4);
    border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; padding: 0 20px; z-index: 2000; gap: 16px;
}
.topbar-section { display: flex; align-items: center; gap: 8px; }
.app-logo { font-weight: 700; font-size: 15px; white-space: nowrap; }
.app-logo span { color: var(--primary); }
.topbar-divider { width: 1px; height: 20px; background: var(--border); flex-shrink: 0; }

.btn-top { background: transparent; border: none; padding: 5px 11px; font-weight: 600; font-size: 12px; cursor: pointer; border-radius: 7px; color: var(--text-muted); transition: background 0.12s, color 0.12s; display: flex; align-items: center; gap: 4px; white-space: nowrap; }
.btn-top:hover { background: #f0f0f5; color: var(--text-main); }
.btn-top.active { background: var(--primary-dim); color: var(--primary); }
.btn-top:disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }
.btn-top.btn-export { background: var(--primary); color: #fff; font-weight: 700; padding: 6px 14px; }
.btn-top.btn-export:hover { background: #0052cc; color: #fff; }

.topbar-select {
    background: #fff; border: 1px solid var(--border); color: var(--text-main);
    border-radius: 7px; padding: 4px 24px 4px 8px; font-size: 12px; font-weight: 600;
    cursor: pointer; outline: none; height: 26px; appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2386868b'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 8px center;
}
.topbar-select:hover { border-color: #b0b0b8; }
.topbar-select:focus { border-color: var(--primary); }

/* ── TOPBAR CENTER ── */
.topbar-center { flex: 1; justify-content: center; gap: 8px; }

/* Pages group: select + add-btn welded together */
.tb-pages-group {
    display: flex; align-items: stretch;
    background: #fff; border: 1px solid var(--border); border-radius: 8px;
    /* NO overflow:hidden — that clips the dropdown menu */
    height: 28px; position: relative;
}
.tb-pages-select {
    border: none !important; border-right: 1px solid var(--border) !important;
    border-radius: 8px 0 0 8px !important; height: 100% !important;
    max-width: 170px; min-width: 110px;
}
.tb-pages-add-btn {
    border-radius: 0 8px 8px 0 !important; height: 100% !important;
    padding: 0 9px !important; font-size: 11px !important;
    color: var(--text-muted) !important; white-space: nowrap;
    display: flex; align-items: center; gap: 3px;
}
.tb-pages-add-btn:hover { background: #f0f0f5 !important; color: var(--text-main) !important; }
.tb-pages-group .tb-dropdown { display: flex; align-items: stretch; }

/* Segmented desktop/mobile toggle */
.tb-seg-ctrl {
    display: flex; align-items: center;
    background: #f0f0f5; border-radius: 8px; padding: 2px; gap: 1px;
}
.tb-seg-btn {
    background: transparent; border: none;
    padding: 4px 11px; font-family: inherit; font-size: 12px; font-weight: 600;
    cursor: pointer; border-radius: 6px;
    color: var(--text-muted); white-space: nowrap;
    display: flex; align-items: center; gap: 4px;
    transition: background 0.12s, color 0.12s, box-shadow 0.12s;
}
.tb-seg-btn.active {
    background: #fff; color: var(--text-main);
    box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
.tb-seg-btn:hover:not(.active) { color: var(--text-main); }

/* Settings button — visually distinct */
.btn-settings {
    background: var(--primary-dim) !important;
    color: var(--primary) !important;
    border: 1px solid rgba(0,102,255,0.18) !important;
    font-weight: 700 !important;
}
.btn-settings:hover { background: rgba(0,102,255,0.15) !important; }

/* File button */
.tb-file-btn { gap: 3px; }
.tb-caret { font-size: 8px; opacity: 0.55; }

/* ── TOPBAR DROPDOWN MENUS ── */
.tb-dropdown { position: relative; }

.tb-dropdown-menu {
    position: absolute; top: calc(100% + 7px); left: 0;
    background: #fff; border: 1px solid var(--border); border-radius: 11px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.04), 0 16px 48px rgba(0,0,0,0.14);
    min-width: 165px; z-index: 3000;
    display: none; flex-direction: column; padding: 5px;
    animation: tb-menu-in 0.14s ease;
}
.tb-dropdown-menu.open { display: flex; }
.tb-dropdown-right { left: auto; right: 0; }

@keyframes tb-menu-in {
    from { opacity: 0; transform: translateY(-5px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0)  scale(1); }
}

.tb-menu-header {
    font-size: 10px; font-weight: 700; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: 0.6px;
    padding: 6px 10px 3px;
}
.tb-menu-item {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 10px; border-radius: 7px;
    background: transparent; border: none; cursor: pointer;
    font-size: 12px; font-weight: 600; font-family: inherit;
    color: var(--text-main); width: 100%; text-align: left;
    transition: background 0.1s, color 0.1s;
}
.tb-menu-item:hover { background: var(--primary-dim); color: var(--primary); }
.tb-menu-icon { font-size: 14px; width: 18px; text-align: center; flex-shrink: 0; }
.tb-menu-sep { height: 1px; background: var(--border-subtle); margin: 4px 0; }

/* ── WORKSPACE ── */
#workspace { margin-top: var(--topbar-h); height: calc(100vh - var(--topbar-h)); overflow: auto; position: relative; display: flex; justify-content: center; align-items: flex-start; padding: 80px 24px 120px; transition: padding 0.2s; }
body:not(.editing-popup) #workspace::before { content: ''; position: fixed; inset: var(--topbar-h) 0 0 0; z-index: -1; background-image: radial-gradient(circle, #b4b4b8 1px, transparent 1px); background-size: 22px 22px; }
body.editing-popup #workspace { align-items: flex-start; padding-top: 80px; padding-bottom: 120px; }
/* ── CANVAS ── */
.canvas {
    background-color: #fff; width: 1000px; min-height: 1200px; position: relative;
    box-shadow: 0 2px 1px rgba(0,0,0,0.04), 0 8px 40px rgba(0,0,0,0.12); border-radius: 3px;
    background-size: calc(1000px / 60) 10px;
    background-image: linear-gradient(to right, rgba(0,0,0,0.025) 1px, transparent 1px), linear-gradient(to bottom, rgba(0,0,0,0.025) 1px, transparent 1px);
    transition: box-shadow 0.2s, border-radius 0.2s;
    overflow: hidden;
    /* Contain all canvas children (incl. #selection-box) in their own stacking context
       so they can never bleed above fixed modals in the root stacking context */
    isolation: isolate;
}

/* Make popups physically act like lightboxes while editing! */
body.editing-popup .canvas { 
    width: 600px !important; 
    min-height: 600px; 
    background-color: #fff !important;
    border-radius: 16px; 
    box-shadow: 0 30px 80px rgba(0,0,0,0.4); 
    margin: 0 auto; /* Ensure it stays horizontally centered but respects the top padding */
}

.canvas.mobile-mode { width: 420px !important; min-height: 812px !important; border-radius: 40px !important; border: 14px solid #1d1d1f !important; box-shadow: 0 24px 80px rgba(0,0,0,0.3) !important; display: flex !important; flex-direction: column !important; gap: 16px; padding: 20px 0; background-image: none !important; overflow-y: auto; overflow-x: hidden; }
.canvas.mobile-mode::-webkit-scrollbar { display: none; }
.canvas.mobile-mode .builder-block { position: relative !important; left: 0 !important; top: 0 !important; width: var(--mob-w, calc(100% - 40px)) !important; margin: var(--mob-mt, 0px) var(--mob-mr, 20px) var(--mob-mb, 0px) var(--mob-ml, 20px) !important; height: auto !important; min-height: var(--h, 40px) !important; order: var(--y-index); }
.canvas.mobile-mode .type-shape > .content-area { display: flex !important; flex-direction: column !important; gap: 12px; padding: 18px !important; }
.canvas.mobile-mode .type-media { height: 220px !important; }
.canvas.mobile-mode .type-text { 
    min-height: auto !important; /* Allow text boxes to shrink naturally on mobile */
}
.canvas.mobile-mode .type-text > .content-area {
    font-size: max(16px, calc(var(--fs) * 0.8)) !important; /* Gentler font scaling */
    height: auto !important;
    overflow-wrap: break-word !important;
    word-break: break-word !important;
    white-space: normal !important;
}

#mobile-hint { display: none; position: fixed; top: calc(var(--topbar-h) + 10px); left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.7); color: #fff; font-size: 12px; font-weight: 600; padding: 6px 14px; border-radius: 20px; z-index: 2000; pointer-events: none; white-space: nowrap; }

/* ── CANVAS BLOCKS ── */
.builder-block { position: absolute; box-sizing: border-box; cursor: grab; }
.builder-block:active { cursor: grabbing; }
.builder-block.is-dragging { opacity: 0.85; box-shadow: 0 20px 40px rgba(0,0,0,0.15) !important; }
.builder-block img, .builder-block video { pointer-events: none; -webkit-user-drag: none; user-select: none; }
.drop-highlight { box-shadow: 0 0 0 3px var(--primary) inset !important; }

/* Content ALWAYS centered unconditionally now! */
.content-area {
    width: 100%; height: 100%;
    padding: var(--pad, 12px);
    font-size: var(--fs, 16px);
    font-family: var(--ff, 'Nunito', sans-serif);
    text-align: var(--align, left);
    display: flex; flex-direction: column; flex-grow: 1;
    justify-content: center; 
    letter-spacing: var(--lsp, 0px);
    line-height: var(--lh, 1.4);
    text-shadow: var(--text-shadow, none);
    box-sizing: border-box; position: relative; outline: none;
    overflow-wrap: break-word; word-break: break-word;
}
.content-area[contenteditable="true"] { cursor: text; outline: 2px dashed var(--primary); outline-offset: -2px; background: rgba(255,255,255,0.75); }

/* ── SELECTION BOX ── */
#selection-box { position: absolute; border: 1.5px solid var(--primary); pointer-events: none; z-index: 9999; transform-origin: 50% 50%; }

.rotate-handle {
    position: absolute;
    width: 13px; height: 13px;
    background: #fff;
    border: 1.5px solid var(--primary);
    border-radius: 50%;
    pointer-events: auto;
    cursor: grab;
    z-index: 3;
    /* Centre above the top edge */
    left: calc(50% - 6.5px);
    top: -34px;
}
.rotate-handle:active { cursor: grabbing; }
/* Connector line from handle down to box edge */
.rotate-handle::after {
    content: '';
    position: absolute;
    width: 1.5px;
    height: 22px;
    background: var(--primary);
    left: calc(50% - 0.75px);
    top: 100%;
}

.resize-handle { position: absolute; width: 9px; height: 9px; background: #fff; border: 1.5px solid var(--primary); pointer-events: auto; z-index: 2; border-radius: 2px; }
.resize-handle.nw { top: -5px; left: -5px; cursor: nwse-resize; } .resize-handle.ne { top: -5px; right: -5px; cursor: nesw-resize; }
.resize-handle.sw { bottom: -5px; left: -5px; cursor: nesw-resize; } .resize-handle.se { bottom: -5px; right: -5px; cursor: nwse-resize; }
.resize-handle.n  { top: -5px; left: calc(50% - 5px); cursor: ns-resize; } .resize-handle.s  { bottom: -5px; left: calc(50% - 5px); cursor: ns-resize; }
.resize-handle.e  { top: calc(50% - 5px); right: -5px; cursor: ew-resize; } .resize-handle.w  { top: calc(50% - 5px); left: -5px; cursor: ew-resize; }
.radius-handle { position: absolute; width: 11px; height: 11px; background: transparent; border: 1.5px solid var(--orange); border-radius: 50%; pointer-events: auto; cursor: crosshair; z-index: 1; transition: transform 0.1s; }
.radius-handle:hover { transform: scale(1.3); }
.radius-handle.tl { top: 8px; left: 8px; } .radius-handle.tr { top: 8px; right: 8px; }
.radius-handle.bl { bottom: 8px; left: 8px; } .radius-handle.br { bottom: 8px; right: 8px; }


/* ── CUSTOM VISUAL FONT PICKER ── */
.tt-font-picker { position: relative; user-select: none; }
.tt-fp-selected { background: #fff; border: 1px solid var(--border); color: var(--text-main); border-radius: 5px; padding: 4px 24px 4px 8px; font-size: 12px; font-weight: 600; cursor: pointer; height: 26px; display: flex; align-items: center; width: 100%; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2386868b'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 8px center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transition: border-color 0.1s; }
.tt-fp-selected:hover { border-color: #b0b0b8; }
.tt-fp-dropdown { position: absolute; top: 100%; left: 0; background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow-panel); margin-top: 4px; width: 100%; min-width: 150px; max-height: 340px; overflow-y: auto; display: none; z-index: 10001; padding: 6px 0; }
.tt-fp-dropdown.active { display: block; animation: lib-in 0.15s ease; }
.tt-fp-cat { font-size: 10px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; padding: 10px 12px 4px; letter-spacing: 0.05em; background: #fff; position: sticky; top: 0; }
.tt-fp-item { padding: 8px 12px; font-size: 15px; cursor: pointer; transition: background 0.1s; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tt-fp-item:hover { background: var(--primary-dim); color: var(--primary); }

.tt-btn { background: transparent; border: none; color: var(--text-main); padding: 4px 8px; border-radius: 5px; cursor: pointer; font-size: 13px; font-weight: 700; line-height: 1; transition: background 0.1s, color 0.1s; }
.tt-btn:hover { background: #f0f0f5; }
.tt-btn.tt-active { background: var(--primary-dim); color: var(--primary); }
.tt-div { width: 1px; height: 18px; background: var(--border); margin: 0 3px; flex-shrink: 0; }
.tt-select { background: #fff; border: 1px solid var(--border); color: var(--text-main); border-radius: 5px; padding: 3px 6px; font-size: 11px; cursor: pointer; outline: none; }
.tt-select:hover { border-color: #b0b0b8; } .tt-select:focus { border-color: var(--primary); }
.tt-input { background: #fff; border: 1px solid var(--border); color: var(--text-main); border-radius: 5px; padding: 3px 6px; font-size: 11px; outline: none; }
.tt-input:focus { border-color: var(--primary); }
.tt-input::-webkit-inner-spin-button, .tt-input::-webkit-outer-spin-button { -webkit-appearance: none; }
.tt-color { width: 24px; height: 24px; padding: 1px; border-radius: 4px; border: 1px solid var(--border); cursor: pointer; background: none; }
.tt-color::-webkit-color-swatch-wrapper { padding: 0; }
.tt-color::-webkit-color-swatch { border: none; border-radius: 3px; }

/* Font-size stepper */
.tt-size-group { display: flex; align-items: center; gap: 1px; }
.tt-size-btn { padding: 4px 6px; font-size: 14px; font-weight: 400; line-height: 1; min-width: 22px; }
#tt-fontSize { width: 36px; text-align: center; padding: 3px 4px; }

/* Heading preset pills */
.tt-preset { padding: 3px 7px; font-size: 11px; font-weight: 700; border-radius: 5px; background: transparent; border: 1px solid var(--border); color: var(--text-muted); cursor: pointer; line-height: 1.4; transition: background 0.1s, color 0.1s, border-color 0.1s; }
.tt-preset:hover { background: #f0f0f5; color: var(--text-main); border-color: #b0b0b8; }

/* ── CONTEXT PANEL ── */
#context-panel {
    position: fixed; width: 258px;
    background: var(--panel-bg); backdrop-filter: blur(24px) saturate(1.5); -webkit-backdrop-filter: blur(24px) saturate(1.5);
    border: 1px solid var(--border); border-radius: var(--radius-panel); box-shadow: var(--shadow-panel);
    z-index: 3000; overflow: hidden; font-size: 12px; display: none;
    max-height: calc(100vh - 80px); overflow-y: auto; overflow-x: hidden;
}
#ctx-header {
    display: flex; justify-content: space-between; align-items: center; padding: 10px 12px;
    background: var(--panel-bg); border-bottom: 1px solid var(--border-subtle);
    cursor: move; position: sticky; top: 0; z-index: 1;
}
#ctx-title { font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-muted); }
.ctx-header-actions { display: flex; gap: 3px; }
.ctx-icon-btn { background: rgba(0,0,0,0.04); border: none; width: 26px; height: 26px; border-radius: 6px; cursor: pointer; font-size: 13px; line-height: 1; display: flex; align-items: center; justify-content: center; transition: background 0.1s; color: var(--text-main); }
.ctx-icon-btn:hover { background: rgba(0,0,0,0.1); }
.ctx-icon-btn.active { background: var(--primary-dim); color: var(--primary); }
.ctx-icon-btn.danger:hover { background: #fee2e2; color: #dc2626; }

.ctx-section { border-bottom: 1px solid var(--border-subtle); }
.ctx-section-header { display: flex; justify-content: space-between; align-items: center; padding: 9px 12px; font-size: 11px; font-weight: 700; color: var(--text-muted); cursor: pointer; transition: background 0.1s; user-select: none; }
.ctx-section-header:hover { background: rgba(0,0,0,0.025); }
.ctx-chevron { font-size: 10px; transition: transform 0.18s; display: inline-block; }
.ctx-section.collapsed .ctx-chevron { transform: rotate(-90deg); }
.ctx-section.collapsed .ctx-section-body { display: none; }
.ctx-section-body { padding: 4px 12px 12px; }
.ctx-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 7px; }
.ctx-row:last-child { margin-bottom: 0; }
.ctx-range-row { display: flex; align-items: center; gap: 7px; margin-bottom: 7px; }
.ctx-range-row:last-child { margin-bottom: 0; }
.ctx-label { font-size: 11px; font-weight: 600; color: var(--text-muted); flex-shrink: 0; min-width: 44px; }
.ctx-value { font-size: 10px; color: var(--text-muted); font-family: monospace; min-width: 22px; text-align: right; flex-shrink: 0; }

.ctx-color { width: 30px; height: 26px; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; padding: 2px; background: #fff; flex-shrink: 0; }
.ctx-color::-webkit-color-swatch-wrapper { padding: 0; }
.ctx-color::-webkit-color-swatch { border: none; border-radius: 4px; }
input[type="range"].ctx-range { -webkit-appearance: none; flex: 1; height: 4px; background: #dddde5; border-radius: 2px; outline: none; border: none; cursor: pointer; min-width: 0; }
input[type="range"].ctx-range::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--primary); cursor: pointer; border: 2px solid #fff; box-shadow: 0 0 0 1.5px rgba(0,102,255,0.3); }

.ctx-select { flex: 1; border: 1px solid var(--border); border-radius: 6px; padding: 4px 7px; background: #fff; cursor: pointer; outline: none; font-size: 11px; color: var(--text-main); appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2386868b'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 7px center; padding-right: 22px; }
.ctx-select:focus { border-color: var(--primary); }
.ctx-input { border: 1px solid var(--border); border-radius: 6px; padding: 4px 7px; background: #fff; outline: none; font-size: 11px; font-family: inherit; }
.ctx-input:focus { border-color: var(--primary); }

/* Toggle switch */
.ctx-toggle { position: relative; display: inline-block; width: 34px; height: 20px; cursor: pointer; flex-shrink: 0; }
.ctx-toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.ctx-toggle-track { position: absolute; inset: 0; background: #dddde5; border-radius: 10px; transition: background 0.18s; }
.ctx-toggle-track::after { content: ''; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.2); transition: transform 0.18s; }
.ctx-toggle input:checked + .ctx-toggle-track { background: var(--primary); }
.ctx-toggle input:checked + .ctx-toggle-track::after { transform: translateX(14px); }

/* Angle knob */
.angle-knob { width: 30px; height: 30px; border-radius: 50%; background: #f0f0f5; border: 1.5px solid var(--border); position: relative; cursor: grab; flex-shrink: 0; transition: border-color 0.1s; }
.angle-knob:hover { border-color: var(--primary); }
.angle-knob:active { cursor: grabbing; }
.angle-indicator { position: absolute; width: 3px; height: 10px; background: var(--primary); border-radius: 2px; left: 50%; top: 3px; margin-left: -1.5px; transform-origin: 50% 12px; }

/* ── DOCK ── */
.floating-dock { position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%); background: rgba(255,255,255,0.92); backdrop-filter: blur(20px) saturate(1.4); -webkit-backdrop-filter: blur(20px) saturate(1.4); padding: 7px 10px; border-radius: 18px; box-shadow: 0 2px 4px rgba(0,0,0,0.05), 0 8px 32px rgba(0,0,0,0.15); border: 1px solid var(--border); display: flex; gap: 4px; z-index: 2000; align-items: center; }
.dock-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 66px; height: 56px; background: transparent; border: none; border-radius: 11px; cursor: pointer; font-size: 11px; font-weight: 700; color: var(--text-main); transition: background 0.12s, transform 0.1s; gap: 3px; }
.dock-btn:hover { background: #f0f0f5; transform: translateY(-1px); }
.dock-btn:active { transform: scale(0.95); background: var(--primary-dim); }
.dock-btn .icon { font-size: 20px; line-height: 1; }
.dock-separator { width: 1px; height: 32px; background: var(--border); margin: 0 4px; flex-shrink: 0; }

/* ── LIBRARY & SETTINGS MODALS ── */
#library-modal, #settings-modal { position: fixed; inset: 0; z-index: 9000; display: none; align-items: center; justify-content: center; }
.lib-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.4); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.lib-dialog { position: relative; z-index: 1; background: #fff; border-radius: 16px; width: 520px; max-width: calc(100vw - 32px); max-height: 80vh; display: flex; flex-direction: column; box-shadow: 0 8px 16px rgba(0,0,0,0.08), 0 32px 80px rgba(0,0,0,0.2); overflow: hidden; animation: lib-in 0.2s ease; }
@keyframes lib-in { from { opacity: 0; transform: scale(0.95) translateY(8px); } to { opacity: 1; transform: scale(1) translateY(0); } }
.lib-header { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px 14px; border-bottom: 1px solid var(--border-subtle); flex-shrink: 0; }
.lib-title { font-weight: 700; font-size: 15px; color: var(--text-main); }
.lib-close { background: rgba(0,0,0,0.06); border: none; width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 14px; line-height: 1; color: var(--text-muted); display: flex; align-items: center; justify-content: center; transition: background 0.1s; }
.lib-close:hover { background: rgba(0,0,0,0.12); color: var(--text-main); }
.lib-list { flex: 1; overflow-y: auto; padding: 12px 16px; }
.lib-empty { text-align: center; color: var(--text-muted); font-size: 13px; line-height: 1.6; padding: 32px 24px; }
.lib-item { display: flex; align-items: center; gap: 12px; padding: 12px 12px; border-radius: 10px; border: 1px solid var(--border-subtle); margin-bottom: 8px; transition: border-color 0.12s, background 0.12s; }
.lib-item:hover { border-color: var(--border); background: #fafafa; }
.lib-item-swatch { width: 36px; height: 36px; border-radius: 8px; flex-shrink: 0; border: 1px solid rgba(0,0,0,0.08); }
.lib-item-info { flex: 1; min-width: 0; }
.lib-item-title { font-weight: 700; font-size: 13px; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px; }
.lib-item-meta { font-size: 11px; color: var(--text-muted); }
.lib-item-meta code { font-family: monospace; background: rgba(0,0,0,0.06); padding: 1px 5px; border-radius: 4px; }
.lib-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.lib-btn { padding: 6px 12px; border-radius: 7px; cursor: pointer; font-size: 12px; font-weight: 700; font-family: inherit; border: 1px solid var(--border); background: #fff; color: var(--text-main); transition: background 0.1s, border-color 0.1s; white-space: nowrap; }
.lib-btn:hover { background: #f0f0f5; border-color: #b0b0b8; }
.lib-btn.danger { border-color: #fca5a5; color: #dc2626; background: #fff5f5; }
.lib-btn.danger:hover { background: #fee2e2; border-color: #f87171; }
.lib-footer { padding: 14px 20px; border-top: 1px solid var(--border-subtle); background: #fafafa; flex-shrink: 0; }

/* Settings modal */
.settings-body { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.settings-label { font-size: 12px; font-weight: 700; color: var(--text-muted); display: block; margin-bottom: 6px; }
.settings-section-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-muted); padding-bottom: 10px; border-bottom: 1px solid var(--border-subtle); margin-bottom: 10px; }
.set-page-row { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 8px; border: 1px solid var(--border-subtle); margin-bottom: 6px; background: #fafafa; }
.set-page-row:last-child { margin-bottom: 0; }
.set-page-type { font-size: 14px; flex-shrink: 0; }
.set-page-name { flex: 1; min-width: 0; }

/* ── ASSET LIBRARY MODAL ── */
#asset-library-modal { position: fixed; inset: 0; z-index: 9000; display: none; align-items: center; justify-content: center; }
.alib-dialog { position: relative; z-index: 1; background: #fff; border-radius: 16px; width: 680px; max-width: calc(100vw - 32px); max-height: 80vh; display: flex; flex-direction: column; box-shadow: 0 8px 16px rgba(0,0,0,0.08), 0 32px 80px rgba(0,0,0,0.2); overflow: hidden; animation: lib-in 0.2s ease; }
.alib-body { flex: 1; overflow-y: auto; padding: 16px 20px 24px; display: flex; flex-direction: column; gap: 16px; }
.alib-section-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-muted); padding-bottom: 8px; border-bottom: 1px solid var(--border-subtle); }
.alib-empty-note { font-weight: 400; text-transform: none; letter-spacing: 0; }
.alib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.alib-audio-grid { display: flex; flex-direction: column; gap: 6px; }
.alib-card { border: 1.5px solid var(--border-subtle); border-radius: 10px; overflow: hidden; cursor: pointer; transition: border-color 0.12s, box-shadow 0.12s, transform 0.1s; background: #fafafa; }
.alib-card:hover { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-dim); transform: translateY(-1px); }
.alib-card-img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; background: var(--bg-app); }
.alib-card-vid { width: 100%; aspect-ratio: 4/3; background: #1d1d1f; display: flex; align-items: center; justify-content: center; font-size: 28px; color: #fff; }
.alib-card-label { font-size: 10px; font-weight: 600; color: var(--text-muted); padding: 5px 7px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.alib-card-audio { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; }
.alib-audio-icon { font-size: 18px; flex-shrink: 0; }
.alib-card-audio .alib-card-label { flex: 1; font-size: 12px; color: var(--text-main); padding: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.alib-audio-play { flex-shrink: 0; background: var(--primary-dim); border: none; color: var(--primary); width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center; transition: background 0.1s; }
.alib-audio-play:hover { background: var(--primary); color: #fff; }
#inp-soundFile { width: 100%; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── PRESERVE LAYOUT (MOBILE SCALE LOCK) ── */
.canvas.mobile-mode .preserve-layout {
    height: auto !important;
    min-height: 0 !important;
    aspect-ratio: var(--desk-w) / var(--desk-h) !important;
    container-type: inline-size;
}
.canvas.mobile-mode .preserve-layout.type-shape > .content-area {
    display: block !important;
    padding: 0 !important;
}
.canvas.mobile-mode .preserve-layout .builder-block {
    position: absolute !important;
    top: var(--top-pct) !important;
    left: var(--left-pct) !important;
    width: var(--w-pct) !important;
    height: var(--h-pct) !important;
    min-height: 0 !important;
    margin: 0 !important;
}
.canvas.mobile-mode .preserve-layout .builder-block > .content-area {
    padding: calc(var(--pad-num) / var(--root-desk-w) * 100cqw) !important;
}
.canvas.mobile-mode .preserve-layout .type-text > .content-area {
    font-size: calc(var(--fs-num) / var(--root-desk-w) * 100cqw) !important;
    letter-spacing: calc(var(--lsp-num) / var(--root-desk-w) * 100cqw) !important;
    line-height: var(--lh) !important;
}

/* ── MULTI-SELECT ── */
/* Blue outline on each individually-selected block */
.multi-selected {
    outline: 2px solid var(--primary) !important;
    outline-offset: 2px;
    z-index: 9998 !important;
}

/* Rubber-band marquee rectangle drawn while drag-selecting */
#marquee-select {
    position: absolute;
    border: 1.5px dashed var(--primary);
    background: rgba(0, 102, 255, 0.06);
    pointer-events: none;
    z-index: 9999;
    display: none;
    border-radius: 3px;
}