/* ============================================================
   Crossword Puzzle Plugin — Frontend Styles
   Author: Aafiya (Fahmina Jawed) — islamhashtag.com
   v1.2 — full mobile fix: clue panel below grid + hidden-input keyboard
   v1.3 — hint button, hinted-cell styling, solve celebration,
          admin-only conflict warning, bigger min cell on tiny phones
   ============================================================ */

:root {
    --cpz-dark:      #1a2744;
    --cpz-blue:      #1e3a6e;
    --cpz-white:     #ffffff;
    --cpz-cell:      46px;
    --cpz-cell-sm:   36px;
    --cpz-cell-xs:   30px;
    --cpz-font:      'Segoe UI', system-ui, sans-serif;
    --cpz-correct:   #d4edda;
    --cpz-wrong:     #f8d7da;
    --cpz-active:    #d6eaff;
    --cpz-highlight: #eef6ff;
}

/* ── Wrapper ─────────────────────────────────────────────── */
.cpz-wrapper {
    font-family: var(--cpz-font);
    color: #1a1a1a;
    max-width: 1000px;
    margin: 24px auto;
    padding: 0 12px;
    box-sizing: border-box;
    position: relative;
}

.cpz-puzzle-title {
    text-align: center;
    font-size: 1.35rem;
    font-weight: 700;
    margin: 0 0 18px;
    color: var(--cpz-dark);
}

/* ── Hidden input for mobile keyboard ────────────────────── */
/* One invisible input positioned off-screen; we focus it on mobile
   so the OS keyboard opens, then route keystrokes to the active cell. */
.cpz-hidden-input {
    position: absolute;
    left: -9999px;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: 0;
    border: none;
    padding: 0;
    margin: 0;
    font-size: 16px;   /* ≥16px prevents iOS zoom */
    pointer-events: none;
}

/* ── Main layout: grid LEFT, clues RIGHT ─────────────────── */
.cpz-layout {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 24px;
}

/* ── Grid column ─────────────────────────────────────────── */
.cpz-grid-wrap {
    flex: 0 0 auto;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}

.cpz-grid {
    display: inline-grid;
    background: var(--cpz-dark);
    border: 3px solid var(--cpz-dark);
    gap: 2px;
    padding: 2px;
    border-radius: 6px;
    user-select: none;
    touch-action: manipulation;
}

/* ── Cells ───────────────────────────────────────────────── */
.cpz-cell {
    width:    var(--cpz-cell);
    height:   var(--cpz-cell);
    background: var(--cpz-dark);
    position: relative;
    display:  flex;
    align-items: center;
    justify-content: center;
    transition: background .12s;
}

.cpz-cell.cpz-open        { background: var(--cpz-white); cursor: text; }
.cpz-cell.cpz-active      { background: var(--cpz-active); }
.cpz-cell.cpz-highlighted { background: var(--cpz-highlight); }
.cpz-cell.cpz-correct     { background: var(--cpz-correct); }
.cpz-cell.cpz-wrong       { background: var(--cpz-wrong); }

/* Cell number */
.cpz-cell-num {
    position:  absolute;
    top: 2px;
    left: 3px;
    font-size: 9px;
    font-weight: 700;
    color: var(--cpz-dark);
    line-height: 1;
    pointer-events: none;
    z-index: 1;
}

/* Cell letter display — replaces the real <input> on mobile */
.cpz-cell-letter {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: max(16px, 1rem);
    font-weight: 700;
    text-transform: uppercase;
    color: var(--cpz-dark);
    position: absolute;
    top: 0; left: 0;
    pointer-events: none;
}

/* Visible cursor blink on active cell (mobile) */
.cpz-cell.cpz-active .cpz-cell-letter::after {
    content: '';
    display: inline-block;
    width: 1px;
    height: 1.1em;
    background: var(--cpz-dark);
    margin-left: 1px;
    vertical-align: middle;
    animation: cpz-blink 1s step-end infinite;
}
/* Hide cursor when there's already a letter */
.cpz-cell.cpz-active .cpz-cell-letter:not(:empty)::after {
    display: none;
}
@keyframes cpz-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

/* Real <input> — kept for desktop keyboard fallback, hidden visually */
.cpz-cell-input {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    border: none;
    background: transparent;
    text-align: center;
    font-size: max(16px, 1.1rem);
    font-weight: 700;
    text-transform: uppercase;
    color: transparent;       /* text shown by .cpz-cell-letter */
    caret-color: transparent;
    outline: none;
    cursor: text;
    padding: 0;
    -webkit-text-size-adjust: 100%;
    /* keep it interactive for desktop */
    z-index: 2;
    opacity: 0;               /* invisible but focusable on desktop */
}

/* ── Active cell outline ─────────────────────────────────── */
.cpz-cell.cpz-active {
    outline: 2px solid #1e3a6e;
    outline-offset: -2px;
}

/* ── Clues column ────────────────────────────────────────── */
.cpz-clues-panel {
    flex: 1 1 240px;
    min-width: 200px;
    max-width: 420px;
    max-height: 70vh;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 4px;
}

.cpz-clues-panel::-webkit-scrollbar { width: 4px; }
.cpz-clues-panel::-webkit-scrollbar-track { background: #f0f0f0; border-radius: 4px; }
.cpz-clues-panel::-webkit-scrollbar-thumb { background: #bbb; border-radius: 4px; }

.cpz-clues-section { margin-bottom: 20px; }

.cpz-clues-heading {
    font-size: .85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    border-bottom: 2px solid var(--cpz-dark);
    padding-bottom: 5px;
    margin: 0 0 8px;
    color: var(--cpz-dark);
    position: sticky;
    top: 0;
    background: white;
    z-index: 1;
}

.cpz-clue-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.cpz-clue-item {
    padding: 7px 8px;
    border-radius: 5px;
    font-size: .875rem;
    line-height: 1.45;
    cursor: pointer;
    margin-bottom: 2px;
    transition: background .12s;
    min-height: 44px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.cpz-clue-item:hover         { background: #eef2ff; }
.cpz-clue-item.cpz-clue-active {
    background: var(--cpz-active);
    font-weight: 600;
}

.cpz-clue-meta {
    display: flex;
    align-items: baseline;
    gap: 4px;
    flex-wrap: wrap;
}

.cpz-clue-number {
    font-weight: 700;
    color: var(--cpz-blue);
    font-size: .85rem;
    min-width: 18px;
    flex-shrink: 0;
}

.cpz-clue-text { flex: 1; }

.cpz-clue-length {
    color: #999;
    font-size: .78rem;
    font-style: italic;
    flex-shrink: 0;
}

/* Answer blanks (show-solution mode) */
.cpz-answer-blanks {
    display: flex;
    gap: 3px;
    margin-top: 5px;
    flex-wrap: wrap;
}

.cpz-blank {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 15px;
    height: 18px;
    border-bottom: 2px solid #aaa;
    font-size: .72rem;
    font-weight: 700;
    color: var(--cpz-dark);
}

/* ── Controls ────────────────────────────────────────────── */
.cpz-controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin: 14px 0 8px;
}

.cpz-btn {
    padding: 10px 18px;
    border: none;
    border-radius: 6px;
    font-size: .875rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity .18s, transform .1s;
    min-height: 44px;
    touch-action: manipulation;
}
.cpz-btn:hover  { opacity: .85; }
.cpz-btn:active { transform: scale(.97); }

.cpz-btn-check    { background: var(--cpz-dark); color: #fff; }
.cpz-btn-hint     { background: #6a4fb3;          color: #fff; }
.cpz-btn-solution { background: #0077cc;          color: #fff; }
.cpz-btn-retry    { background: #e07b00;          color: #fff; }

/* A letter the puzzle gave as a hint (not typed by the student) —
   distinguishable from a self-typed correct answer, but not shown as
   "wrong" or shameful; a small friendly signal that help was given. */
.cpz-cell.cpz-hinted .cpz-cell-letter { color: #6a4fb3; }
.cpz-cell.cpz-hinted { background: #f2edfb; }

/* ── Status message ──────────────────────────────────────── */
.cpz-message {
    padding: 9px 14px;
    border-radius: 6px;
    font-weight: 600;
    font-size: .9rem;
    min-height: 0;
    transition: opacity .25s;
}
.cpz-message:empty  { display: none; }
.cpz-message.success { background: #d4edda; color: #155724; }
.cpz-message.error   { background: #fff3cd; color: #856404; }

/* ── Full-solve celebration ──────────────────────────────── */
.cpz-message.celebrate {
    background: #d4edda;
    color: #155724;
    font-size: 1.05rem;
    text-align: center;
    animation: cpz-pop .4s ease;
}
@keyframes cpz-pop {
    0%   { transform: scale(.9); opacity: 0; }
    60%  { transform: scale(1.04); opacity: 1; }
    100% { transform: scale(1); }
}
.cpz-wrapper.cpz-solved .cpz-grid {
    animation: cpz-grid-glow 1s ease;
}
@keyframes cpz-grid-glow {
    0%, 100% { box-shadow: none; }
    50%      { box-shadow: 0 0 0 4px #ffe08a; }
}

/* ── Mobile active-clue bar (above grid) ─────────────────── */
/* Shown on mobile, hidden on desktop */
.cpz-mobile-clue-bar {
    display: none;
    background: var(--cpz-dark);
    color: white;
    padding: 10px 14px;
    font-size: .9rem;
    line-height: 1.4;
    border-radius: 6px;
    margin-bottom: 10px;
    min-height: 52px;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.cpz-mobile-clue-bar .cpz-mob-badge {
    display: flex;
    align-items: baseline;
    gap: 4px;
    flex-shrink: 0;
}
.cpz-mobile-clue-bar .cpz-mob-num {
    font-weight: 700;
    color: #f0c040;
    font-size: 1.05rem;
}
.cpz-mobile-clue-bar .cpz-mob-dir {
    font-size: .7rem;
    opacity: .75;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: #f0c040;
}
.cpz-mobile-clue-bar .cpz-mob-text {
    flex: 1 1 100%;
    font-size: .85rem;
    opacity: .9;
}

/* ── Error ───────────────────────────────────────────────── */
.cpz-error {
    color: #c62828;
    font-style: italic;
    padding: 8px;
}

/* ── Admin-only conflict warning (never shown to students) ── */
.cpz-admin-only-warning {
    background: #fff3cd;
    color: #664d03;
    border: 1px solid #ffe69c;
    border-radius: 6px;
    padding: 10px 14px;
    font-size: .85rem;
    margin-bottom: 14px;
}
.cpz-admin-only-warning a { font-weight: 600; }

/* ═══════════════════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ═════════════════════════════════════════════════════════ */

/* Tablet */
@media (max-width: 768px) {
    :root { --cpz-cell: var(--cpz-cell-sm); }

    .cpz-clues-panel {
        max-height: 55vh;
        min-width: 160px;
        font-size: .83rem;
    }
    .cpz-clue-item  { font-size: .82rem; padding: 6px; }
    .cpz-cell-num   { font-size: 8px; }
    .cpz-btn        { padding: 8px 14px; font-size: .83rem; }
}

/* Mobile — stack layout, show clue bar above grid, show full clue list below */
@media (max-width: 560px) {
    :root { --cpz-cell: var(--cpz-cell-xs); }

    /* Stack: bar → grid → clues */
    .cpz-layout {
        flex-direction: column;
        gap: 12px;
    }

    /* Grid fills full width */
    .cpz-grid-wrap {
        width: 100%;
        text-align: center;
        overflow-x: auto;
    }

    /* Show the active-clue bar above the grid */
    .cpz-mobile-clue-bar {
        display: flex;
    }

    /* Clue panel below grid: visible, not scrollable, full width */
    .cpz-clues-panel {
        display: block;          /* SHOW it, don't hide */
        max-height: none;        /* no scroll limit */
        min-width: 0;
        max-width: 100%;
        width: 100%;
        overflow-y: visible;
        font-size: .82rem;
    }

    /* Two-column clue layout (across | down side by side) on mobile */
    .cpz-layout > .cpz-clues-panel {
        display: flex;
        flex-direction: row;
        gap: 12px;
        align-items: flex-start;
        flex-wrap: wrap;
    }
    .cpz-clues-section {
        flex: 1 1 140px;
        min-width: 140px;
        margin-bottom: 12px;
    }

    /* Controls stretch */
    .cpz-controls { justify-content: stretch; }
    .cpz-btn { flex: 1; text-align: center; padding: 12px 8px; }

    .cpz-cell-num   { font-size: 7px; top: 1px; left: 2px; }
}

/* Very small phones */
@media (max-width: 380px) {
    :root { --cpz-cell: 30px; } /* was 26px — a touch bigger for younger fingers */
    .cpz-cell-num { font-size: 7px; }
    .cpz-layout > .cpz-clues-panel { flex-direction: column; }
}
