/* ── GOOGLE FONTS ────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800;900&family=Fraunces:ital,wght@0,700;1,500&family=Urbanist:wght@400;500;600;700&display=swap');

/* ── DESIGN TOKENS ───────────────────────────────────── */
:root {
    --bg:           #fdf8f2;
    --surface:      #ffffff;
    --surface-alt:  #fff9f0;
    --border:       #e8dfd4;

    --ink:          #2b2118;
    --ink-muted:    #7a6a5a;

    --accent:       #e8533a;
    --accent-light: #fde8e4;
    --accent-hover: #cf3e28;

    --language:     #ffffff;
    --language-bg:  #f0ebff;
    --thematic:     #ffffff;
    --thematic-bg:  #fef3c7;
    --numeracy:     #ffffff;
    --numeracy-bg:  #e0f2fe;

    --shadow-sm:    0 1px 3px rgba(43,33,24,.08), 0 1px 2px rgba(43,33,24,.06);
    --shadow-md:    0 4px 12px rgba(43,33,24,.10), 0 2px 6px rgba(43,33,24,.06);
    --shadow-lg:    0 12px 32px rgba(43,33,24,.13), 0 4px 12px rgba(43,33,24,.08);
    --shadow-card:  0 2px 8px rgba(43,33,24,.08);

    --radius-sm:    8px;
    --radius-md:    14px;
    --radius-lg:    20px;
    --radius-xl:    28px;

    --font-display: 'Fraunces', Georgia, serif;
    --font-body:    'Nunito', sans-serif;
}

/* ── RESET & BASE ────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    font-family: var(--font-body);
    color: var(--ink);
    min-height: 100vh;

    /* subtle warm dot grid texture */
    background-image: radial-gradient(circle, #d9cfc4 1px, transparent 1px);
    background-size: 28px 28px;
}

/* ── PAGE SHELL ──────────────────────────────────────── */
.container {
    max-width: 1060px;
    margin: 0 auto;
    padding: 40px 24px 60px;
}

/* ── PAGE HEADER ─────────────────────────────────────── */
h1 {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: clamp(2rem, 5vw, 3rem);
    color: var(--ink);
    margin: 0 0 8px;
    letter-spacing: -0.02em;
    line-height: 1.1;
}

h1 span.accent {
    color: var(--accent);
    font-style: italic;
}

.page-subtitle {
    font-size: 15px;
    color: var(--ink-muted);
    margin: 0 0 36px;
    font-weight: 600;
}

/* ── UTILITY ─────────────────────────────────────────── */
.hidden { display: none !important; }

button {
    font-family: var(--font-body);
    font-weight: 700;
    cursor: pointer;
    transition: all .18s ease;
}

/* ── WORKSHEET GRID ──────────────────────────────────── */
.worksheet-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 22px;
}

/* loading state */
.worksheet-grid p {
    grid-column: 1/-1;
    text-align: center;
    color: var(--ink-muted);
    font-size: 15px;
    padding: 48px 0;
    font-weight: 600;
}

.worksheet-card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    overflow: hidden;
    cursor: pointer;
    box-shadow: var(--shadow-card);
    border: 1.5px solid var(--border);
    transition: transform .2s cubic-bezier(.34,1.56,.64,1),
                box-shadow .2s ease,
                border-color .2s ease;
    position: relative;
}

.worksheet-card::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: var(--radius-lg);
    background: linear-gradient(135deg, rgba(255,255,255,.18) 0%, transparent 60%);
    pointer-events: none;
}

.worksheet-card:hover {
    transform: translateY(-5px) scale(1.01);
    box-shadow: var(--shadow-lg);
    border-color: var(--accent);
}

.worksheet-card img {
    width: 100%;
    height: 185px;
    object-fit: cover;
    display: block;
    background: #f0ebe3;
}

.card-body {
    padding: 14px 16px 18px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: var(--surface);
}

.card-body h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 800;
    color: var(--ink);
    line-height: 1.3;
}

.category-pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 12px;
    border-radius: 99px;
    font-size: 11px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: .06em;
    width: fit-content;
}

/* pill variants by category */
.worksheet-card[data-category="language"] .category-pill {
    color: var(--language);
    background: var(--language-bg);
}
.worksheet-card[data-category="thematic"] .category-pill {
    color: var(--thematic);
    background: var(--thematic-bg);
}
.worksheet-card[data-category="numeracy"] .category-pill {
    color: var(--numeracy);
    background: var(--numeracy-bg);
}

/* ── PREVIEW SECTION ─────────────────────────────────── */
#previewSection {
    animation: slideUp .28s ease both;
}

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

.preview-top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
}

#backBtn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--surface);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--ink-muted);
    padding: 9px 18px;
    font-size: 14px;
    font-weight: 700;
    box-shadow: var(--shadow-sm);
}

#backBtn:hover {
    background: var(--accent-light);
    border-color: var(--accent);
    color: var(--accent);
    transform: translateX(-2px);
}

#worksheetTitle {
    font-family: var(--font-display);
    font-size: clamp(1.3rem, 3vw, 2rem);
    font-weight: 700;
    color: var(--ink);
    margin: 0 0 18px;
    letter-spacing: -0.02em;
}

/* ── TOOLBAR ─────────────────────────────────────────── */
.toolbar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 16px;
    padding: 14px 18px;
    background: var(--surface);
    border: 1.5px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
}

.toolbar button {
    padding: 9px 20px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    border: 1.5px solid transparent;
}

/* Edit button */
#editBtn {
    background: var(--ink);
    color: #fff;
    border-color: var(--ink);
}
#editBtn:hover {
    background: #3d2e22;
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

/* Download PNG */
#downloadBtn {
    background: var(--surface);
    color: var(--ink);
    border-color: var(--border);
}
#downloadBtn:hover {
    background: var(--surface-alt);
    border-color: var(--accent);
    color: var(--accent);
    transform: translateY(-1px);
}

/* Download PDF — primary CTA */
#downloadPdfBtn {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}
#downloadPdfBtn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(232,83,58,.35);
}

/* ── IFRAME ──────────────────────────────────────────── */
.worksheet-frame {
    width: 100%;
    height: 1316px;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-md);
    background: white;
    box-shadow: var(--shadow-md);
}

/* ── IMAGE PICKER MODAL ──────────────────────────────── */
.image-modal {
    position: fixed;
    inset: 0;
    background: rgba(43,33,24,.55);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    backdrop-filter: blur(4px);
    animation: fadeIn .18s ease both;
}

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

.image-modal-content {
    background: var(--surface);
    width: min(920px, 95vw);
    max-height: 82vh;
    overflow: auto;
    padding: 28px 28px 32px;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
    border: 1.5px solid var(--border);
    animation: modalIn .22s cubic-bezier(.34,1.56,.64,1) both;
}

@keyframes modalIn {
    from { opacity: 0; transform: scale(.93) translateY(12px); }
    to   { opacity: 1; transform: scale(1)   translateY(0);    }
}

.image-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.image-modal-content h2 {
    font-family: var(--font-display);
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0;
    color: var(--ink);
    letter-spacing: -0.02em;
}

.image-modal-close {
    background: transparent;
    border: none;
    font-size: 26px;
    line-height: 1;
    color: var(--ink-muted);
    cursor: pointer;
    padding: 2px 8px;
    border-radius: 8px;
    flex-shrink: 0;
}

.image-modal-close:hover { background: var(--surface-alt); color: var(--ink); }

/* Upload button */
#uploadImageBtn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 10px 22px;
    background: var(--ink);
    color: #fff;
    border: none;
    border-radius: var(--radius-md);
    font-size: 13.5px;
}
#uploadImageBtn:hover {
    background: #3d2e22;
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

#uploadImageBtn::before {
    content: '↑';
    font-size: 15px;
    font-weight: 900;
}

/* ── FOLDER TABS ─────────────────────────────────────── */
#folderTabs {
    display: flex;
    gap: 8px;
    margin: 18px 0 14px;
    flex-wrap: wrap;
}

.folder-btn {
    padding: 7px 18px;
    border: 1.5px solid var(--border);
    background: var(--surface-alt);
    border-radius: 99px;
    font-size: 13px;
    font-weight: 700;
    color: var(--ink-muted);
}

.folder-btn:hover,
.folder-btn.active {
    background: var(--accent-light);
    border-color: var(--accent);
    color: var(--accent);
}

/* ── IMAGE BANK ──────────────────────────────────────── */
.image-bank {
    display: grid;
    grid-template-columns: repeat(auto-fill, 110px);
    gap: 14px;
    margin-top: 6px;
}

.image-bank img {
    width: 100px;
    height: 100px;
    object-fit: contain;
    cursor: pointer;
    border-radius: var(--radius-sm);
    border: 2px solid var(--border);
    background: var(--surface-alt);
    padding: 6px;
    transition: border-color .15s, transform .15s, box-shadow .15s;
}

.image-bank img:hover {
    border-color: var(--accent);
    transform: scale(1.07);
    box-shadow: var(--shadow-md);
}

/* ── SCROLLBAR ───────────────────────────────────────── */
.image-modal-content::-webkit-scrollbar { width: 6px; }
.image-modal-content::-webkit-scrollbar-track { background: transparent; }
.image-modal-content::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 99px;
}

/* ── RESPONSIVE ──────────────────────────────────────── */
@media (max-width: 600px) {
    .container { padding: 24px 16px 40px; }
    .toolbar   { padding: 10px 12px; }
    .worksheet-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; }
    .image-modal-content { padding: 20px 16px 24px; }
    .image-bank { grid-template-columns: repeat(auto-fill, 85px); }
}

/* Per-field AI pencil icons (the ✎ buttons on each editable field) are no
   longer styled here — each template's own template.html owns its pencil's
   CSS (class .ai-pencil) and exact placement, since design differs per
   template. See backend/templates/language/Answer_and_Colour/template.html
   for the reference implementation. */

/* ── FIELD EDIT MODAL (per-field AI regenerate) ─────────── */
/* ── Field Edit Modal — ai-tool.html style ──────────────────────────────── */

.field-edit-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 200;
    padding: 24px;
    animation: fadeIn .18s ease both;
}

.field-edit-modal-content {
    position: relative;
    background: #fff;
    width: 640px;
    max-width: 100%;
    padding: 30px;
    border-radius: 16px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    gap: 0;
    font-family: "Urbanist", var(--font-body), sans-serif;
}

/* Loading overlay */
.field-edit-loader {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0.78);
    border-radius: 16px;
    align-items: center;
    justify-content: center;
    z-index: 10;
}
.field-edit-loader.visible,
.field-edit-loader:not(.hidden) { display: flex; }
.field-edit-loader.hidden        { display: none; }

.field-edit-loader-dots {
    display: flex;
    gap: 8px;
}
.field-edit-loader-dots span {
    width: 10px; height: 10px;
    border-radius: 50%;
    background: #5f1296;
    animation: fe-bounce 0.6s infinite alternate;
}
.field-edit-loader-dots span:nth-child(2) { animation-delay: 0.2s; }
.field-edit-loader-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes fe-bounce {
    from { transform: translateY(0);   opacity: 0.5; }
    to   { transform: translateY(-8px); opacity: 1;  }
}

/* Header */
.field-edit-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 14px;
}
.field-edit-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}
.field-edit-icon {
    width: 28px; height: 28px;
    background: #5f1296;
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    flex-shrink: 0;
}
.field-edit-icon svg { width: 14px; height: 14px; }
.field-edit-title {
    font-family: "Poppins", var(--font-display), sans-serif;
    font-weight: 700;
    font-size: 15px;
    color: #090011;
}
.field-edit-close {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 2px;
    display: flex; align-items: center; justify-content: center;
    color: rgba(0,0,0,0.54);
    border-radius: 6px;
    transition: background .15s;
}
.field-edit-close:hover { background: #f3f3f3; color: #090011; }
.field-edit-close svg   { width: 22px; height: 22px; }

/* Current text box */
.field-edit-current-box {
    border: 1.5px solid #a098ae;
    border-radius: 11px;
    padding: 10px;
    min-height: 120px;
    max-height: 180px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: #e1e1ea transparent;
    margin-bottom: 0;
}
.field-edit-current-textarea {
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    font-family: "Urbanist", var(--font-body), sans-serif;
    font-size: 16px;
    font-weight: 500;
    line-height: 1.45;
    color: #1b1339;
    background: transparent;
    min-height: 100px;
}

/* Linked fields panel */
.field-edit-linked {
    margin-top: 12px;
    padding: 10px 12px;
    background: #faf5ff;
    border: 1px solid #ecd9fb;
    border-radius: 11px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.field-edit-linked.hidden { display: none; }
.field-edit-linked-title {
    font-size: 10.5px;
    font-weight: 700;
    color: #5f1296;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.field-edit-linked-row    { display: flex; flex-direction: column; gap: 3px; }
.field-edit-linked-label  { font-size: 11px; font-weight: 600; color: #5f1296; }
.field-edit-linked-textarea {
    width: 100%;
    font-size: 13px;
    font-family: "Urbanist", var(--font-body), sans-serif;
    padding: 7px 9px;
    border-radius: 8px;
    border: 1px solid #e4d2f9;
    outline: none;
    resize: none;
    color: #1b1339;
    background: #fff;
    transition: border-color .15s;
}
.field-edit-linked-textarea:focus   { border-color: #5f1296; box-shadow: 0 0 0 2px rgba(95,18,150,.1); }
.field-edit-linked-textarea:disabled { opacity: .65; }

/* Prompt box — instruction + regen button together */
.field-edit-prompt-box {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    border: 1.5px solid #a098ae;
    border-radius: 11px;
    padding: 10px 6px 8px;
    gap: 8px;
    margin-top: 12px;
}
.field-edit-instruction {
    width: 100%;
    border: none;
    outline: none;
    resize: none;
    background: transparent;
    font-family: "Urbanist", var(--font-body), sans-serif;
    font-size: 16px;
    font-weight: 500;
    line-height: 1.4;
    color: #1b1339;
    padding: 0 6px;
    min-height: 48px;
    max-height: 160px;
    overflow-y: auto;
}
.field-edit-instruction::placeholder { color: #a098ae; }

.field-edit-regen-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    align-self: flex-end;
    margin-right: 4px;
    border: none;
    border-radius: 10px;
    padding: 8px 16px;
    font-family: "Urbanist", var(--font-body), sans-serif;
    font-size: 14px;
    font-weight: 700;
    color: #fff;
    cursor: pointer;
    background: linear-gradient(87.08deg, #5f1296 0.83%, #e41588 45.88%, #ffbd76 97.71%);
    transition: opacity .15s;
    white-space: nowrap;
}
.field-edit-regen-btn:hover    { opacity: .9; }
.field-edit-regen-btn:disabled { opacity: .5; cursor: not-allowed; }

/* Error */
.field-edit-error {
    margin-top: 8px;
    font-size: 12.5px;
    color: #b91c1c;
}

/* Undo + Grammar row */
.field-edit-actions-row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    padding-top: 10px;
}
.field-edit-undo {
    background: transparent;
    border: 2px solid #5f1296;
    cursor: pointer;
    padding: 6px;
    width: 50px;
    height: 30px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 8px;
    transition: border-color .15s, background .15s;
    flex-shrink: 0;
}
.field-edit-undo:hover           { border-color: #5f1296; background: #f3e8fd; }
.field-edit-undo.hidden          { display: none; }
.field-edit-undo img             { width: 18px; height: 18px; display: block; }

.field-edit-grammar-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    border: 2px solid #5f1296;
    background: transparent;
    color: #5f1296;
    border-radius: 10px;
    padding: 7px 16px;
    font-family: "Urbanist", var(--font-body), sans-serif;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    height: 36px;
    transition: background .15s;
}
.field-edit-grammar-btn:hover    { background: #f3e8fd; }
.field-edit-grammar-btn:disabled { opacity: .5; cursor: not-allowed; }

/* Cancel / Save bottom row */
.field-edit-bottom-btns {
    display: flex;
    gap: 10px;
    padding-top: 16px;
    width: 100%;
}
.field-edit-cancel,
.field-edit-save {
    flex: 1;
    border: none;
    border-radius: 12px;
    padding: 11px 14px;
    font-family: "Urbanist", var(--font-body), sans-serif;
    font-size: 17px;
    font-weight: 700;
    cursor: pointer;
    transition: background .15s;
}
.field-edit-cancel {
    background: #e1e1ea;
    color: #6a6a82;
}
.field-edit-cancel:hover { background: #d4d4de; }
.field-edit-save {
    background: #5f1296;
    color: #fff;
}
.field-edit-save:hover    { background: #4d0e7a; }
.field-edit-save:disabled { opacity: .5; cursor: not-allowed; }

@media (max-width: 600px) {
    .field-edit-modal-content { padding: 20px 16px 22px; }
    .field-edit-current-textarea,
    .field-edit-instruction { font-size: 15px; }
}

