/* ============================================================
   Project System - Styles
   ============================================================ */

/* Library becomes a column: top bar + row wrapper */
#model-library.model-library {
    flex-direction: column;
}

/* Row wrapper holds the sidebar + content (original library layout) */
.ps-library-row {
    display: flex;
    flex: 1 1 0%;
    min-height: 0;
    width: 100%;
}

/* ---- Library Top Bar ---- */
.ps-library-topbar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding: var(--sp-2) var(--sp-5);
    background: #ffffff;
    border-bottom: 1px solid #e5e5ea;
    flex-shrink: 0;
    z-index: 50;
    gap: var(--sp-3);
    min-height: var(--ctl-h);
    box-sizing: border-box;
}

.ps-topbar-right {
    display: flex;
    gap: var(--sp-3);
}

/* Left group: Back (when visible) + the "Get the app" cross-promo button. flex:1 makes
   the group claim ALL space up to Save/Load so the getapp button inside can fill it
   (margin-right:auto kept as a no-op fallback for any leftover rounding pixel). */
.ps-topbar-left {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    flex: 1 1 auto;
    margin-right: auto;
    min-width: 0;
}
.ps-topbar-left .ps-back-btn { margin-right: 0; } /* the group owns the auto margin now */

/* Cross-promo button — accent-tinted so it reads as an invitation, not a tool.
   Compound selector: this block sits above the .ps-topbar-btn base rule, so it must
   out-rank it rather than rely on source order. */
.ps-topbar-btn.ps-getapp-btn {
    background: rgba(0, 122, 255, 0.08);
    border-color: rgba(0, 122, 255, 0.35);
    color: #007AFF;
    font-weight: 600;
}
.ps-topbar-btn.ps-getapp-btn svg { color: #007AFF; }
.ps-topbar-btn.ps-getapp-btn:hover { background: rgba(0, 122, 255, 0.14); box-shadow: none; }
.ps-topbar-btn.ps-getapp-btn:active { background: rgba(0, 122, 255, 0.2); }
.ps-topbar-btn.ps-getapp-btn span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* GetApp fills ALL the space between the left items and Save (flex-grow), on every
   platform. flex-basis 0 makes its width purely the leftover space — INDEPENDENT of
   which label is shown (no layout feedback with the ResizeObserver that picks the
   label tier in project-system.js). It is also the only SHRINKABLE item (min-width
   floor = an icon button), so it can never overflow under Save or collide with Back.
   Icon left-aligned with the button's normal padding, matching Save/Load. */
.ps-topbar-btn.ps-getapp-btn {
    /* Content-sized (NOT flex-grown): project-system.js measures the real space between Back
       and Save, then picks the richest label whose width fits — or hides the button when not
       even the single word fits. A flex-grow button collapsed to 0px here (shrink-wrapped
       left group), which is why it vanished even with space to spare. */
    flex: 0 0 auto;
    overflow: hidden;
    position: relative;
    justify-content: flex-start;  /* label sits right after the icon (was centred, which wasted room) */
    /* Left padding clears the absolutely-pinned icon; right padding is normal so the label gets the
       room. The OLD symmetric icon-width reservation on BOTH sides is what forced icon-only + clip. */
    padding-left: calc(var(--sp-7) + var(--icon-sm) + var(--sp-2));
    padding-right: var(--sp-7);
}
.ps-topbar-btn.ps-getapp-btn svg {
    position: absolute;
    left: var(--sp-7);
    top: 50%;
    transform: translateY(-50%);
}
.ps-topbar-btn.ps-getapp-btn span {
    min-width: 0;
    display: none;                /* one tier class enables exactly one label */
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
/* Richest text that fits (tier class set from project-system.js): icon → tiny → short → long → xl.
   gs-icon shows only the (re-centred) icon; below that the button is hidden outright. */
.ps-getapp-btn.gs-tiny  .ps-getapp-tiny  { display: inline; }
.ps-getapp-btn.gs-short .ps-getapp-short { display: inline; }
.ps-getapp-btn.gs-long  .ps-getapp-long  { display: inline; }
.ps-getapp-btn.gs-xl    .ps-getapp-xl    { display: inline; }
/* Icon-only: no label shows (default), and the absolute icon re-centres (moves nothing in
   layout — the button is content-sized to just its padding, i.e. an icon chip). */
.ps-getapp-btn.gs-icon svg { left: 50%; transform: translate(-50%, -50%); }
/* Very narrow + Back visible: Back drops its text label too (keeps the arrow icon). */
@media (max-width: 380px) {
    .ps-topbar-left .ps-back-btn span { display: none; }
    .ps-topbar-left .ps-back-btn { padding: var(--sp-3); gap: 0; }
}

.ps-topbar-btn {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-7);
    border: 1px solid #e5e5ea;
    border-radius: var(--r-md);
    background: #f5f5f7;
    color: #1d1d1f;
    font-size: var(--fs-label);
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, box-shadow 0.15s;
    -webkit-tap-highlight-color: transparent;
    line-height: 1;
    min-height: var(--ctl-h-sm);
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
}

.ps-topbar-btn:hover {
    background: #eaeaec;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.ps-topbar-btn:active {
    background: #ddd;
    transform: scale(0.97);
}

.ps-topbar-btn svg {
    flex-shrink: 0;
    width: 18px;
    height: 18px;
    color: #555;
}

.ps-back-btn {
    background: transparent;
    border-color: transparent;
    margin-right: auto;
    color: #007AFF;
}

.ps-back-btn svg {
    color: #007AFF;
}

.ps-back-btn:hover {
    background: rgba(0, 122, 255, 0.08);
    box-shadow: none;
}

.ps-back-btn:active {
    background: rgba(0, 122, 255, 0.12);
}

/* ---- Overlay (shared by Save dialog & Project browser) ---- */
.ps-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    animation: psFadeIn 0.15s ease;
}

@keyframes psFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ---- Save Dialog ---- */
.ps-save-dialog {
    max-height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - var(--sp-9));
    max-height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - var(--sp-9));
    overflow-y: auto;
    background: #1e1e1e;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--r-lg);
    padding: var(--sp-9) var(--sp-9) var(--sp-8);
    width: 90%;
    max-width: 380px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    animation: psSlideUp 0.2s ease;
}

@keyframes psSlideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.ps-dialog-title {
    margin: 0 0 var(--sp-6);
    font-size: var(--fs-title);
    font-weight: 600;
    color: #fff;
}

.ps-name-input {
    width: 100%;
    box-sizing: border-box;
    padding: var(--sp-4) var(--sp-5);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: var(--r-sm);
    background: rgba(255, 255, 255, 0.07);
    color: #fff;
    font-size: var(--fs-input);
    outline: none;
    transition: border-color 0.15s;
}

.ps-name-input:focus {
    border-color: #007aff;
}

/* ---- Overwrite existing project list ---- */
.ps-overwrite-section {
    margin-top: var(--sp-5);
}

.ps-overwrite-label {
    font-size: var(--fs-caption);
    color: rgba(255, 255, 255, 0.5);
    margin-bottom: var(--sp-2);
}

.ps-overwrite-list {
    max-height: 180px;
    overflow-y: auto;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--r-sm);
    background: rgba(0, 0, 0, 0.2);
}

.ps-overwrite-item {
    display: flex;
    align-items: center;
    gap: var(--sp-4);
    padding: var(--sp-3) var(--sp-4);
    cursor: pointer;
    transition: background 0.12s;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.ps-overwrite-item:last-child {
    border-bottom: none;
}

.ps-overwrite-item:hover {
    background: rgba(255, 255, 255, 0.08);
}

.ps-overwrite-item.ps-overwrite-selected {
    background: rgba(0, 122, 255, 0.2);
    outline: 1px solid rgba(0, 122, 255, 0.5);
    outline-offset: -1px;
    border-radius: var(--r-sm);
}

.ps-overwrite-thumb {
    width: 36px;
    height: 36px;
    border-radius: var(--r-sm);
    background: rgba(255, 255, 255, 0.07);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--icon-md);
    overflow: hidden;
}

.ps-overwrite-info {
    flex: 1;
    min-width: 0;
}

.ps-overwrite-name {
    font-size: var(--fs-body);
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ps-overwrite-date {
    font-size: var(--fs-caption);
    color: rgba(255, 255, 255, 0.4);
    margin-top: 1px;
}

.ps-btn-row {
    display: flex;
    flex-wrap: wrap; /* the 3-button free-plan prompt (Cancel/Go Premium/Overwrite) would overflow a narrow screen; wrapping keeps the primary right-aligned + reachable */
    justify-content: flex-end;
    gap: var(--sp-3);
    margin-top: var(--sp-7);
}

.ps-btn {
    padding: var(--sp-3) var(--sp-8);
    border: none;
    border-radius: var(--r-sm);
    font-size: var(--fs-label);
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.ps-btn-cancel {
    background: rgba(255, 255, 255, 0.1);
    color: #ccc;
}

.ps-btn-cancel:hover { background: rgba(255, 255, 255, 0.16); }

.ps-btn-save {
    background: #007aff;
    color: #fff;
}

.ps-btn-save:hover { background: #0068d6; }

/* Free-plan single-project override prompt */
.ps-free-msg {
    color: #bcbfc6;
    font-size: var(--fs-body);
    line-height: 1.45;
    margin: var(--sp-2) 0 0;
}
.ps-btn-premium {
    background: rgba(45, 108, 246, 0.16);
    color: #7aa5ff;
    font-weight: 700;
}
.ps-btn-premium:hover { background: rgba(45, 108, 246, 0.26); }

/* ---- Project Browser Panel ---- */
.ps-browser-panel {
    background: #1e1e1e;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--r-lg);
    width: 92%;
    max-width: 520px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    animation: psSlideUp 0.2s ease;
    overflow: hidden;
}

.ps-browser-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-7) var(--sp-9) var(--sp-5);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.ps-browser-close {
    background: none;
    border: none;
    color: #aaa;
    font-size: var(--icon-lg);
    line-height: 1;
    cursor: pointer;
    padding: 0 var(--sp-1);
}

.ps-browser-close:hover { color: #fff; }

.ps-browser-grid {
    padding: var(--sp-6);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--sp-4);
}

.ps-browser-empty {
    text-align: center;
    color: #888;
    padding: 40px 0;
    font-size: var(--fs-body);
}

/* Scroll container holding the Cloud / Local sections */
.ps-browser-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.ps-section-label {
    padding: var(--sp-5) var(--sp-7) 2px;
    font-size: var(--fs-caption);
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #9aa0a8;
}

/* Cloud-synced badge on a project card preview */
.ps-project-cloud-badge {
    position: absolute;
    top: 3px;
    right: 3px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #6cb8ff;
    font-size: 11px;
    line-height: 18px;
    text-align: center;
}

/* Legacy WebGL Home menu markup; current builds share the native model library. */
.ps-webgl-home { display: none; }
.ps-webgl-home-inner {
    width: 320px;
    max-width: 86vw;
    display: flex;
    flex-direction: column;
    gap: var(--sp-5);
}
.ps-webgl-home-title {
    font-size: var(--fs-label);
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #86868b;
    text-align: center;
    margin-bottom: var(--sp-1);
}
.ps-home-btn {
    display: flex;
    align-items: center;
    gap: var(--sp-5);
    width: 100%;
    padding: var(--sp-6) var(--sp-8);
    border: 1px solid #d2d2d7;
    border-radius: var(--r-md);
    background: #fff;
    color: #1d1d1f;
    font-size: var(--fs-input);
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.12s, border-color 0.12s, transform 0.08s;
}
.ps-home-btn:hover { background: #f0f0f3; }
.ps-home-btn:active { transform: scale(0.99); }
.ps-home-btn svg { width: var(--icon-md); height: var(--icon-md); flex-shrink: 0; }
.ps-home-btn--primary { background: #007AFF; border-color: #007AFF; color: #fff; }
.ps-home-btn--primary:hover { background: #0066d6; }
.ps-home-btn--ghost {
    background: transparent;
    border-color: transparent;
    color: #007AFF;
    justify-content: center;
    font-size: var(--fs-label);
    padding: var(--sp-4);
}
.ps-home-btn--ghost:hover { background: rgba(0, 122, 255, 0.08); }

/* ---- Project Card ---- */
.ps-project-card {
    display: flex;
    align-items: center;
    gap: var(--sp-5);
    padding: var(--sp-4) var(--sp-5);
    border-radius: var(--r-md);
    background: rgba(255, 255, 255, 0.04);
    cursor: pointer;
    transition: background 0.15s;
    position: relative;
}

.ps-project-card:hover {
    background: rgba(255, 255, 255, 0.09);
}

.ps-project-preview {
    position: relative;
    width: 56px;
    height: 56px;
    border-radius: var(--r-sm);
    background-color: #333;
    background-size: cover;
    background-position: center;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.ps-project-info {
    flex: 1;
    min-width: 0;
}

.ps-project-name {
    font-size: var(--fs-body);
    font-weight: 500;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ps-project-details {
    font-size: var(--fs-caption);
    color: #888;
    margin-top: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ps-project-delete {
    background: none;
    border: none;
    color: #fff;
    font-size: 20px;
    padding: var(--sp-1) var(--sp-3);
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.15s, transform 0.15s;
    flex-shrink: 0;
}

.ps-project-delete:hover {
    opacity: 1;
    transform: scale(1.05);
}

/* ---- Toast ---- */
.ps-toast {
    position: fixed;
    bottom: calc(24px + env(safe-area-inset-bottom)); /* clear the home indicator on notched phones */
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: #333;
    color: #fff;
    padding: var(--sp-4) var(--sp-9);
    border-radius: var(--r-md);
    font-size: var(--fs-body);
    font-weight: 500;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    z-index: 20000;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.ps-toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}
