@font-face {
    font-family: 'Khalaf Quran P001';
    src: url('./Q.Khalaf_P001.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'Khalaf Quran P002';
    src: url('./Q.Khalaf_P002.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

html {
    background: #02030a;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background:
        radial-gradient(circle at 50% -10%, rgba(120, 119, 255, 0.18), transparent 34%),
        radial-gradient(circle at 15% 20%, rgba(34, 211, 238, 0.10), transparent 28%),
        #02030a;
    color: #f7f7fb;
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
    min-height: 100vh;
    position: relative;
    isolation: isolate;
}

/* Dotted surface background — sits behind all content */
#dotted-surface {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    overflow: hidden;
    background-image:
        radial-gradient(circle at center, rgba(255, 255, 255, 0.18) 1.1px, transparent 1.6px),
        radial-gradient(circle at 50% 35%, rgba(145, 115, 255, 0.20), transparent 35%);
    background-size: 28px 28px, 100% 100%;
}

#dotted-surface canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

.container {
    max-width: 1200px;
    width: 100%;
    text-align: center;
    position: relative;
    z-index: 1;
    --screen-w: clamp(340px, 27vw, 430px);
    --screen-ratio: 1.7777778;
    --screen-radius: 40px;
    --screen-pad: clamp(28px, 2.6vw, 42px);
}

.container.editor-collapsed {
    max-width: 1600px;
    --screen-w: clamp(380px, 30vw, 520px);
}

.container.format-youtube {
    max-width: 1800px;
    --screen-w: clamp(520px, 44vw, 760px);
    --screen-ratio: 0.5625;
    --screen-radius: 26px;
    --screen-pad: clamp(24px, 2.2vw, 44px);
}

.container.format-youtube.editor-collapsed {
    --screen-w: clamp(620px, 48vw, 900px);
}

.editor-flip-card {
    width: min(1040px, 100%);
    min-height: 205px;
    margin: 0 auto 24px;
    perspective: 1600px;
    transition: min-height 0.35s ease, margin 0.35s ease, width 0.35s ease;
}

.editor-flip-inner {
    position: relative;
    width: 100%;
    min-height: 205px;
    transform-style: preserve-3d;
    transition: transform 0.75s cubic-bezier(.2, .8, .2, 1), min-height 0.35s ease;
}

.editor-flip-card.is-flipped,
.editor-flip-card.is-flipped .editor-flip-inner {
    min-height: 260px;
}

.editor-flip-card.is-collapsed,
.editor-flip-card.is-collapsed .editor-flip-inner {
    min-height: 70px;
}

.editor-flip-card.is-flipped.is-collapsed,
.editor-flip-card.is-flipped.is-collapsed .editor-flip-inner {
    min-height: 70px;
}

.editor-flip-card.is-collapsed {
    width: min(760px, 100%);
    margin-bottom: 14px;
}

.editor-flip-card.is-flipped .editor-flip-inner {
    transform: rotateY(180deg);
}

.editor-face {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    border-radius: 24px;
    background:
        radial-gradient(circle at 50% 0%, rgba(99, 102, 241, 0.16), transparent 46%),
        linear-gradient(180deg, rgba(15, 23, 42, 0.82), rgba(2, 6, 23, 0.70));
    border: 1px solid rgba(148, 163, 184, 0.20);
    box-shadow:
        0 28px 85px rgba(0, 0, 0, 0.48),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(22px) saturate(145%);
    -webkit-backdrop-filter: blur(22px) saturate(145%);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    box-sizing: border-box;
    overflow: visible;
}

.editor-face-back {
    transform: rotateY(180deg);
}

.editor-face-back,
.editor-flip-card.is-flipped .editor-face-front {
    pointer-events: none;
}

.editor-flip-card.is-flipped .editor-face-back {
    pointer-events: auto;
}

.face-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    color: #e2e8f0;
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.brand-block {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    line-height: 1.1;
}

.brand-block strong {
    color: #f8fafc;
    font-size: 16px;
    letter-spacing: 0.01em;
    text-transform: none;
    text-shadow: 0 0 22px rgba(255,255,255,0.16);
}

.brand-block span {
    color: #94a3b8;
    font-size: 11px;
    letter-spacing: 0.08em;
}

.header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
}

.flip-btn {
    padding: 7px 11px;
    border: 1px solid rgba(125, 211, 252, 0.28);
    border-radius: 999px;
    background: rgba(99, 102, 241, 0.16);
    color: #f8fafc;
    cursor: pointer;
    font-size: 11px;
    font-weight: 800;
    transition: transform 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.editor-flip-card.is-collapsed .controls,
.editor-flip-card.is-collapsed .bg-controls,
.editor-flip-card.is-collapsed .font-controls {
    opacity: 0;
    transform: translateY(-8px) scale(0.98);
    pointer-events: none;
}

.flip-btn:hover {
    transform: translateY(-1px);
    background: rgba(99, 102, 241, 0.28);
    border-color: rgba(125, 211, 252, 0.48);
}

.editor-face .controls,
.editor-face .bg-controls,
.editor-face .font-controls {
    width: 100%;
    margin: 0;
}

.editor-face .bg-controls {
    margin-bottom: 10px;
}

.controls {
    background: rgba(7, 10, 22, 0.72);
    border: 1px solid rgba(255,255,255,0.10);
    backdrop-filter: blur(18px);
    padding: 10px;
    border-radius: 14px;
    box-shadow: 0 18px 55px rgba(0,0,0,0.55), inset 0 1px 0 rgba(255,255,255,0.06);
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: flex-end;
    margin-bottom: 0;
    flex-wrap: wrap;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.control-group {
    display: flex;
    flex-direction: column;
    text-align: left;
    gap: 5px;
    color: #cbd5e1;
}

.control-group label {
    font-size: 11px;
    color: #cbd5e1;
    white-space: nowrap;
}

.control-label {
    font-size: 11px;
    color: #cbd5e1;
    white-space: nowrap;
}

select {
    padding: 7px 9px;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.14);
    background: rgba(2, 6, 23, 0.86);
    color: #f8fafc;
    min-width: 145px;
    max-width: 230px;
    font-size: 12px;
}

select option {
    background: #020617;
    color: #f8fafc;
}

button#generate-btn {
    padding: 8px 16px;
    background: linear-gradient(135deg, #6366f1, #14b8a6);
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    height: 34px;
    font-size: 12px;
}

button#generate-btn:hover {
    filter: brightness(1.12);
}

.format-control-group {
    min-width: 178px;
}

.format-toggle {
    display: inline-grid;
    grid-template-columns: repeat(2, minmax(78px, 1fr));
    gap: 3px;
    padding: 3px;
    border-radius: 10px;
    background: rgba(2, 6, 23, 0.88);
    border: 1px solid rgba(148, 163, 184, 0.16);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.format-btn {
    min-width: 0;
    height: 34px;
    padding: 4px 8px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    color: #94a3b8;
    cursor: pointer;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    line-height: 1;
    transition: background 0.16s ease, border-color 0.16s ease, color 0.16s ease, transform 0.16s ease;
}

.format-btn span {
    font-size: 10px;
    font-weight: 700;
}

.format-btn strong {
    font-size: 11px;
    font-weight: 900;
    letter-spacing: 0;
}

.format-btn:hover {
    color: #e2e8f0;
    transform: translateY(-1px);
}

.format-btn.is-active {
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.38), rgba(20, 184, 166, 0.24));
    border-color: rgba(125, 211, 252, 0.34);
    color: #f8fafc;
    box-shadow: 0 8px 18px rgba(20, 184, 166, 0.12);
}

.bg-controls,
.font-controls {
    width: min(100%, 100%);
    margin-left: auto;
    margin-right: auto;
    background:
        linear-gradient(180deg, rgba(15, 23, 42, 0.84), rgba(2, 6, 23, 0.72));
    border: 1px solid rgba(148, 163, 184, 0.18);
    backdrop-filter: blur(22px) saturate(140%);
    -webkit-backdrop-filter: blur(22px) saturate(140%);
    padding: 8px;
    border-radius: 18px;
    box-shadow:
        0 22px 70px rgba(0, 0, 0, 0.42),
        inset 0 1px 0 rgba(255, 255, 255, 0.08),
        0 0 0 1px rgba(255, 255, 255, 0.02);
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.bg-controls {
    margin-bottom: 12px;
}

.bg-group,
.font-control-group {
    min-height: 32px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 7px;
    border: 1px solid rgba(148, 163, 184, 0.14);
    border-radius: 14px;
    background: rgba(2, 6, 23, 0.54);
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
    transition: border-color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}

.bg-group:hover,
.font-control-group:hover {
    border-color: rgba(125, 211, 252, 0.34);
    background: rgba(15, 23, 42, 0.68);
    transform: translateY(-1px);
}

.bg-label {
    font-size: 11px;
    color: #cbd5e1;
    white-space: nowrap;
    letter-spacing: 0.01em;
}

input[type="color"] {
    width: 28px;
    height: 28px;
    border: 1px solid rgba(226, 232, 240, 0.28);
    border-radius: 12px;
    cursor: pointer;
    padding: 2px;
    background: rgba(15, 23, 42, 0.95);
    box-shadow:
        0 0 0 3px rgba(255, 255, 255, 0.04),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

input[type="color"]::-webkit-color-swatch {
    border: none;
    border-radius: 9px;
}

input[type="color"]::-moz-color-swatch {
    border: none;
    border-radius: 9px;
}

#bg-opacity {
    width: 82px;
    height: 18px;
    cursor: pointer;
    accent-color: #8b5cf6;
}

#bg-opacity-label {
    min-width: 42px;
    padding: 4px 7px;
    border-radius: 999px;
    background: rgba(99, 102, 241, 0.16);
    border: 1px solid rgba(129, 140, 248, 0.22);
    font-size: 11px;
    font-weight: bold;
    color: #f8fafc;
}

.upload-label {
    padding: 7px 11px;
    background: linear-gradient(135deg, #6366f1, #14b8a6);
    color: white;
    border-radius: 12px;
    cursor: pointer;
    font-size: 11px;
    font-weight: 700;
    box-shadow: 0 10px 25px rgba(99, 102, 241, 0.22);
    transition: transform 0.15s ease, filter 0.15s ease, box-shadow 0.15s ease;
}

.upload-label:hover {
    filter: brightness(1.12);
    transform: translateY(-1px);
    box-shadow: 0 14px 32px rgba(20, 184, 166, 0.22);
}

#bg-clear-btn {
    padding: 7px 10px;
    background: rgba(239, 68, 68, 0.16);
    color: white;
    border: 1px solid rgba(248, 113, 113, 0.30);
    border-radius: 12px;
    cursor: pointer;
    font-size: 11px;
    font-weight: 700;
    transition: background 0.15s ease, transform 0.15s ease;
}

#bg-clear-btn:hover {
    background: rgba(239, 68, 68, 0.30);
    transform: translateY(-1px);
}

.transparent-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 11px;
    color: #cbd5e1;
    user-select: none;
    white-space: nowrap;
}

.transparent-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
    accent-color: #8b5cf6;
}

/* Beautiful preview for transparent state.
   This is only a preview background in the editor.
   It is removed before PNG export, so downloaded file stays transparent. */
.screen-preview.is-transparent {
    background-image:
        radial-gradient(circle at 50% 38%, rgba(124, 58, 237, 0.26), transparent 36%),
        radial-gradient(circle at 28% 62%, rgba(20, 184, 166, 0.12), transparent 30%),
        radial-gradient(circle, rgba(255, 255, 255, 0.18) 1px, transparent 1.7px),
        linear-gradient(180deg, rgba(8, 13, 31, 0.98), rgba(2, 6, 23, 0.98)) !important;
    background-size: 100% 100%, 100% 100%, 26px 26px, 100% 100% !important;
    background-position: center, center, center, center !important;
    border-color: rgba(255, 255, 255, 0.22);
    box-shadow:
        0 28px 80px rgba(0, 0, 0, 0.70),
        0 0 42px rgba(124, 58, 237, 0.18),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.font-controls {
    margin-bottom: 0;
}

.font-select-group {
    flex-wrap: nowrap;
}

.font-select {
    min-width: 136px;
    max-width: 155px;
    padding: 6px 8px;
    font-size: 11px;
}

.font-label {
    font-size: 11px;
    color: #cbd5e1;
    min-width: 80px;
    text-align: left;
    white-space: nowrap;
}

.font-buttons {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 3px;
    border-radius: 14px;
    background: rgba(15, 23, 42, 0.62);
    border: 1px solid rgba(148, 163, 184, 0.12);
}

.size-btn {
    width: 28px;
    height: 28px;
    border: 1px solid rgba(148, 163, 184, 0.22);
    background: linear-gradient(180deg, rgba(30, 41, 59, 0.98), rgba(15, 23, 42, 0.96));
    color: #f8fafc;
    border-radius: 11px;
    font-size: 15px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
    transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.size-btn:hover {
    border-color: rgba(125, 211, 252, 0.38);
    background: linear-gradient(180deg, rgba(51, 65, 85, 0.98), rgba(30, 41, 59, 0.96));
    transform: translateY(-1px);
}

.size-display {
    min-width: 46px;
    padding: 5px 8px;
    border-radius: 11px;
    background: rgba(99, 102, 241, 0.16);
    border: 1px solid rgba(129, 140, 248, 0.22);
    text-align: center;
    font-size: 11px;
    color: #f8fafc;
    font-weight: bold;
}

@media (max-width: 760px) {
    body {
        padding: 12px;
    }

    /* Both faces share one grid cell, but still contribute their real height.
       This keeps the absolutely layered desktop flip effect from collapsing
       over the controls when they wrap on a narrow screen. */
    .editor-flip-card,
    .editor-flip-card.is-flipped,
    .editor-flip-card.is-flipped.is-collapsed {
        min-height: 0;
    }

    .editor-flip-inner,
    .editor-flip-card.is-flipped .editor-flip-inner,
    .editor-flip-card.is-flipped.is-collapsed .editor-flip-inner {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
        min-height: 0;
        min-width: 0;
    }

    .editor-face {
        position: relative;
        inset: auto;
        grid-area: 1 / 1;
        min-width: 0;
    }

    .editor-flip-card.is-collapsed,
    .editor-flip-card.is-collapsed .editor-flip-inner {
        min-height: 70px;
    }

    .editor-flip-card.is-collapsed .controls,
    .editor-flip-card.is-collapsed .bg-controls,
    .editor-flip-card.is-collapsed .font-controls {
        display: none;
    }

    .bg-controls,
    .font-controls {
        justify-content: stretch;
    }

    .bg-group,
    .font-control-group {
        width: 100%;
        box-sizing: border-box;
        justify-content: space-between;
    }

    #bg-opacity {
        flex: 1;
    }

    .container,
    .container.editor-collapsed,
    .container.format-youtube,
    .container.format-youtube.editor-collapsed {
        --screen-w: calc(100vw - 40px);
    }

    .format-control-group {
        width: 100%;
    }

    .format-toggle {
        width: 100%;
    }
}

@media (max-width: 520px) {
    .controls {
        align-items: stretch;
    }

    .control-group {
        width: 100%;
    }

    .control-group select,
    button#generate-btn {
        width: 100%;
        max-width: none;
        box-sizing: border-box;
    }

    .face-header {
        align-items: flex-start;
    }

    .brand-block {
        min-width: 0;
    }

    .header-actions {
        max-width: 52%;
    }
}

@media (max-width: 360px) {
    .face-header {
        flex-wrap: wrap;
    }

    .header-actions {
        width: 100%;
        max-width: none;
        justify-content: space-between;
    }
}

.preview-area {
    display: flex;
    gap: 24px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 12px;
    transition: gap 0.35s ease, margin 0.35s ease;
}

.container.editor-collapsed .preview-area {
    gap: 18px;
    margin-top: 4px;
}

.screen-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

.screen-label {
    font-weight: bold;
    color: #94a3b8;
    text-transform: uppercase;
    font-size: 14px;
}

.screen-preview {
    width: var(--screen-w);
    height: calc(var(--screen-w) * var(--screen-ratio));
    background-color: transparent;
    background-size: cover;
    background-position: center;
    border: 1px solid rgba(255,255,255,0.16);
    border-radius: var(--screen-radius);
    box-shadow: 0 28px 70px rgba(0,0,0,0.55), 0 0 35px rgba(99,102,241,0.08);
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: var(--screen-pad);
    box-sizing: border-box;
    overflow: hidden;
    transition: width 0.35s ease, height 0.35s ease, padding 0.35s ease, border-radius 0.35s ease, box-shadow 0.35s ease;
}

.container.editor-collapsed .screen-preview {
    box-shadow: 0 34px 90px rgba(0,0,0,0.62), 0 0 55px rgba(99,102,241,0.12);
}

/* Color + opacity overlay — sits between bg image and text */
.bg-overlay {
    position: absolute;
    inset: 0;
    z-index: 0;
    border-radius: var(--screen-radius);
    background-color: rgba(5, 5, 5, 1);
    pointer-events: none;
}

.content {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
    min-width: 0;
}

.download-btn {
    position: absolute;
    z-index: 2;
}

.arabic-text {
    font-family: 'Khalaf Quran P001', 'Khalaf Quran P002', 'Amiri', serif;
    font-size: 18px;
    direction: rtl;
    line-height: 1.72;
    color: #fff;
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    margin: 0;
    word-spacing: 5px;
    text-align: center;
    -webkit-font-smoothing: antialiased;
    text-rendering: geometricPrecision;
}

.arabic-line-measure {
    display: inline;
}

/* Shorts keep each semantic Arabic segment on one visual line. The 16:9
   layout intentionally retains its existing wrapping behaviour. */
.container:not(.format-youtube) .arabic-text {
    white-space: nowrap;
}

.container:not(.format-youtube) .arabic-line-measure {
    display: inline-block;
    white-space: nowrap;
}

.ayah-badge {
    position: relative;
    display: inline-grid;
    align-items: center;
    justify-content: center;
    width: 1.75em;
    height: 1.75em;
    margin-inline: 0.06em;
    color: currentColor;
    line-height: 1;
    vertical-align: middle;
}

.ayah-badge-symbol {
    grid-area: 1 / 1;
    display: block;
    font-family: 'Khalaf Quran P001', 'Khalaf Quran P002', Amiri, "Traditional Arabic", serif;
    font-size: 1.85em;
    font-weight: 400;
    line-height: 1;
    color: currentColor;
}

.ayah-badge-svg {
    width: 100%;
    height: 100%;
    display: block;
    overflow: visible;
}

.ayah-badge-img {
    grid-area: 1 / 1;
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
}

.ayah-badge-fill {
    fill: transparent;
}

.ayah-badge-lines {
    filter: drop-shadow(0 0 1.8px rgba(255, 255, 255, 0.35));
}

.ayah-badge-number {
    grid-area: 1 / 1;
    align-self: center;
    justify-self: center;
    transform: translateY(0.05em);
    direction: rtl;
    dominant-baseline: auto;
    fill: currentColor;
    paint-order: stroke;
    color: currentColor;
    font-family: 'Khalaf Quran P001', 'Khalaf Quran P002', Amiri, serif;
    font-size: 0.45em;
    font-weight: 700;
    line-height: 1;
    pointer-events: none;
}

.translation-text {
    font-size: 10px;
    color: #e8e8e8;
    line-height: 1.5;
    position: absolute;
    top: calc(50% + 1.4em);
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    max-width: 100%;
    text-align: center;
    font-weight: 300;
    overflow-wrap: anywhere;
    word-break: normal;
    hyphens: auto;
    -webkit-font-smoothing: antialiased;
    text-rendering: geometricPrecision;
}

.download-btn {
    position: absolute;
    bottom: 20px;
    padding: 8px 16px;
    background: #27ae60;
    color: white;
    border: none;
    border-radius: 20px;
    cursor: pointer;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.3s;
}

.screen-preview:hover .download-btn {
    opacity: 1;
}

/* Hide download buttons when capturing */
.capturing .download-btn {
    display: none;
}
