:root {
    --blue: #0b3dae;
    --blue-dark: #071d70;
    --cyan: #11bde7;
    --text: #072f83;
    --muted: #717b91;
    --line: #dce3ee;
    --shadow: 0 12px 40px rgba(8, 42, 105, .12);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: #fff; font-family: Arial, Helvetica, sans-serif; color: #16213a; }
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
body.modal-open { overflow: hidden; }

.page { min-height: 100vh; }
.topbar { padding: 18px 14px 0; }
.tabs {
    width: fit-content;
    margin: 0 auto;
    display: flex;
    gap: 7px;
    padding: 5px;
    background: #f2f5fa;
    border: 1px solid #e3e9f2;
    border-radius: 999px;
}
.tab {
    border: 0;
    background: transparent;
    color: #46617f;
    font-weight: 800;
    font-size: 13px;
    padding: 8px 15px;
    border-radius: 999px;
    cursor: pointer;
}
.tab.active { background: var(--blue); color: white; box-shadow: 0 4px 12px rgba(11,61,174,.25); }

.content { padding: 22px 14px 40px; }
.builder { width: min(760px, 100%); margin: 0 auto; text-align: center; }

.preview-wrap {
    position: relative;
    margin: 0 auto;
    max-width: min(620px, 96vw);
    max-height: 70vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f7f8fb;
    border: 1px solid #e2e7ef;
    box-shadow: var(--shadow);
    overflow: hidden;
}
.preview-wrap.avatar-mode {
    width: min(510px, 88vw);
    aspect-ratio: 1 / 1;
    border-radius: 50%;
}
.preview-wrap.feed-mode {
    width: min(500px, 88vw);
    border-radius: 3px;
}
.preview-wrap.stories-mode {
    width: min(400px, 82vw);
    border-radius: 3px;
}
#editorCanvas {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 70vh;
    cursor: grab;
    touch-action: none;
}
.preview-wrap.avatar-mode #editorCanvas {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
}
#editorCanvas.dragging { cursor: grabbing; }

.empty-state {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: #7f899c;
    background: #f3f4f6;
    padding: 30px;
}
.silhouette { font-size: 86px; filter: grayscale(1); opacity: .45; }

.controls { margin: 16px auto 0; width: min(520px, 95%); }
.controls.hidden, .hidden { display: none !important; }
.zoom-control { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 12px; color: #324663; font-weight: 700; font-size: 13px; }
.zoom-control input { width: 100%; accent-color: var(--blue); }
.reset-btn { margin-top: 9px; border: 0; background: transparent; color: var(--blue); font-size: 12px; font-weight: 800; cursor: pointer; }
.hint { margin-top: 6px; color: var(--muted); font-size: 12px; }

.actions { display: flex; justify-content: center; gap: 14px; margin: 20px auto 0; flex-wrap: wrap; }
.btn {
    border: 0;
    border-radius: 999px;
    min-width: 235px;
    min-height: 52px;
    padding: 0 24px;
    color: white;
    font-weight: 900;
    cursor: pointer;
    transition: transform .15s ease, box-shadow .15s ease, opacity .15s ease;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(0,0,0,.12); }
.btn:active { transform: translateY(0); }
.btn-cyan { background: var(--cyan); color: #063792; }
.btn-blue { background: var(--blue); }

.frames-section { margin-top: 24px; }
.frames-section h2 { font-size: 14px; margin: 0 0 12px; color: #50627d; }
.frames-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
    align-items: start;
}
.frame-card {
    border: 3px solid transparent;
    background: white;
    padding: 0;
    cursor: pointer;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0,0,0,.06);
    transition: border-color .15s ease, transform .15s ease;
}
.frame-card:hover { transform: translateY(-2px); }
.frame-card.active { border-color: var(--cyan); }
.frame-card.avatar-thumb { border-radius: 50%; aspect-ratio: 1 / 1; }
.frame-card.feed-thumb { border-radius: 2px; aspect-ratio: 4 / 5; }
.frame-card.stories-thumb { border-radius: 2px; aspect-ratio: 9 / 16; }
.frame-card img { width: 100%; height: 100%; object-fit: cover; display: block; }
.no-frames { color: #7c8594; font-size: 14px; margin: 18px 0 0; }

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(5, 18, 49, .62);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 60;
    padding: 18px;
}
.modal-card {
    position: relative;
    width: min(460px, 100%);
    background: #fff;
    border-radius: 20px;
    box-shadow: 0 30px 80px rgba(0,0,0,.22);
    padding: 28px 22px 20px;
    text-align: center;
}
.modal-card h3 {
    margin: 0 0 8px;
    color: var(--blue-dark);
    font-size: 22px;
}
.modal-card p {
    margin: 0 0 18px;
    color: #58667d;
    font-size: 14px;
}
.modal-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 50%;
    background: #eef3fb;
    color: #42516e;
    font-size: 26px;
    cursor: pointer;
}
.modal-actions.vertical {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.modal-actions.vertical .btn {
    width: 100%;
    min-width: 0;
}
.modal-footnote {
    margin-top: 12px;
    color: #7a869e;
    font-size: 12px;
}

.camera-modal {
    position: fixed;
    inset: 0;
    z-index: 80;
    background: rgba(0,0,0,.88);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
}
.camera-shell {
    width: min(100%, 560px);
    height: 100%;
    max-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
    overscroll-behavior: contain;
}
.camera-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #fff;
    gap: 12px;
}
.camera-title {
    font-size: 18px;
    font-weight: 800;
}
.camera-close {
    width: 42px;
    height: 42px;
    border: 0;
    border-radius: 50%;
    background: rgba(255,255,255,.14);
    color: #fff;
    font-size: 28px;
    cursor: pointer;
}
.camera-viewport {
    position: relative;
    width: min(100%, 500px);
    margin: 0 auto;
    background: #111;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255,255,255,.09);
    box-shadow: 0 14px 40px rgba(0,0,0,.35);
    flex-shrink: 0;
}
.camera-viewport.avatar-mode {
    aspect-ratio: 1 / 1;
    border-radius: 50%;
}
.camera-viewport.feed-mode {
    aspect-ratio: 4 / 5;
    border-radius: 8px;
}
.camera-viewport.stories-mode {
    aspect-ratio: 9 / 16;
    border-radius: 8px;
}
#cameraBgVideo,
#cameraVideo,
#cameraStillCanvas,
#cameraFrameOverlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}
#cameraBgVideo,
#cameraStillCanvas {
    background: #000;
}
#cameraVideo {
    background: transparent;
    object-position: center center;
}
#cameraBgVideo {
    object-fit: cover;
    filter: blur(22px) brightness(.82);
    transform: scale(1.12);
    z-index: 0;
}
#cameraVideo {
    object-fit: contain;
    z-index: 1;
}
#cameraStillCanvas {
    object-fit: contain;
    object-position: center center;
    z-index: 1;
}
.camera-viewport.stories-mode #cameraVideo,
.camera-viewport.stories-mode #cameraStillCanvas {
    object-fit: cover;
    object-position: center center;
}
#cameraFrameOverlay {
    object-fit: contain;
    pointer-events: none;
    z-index: 2;
}
#cameraVideo.mirrored {
    transform: scaleX(-1);
}
#cameraBgVideo.mirrored {
    transform: scale(1.12) scaleX(-1);
}
.camera-message {
    position: absolute;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    width: calc(100% - 24px);
    padding: 10px 12px;
    border-radius: 999px;
    background: rgba(0,0,0,.46);
    color: #fff;
    text-align: center;
    font-size: 12px;
    font-weight: 700;
    z-index: 3;
    pointer-events: none;
}
.camera-toolbar {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}
.camera-btn {
    border: 0;
    border-radius: 999px;
    min-height: 52px;
    padding: 0 18px;
    font-weight: 900;
    color: #fff;
    cursor: pointer;
}
.camera-btn.primary { background: var(--cyan); color: #052c75; }
.camera-btn.secondary { background: rgba(255,255,255,.12); }

@media (max-width: 620px) {
    .topbar { padding-top: 12px; }
    .content { padding-top: 15px; }
    .preview-wrap.avatar-mode { width: min(430px, 92vw); }
    .preview-wrap.feed-mode { width: min(430px, 92vw); }
    .preview-wrap.stories-mode { width: min(360px, 86vw); }
    #editorCanvas { max-height: 68vh; }
    .actions { gap: 10px; }
    .btn { flex: 1 1 46%; min-width: 0; min-height: 49px; padding: 0 15px; font-size: 13px; }
    .frames-grid { gap: 9px; }
}

@media (max-width: 390px) {
    .tab { padding: 7px 12px; font-size: 12px; }
    .btn { flex-basis: 100%; }
    .camera-shell { gap: 10px; }
    .camera-title { font-size: 16px; }
    .camera-btn { width: 100%; }
    .camera-message { font-size: 11px; border-radius: 14px; }
}

/* Correção mobile Stories: a câmera deve preencher toda a área vertical. */
@media (max-width: 620px) {
    .camera-viewport.stories-mode {
        width: auto;
        height: min(68dvh, 680px);
        max-width: calc(100vw - 24px);
        aspect-ratio: 9 / 16 !important;
    }
    .camera-viewport.stories-mode #cameraVideo,
    .camera-viewport.stories-mode #cameraStillCanvas {
        top: 5% !important;
        left: 6% !important;
        width: 88% !important;
        height: 90% !important;
        object-fit: contain !important;
        object-position: center center !important;
        background: transparent !important;
    }
    .camera-viewport.stories-mode #cameraBgVideo {
        inset: 0 !important;
        width: 100% !important;
        height: 100% !important;
        object-fit: cover !important;
        object-position: center center !important;
        filter: blur(10px) brightness(.92) !important;
        transform: scale(1.06) !important;
        display: block;
    }
    .camera-viewport.stories-mode #cameraBgVideo.mirrored {
        transform: scale(1.06) scaleX(-1) !important;
    }
}
