/* The dedicated phone layout is isolated from desktop and tablet styling. */
#phone-dock, .phone-back, .phone-history, .phone-sheet-backdrop, .phone-overlay-tracks { display: none; }
@media (max-width: 768px), (max-width: 1024px) and (max-height: 500px) and (pointer: coarse) {
    html[data-theme] body { height: 100dvh; overflow: hidden; }
    html[data-theme] body > header { height: calc(52px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left)); gap: 6px; }
    .phone-back { display: grid; place-items: center; min-width: 44px; height: 44px; font-size: 32px; text-decoration: none; color: var(--studio-text); }
    header .brand img { width: 22px; height: 22px; }
    html[data-theme] header .brand strong { font-size: 14px; }
    header .brand strong span { display: inline; }
    header .actions { margin-left: auto; }
    header #theme-toggle, header #access-label, header .pill { display: none; }
    html[data-theme] header #export { font-size: 12px; min-height: 44px; padding: 8px 10px; }
    html[data-theme] body main, html[data-theme] body[data-mobile-panel] main { height: calc(100dvh - 52px - env(safe-area-inset-top)); min-height: 0; }
    html[data-theme] main > .editor { height: 100%; padding: 0 max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left)); overflow: hidden; gap: 0; }
    .editor > .editor-heading, .editor > .canvas-toolbar, .editor .transport > .gps-actions, .editor > .timeline-edit-tools, .editor > .gps-prompt, .editor > #replace-gps, .editor > #range-hint, .mobile-tools, .mobile-tool-backdrop, .editor > .tracking-summary { display: none !important; }
    html[data-theme] .editor > .preview-shell { flex: 1 1 0; min-height: 50px; margin: 0; padding: 4px; border: 0; border-radius: 8px; box-shadow: none; background: #080a0d; overflow: hidden; }
    html[data-theme] .transport { flex: none; flex-wrap: nowrap; padding: 4px 0; gap: 8px; min-height: 48px; }
    html[data-theme] #clock { font-size: 11px; font-variant-numeric: tabular-nums; white-space: nowrap; }
    .phone-history { display: flex; flex: none; align-items: center; gap: 6px; margin-left: auto; }
    html[data-theme] .phone-history button { display: grid; place-items: center; flex: 0 0 44px; width: 44px; height: 44px; padding: 0; border: 1px solid var(--studio-border); border-radius: 11px; background: var(--studio-raised); color: var(--studio-text); opacity: 1; }
    html[data-theme] .phone-history .phone-timeline-toggle {
        border-color: transparent;
        background: transparent;
    }
    html[data-theme] .phone-history .phone-timeline-toggle[aria-expanded="true"] { color: var(--studio-accent); }
    .phone-history svg { display: block; width: 21px; height: 21px; pointer-events: none; }
    html[data-theme] .phone-history button:disabled { color: var(--studio-muted); background: color-mix(in srgb, var(--studio-raised) 55%, var(--studio-panel)); opacity: .65; cursor: default; }
    html[data-theme] .phone-history button:enabled:hover { background: var(--studio-hover); }
    html[data-theme] .phone-history button:enabled:active { background: var(--studio-hover); border-color: var(--studio-accent); }
    html[data-theme] .phone-history button:focus-visible { outline: 2px solid var(--studio-accent); outline-offset: 2px; }
    html[data-theme] .editor > .timeline:not([hidden]) { display: block !important; flex: none; padding: 0; margin: 0; border: 0; }
    .timeline > .timeline-stack { min-width: 0; }
    .phone-timeline-scroll { overflow-x: auto; overflow-y: hidden; width: 100%; overscroll-behavior-x: contain; touch-action: pan-x; scrollbar-width: thin; padding: 0 12px 4px; }
    .phone-timeline-scroll .timeline-stack { display: flex; flex-direction: column; min-width: 100%; max-width: none; padding: 0; }
    html[data-theme] .timeline-track { height: 52px; margin: 0; flex: none; touch-action: pan-x; }
    html[data-theme] .timeline-ruler { height: 28px; flex: none; }
    html[data-theme] .timeline-trim { width: 28px; min-height: 44px; touch-action: none; }
    .timeline-stack > .timeline-playhead { width: 28px; margin-left: 0; }
    #phone-dock { display: flex; flex: none; flex-direction: column; min-height: 0; padding-bottom: env(safe-area-inset-bottom); }
    .phone-context-wrap { position: relative; min-width: 0; overflow: hidden; border-top: 1px solid var(--studio-border); --action-arrow-width: 36px; }
    .phone-context-wrap:has(.phone-context-back:not([hidden])) { --action-clip-left: var(--action-arrow-width); }
    .phone-context-wrap:has(.phone-context-more:not([hidden])) { --action-clip-right: var(--action-arrow-width); }
    .phone-context-wrap:has(:is(.phone-context-back, .phone-context-more):not([hidden])) :is(.phone-context, .phone-categories) {
        clip-path: inset(0 var(--action-clip-right, 0px) 0 var(--action-clip-left, 0px));
    }
    html[data-theme] :is(.phone-context-more, .phone-context-back) {
        position: absolute;
        inset: 0 0 0 auto;
        display: grid;
        place-items: center;
        width: var(--action-arrow-width);
        padding: 0;
        border: 0;
        border-radius: 0;
        background: var(--studio-panel);
        color: var(--studio-accent);
    }
    html[data-theme] .phone-context-back { inset: 0 auto 0 0; }
    :is(.phone-context-more, .phone-context-back) svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; }
    .phone-context { display: flex; gap: 4px; overflow-x: auto; min-height: 47px; align-items: center; scrollbar-width: none; }
    html[data-theme] .phone-context button { flex: none; min-height: 44px; padding: 6px 10px; white-space: nowrap; font-size: 11px; border: 0; border-radius: 6px; background: transparent; box-shadow: none; color: var(--studio-text); }
    html[data-theme] .phone-context button:enabled:hover { background: var(--studio-hover); }
    html[data-theme] .phone-context button:focus-visible { outline: 2px solid var(--studio-accent); outline-offset: -2px; }
    html[data-theme] .phone-context .phone-widget-action { display: inline-flex; align-items: center; gap: 7px; padding-inline: 12px; }
    .phone-widget-action svg { width: 18px; height: 18px; flex: none; }
    .phone-context p { font-size: 12px; color: var(--studio-muted); padding: 0 10px; }
    .phone-categories { display: flex; gap: 4px; overflow-x: auto; padding: 5px 0; scrollbar-width: none; }
    .phone-categories::-webkit-scrollbar { display: none; }
    html[data-theme] .phone-categories button { display: flex; flex: 1 0 54px; flex-direction: column; align-items: center; justify-content: center; gap: 4px; min-height: 56px; border: 0; background: transparent; font-size: 10px; padding: 4px; color: var(--studio-muted); }
    .phone-categories span { font-size: 23px; line-height: 24px; }
    html[data-theme] .phone-categories button[aria-pressed=true] { background: var(--studio-raised); color: var(--studio-accent); border-radius: 10px; }
    .phone-sheet { height: min(230px, 30dvh); border: 1px solid var(--studio-border); border-radius: 12px 12px 0 0; background: var(--studio-panel); overflow: hidden; }
    .phone-sheet-heading { display: flex; align-items: center; justify-content: space-between; padding: 0 10px; height: 44px; font-size: 13px; }
    html[data-theme] .phone-sheet-heading button { min-height: 44px; padding: 8px 12px; border: 0; color: var(--studio-accent); }
    .phone-sheet-content { height: calc(100% - 44px); overflow: auto; overscroll-behavior: contain; padding: 0 10px 12px; }
    html[data-theme] .phone-sheet .inspector { display: block; padding: 0; border: 0; background: transparent; height: auto; overflow: visible; }
    html[data-theme] .phone-sheet .canvas-toolbar { display: flex; flex-wrap: wrap; position: static; transform: none; width: 100%; margin: 0; }
    html[data-theme] .phone-sheet .gps-actions {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 2px 8px;
        padding: 0;
        border: 0;
    }
    html[data-theme] .phone-sheet .gps-actions button {
        display: flex;
        align-items: center;
        gap: 8px;
        min-width: 0;
        padding: 6px 8px;
        border: 1px solid transparent;
        border-radius: 7px;
        background: transparent;
        text-align: left;
        font-size: 12px;
        font-weight: 500;
    }
    html[data-theme] .phone-sheet .gps-actions #sync:not(:disabled) {
        background: color-mix(in srgb, var(--studio-accent) 12%, transparent);
        color: var(--studio-accent);
    }
    html[data-theme] .phone-sheet .gps-actions button:is(:hover, :active):not(:disabled) {
        background: var(--studio-hover);
    }
    .phone-sheet .gps-actions button::before {
        content: '';
        flex: 0 0 16px;
        height: 16px;
        background: currentColor;
        mask: var(--tool-icon) center / contain no-repeat;
    }
    .phone-sheet .gps-actions #gps-map {
        --tool-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.7'%3E%3Cpath d='M6 7h10a4 4 0 0 1 0 8H8a3 3 0 0 0 0 6h10'/%3E%3Ccircle cx='4' cy='7' r='2'/%3E%3Ccircle cx='20' cy='21' r='2'/%3E%3C/svg%3E");
    }
    html[data-theme] .phone-sheet button, html[data-theme] .phone-sheet select, html[data-theme] .phone-sheet input { min-height: 44px; }
    html[data-theme] .phone-sheet input[type=number], html[data-theme] .phone-sheet select { font-size: 16px; }
    html[data-theme] .phone-sheet .widget-catalog { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
    html[data-theme] .phone-sheet .widget-card { width: 100%; padding: 5px; margin: 0; min-height: 88px; }
    .phone-sheet .widget-card canvas { display: none; }
    .phone-sheet .widget-card img { max-width: 100%; height: 48px; object-fit: contain; }
    html[data-theme] .phone-sheet .widget-card strong { font-size: 10px; }
    html[data-theme] .status-row { bottom: calc(125px + env(safe-area-inset-bottom)); }
    dialog { max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 24px); overflow-y: auto; }
}
@media (max-width: 768px) and (max-height: 500px), (max-width: 1024px) and (max-height: 500px) and (pointer: coarse) {
    html[data-theme] body > header { height: calc(44px + env(safe-area-inset-top)); }
    html[data-theme] body main { height: calc(100dvh - 44px - env(safe-area-inset-top)); }
    html[data-theme] .transport { min-height: 44px; padding: 0; }
    html[data-theme] .timeline-track { height: 36px; }
    .phone-context { min-height: 43px; }
    html[data-theme] .phone-categories button { min-height: 44px; flex-direction: row; }
    .phone-categories { padding: 0; }
    .phone-sheet { height: 110px; }
}
@media (max-width: 768px) {
    html[data-theme] .clip-edge { width: 28px; min-height: 44px; }
    .clip-edge::after { left: 13px; }
}
@media (max-width: 768px) and (max-height: 650px) {
    .phone-sheet { height: 120px; }
}
@media (max-width: 768px), (max-width: 1024px) and (max-height: 500px) and (pointer: coarse) {
    /* One media strip, a quiet ruler and a shared time axis for overlay lanes. */
    html[data-theme] .editor > .timeline,
    html[data-theme] .timeline > .phone-timeline-shell,
    html[data-theme] .phone-timeline-scroll,
    html[data-theme] .phone-timeline-scroll .timeline-stack { background: transparent; background-image: none; border: 0; box-shadow: none; border-radius: 0; padding-block: 0; }
    html[data-theme] .timeline > .phone-timeline-shell { position: relative; overflow: hidden; padding: 0; }
    html[data-theme] .phone-timeline-scroll { padding-bottom: 8px; scrollbar-width: none; scroll-behavior: auto; scroll-snap-type: none; overflow-anchor: none; overscroll-behavior-x: none; -webkit-overflow-scrolling: touch; }
    html[data-theme] .phone-timeline-scroll .timeline-stack { min-width: 0; max-width: none; flex: none; }
    html[data-theme] .timeline-track { border: 0; box-shadow: none; border-radius: 5px; background: transparent; overflow: visible; }
    .timeline-frames { overflow: hidden; border-radius: 5px; }
    html[data-theme] .timeline-range { border: 0; box-shadow: none; background: transparent; }
    .timeline-name { display: none; }
    html[data-theme] .timeline-ruler { margin: 0; border: 0; height: 28px; background: repeating-linear-gradient(90deg, var(--studio-muted) 0 1px, transparent 1px 5%) left bottom / 100% 4px no-repeat; }
    html[data-theme] .timeline-ruler span { font-size: 9px; color: var(--studio-muted); }
    html[data-theme] .timeline-clip-section { border: 0; border-radius: 4px; box-shadow: none; }
    html[data-theme] .timeline-clip-section.selected { border: 1px solid var(--studio-accent); }
    html[data-theme] .timeline-clip-section > .clip-filmstrip { inset: 0; border-radius: 4px; }
    .timeline-clip-section:not(.selected) > .clip-caption { display: none; }
    html[data-theme] .clip-caption { font-size: 9px; padding: 1px 4px; }
    html[data-theme] .timeline-trim, html[data-theme] .clip-edge { background: #c3cbd62b; width: 24px; }
    html[data-theme] .phone-timeline-shell > .timeline-playhead { position: absolute; left: 50% !important; top: 21px; bottom: 7px; width: 28px; transform: translateX(-50%); background: transparent; box-shadow: none; pointer-events: none; touch-action: none; z-index: 8; }
    .phone-timeline-shell > .timeline-playhead::before { left: 9px; top: 0; border: 5px solid transparent; border-top: 6px solid var(--studio-accent); }
    .phone-timeline-shell > .timeline-playhead::after { content: ''; position: absolute; left: 13px; top: 5px; bottom: 0; width: 1px; background: #fff; }
    .phone-overlay-tracks { display: grid; gap: 3px; margin-top: 5px; }
    .phone-overlay-lane { position: relative; height: 20px; }
    .phone-overlay-lane span { position: absolute; top: 0; bottom: 0; display: flex; align-items: center; padding: 0 8px; border-radius: 4px; color: var(--studio-text); background: color-mix(in srgb, var(--studio-accent) 18%, var(--studio-panel)); overflow: hidden; white-space: nowrap; font-size: 9px; }
    .phone-categories svg { width: 21px; height: 21px; flex: none; }
    .phone-categories span { font-size: 10px; line-height: 14px; }
    html[data-theme] .phone-categories button[aria-pressed=true] { background: color-mix(in srgb, var(--studio-accent) 10%, transparent); border-radius: 7px; }
    html[data-theme] .phone-context .phone-widget-action { flex: 1; justify-content: center; min-height: 40px; margin: 4px 0; }
    html[data-theme] .phone-widget-action:first-child { background: var(--studio-accent); color: #101a2a; }
    html[data-theme] .phone-widget-action[data-widget-properties] { border: 1px solid var(--studio-border); }
    html[data-theme] .status-row { top: calc(60px + env(safe-area-inset-top)); bottom: auto; pointer-events: none; }
    /* Sheets float over the editor, preserving the preview geometry. */
    .phone-sheet-backdrop:not([hidden]) { display: block; position: fixed; inset: 0; z-index: 1100; border: 0; border-radius: 0; background: #0006; padding: 0; }
    html[data-theme] #phone-dock .phone-sheet { position: fixed; z-index: 1120; inset: auto 0 0; width: 100%; height: var(--sheet-height, 56dvh); max-height: 94dvh; border-radius: 18px 18px 0 0; padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -12px 50px #0004; }
    html[data-theme] #phone-dock .phone-sheet[data-expanded=true] { height: var(--sheet-height, 90dvh); }
    #phone-dock[data-sheet-open] > .phone-context-wrap { visibility: hidden; }
    .phone-sheet-heading { position: relative; height: 70px; padding: 18px 16px 0; touch-action: none; user-select: none; }
    html[data-theme] .phone-sheet-heading > button:not(.phone-sheet-grip) { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; background: transparent; color: var(--studio-text); }
    .phone-sheet-heading svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }
    html[data-theme] .phone-sheet-heading .phone-sheet-grip { position: absolute; inset: 0 44px auto; display: grid; place-items: center; min-height: 20px; height: 20px; padding: 0; background: transparent; border: 0; touch-action: none; }
    .phone-sheet-grip span { display: block; width: 34px; height: 4px; border-radius: 2px; background: var(--studio-muted); opacity: .5; }
    .phone-sheet-content { height: calc(100% - 70px); padding: 0 16px 16px; }
    html[data-theme] .phone-sheet .widget-catalog { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
    html[data-theme] .phone-sheet .widget-card { display: flex; flex-direction: column; align-items: center; gap: 10px; min-height: 154px; padding: 12px 8px; border: 0; border-radius: 10px; background: var(--studio-raised); box-shadow: none; }
    html[data-theme] .phone-sheet .widget-card img { display: block; width: 100%; max-width: 160px; height: 105px; object-fit: contain; margin: 0; }
    html[data-theme] .phone-sheet .widget-card strong { font-size: 12px; font-weight: 500; margin: 0; }
    .phone-sheet .widget-card small { display: none; }
    html[data-theme] .phone-sheet .widget-card:disabled { opacity: .45; }
    #phone-dock .canvas-size-menu, #phone-dock .video-tools-menu { position: static; max-width: 100%; box-shadow: none; }
}

@media (max-width: 360px) {
    html[data-theme] .transport { gap: 4px; }
    .phone-history { gap: 0; }
    html[data-theme] .phone-history button { flex-basis: 38px; width: 38px; }
}

.placed-widget.grade-selected { outline: 1px solid var(--studio-accent); }
html[data-theme] .widget-catalog .grade-card canvas,
html[data-theme] .phone-sheet .widget-catalog .grade-card canvas {
    display: block;
    width: 100%;
    height: auto;
    margin: auto 0;
}
html[data-theme] .widget-catalog .flight-card {
    overflow: hidden;
}
html[data-theme] .widget-catalog .flight-card canvas {
    min-height: 0;
    object-fit: contain;
}
.inspector.is-grade .style-picker canvas { object-fit: contain; }

html[data-theme] .widget-catalog .flight-card small,
html[data-theme] .phone-sheet .widget-catalog .flight-card small {
    display: block;
    font-size: .68rem;
    line-height: 1.25;
}

.guest-upgrade {
    position: fixed;
    width: min(28rem, calc(100% - 2rem));
    padding: 1.25rem;
    border: 1px solid var(--studio-border);
    border-radius: 14px;
    background: var(--studio-panel);
    color: var(--studio-text);
}
.guest-upgrade::backdrop { background: #0008; }
.guest-upgrade h2 { padding-right: 2rem; }
.guest-upgrade-close { float: right; min-width: 40px; min-height: 40px; }
.guest-upgrade-plans { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
.guest-upgrade-plans a { display: grid; gap: .3rem; padding: .65rem; border: 1px solid var(--studio-border); border-radius: 8px; color: inherit; text-decoration: none; }
.guest-upgrade-plans a:hover { border-color: var(--studio-accent); }
.guest-upgrade-plans span { font-size: .8rem; }
.frame-step-controls:not([hidden]) { display: inline-flex; align-items: center; }
html[data-theme] .frame-step-controls button {
    display: grid;
    place-items: center;
    min-width: 40px;
    min-height: 40px;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
}
