/* ===================================
   CANVAS & VIEWPORT — BloxUI Studio
   FILE: css/canvas.css
   ROLE: Canvas stage, viewport sizes (pc/mobile/tablet), element chrome, resize handles, smart guides overlay
   RELATED JS: js/renderer.js, js/canvasNav.js
   =================================== */

/* ========== CANVAS AREA ========== */
.canvas-area {
    flex: 1;
    background: var(--bg-main);
    background-image: radial-gradient(#2c303d 1px, transparent 1px);
    background-size: 20px 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    /* Dot grid stays fixed; only the stage pans/zooms */
    background-attachment: local;
}

/* Transform target for Figma-like pan & zoom */
.canvas-stage {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transform-origin: center center;
    will-change: transform;
    /* Keep stage itself from capturing layout size changes oddly */
    flex-shrink: 0;
}

.canvas-area.is-panning,
.canvas-area.is-panning * {
    cursor: grabbing !important;
}

.canvas-area.space-pan,
.canvas-area.space-pan .canvas-element {
    cursor: grab !important;
}

body.is-canvas-panning,
body.is-canvas-panning * {
    user-select: none !important;
    -webkit-user-select: none !important;
    cursor: grabbing !important;
}

/* Palette → canvas component drop */
body.is-component-dragging,
body.is-component-dragging * {
    cursor: copy !important;
    user-select: none !important;
    -webkit-user-select: none !important;
}

.canvas-area.drop-target::after {
    content: '';
    position: absolute;
    inset: 8px;
    border: 2px dashed var(--blue-roblox);
    border-radius: 10px;
    pointer-events: none;
    z-index: 80;
    box-shadow: inset 0 0 0 9999px rgba(41, 121, 255, 0.06);
}

/* Status cluster (magnet + zoom) — bottom-right of canvas */
.canvas-status {
    position: absolute;
    right: 16px;
    bottom: 16px;
    z-index: 60;
    display: flex;
    align-items: center;
    gap: 8px;
}

.canvas-magnet-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border-radius: 6px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    color: var(--text-dim, var(--text-muted));
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.canvas-magnet-btn svg {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}

.canvas-magnet-btn:hover {
    color: var(--text-main);
    border-color: var(--blue-roblox);
}

.canvas-magnet-btn.is-on {
    color: var(--blue-roblox);
    border-color: rgba(41, 121, 255, 0.55);
    background: rgba(41, 121, 255, 0.12);
}

.canvas-magnet-btn.is-hidden {
    display: none;
}

.canvas-zoom-badge {
    min-width: 52px;
    padding: 6px 10px;
    border-radius: 6px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    text-align: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: color 0.15s, border-color 0.15s, background 0.15s;
    user-select: none;
}

.canvas-zoom-badge:hover {
    color: var(--text-main);
    border-color: var(--blue-roblox);
}

.canvas-zoom-badge.is-default {
    opacity: 0.7;
}

/* ========== VIEWPORT TOOLBAR ========== */
.viewport-toolbar {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px;
    display: flex;
    gap: 6px;
    z-index: 50;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}

.viewport-toolbar.closed {
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, -10px);
    visibility: hidden;
}

.viewport-btn {
    background: var(--bg-main);
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: 0.15s;
}

.viewport-btn:hover {
    border-color: var(--blue-roblox);
    color: var(--text-main);
}

.viewport-btn.active {
    background: var(--blue-roblox);
    border-color: var(--blue-roblox);
    color: white;
}

.viewport-btn svg {
    width: 14px;
    height: 14px;
}

/* ========== CANVAS CONTAINER (ADAPTIVE) ========== */
.canvas-container {
    position: relative;
    /* Animate only size/radius when switching PC/Mobile/Tablet —
       never fight with the pan/zoom transform on .canvas-stage */
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                height 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                border-radius 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                box-shadow 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
}

/* PC Desktop 16:9 */
.canvas-container.viewport-pc {
    width: 1000px;
    height: 563px;
    background: var(--bg-panel);
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

/* Mobile Phone LANDSCAPE (16:9) — Roblox games run in landscape,
   so the phone preview is wide & short instead of tall & narrow. */
.canvas-container.viewport-mobile {
    width: 680px;
    height: 340px;
    border-radius: 30px;
    background-color: #000;
    box-shadow: inset 0 0 0 3px #333, inset 0 0 0 14px #0f0f12, 0 30px 60px rgba(0, 0, 0, 0.6), 0 0 0 1px #555;
    overflow: hidden;
}

/* Tablet 4:3 */
.canvas-container.viewport-tablet {
    width: 640px;
    height: 480px;
    background: var(--bg-panel);
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

/* ========== PHONE SCREEN ========== */
.phone-screen {
    position: absolute;
    top: 14px;
    left: 14px;
    right: 14px;
    bottom: 14px;
    border-radius: 24px;
    overflow: hidden;
    background-color: #111;
}

.viewport-mobile .phone-screen {
    display: block;
}

.viewport-pc .phone-screen,
.viewport-tablet .phone-screen {
    display: none;
}

/* ========== PHONE BACKGROUND ========== */
.phone-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('../images/main_fon_game.png');
    background-size: cover;
    background-position: center;
    filter: blur(2px);
    transform: scale(1.05);
}

/* ========== CANVAS CONTENT (ROBLOX GUI) ========== */
.canvas-content {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.viewport-mobile .canvas-content {
    position: absolute;
    top: 14px;
    left: 14px;
    width: calc(100% - 28px);
    height: calc(100% - 28px);
    /* Do not combine the base 100% dimensions with right/bottom offsets:
       that makes the mobile flex area taller than the phone screen and
       moves every centred Roblox element downward. */
    box-sizing: border-box;
}

/* PC Desktop Background */
.viewport-pc .canvas-content::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('../images/main_fon_game.png');
    background-size: cover;
    background-position: center;
    filter: blur(2px);
    transform: scale(1.05);
    z-index: 0;
}

/* Tablet Background */
.viewport-tablet .canvas-content::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url('../images/main_fon_game.png');
    background-size: cover;
    background-position: center;
    filter: blur(1px);
    z-index: 0;
}

/* ========== INTERACTIVE ELEMENTS (DRAGGABLE) ========== */
.canvas-element {
    position: absolute;
    cursor: move;
    /* Outer node stays visible so resize handles are not cropped.
       Clip Content is applied on the inner .element-clip layer. */
    overflow: visible;
    transition: box-shadow 0.15s;
}

/* Hover / Press Scale — must live in the same `transform` as AnchorPoint.
   Independent CSS `scale` is applied around the border-box origin (0,0),
   then translate() shifts the already-grown box, so the button appears
   to expand from a corner (often right→left with AnchorPoint 0.5).
   Roblox tweens Size+Position from the visual center; this matches that. */
.canvas-element.has-interactive-scale {
    --hover-scale: 1.05;
    --press-scale: 0.95;
    --anim-speed: 0.15s;
    --anchor-tx: 0%;
    --anchor-ty: 0%;
    --preview-scale: 1;
    transform-origin: center center;
    transform: translate(var(--anchor-tx), var(--anchor-ty)) scale(var(--preview-scale));
    transition: box-shadow 0.15s,
                transform var(--anim-speed) cubic-bezier(0.34, 1.56, 0.64, 1),
                filter var(--anim-speed) ease;
}

.canvas-element.has-interactive-scale:not(.scale-locked):hover,
.canvas-element.has-interactive-scale.force-hover {
    --preview-scale: var(--hover-scale);
    filter: brightness(1.15);
    z-index: 11;
}

/* :active is unreliable: canvas mousedown calls preventDefault() and also
   used to set body.is-element-interacting immediately (--preview-scale: 1).
   Live press is driven by .is-pressed from renderer.setLivePress(). */
.canvas-element.has-interactive-scale:not(.scale-locked):active,
.canvas-element.has-interactive-scale:not(.scale-locked).is-pressed,
.canvas-element.has-interactive-scale.force-pressed {
    --preview-scale: var(--press-scale);
    filter: brightness(0.9);
    z-index: 11;
}

body.is-element-interacting .canvas-element.has-interactive-scale,
.canvas-area.is-panning .canvas-element.has-interactive-scale,
.canvas-area.space-pan .canvas-element.has-interactive-scale {
    --preview-scale: 1 !important;
    filter: none !important;
    transition: box-shadow 0.15s !important;
}

.element-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    pointer-events: none;
    border-radius: inherit;
    overflow: hidden;
    z-index: 0;
}

.element-clip {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border-radius: inherit;
    overflow: visible;
    z-index: 1;
    /* Own stacking context so a filtered glow sibling cannot composite
       over the GuiObject fill / text / image. */
    isolation: isolate;
}

/* Glow lives on a sibling of the clip layer so selection/hover box-shadow
   on .canvas-element does not replace the effect, and Clip Content does
   not crop the halo (matches the Roblox wrapper export).
   Solid glow uses box-shadow (transparent fill). Gradient glow paints a
   blurred background — the component fill therefore lives on .element-clip
   (z-index: 1) so the halo stays behind the GuiObject. */
.element-glow {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border-radius: inherit;
    pointer-events: none;
    z-index: 0;
    overflow: visible;
}

/* Gradient UIStroke only. The parent keeps a transparent CSS border of
   the same thickness so children stay inset (Solid → Gradient must not
   grow content). Negative inset expands the overlay over that spacer;
   the mask punches the padding-box so the fill / children show through.
   Inner clip radius is R − thickness so corners meet. */
.element-stroke-grad {
    position: absolute;
    inset: calc(-1 * var(--stroke-thickness, 0px));
    box-sizing: border-box;
    border-radius: inherit;
    pointer-events: none;
    z-index: 3;
    padding: var(--stroke-thickness, 0px);
    background: var(--stroke-grad, transparent);
    -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask-composite: exclude;
}

.element-shimmer {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 4;
    overflow: hidden;
    border-radius: inherit;
}

.element-shimmer-line {
    position: absolute;
    top: -60%;
    left: -60%;
    width: 220%;
    height: 220%;
    background: linear-gradient(
        115deg,
        rgba(255, 255, 255, 0) 35%,
        rgba(255, 255, 255, 0.55) 50%,
        rgba(255, 255, 255, 0) 65%
    );
    transform: translateX(-100%);
    animation: rbxShimmer var(--shimmer-speed, 2s) infinite ease-in-out;
}

@keyframes rbxShimmer {
    0% { transform: translateX(-100%); }
    60%, 100% { transform: translateX(100%); }
}

/* Pixel SVG (no viewBox stretch). Fill + stroke live on one <text>,
   matching demo_rbx_gradient_stroke_text.html. */
.element-text-stroke-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
    z-index: 2;
}

.element-text-stroke-svg text {
    paint-order: stroke fill;
}

.element-text.rich-text .rich-text-content {
    min-width: 0;
    white-space: inherit;
}

.element-text.rich-text [data-rt-space],
.richtext-editor [data-rt-space] {
    white-space: pre;
}

.element-text.rich-text b,
.element-text.rich-text strong {
    font-weight: 800;
}

.element-text.rich-text i,
.element-text.rich-text em {
    font-style: italic;
}

.element-text.rich-text s,
.element-text.rich-text strike,
.element-text.rich-text del {
    text-decoration: line-through;
    text-decoration-color: currentColor;
}

.canvas-element.clips-descendants > .element-clip {
    overflow: hidden;
    contain: paint;
    isolation: isolate;
}

.canvas-element.scrolling-frame > .element-clip {
    overflow-y: auto;
    overflow-x: hidden;
    isolation: isolate;
    margin-right: 4px;
}

.canvas-element.scrolling-frame.scroll-dir-x > .element-clip {
    overflow-x: auto;
    overflow-y: hidden;
    margin-right: 0;
}

/* Right/Left scrollbar side. VerticalScrollBarPosition is a Roblox property
   and Roblox mirrors the content along with the bar, hence the rtl trick. */
.canvas-element.scrolling-frame.scroll-bar-left > .element-clip {
    direction: rtl;
    margin-right: 0;
    margin-left: 4px;
}

.canvas-element.scrolling-frame.scroll-bar-left > .element-clip > * {
    direction: ltr;
}

/* ---------- Scrollbar thumb (color / pill / Texture ID) ---------- */
/* demo_ScrollFrame.html style exact matching */
.canvas-element.scrolling-frame > .element-clip::-webkit-scrollbar-button,
.canvas-element.scrolling-frame .element-clip::-webkit-scrollbar-button,
.canvas-element.scrolling-frame::-webkit-scrollbar-button,
::-webkit-scrollbar-button {
    display: none !important;
    width: 0 !important;
    height: 0 !important;
}

.canvas-element.scrolling-frame > .element-clip::-webkit-scrollbar {
    width: var(--sb-thickness, 6px);
    height: var(--sb-thickness, 6px);
}

.canvas-element.scrolling-frame > .element-clip::-webkit-scrollbar-track {
    background: transparent;
}

.canvas-element.scrolling-frame > .element-clip::-webkit-scrollbar-thumb {
    background-color: var(--sb-thumb-color, rgba(255, 0, 234, 1));
    background-image: var(--sb-thumb-image, none);
    background-repeat: repeat;
    background-size: contain;
    border-radius: var(--sb-radius, 0px);
    border: none;
}

.canvas-element.scrolling-frame > .element-clip::-webkit-scrollbar-corner {
    background: transparent;
}

/* ---------- UIPadding ----------
   Padding is written inline by renderer.applyScrollFrameStyles because
   ensureClipLayer() pins the clip geometry (including padding) inline. */

/* ---------- UIListLayout ---------- */
.canvas-element.scrolling-frame.scroll-layout-list > .element-clip {
    display: flex;
    gap: var(--scroll-list-pad, 6px);
    align-items: var(--scroll-list-align-items, flex-start);
    justify-content: var(--scroll-list-justify, flex-start);
    align-content: flex-start;
    flex-wrap: nowrap;
}

/* In-flow items must still be a containing block for their .element-clip
   (absolute inset 0). `static` would paint the fill across the whole scroll.
   The class lives on the scrolling .element-clip itself, so the child rule
   has to match .scroll-layout-* directly — not only as a descendant of
   .scrolling-frame > .element-clip. */
.canvas-element.scrolling-frame.scroll-layout-list > .element-clip > .canvas-element,
.canvas-element.scrolling-frame.scroll-layout-grid > .element-clip > .canvas-element,
.element-clip.scroll-layout-list > .canvas-element,
.element-clip.scroll-layout-grid > .canvas-element {
    position: relative;
    left: auto;
    top: auto;
    flex-shrink: 0;
}

/* ---------- UIGridLayout ---------- */
.canvas-element.scrolling-frame.scroll-layout-grid > .element-clip,
.element-clip.scroll-layout-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, var(--scroll-grid-cell-x, 95px));
    grid-auto-rows: var(--scroll-grid-cell-y, 95px);
    grid-auto-columns: var(--scroll-grid-cell-x, 95px);
    column-gap: var(--scroll-grid-gap-x, 8px);
    row-gap: var(--scroll-grid-gap-y, 8px);
    align-content: var(--scroll-grid-align-content, start);
    justify-content: var(--scroll-grid-justify-content, start);
    justify-items: var(--scroll-grid-justify-items, start);
    align-items: var(--scroll-grid-align-items, start);
}

/* Horizontal scroll (ScrollingDirection X): FixedSize divides the rows, so a
   column-flow grid must pin its rows to the track instead of using auto-fill. */
.element-clip.scroll-layout-grid.scroll-dir-x {
    grid-template-columns: none;
    grid-template-rows: repeat(auto-fill, var(--scroll-grid-cell-y, 95px));
}

.element-clip.scroll-layout-grid:not(.scroll-dir-x) {
    grid-template-rows: none;
}

.canvas-element.selected {
    box-shadow: 0 0 0 2px var(--focus-color);
    z-index: 10;
}

.canvas-element:hover {
    box-shadow: 0 0 0 1px var(--blue-roblox);
}

body.is-element-interacting,
body.is-element-interacting * {
    user-select: none !important;
    -webkit-user-select: none !important;
}

body.is-element-interacting,
body.is-element-interacting * {
    cursor: grabbing !important;
}

body.is-element-interacting[data-resize-cursor="nw-resize"],
body.is-element-interacting[data-resize-cursor="nw-resize"] * { cursor: nw-resize !important; }
body.is-element-interacting[data-resize-cursor="ne-resize"],
body.is-element-interacting[data-resize-cursor="ne-resize"] * { cursor: ne-resize !important; }
body.is-element-interacting[data-resize-cursor="sw-resize"],
body.is-element-interacting[data-resize-cursor="sw-resize"] * { cursor: sw-resize !important; }
body.is-element-interacting[data-resize-cursor="se-resize"],
body.is-element-interacting[data-resize-cursor="se-resize"] * { cursor: se-resize !important; }
body.is-element-interacting[data-resize-cursor="n-resize"],
body.is-element-interacting[data-resize-cursor="n-resize"] * { cursor: n-resize !important; }
body.is-element-interacting[data-resize-cursor="s-resize"],
body.is-element-interacting[data-resize-cursor="s-resize"] * { cursor: s-resize !important; }
body.is-element-interacting[data-resize-cursor="w-resize"],
body.is-element-interacting[data-resize-cursor="w-resize"] * { cursor: w-resize !important; }
body.is-element-interacting[data-resize-cursor="e-resize"],
body.is-element-interacting[data-resize-cursor="e-resize"] * { cursor: e-resize !important; }

/* ========== RESIZE HANDLES ========== */
.resize-handle {
    position: absolute;
    width: 8px;
    height: 8px;
    background: white;
    border: 1px solid var(--blue-roblox);
    border-radius: 50%;
    z-index: 20;
    pointer-events: auto;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.resize-handle.nw { top: -4px; left: -4px; cursor: nw-resize; }
.resize-handle.ne { top: -4px; right: -4px; cursor: ne-resize; }
.resize-handle.sw { bottom: -4px; left: -4px; cursor: sw-resize; }
.resize-handle.se { bottom: -4px; right: -4px; cursor: se-resize; }
.resize-handle.n { top: -4px; left: 50%; transform: translateX(-50%); cursor: n-resize; }
.resize-handle.s { bottom: -4px; left: 50%; transform: translateX(-50%); cursor: s-resize; }
.resize-handle.w { left: -4px; top: 50%; transform: translateY(-50%); cursor: w-resize; }
.resize-handle.e { right: -4px; top: 50%; transform: translateY(-50%); cursor: e-resize; }

/* AnchorPoint pin — editor-only, not exported. Sits on the selected element. */
.anchor-marker {
    position: absolute;
    width: 10px;
    height: 10px;
    left: 50%;
    top: 50%;
    background: #ef4444;
    border: 2px solid #ffffff;
    border-radius: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 25;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.75);
}

/* Smart Guides overlay — pointer-events none, cleared on mouseup */
.guides-container {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 400;
    overflow: visible;
}

.guide-line {
    position: absolute;
    pointer-events: none;
}

.guide-line.vertical {
    width: 1px;
    background: var(--accent);
}

.guide-line.horizontal {
    height: 1px;
    background: var(--accent);
}

.guide-line.screen-center {
    background: var(--blue-roblox) !important;
}

.distance-measure {
    position: absolute;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 91;
}

.distance-measure.horizontal {
    height: 14px;
    border-left: 1px solid var(--accent);
    border-right: 1px solid var(--accent);
}

.distance-measure.horizontal .measure-bar {
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--accent);
}

.distance-measure.vertical {
    width: 14px;
    border-top: 1px solid var(--accent);
    border-bottom: 1px solid var(--accent);
}

.distance-measure.vertical .measure-bar {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--accent);
}

.distance-badge {
    position: relative;
    z-index: 2;
    background: var(--bg-panel);
    border: 1px solid var(--accent);
    color: #ffffff;
    font-size: 10px;
    font-weight: 700;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    padding: 1px 5px;
    border-radius: 4px;
    white-space: nowrap;
    line-height: 1.2;
}
