:root {
    --red: #c90d12;
    --ink: #161616;
    --muted: #6b6b6b;
    --line: #e5e0da;
    --paper: #fbfaf8;
    --white: #ffffff;
    --shadow: 0 18px 50px rgba(40, 34, 30, 0.14);
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    overflow-x: hidden;
}

body {
    margin: 0;
    width: 100%;
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow-x: hidden;
    color: var(--ink);
    background:
        linear-gradient(135deg, rgba(201, 13, 18, 0.08), transparent 36%),
        linear-gradient(315deg, rgba(22, 22, 22, 0.05), transparent 38%),
        var(--paper);
    font-family: "IBM Plex Sans Arabic", Tahoma, Arial, sans-serif;
    -webkit-text-size-adjust: 100%;
}

button,
input {
    font: inherit;
}

.app {
    width: min(1320px, calc(100% - 32px));
    margin: 0 auto;
    min-height: 100vh;
    display: grid;
    grid-template-columns: minmax(260px, 390px) minmax(300px, 430px) minmax(260px, 390px);
    column-gap: 24px;
    row-gap: 18px;
    align-items: start;
    padding: 32px 0;
}

.panel {
    grid-column: 2;
    grid-row: 1;
    width: 100%;
    margin: 0 auto;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 24px;
}

.controls-panel {
    grid-column: 2;
    grid-row: 2;
    width: 100%;
    margin: 0 auto;
    background: var(--white);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: var(--shadow);
    padding: 24px;
}

.brand-logo-link {
    display: block;
    width: min(168px, 43%);
    margin: 0 auto 14px;
    border-radius: 8px;
}

.brand-logo-link:focus-visible {
    outline: 3px solid rgba(201, 13, 18, 0.28);
    outline-offset: 4px;
}

.brand-logo {
    display: block;
    width: 100%;
    height: auto;
}

h1 {
    margin: 0 auto 18px;
    font-size: clamp(26px, 4vw, 38px);
    line-height: 1.35;
    letter-spacing: 0;
    font-weight: 800;
    text-align: center;
    padding: 14px 18px;
    border: 1px solid rgba(201, 13, 18, 0.18);
    border-radius: 8px;
    background: rgba(201, 13, 18, 0.045);
}

.controls {
    width: min(100%, 456px);
    margin-inline: auto;
}

.lead {
    margin: 0;
    color: #4a4a4a;
    font-size: 16px;
    font-weight: 500;
    line-height: 1.9;
    text-align: justify;
    text-align-last: center;
}

label {
    display: block;
    margin-bottom: 10px;
    color: var(--muted);
    font-weight: 700;
}

.input-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
}

input {
    width: 100%;
    min-width: 0;
    min-height: 48px;
    border: 1px solid #d7d0c9;
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 16px;
    color: var(--ink);
    background: #fff;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    direction: rtl;
}

input:focus {
    border-color: var(--red);
    box-shadow: 0 0 0 3px rgba(201, 13, 18, 0.12);
}

button {
    min-height: 48px;
    border: 1px solid #d7d0c9;
    border-radius: 8px;
    padding: 10px 18px;
    color: var(--ink);
    background: #fff;
    cursor: pointer;
    font-weight: 700;
    transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
    -webkit-appearance: none;
    appearance: none;
    touch-action: manipulation;
}

button:hover {
    transform: translateY(-1px);
    border-color: #bfb6ad;
}

.actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 14px;
}

.actions #clear-btn {
    grid-column: 1 / -1;
}

.primary {
    border-color: var(--red);
    color: #fff;
    background: var(--red);
}

.preview-card {
    min-width: 0;
    background: rgba(255, 255, 255, 0.72);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 12px;
    box-shadow: var(--shadow);
}

.story-card {
    grid-column: 3;
    grid-row: 3;
}

.post-card {
    grid-column: 1;
    grid-row: 3;
}

.preview-head {
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 10px;
    margin-bottom: 10px;
    text-align: center;
}

.preview-head h2 {
    margin: 0;
    font-size: 17px;
    line-height: 1.4;
}

.preview-head span {
    color: var(--muted);
    direction: ltr;
    unicode-bidi: isolate;
    font-size: 13px;
    white-space: nowrap;
}

.footer-credit {
    grid-column: 1 / -1;
    order: 10;
    display: grid;
    gap: 6px;
    text-align: center;
    color: var(--muted);
    font-size: 15px;
    line-height: 1.45;
}

.generation-counter {
    direction: rtl;
    color: var(--ink);
    font-weight: 700;
}

.generation-counter strong {
    color: var(--red);
    font-weight: 800;
}

.credit-line {
    margin: 0;
}

.credit-line-en {
    direction: ltr;
    unicode-bidi: isolate;
}

.credit-line-ar {
    direction: rtl;
}

.whatsapp-float {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: #25d366;
    border: 2px solid #fff;
    box-shadow: 0 8px 22px rgba(7, 94, 84, 0.28);
    text-decoration: none;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.whatsapp-float:hover {
    transform: translateY(-2px);
    background: #1ebe5d;
    box-shadow: 0 10px 26px rgba(7, 94, 84, 0.34);
}

.whatsapp-float svg {
    width: 27px;
    height: 27px;
    fill: #fff;
}

.footer-credit a {
    color: var(--red);
    font-weight: 700;
    text-decoration: none;
}

.footer-credit a:hover {
    text-decoration: underline;
}

canvas {
    width: 100%;
    max-width: 100%;
    height: auto;
    display: block;
    border-radius: 8px;
    background: #fff;
}

.story-canvas {
    aspect-ratio: 9 / 16;
    max-height: 76vh;
    object-fit: contain;
}

.post-canvas {
    aspect-ratio: 4 / 5;
}

@media (max-width: 820px) {
    .app {
        grid-template-columns: 1fr;
        width: min(calc(100% - 24px), 640px);
        gap: 18px;
        padding: max(14px, env(safe-area-inset-top)) 0 calc(max(18px, env(safe-area-inset-bottom)) + 30px);
    }

    .panel {
        grid-column: 1;
        grid-row: auto;
        order: 1;
        padding: 20px 18px 22px;
    }

    .controls-panel {
        grid-column: 1;
        grid-row: auto;
        order: 2;
        padding: 18px;
    }

    .story-card {
        grid-column: 1;
        grid-row: auto;
        order: 3;
    }

    .post-card {
        grid-column: 1;
        grid-row: auto;
        order: 4;
    }

    .footer-credit {
        order: 5;
        gap: 4px;
        font-size: 14px;
    }

    .story-canvas {
        max-height: none;
    }

    .whatsapp-float {
        width: 44px;
        height: 44px;
        bottom: calc(max(12px, env(safe-area-inset-bottom)) + 6px);
    }
}

@media (max-width: 480px) {
    .app {
        width: min(calc(100% - 18px), 640px);
    }

    h1 {
        font-size: 27px;
        margin-bottom: 14px;
        padding: 12px;
    }

    .lead {
        font-size: 15px;
        line-height: 1.85;
    }

    .brand-logo-link {
        width: min(154px, 52%);
        padding: 10px 14px;
    }

    .input-row,
    .actions {
        grid-template-columns: 1fr;
    }

    .actions #clear-btn {
        grid-column: auto;
    }

    button {
        width: 100%;
    }

    .whatsapp-float {
        right: max(10px, env(safe-area-inset-right));
        bottom: calc(max(10px, env(safe-area-inset-bottom)) + 4px);
    }
}

@media (max-width: 380px) {
    .panel,
    .preview-card {
        padding: 12px;
    }

    h1 {
        font-size: 24px;
    }

    .preview-head {
        display: block;
    }
}
