/* Ghani's QR Studio — app chrome + printable sticker. */

:root {
    color-scheme: light;

    --accent:      #14532d;
    --accent-soft: #15803d;

    /* Controls stay monochrome; the colour belongs to the QR. */
    --solid:       #1a1d21;
    --solid-hover: #2c3037;
    --solid-ink:   #ffffff;
    --ring:        rgba(26, 29, 33, .15);

    --bg:          #f2f5f3;
    --surface:     #ffffff;
    --surface-2:   #f7faf8;
    --border:      #dde5e0;
    --border-soft: #e9efeb;
    --ink:         #10231a;
    --ink-2:       #3d4f46;
    --muted:       #6b7d73;
    --shadow-sm:   0 1px 2px rgba(16, 35, 26, .06);
    --shadow-md:   0 4px 16px rgba(16, 35, 26, .07);
    --shadow-lg:   0 18px 48px rgba(16, 35, 26, .12);
    --radius:      14px;
}

[data-theme="dark"] {
    color-scheme: dark;

    /* Neutral greys — the green stays as an accent only. */
    --accent:      #16a34a;
    --accent-soft: #22c55e;

    /* Inverted in dark mode, so a solid control still reads as the emphasis. */
    --solid:       #e9ebee;
    --solid-hover: #ffffff;
    --solid-ink:   #16181c;
    --ring:        rgba(233, 235, 238, .18);

    --bg:          #121316;
    --surface:     #1a1c20;
    --surface-2:   #212429;
    --border:      #33373e;
    --border-soft: #292d34;
    --ink:         #e8eaed;
    --ink-2:       #b9bec6;
    --muted:       #8b929c;
    --shadow-sm:   0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md:   0 4px 16px rgba(0, 0, 0, .35);
    --shadow-lg:   0 18px 48px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

/* Safety net: no image may widen the page, whatever its intrinsic size. */
img { max-width: 100%; }

/* --- Motion -------------------------------------------------------------
   Named properties, not `transition: all`, which would drag layout too. */

body, .app-bar, .controls-col, .preview-stage, .tabs, .tab, .btn, .chip,
.preset, .contrast-note, .brand-mark, .switch-track, .sticker-foot,
input[type="text"], input[type="file"], select {
    transition: background-color .3s ease, border-color .3s ease,
                color .3s ease, box-shadow .3s ease;
}

/* --- The style-change choreography ---------------------------------------
   Four beats: eyes gather → morph → split back out → modules sweep in.
   Timings are interlocked; see the .qr-enter / .qr-leave rules. */

@keyframes fadeIn  { from { opacity: 0 } to { opacity: 1 } }
@keyframes fadeOut { from { opacity: 1 } to { opacity: 0 } }

/* Beat 1 — outgoing eyes converge on the centre. */
@keyframes eyeGather {
    0%   { opacity: 1; transform: translate(0, 0) scale(1); }
    78%  { opacity: 1; }
    100% { opacity: 0; transform: translate(var(--tx, 0), var(--ty, 0)) scale(.9); }
}

/* Beats 2–3 — new shape resolves, then splits back out. */
@keyframes eyeMorphSplit {
    0% {
        opacity: 0;
        transform: translate(var(--tx, 0), var(--ty, 0)) scale(.86) rotate(-28deg);
        animation-timing-function: cubic-bezier(.2, .8, .3, 1);
    }
    20% {
        opacity: 1;
        transform: translate(var(--tx, 0), var(--ty, 0)) scale(1.14) rotate(0deg);
        animation-timing-function: ease-in-out;
    }
    38% {
        opacity: 1;
        transform: translate(var(--tx, 0), var(--ty, 0)) scale(.95);
        animation-timing-function: cubic-bezier(.3, .9, .25, 1);
    }
    100% { opacity: 1; transform: translate(0, 0) scale(1); }
}

/* Beat 4 — module rings grow outward from the centre. */
@keyframes bandIn {
    from { opacity: 0; transform: scale(.82); }
    to   { opacity: 1; transform: none; }
}

@keyframes panelIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: none; }
}

@keyframes popIn {
    from { opacity: 0; transform: scale(.9); }
    to   { opacity: 1; transform: none; }
}

@keyframes spinIn {
    from { opacity: 0; transform: rotate(-120deg) scale(.6); }
    to   { opacity: 1; transform: none; }
}

/* Honour the OS reduced-motion setting. */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- App bar */

.app-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px 28px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

.brand { display: flex; align-items: center; gap: 14px; }

.brand-mark {
    flex: none;
    display: grid;
    place-items: center;
    width: 42px; height: 42px;
    border-radius: 11px;
    background: var(--solid);
    color: var(--solid-ink);
}

.brand-mark svg { width: 26px; height: 26px; }

.brand-text h1 {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: -.01em;
}

.brand-text p { margin: 2px 0 0; font-size: .8rem; color: var(--muted); }

/* ----------------------------------------------------------------- Layout */

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 400px;
    gap: 28px;
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px;
    align-items: start;
}

@media (max-width: 940px) {
    .layout { grid-template-columns: minmax(0, 1fr); }
    .app-bar { padding: 14px 18px; }
}

/* ------------------------------------------------------------ Preview col */

.preview-col { position: sticky; top: 96px; }

@media (max-width: 940px) { .preview-col { position: static; } }

.preview-stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 36px 24px 28px;
    background:
        radial-gradient(circle at 1px 1px, var(--border) 1px, transparent 0) 0 0 / 18px 18px,
        var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.qr-meta {
    margin: 20px 0 0;
    font-size: .74rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.export-bar { margin-top: 16px; display: flex; gap: 10px; }
.export-bar .btn { flex: 1; padding: 13px 16px; }

.export-note {
    margin: 10px 2px 0;
    font-size: .74rem;
    color: var(--muted);
    text-align: center;
}

/* ---------------------------------------------------------- The sticker */

.sticker {
    width: 360px;
    /* Top band repeats the header colour so the anti-aliased rounded clip has
       something dark to blend into; over plain #fff it leaves a white hairline
       around the corners. Taller than the 20px radius, so it covers the curve. */
    background:
        linear-gradient(140deg, var(--sticker-accent, #1a1d21),
                                var(--sticker-accent-2, #3a4048))
            top / 100% 24px no-repeat,
        #fff;
    color: #10231a;
    /* Most browsers drop the header gradient when printing without this. */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    border-radius: 20px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    text-align: center;
}

/* No header to back — the band would just be a bar across a white card. */
.sticker.is-headless { background: #fff; }

.sticker-head {
    position: relative;
    background: linear-gradient(140deg, var(--sticker-accent, #1a1d21), var(--sticker-accent-2, #3a4048));
    color: #fff;
    padding: 22px 26px 24px;
    overflow: hidden;
}

/* Soft highlight so the colour block doesn't read as a plain rectangle.
   Radial and fully transparent by 72%: a rotated hard-edged gradient shows
   its own seams once the header is dark. */
.sticker-head::after {
    content: "";
    position: absolute;
    inset: 0;
    background: radial-gradient(130% 105% at 10% -25%,
                                rgba(255, 255, 255, .13),
                                rgba(255, 255, 255, .04) 45%,
                                rgba(255, 255, 255, 0) 72%);
    pointer-events: none;
}

.sticker-head .en {
    position: relative;
    font-size: 1.12rem;
    font-weight: 700;
    line-height: 1.32;
    letter-spacing: -.01em;
}

.sticker-head .ar {
    position: relative;
    font-size: 1.05rem;
    font-weight: 600;
    margin-top: 6px;
    direction: rtl;
    line-height: 1.6;
    opacity: .92;
}

.sticker-head .en:empty, .sticker-head .ar:empty { display: none; }

/* Drops the gap when only one of the two lines is filled. */
.sticker-head .en:empty + .ar { margin-top: 0; }

.sticker-body { padding: 24px 26px 20px; }

/* Recessed panel, so the code reads as placed rather than pasted on. */
.qr-frame {
    display: inline-block;
    padding: 14px;
    border-radius: 18px;
    background: #fbfcfb;
    border: 1px solid #e8edea;
    box-shadow: inset 0 1px 3px rgba(16, 35, 26, .05);
}

.qr-wrap {
    position: relative;
    display: inline-block;
    line-height: 0;
    border-radius: 10px;
    overflow: hidden;
}

.qr-wrap svg { display: block; width: 250px; height: 250px; }

/* Incoming code stacks over the outgoing one (script.js drops the old node at
   the end). Its background stays transparent for beat 1 so the old eyes show
   while they travel. */
.qr-wrap svg.qr-enter { position: absolute; top: 0; left: 0; }

/* Eyes pivot on themselves (the shape changing); bands on the symbol centre
   (closing inward). */
.qr-enter .qr-eye,
.qr-leave .qr-eye {
    transform-box: fill-box;
    transform-origin: center;
}

.qr-enter .qr-band {
    transform-box: view-box;
    transform-origin: center;
}

/* Beat 1 (0 – .40s) — old modules clear out, old eyes gather to the centre. */
.qr-leave .qr-band,
.qr-leave .qr-fade { animation: fadeOut .24s ease both; }
.qr-leave .qr-eye  { animation: eyeGather .4s cubic-bezier(.5, 0, .35, 1) both; }

/* Beats 2–3 (.34 – 1.14s) — morph at the centre, then split to the corners. */
.qr-enter .qr-bg  { animation: fadeIn .16s ease .34s both; }
.qr-enter .qr-eye { animation: eyeMorphSplit .8s .34s both; }

/* Beat 4 (.92s onward) — the innermost ring first, rippling outward. */
.qr-enter .qr-band {
    animation: bandIn .34s cubic-bezier(.2, .8, .3, 1) both;
    animation-delay: calc(920ms + var(--b, 0) * 20ms);
}

.qr-enter .qr-fade { animation: fadeIn .3s ease 1.16s both; }

.hint {
    margin: 16px 0 0;
    font-size: .84rem;
    font-weight: 500;
    color: #46564d;
}

.hint:empty { display: none; }

.url-text {
    margin: 7px 0 0;
    font-size: .68rem;
    color: #90a098;
    word-break: break-all;
    line-height: 1.45;
    font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}

.url-text:empty { display: none; }

.sticker-foot {
    margin: 0 26px;
    padding: 12px 0 15px;
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: #93a29a;
    border-top: 1px solid #edf1ef;
}

.sticker-foot:empty { display: none; }

/* Shown in place of the code until a destination is entered. */
.qr-empty {
    display: grid;
    place-items: center;
    width: 250px; height: 250px;
    border: 2px dashed #dbe3de;
    border-radius: 12px;
    padding: 0 24px;
    font-size: .8rem;
    line-height: 1.5;
    color: #9aa8a1;
    text-align: center;
}

/* ----------------------------------------------------------- Controls col */

.controls-col {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.tabs {
    display: flex;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
}

.tab {
    flex: 1;
    padding: 13px 8px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    font: inherit;
    font-size: .82rem;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    white-space: nowrap;
}

.tab:hover { color: var(--ink-2); }

.tab.is-active {
    color: var(--ink);
    border-bottom-color: var(--solid);
    background: var(--surface);
}

.panel { display: none; padding: 22px; }
.panel.is-active { display: block; animation: panelIn .24s ease both; }

.field { margin-bottom: 20px; }
.field:last-child { margin-bottom: 0; }

/* Read-only orientation text at the head of a panel. */
.panel-intro {
    margin: 0 0 20px;
    padding: 12px 14px;
    border-radius: 10px;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    font-size: .78rem;
    line-height: 1.55;
    color: var(--muted);
}

.req {
    float: right;
    font-size: .68rem;
    font-weight: 600;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--muted);
}

::placeholder { color: var(--muted); opacity: .75; }

label {
    display: block;
    font-size: .8rem;
    font-weight: 600;
    margin-bottom: 7px;
    color: var(--ink-2);
}

.sub-label { margin-top: 16px; }

.sub-label .value {
    float: right;
    font-weight: 500;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

input[type="text"], select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: .88rem;
}

input[type="text"]:focus, select:focus, input[type="file"]:focus {
    outline: none;
    border-color: var(--solid);
    box-shadow: 0 0 0 3px var(--ring);
}

input[type="file"] {
    width: 100%;
    font-size: .8rem;
    color: var(--muted);
    padding: 8px;
    border: 1px dashed var(--border);
    border-radius: 9px;
    background: var(--surface-2);
}

input[type="range"] {
    width: 100%;
    accent-color: var(--solid);
    margin: 4px 0 0;
}

.help {
    margin: 7px 0 0;
    font-size: .75rem;
    line-height: 1.5;
    color: var(--muted);
}

.help code {
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    border-radius: 4px;
    padding: 0 4px;
    font-size: .95em;
}

.help.warn { color: #b45309; }

.is-dependent[hidden] { display: none; }

/* --------------------------------------------------------------- Switches */

.switch-row { margin-bottom: 18px; }

.switch {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: .84rem;
    font-weight: 500;
    color: var(--ink-2);
    cursor: pointer;
    margin: 0;
}

.switch input { position: absolute; opacity: 0; pointer-events: none; }

.switch-track {
    flex: none;
    width: 38px; height: 22px;
    border-radius: 11px;
    background: var(--border);
    position: relative;
    transition: background .16s;
}

.switch-track::after {
    content: "";
    position: absolute;
    top: 3px; left: 3px;
    width: 16px; height: 16px;
    border-radius: 50%;
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    transition: transform .16s, background-color .16s;
}

/* The knob has to invert with the track, or it vanishes in dark mode. */
.switch input:checked + .switch-track::after { background: var(--solid-ink); }

.switch input:checked + .switch-track { background: var(--solid); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 3px var(--ring); }

/* ----------------------------------------------------------------- Colors */

.color-row { display: flex; gap: 10px; align-items: center; }

input[type="color"] {
    flex: none;
    width: 46px; height: 40px;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface);
    cursor: pointer;
}

.hex { text-transform: lowercase; font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }

.contrast-note {
    font-size: .75rem;
    line-height: 1.5;
    padding: 10px 12px;
    border-radius: 9px;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    color: var(--muted);
}

.contrast-note.is-bad {
    background: #fef2f2;
    border-color: #fecaca;
    color: #b91c1c;
}

[data-theme="dark"] .contrast-note.is-bad {
    background: #2a1414;
    border-color: #5c2626;
    color: #fca5a5;
}

/* ------------------------------------------------------- Presets + chips */

.preset-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.preset {
    padding: 7px 5px 5px;
    background: var(--surface-2);
    border: 1.5px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    text-align: center;
    animation: popIn .3s ease both;
    animation-delay: calc(var(--i, 0) * 25ms);
    transition: background-color .3s ease, border-color .18s ease,
                transform .18s cubic-bezier(.2, .8, .3, 1), box-shadow .18s ease;
}

.preset:hover { transform: translateY(-2px); border-color: var(--ink-2); }
.preset:active { transform: translateY(0) scale(.96); }

.preset.is-active {
    border-color: var(--solid);
    box-shadow: 0 0 0 3px var(--ring);
}

.preset svg { display: block; width: 100%; height: auto; border-radius: 5px; }

.preset span {
    display: block;
    margin-top: 4px;
    font-size: .64rem;
    font-weight: 600;
    color: var(--muted);
}

.preset.is-active span { color: var(--ink); }

.chip-row { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
    padding: 7px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    font: inherit;
    font-size: .78rem;
    font-weight: 500;
    color: var(--ink-2);
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease,
                color .18s ease, transform .18s cubic-bezier(.2, .8, .3, 1);
}

.chip:hover { border-color: var(--ink-2); transform: translateY(-1px); }
.chip:active { transform: scale(.94); }
.chip:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }

.chip.is-active {
    background: var(--solid);
    border-color: var(--solid);
    color: var(--solid-ink);
}

/* ---------------------------------------------------------------- Buttons */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 11px 16px;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: var(--surface);
    color: var(--ink-2);
    font: inherit;
    font-size: .85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color .18s ease, border-color .18s ease,
                color .18s ease, transform .18s cubic-bezier(.2, .8, .3, 1);
}

.btn:hover { background: var(--surface-2); border-color: var(--ink-2); transform: translateY(-1px); }
.btn:active { transform: translateY(0) scale(.97); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }

/* Re-keyed by script.js on every toggle so the icon spins in. */
#themeIcon { display: inline-block; animation: spinIn .4s cubic-bezier(.2, .8, .3, 1) both; }

.btn-primary {
    background: var(--solid);
    border-color: var(--solid);
    color: var(--solid-ink);
    box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
    background: var(--solid-hover);
    border-color: var(--solid-hover);
    box-shadow: var(--shadow-md);
}

.btn:disabled {
    opacity: .45;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.btn:disabled:hover { background: var(--surface); border-color: var(--border); }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-block { width: 100%; }

@media (max-width: 620px) { .btn-label { display: none; } }

/* ------------------------------------------------------------------ Modal */

.modal {
    width: min(92vw, 340px);
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--shadow-lg);
}

.modal::backdrop { background: rgba(0, 0, 0, .5); }

.modal-title { margin: 0 0 14px; font-size: .95rem; font-weight: 700; }

.modal-preview {
    display: grid;
    place-items: center;
    padding: 12px;
    background: var(--surface-2);
    border: 1px solid var(--border-soft);
    border-radius: 10px;
}

.modal-preview img { display: block; width: 170px; height: 170px; border-radius: 4px; }

/* The display rule above outranks [hidden]'s, so say it explicitly. */
.modal-preview[hidden] { display: none; }

.modal-meta {
    margin: 12px 2px 16px;
    font-size: .78rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ------------------------------------------------------------------ Print */

@media print {
    body { background: #fff; }
    .app-bar, .controls-col, .export-bar, .export-note, .qr-meta { display: none !important; }
    .layout { display: block; padding: 0; max-width: none; }
    .preview-col { position: static; }
    .preview-stage { padding: 0; background: none; border: none; }
    .sticker { box-shadow: none; border: 1px solid #d5e0d9; margin: 30px auto; break-inside: avoid; }
}
