/* =========================================================
   Tutorial Studio
   编辑画布与视口样式

   文件：stage.css

   主要功能：
   1. 中间编辑区域布局
   2. 编辑画布面板
   3. 编辑区顶部工具栏
   4. 画布视口区域
   5. 画布外框
   6. Preview iframe
   7. 元素选中框
   8. 八方向缩放控制点
   9. 旋转控制点
   10. 底部状态栏
   11. 画布缩放工具栏

   说明：
   画布的设计尺寸、缩放比例和位置由
   ViewportController 统一管理。
========================================================= */


/* =========================================================
   中间编辑区域
   中间区域独立滚动，不带动整个工作区
========================================================= */

.center-column {
    position: relative;

    width: 100%;
    min-width: 0;
    min-height: 0;
    height: 100%;

    display: flex;
    flex-direction: column;
    flex: 1 1 0;

    gap: 10px;
    align-self: stretch;

    box-sizing: border-box;

    overflow-x: hidden;
    overflow-y: auto;

    overscroll-behavior-y: contain;
}

/* =========================================================
   二、编辑画布面板
========================================================= */

.stage-panel {
    position: relative;

    width: 100%;
    min-width: 0;
    min-height: 0;

    height: auto;
    flex: 0 0 auto;

    display: flex;
    flex-direction: column;

    overflow: visible;
}

/* =========================================================
   三、编辑区顶部工具栏
========================================================= */

.stage-toolbar {
    position: relative;

    width: 100%;
    height: 48px;
    min-height: 48px;

    flex: 0 0 48px;

    background: #ffffff;

    border-bottom: 1px solid var(--line);

    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 0 14px;

    box-sizing: border-box;

    font-size: 12px;

    z-index: 30;
}


/* 工具栏辅助文字 */

.muted {
    color: #9ba4b2;

    font-weight: 400;
}


/* 工具栏操作区域 */

.stage-actions {
    display: flex;

    align-items: center;

    gap: 7px;

    min-width: 0;
}


/* 普通工具栏按钮 */

.icon-btn {
    width: 29px;
    height: 29px;

    flex: 0 0 auto;

    display: inline-flex;
    align-items: center;
    justify-content: center;

    padding: 0;

    border: 1px solid var(--line);

    background: #ffffff;

    border-radius: 7px;

    cursor: pointer;

    font-size: 19px;

    color: #344054;

    box-sizing: border-box;

    transition:
        background-color 0.15s ease,
        border-color 0.15s ease;
}


.icon-btn:hover {
    background: #f3f5f9;

    border-color: #c5cddd;
}


/* =========================================================
   四、编辑画布区域
========================================================= */

.stage-wrap {
    position: relative;
    min-width: 0;
    width: 100%;

    /*
     * 首屏高度占位。
     *
     * 画布的真实高度由 ViewportController.updateWrapHeight()
     * 按页面比例写入行内样式。
     *
     * 这里用 16:9 占位，避免初始化前后
     * 从固定 500px 跳到真实高度、整块布局跟着抖一下。
     */
    aspect-ratio: 16 / 9;
    height: auto;
    min-height: 0;
    flex: 0 0 auto;

    display: flex;
    align-items: center;
    justify-content: center;

    padding: 8px;
    box-sizing: border-box;

    overflow: hidden;
}


/*
 * 画布就绪前的占位块。
 *
 * 位置和大小与“适应画布”之后的画布一致：
 * 视口 padding 为 24px，
 * 这里相对 padding box 定位，所以取 24 - 8 = 16px。
 */
.stage-wrap:not(.is-stage-ready)::before {
    content: "";

    position: absolute;

    left: 16px;
    right: 16px;
    top: 50%;

    aspect-ratio: 16 / 9;

    transform: translateY(-50%);

    border-radius: 6px;

    background: rgba(128, 140, 160, 0.1);

    pointer-events: none;
}

/* =========================================================
   五、编辑画布外框
========================================================= */

/*
 * 注意：
 *
 * 画布实际尺寸由 ViewportController 设置。
 *
 * 例如：
 *
 * 1280 × 720
 * 750 × 1950
 * 1080 × 1920
 *
 * 不再使用固定的 16:9 比例限制。
 */

.stage-frame {
    position: absolute;

    left: 0;
    top: 0;

    /*
     * 初始设计尺寸。
     * 初始化视口控制器后会更新。
     */
    width: 1280px;
    height: 720px;

    /*
     * 视口完成“适应画布”之前不显示画布。
     *
     * 否则首屏会先按上面的裸尺寸
     * 贴在左上角画一帧，
     * 右边和下面各留一段空白，
     * 过一下才跳到正确大小。
     *
     * 用 visibility 而不是 display，
     * 保证元素仍然参与布局，
     * 视口测量和辅助线计算不受影响。
     */
    visibility: hidden;

    min-width: 0;
    min-height: 0;

    max-width: none;
    max-height: none;

    /*
     * 实际显示尺寸由视口控制器负责。
     */
    aspect-ratio: auto;

    flex: none;

    margin: 0;
    padding: 0;

    border: 0;

    box-sizing: border-box;

    transform: none;

    transform-origin: top left;

    /*
     * 不在这里设置 overflow: hidden，
     * 避免干扰选中框和控制点的显示。
     */
}


/*
 * 视口完成首屏适配后，
 * 由 ViewportController 给 .stage-wrap 加上 is-stage-ready，
 * 画布这时才显示。
 */
.stage-wrap.is-stage-ready .stage-frame {
    visibility: visible;
}

/* =========================================================
   页面切换效果
========================================================= */

.stage-frame[data-transition="fade"].is-page-transitioning iframe {
    animation: ts-editor-page-fade .32s ease-out both;
}

.stage-frame[data-transition="slide"].is-page-transitioning iframe {
    animation: ts-editor-page-push .36s cubic-bezier(.22, .61, .36, 1) both;
}

.stage-frame[data-transition="zoom"].is-page-transitioning iframe {
    animation: ts-editor-page-zoom .36s ease-out both;
}

.stage-frame[data-transition="wipe"].is-page-transitioning iframe {
    animation: ts-editor-page-wipe .4s cubic-bezier(.22, .61, .36, 1) both;
}

@keyframes ts-editor-page-fade {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes ts-editor-page-push {
    from { clip-path: inset(0 0 0 100%); }
    to { clip-path: inset(0); }
}

@keyframes ts-editor-page-zoom {
    from {
        opacity: 0;
        filter: blur(7px);
    }

    to {
        opacity: 1;
        filter: blur(0);
    }
}

@keyframes ts-editor-page-wipe {
    from { clip-path: inset(100% 0 0 0); }
    to { clip-path: inset(0); }
}

@media (prefers-reduced-motion: reduce) {
    .stage-frame.is-page-transitioning iframe {
        animation: none !important;
    }
}


/* =========================================================
   六、Preview iframe
========================================================= */

.stage-frame iframe {
    position: absolute;

    left: 0;
    top: 0;

    /*
     * 默认设计尺寸。
     * preview.js 和 ViewportController 会同步更新。
     */
    width: 1280px;
    height: 720px;

    min-width: 0;
    min-height: 0;

    max-width: none;
    max-height: none;

    display: block;

    margin: 0;
    padding: 0;

    border: 0;

    background: #ffffff;

    border-radius: 5px;

    box-sizing: border-box;

    /*
     * 缩放从左上角开始。
     */
    transform-origin: top left;

    /*
     * iframe 的缩放由 ViewportController 控制。
     */

    box-shadow:
        0 20px 60px rgba(32, 42, 64, 0.16);
}


/* =========================================================
   七、元素选中框
========================================================= */

.selection-box {
    position: absolute;

    border: 1px solid #6171e8;

    box-shadow:
        0 0 0 1px rgba(97, 113, 232, 0.16);

    box-sizing: border-box;

    /*
     * 选中框主体不拦截鼠标事件。
     */
    pointer-events: none;

    z-index: 20;
}


/* 隐藏选中框 */

.selection-box.hidden {
    display: none;
}


/* =========================================================
   八、八方向缩放控制点
========================================================= */

.selection-box .transform-handle {
    position: absolute;

    width: 10px;
    height: 10px;

    background: #ffffff;

    border: 2px solid #6171e8;

    border-radius: 50%;

    box-sizing: border-box;

    /*
     * 控制点允许接收鼠标事件。
     */
    pointer-events: auto;

    z-index: 2;
}


/* 左上 */

.transform-handle-nw {
    left: -5px;
    top: -5px;

    cursor: nwse-resize;
}


/* 上 */

.transform-handle-n {
    left: 50%;
    top: -5px;

    transform: translateX(-50%);

    cursor: ns-resize;
}


/* 右上 */

.transform-handle-ne {
    right: -5px;
    top: -5px;

    cursor: nesw-resize;
}


/* 右 */

.transform-handle-e {
    right: -5px;
    top: 50%;

    transform: translateY(-50%);

    cursor: ew-resize;
}


/* 右下 */

.transform-handle-se {
    right: -5px;
    bottom: -5px;

    cursor: nwse-resize;
}


/* 下 */

.transform-handle-s {
    left: 50%;
    bottom: -5px;

    transform: translateX(-50%);

    cursor: ns-resize;
}


/* 左下 */

.transform-handle-sw {
    left: -5px;
    bottom: -5px;

    cursor: nesw-resize;
}


/* 左 */

.transform-handle-w {
    left: -5px;
    top: 50%;

    transform: translateY(-50%);

    cursor: ew-resize;
}


/* =========================================================
   九、旋转控制点
========================================================= */

.selection-box .transform-rotate-handle {
    position: absolute;

    left: 50%;
    top: -28px;

    width: 12px;
    height: 12px;

    transform: translateX(-50%);

    background: #ffffff;

    border: 2px solid #6171e8;

    border-radius: 50%;

    box-sizing: border-box;

    pointer-events: auto;

    cursor: grab;

    z-index: 3;
}


/* 旋转时的鼠标状态 */

.selection-box .transform-rotate-handle:active {
    cursor: grabbing;
}


/* 旋转控制点连接线 */

.selection-box .transform-rotate-handle::before {
    content: "";

    position: absolute;

    left: 50%;
    top: 10px;

    width: 1px;
    height: 15px;

    background: #6171e8;

    transform: translateX(-50%);

    pointer-events: none;
}


/* =========================================================
   十、编辑区底部状态栏
========================================================= */

.stage-footer {
    position: relative;

    width: 100%;
    height: 40px;
    min-height: 40px;

    flex: 0 0 40px;

    background: #ffffff;

    border-top: 1px solid var(--line);

    display: flex;

    justify-content: space-between;
    align-items: center;

    padding: 0 17px;

    box-sizing: border-box;

    color: #9aa3b0;

    font-size: 10px;

    z-index: 30;
}


/* =========================================================
   十一、画布视口工具栏
========================================================= */

.viewport-toolbar {
    display: flex;

    align-items: center;

    gap: 7px;

    flex-shrink: 0;

    min-width: 0;
}


/* 视口按钮 */

.viewport-toolbar .icon-btn {
    display: inline-flex;

    align-items: center;
    justify-content: center;

    width: 30px;
    height: 30px;

    flex: 0 0 30px;

    padding: 0;

    font-size: 20px;

    color: #344054;

    background: #ffffff;

    border: 1px solid var(--line);

    border-radius: 7px;

    cursor: pointer;

    box-sizing: border-box;

    transition:
        background-color 0.15s ease,
        border-color 0.15s ease;
}


.viewport-toolbar .icon-btn:hover {
    background: #f3f5f9;

    border-color: #c5cddd;
}


/* 缩放百分比 */

.viewport-zoom-value {
    min-width: 48px;

    text-align: center;

    color: #344054;

    font-size: 12px;

    font-weight: 600;

    font-variant-numeric: tabular-nums;

    white-space: nowrap;
}


/* 适应画布、实际大小等操作按钮 */

.viewport-action-btn {
    height: 30px;

    flex: 0 0 auto;

    padding: 0 11px;

    border: 1px solid var(--line);

    border-radius: 7px;

    background: #ffffff;

    color: #344054;

    font-size: 12px;

    white-space: nowrap;

    cursor: pointer;

    box-sizing: border-box;

    transition:
        background-color 0.15s ease,
        border-color 0.15s ease;
}


.viewport-action-btn:hover {
    background: #f3f5f9;

    border-color: #c5cddd;
}


/* =========================================================
   十二、窄屏布局
========================================================= */

@media (max-width: 900px) {
    .stage-toolbar {
        padding: 0 10px;
    }

    .stage-wrap {
        padding: 6px;
    }

    .stage-footer {
        padding: 0 10px;
    }

    .viewport-toolbar {
        gap: 4px;
    }

    .viewport-toolbar .icon-btn {
        width: 28px;
        height: 28px;

        flex-basis: 28px;
    }

    .viewport-zoom-value {
        min-width: 42px;
    }

    .viewport-action-btn {
        padding: 0 7px;

        font-size: 11px;
    }
}

/* =========================================================
   Workspace Dock
   工作区底部工具栏
   仅控制底栏及二级面板，不修改画布和变换系统
========================================================= */

.workspace-dock {
    position: relative;

    z-index: 30;

    flex: 0 0 auto;

    width: 100%;
    min-width: 0;

    box-sizing: border-box;

    background: #ffffff;

    border-top: 1px solid #e8ebf0;

    color: #344054;

    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        "PingFang SC",
        "Microsoft YaHei",
        sans-serif;
}


/* =========================================================
   底栏主体
========================================================= */

.workspace-toolbar {
    display: flex;

    align-items: center;
    justify-content: center;

    gap: 8px;

    width: 100%;
    height: 46px;

    padding: 4px 12px;

    box-sizing: border-box;

    overflow-x: auto;
    overflow-y: hidden;

    scrollbar-width: none;
}

.workspace-toolbar::-webkit-scrollbar {
    display: none;
}


/* =========================================================
   统一按钮样式
========================================================= */

.workspace-tool {
    position: relative;

    display: inline-flex;

    flex: 0 0 40px;

    align-items: center;
    justify-content: center;

    width: 40px;
    height: 36px;

    padding: 0;
    margin: 0;

    border: 1px solid transparent;
    border-radius: 8px;

    background: transparent;

    color: #475467;

    cursor: pointer;

    outline: none;

    -webkit-tap-highlight-color: transparent;

    transition:
        background-color 0.16s ease,
        border-color 0.16s ease,
        color 0.16s ease;
}

.workspace-tool svg {
    display: block;

    width: 19px;
    height: 19px;

    fill: none;

    stroke: currentColor;
    stroke-width: 1.7;

    stroke-linecap: round;
    stroke-linejoin: round;

    pointer-events: none;
}


/* 鼠标悬停 */

.workspace-tool:hover {
    background: #f2f5f9;

    color: #175cd3;
}


/* 当前打开的工具 */

.workspace-tool.is-active {
    background: #edf4ff;

    border-color: #d6e6ff;

    color: #175cd3;
}


/* 键盘焦点 */

.workspace-tool:focus-visible {
    outline: 2px solid #84adff;

    outline-offset: 2px;
}


/* =========================================================
   Hover Tooltip
   仅在支持鼠标悬停的设备上显示
========================================================= */

.workspace-tooltip {
    position: absolute;

    left: 50%;
    bottom: calc(100% + 8px);

    z-index: 100;

    display: block;

    width: max-content;
    max-width: 120px;

    padding: 5px 8px;

    border-radius: 5px;

    background: #1d2939;

    color: #ffffff;

    font-size: 11px;
    font-weight: 500;
    line-height: 1.4;

    white-space: nowrap;

    opacity: 0;

    visibility: hidden;

    pointer-events: none;

    transform: translate(-50%, 3px);

    transition:
        opacity 0.14s ease,
        transform 0.14s ease,
        visibility 0.14s ease;
}

@media (hover: hover) and (pointer: fine) {

    .workspace-tool:hover .workspace-tooltip {
        opacity: 1;

        visibility: visible;

        transform: translate(-50%, 0);
    }

}


/* =========================================================
   二级面板
========================================================= */

.workspace-popover {
    position: absolute;

    left: 50%;
    bottom: calc(100% + 8px);

    z-index: 120;

    width: min(400px, calc(100% - 24px));

    max-height: min(360px, 65vh);

    overflow-x: hidden;
    overflow-y: auto;

    box-sizing: border-box;

    border: 1px solid #e4e7ec;

    border-radius: 12px;

    background: #ffffff;

    color: #344054;

    box-shadow:
        0 12px 32px rgba(16, 24, 40, 0.12),
        0 2px 6px rgba(16, 24, 40, 0.04);

    transform: translateX(-50%);

    scrollbar-width: thin;
}


/* hidden 必须始终有效 */

.workspace-popover[hidden] {
    display: none !important;
}


/* =========================================================
   面板标题栏
========================================================= */

.workspace-popover-head {
    position: sticky;

    top: 0;

    z-index: 2;

    display: flex;

    align-items: center;
    justify-content: space-between;

    min-height: 42px;

    padding: 0 12px;

    border-bottom: 1px solid #eef0f4;

    background: #ffffff;
}

.workspace-popover-title {
    overflow: hidden;

    color: #101828;

    font-size: 13px;
    font-weight: 600;

    text-overflow: ellipsis;
    white-space: nowrap;
}

.workspace-popover-close {
    display: inline-flex;

    flex: 0 0 28px;

    align-items: center;
    justify-content: center;

    width: 28px;
    height: 28px;

    padding: 0;

    border: 0;

    border-radius: 6px;

    background: transparent;

    color: #667085;

    font-size: 22px;
    line-height: 1;

    cursor: pointer;
}

.workspace-popover-close:hover {
    background: #f2f4f7;

    color: #101828;
}


/* =========================================================
   面板内容
========================================================= */

.workspace-popover-content {
    display: grid;

    grid-template-columns: repeat(3, minmax(0, 1fr));

    gap: 8px;

    padding: 12px;
}


/* 二级菜单按钮 */

.workspace-action {
    display: flex;

    align-items: center;
    justify-content: flex-start;

    gap: 8px;

    min-width: 0;
    min-height: 38px;

    padding: 8px 10px;

    border: 1px solid #eaecf0;

    border-radius: 8px;

    background: #ffffff;

    color: #344054;

    font-size: 12px;
    font-weight: 500;

    line-height: 1.4;

    text-align: left;

    cursor: pointer;

    transition:
        background-color 0.14s ease,
        border-color 0.14s ease,
        color 0.14s ease;
}

.workspace-action:hover {
    border-color: #b2ccff;

    background: #f5f8ff;

    color: #175cd3;
}

.workspace-action:focus-visible {
    outline: 2px solid #84adff;

    outline-offset: 1px;
}

.workspace-action:disabled {
    opacity: 0.45;

    cursor: not-allowed;
}

.workspace-action-icon {
    display: inline-flex;

    flex: 0 0 18px;

    align-items: center;
    justify-content: center;

    width: 18px;
    height: 18px;

    color: #667085;

    font-size: 15px;
}

.workspace-action-label {
    min-width: 0;

    overflow: hidden;

    text-overflow: ellipsis;
    white-space: nowrap;
}


/* =========================================================
   面板底部提示
========================================================= */

.workspace-popover-note {
    padding: 0 12px 12px;

    color: #667085;

    font-size: 11px;
    line-height: 1.6;
}


/* =========================================================
   手机端
========================================================= */

@media (max-width: 640px) {

    .workspace-toolbar {
        justify-content: space-around;

        gap: 2px;

        height: 44px;

        padding: 3px 6px;
    }

    .workspace-tool {
        flex: 1 1 0;

        min-width: 0;
        max-width: 56px;

        height: 36px;
    }

    .workspace-tooltip {
        display: none;
    }

    .workspace-popover {
        left: 8px;
        right: 8px;

        bottom: calc(100% + 6px);

        width: auto;

        max-height: min(55vh, 400px);

        transform: none;
    }

    .workspace-popover-content {
        grid-template-columns: repeat(2, minmax(0, 1fr));

        gap: 7px;

        padding: 10px;
    }

    .workspace-action {
        min-height: 40px;

        padding: 8px;
    }

}


/* =========================================================
   减少动态效果
========================================================= */

@media (prefers-reduced-motion: reduce) {

    .workspace-tool,
    .workspace-tooltip,
    .workspace-action {
        transition: none;
    }

}
