/* ============================================================
   NETS Builder — Library page (2026-04-30 redesign v2)
   Inherits app.css tokens directly. No bespoke --lib-* layer.
   In-grid tile expansion via View Transitions API replaces the
   prior overlay-panel architecture.
   ============================================================ */

/* ---------- Apple animation curves (page-scoped) ---------- */
:root {
    --apple-spring-open:  cubic-bezier(.16, 1, .3, 1);
    --apple-spring-close: cubic-bezier(.22, 1, .36, 1);
    --apple-ease:         cubic-bezier(.25, .1, .25, 1);
}

/* ---------- Page-level typography ---------- */
body[data-page="library"] {
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text",
                 "Helvetica Neue", "Inter", "Segoe UI", sans-serif;
    overflow-x: hidden;
    /* Inherit body bg from app.css (plain --bg + global accent radials) */
}

body[data-page="library"] h1 {
    letter-spacing: -.025em;
    font-size: clamp(20px, 2.4vw, 26px);
    line-height: 1.1;
}
body[data-page="library"] .topbar h1,
body[data-page="library"] .brand-block h1 {
    font-size: 1.35rem;
    letter-spacing: -.025em;
    line-height: 1.1;
}
body[data-page="library"] h2 { letter-spacing: -.03em; font-size: 24px; }
body[data-page="library"] h3 { letter-spacing: -.02em; font-size: 18px; }

/* ---------- Glass toolbar (search + lang chips) ---------- */
.lib-toolbar {
    display: grid;
    grid-template-columns: auto minmax(280px, 1fr) auto;
    align-items: center;
    gap: 16px;
    padding: 14px 16px;
    border-radius: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-low);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    position: relative;
    z-index: 1;
}

.lib-filter-row {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.lib-filter-label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.13em;
    color: var(--text);
}

.lib-lang-row { min-width: 0; }

.lib-chip-group {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    padding: 4px;
    background: var(--accent-light);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.lib-chip {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.02em;
    padding: 6px 12px;
    border-radius: 999px;
    color: var(--text-muted);
    background: transparent;
    border: none;
    cursor: pointer;
    transition: background 180ms var(--apple-ease),
                color 180ms var(--apple-ease),
                box-shadow 180ms var(--apple-ease);
    line-height: 1.2;
}

.lib-chip:hover {
    color: var(--accent-hover);
    background: color-mix(in oklab, var(--accent) 14%, transparent);
}

.lib-chip:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.lib-chip.is-active {
    color: #fff;
    background: var(--accent);
    box-shadow: 0 4px 12px var(--accent-glow);
}

[data-theme="dark"] .lib-chip {
    color: #e5e7eb;
}

[data-theme="dark"] .lib-chip:hover {
    color: #ffffff;
}

.lib-chip[disabled] {
    opacity: 0.4;
    cursor: not-allowed;
}

.lib-search {
    width: 100%;
    min-width: 260px;
    max-width: none;
    justify-self: stretch;
}

.lib-search input { min-width: 0; }

#lib-clear-btn {
    min-height: 38px;
    padding: 0 14px;
    border-radius: 10px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-weight: 500;
    transition: background 150ms ease, border-color 150ms ease, color 150ms ease;
}
#lib-clear-btn:hover {
    background: var(--accent-light);
    border-color: var(--accent);
    color: var(--accent);
}

/* ---------- Stats row ---------- */
.lib-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
    margin-bottom: 16px;
    position: relative;
    z-index: 1;
}

.lib-stat {
    position: relative;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 18px;
    border-radius: 16px;
    background: var(--surface-elevated);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-low);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    transition: transform 220ms var(--apple-ease),
                border-color 220ms var(--apple-ease),
                box-shadow 220ms var(--apple-ease);
}

.lib-stat:hover {
    transform: translateY(-2px);
    border-color: color-mix(in oklab, var(--accent) 30%, var(--border));
    box-shadow: var(--shadow-mid);
}

.lib-stat-icon {
    width: 44px;
    height: 44px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    border-radius: 12px;
    font-size: 20px;
    color: var(--accent, #0066CC);
    background: color-mix(in oklab, var(--accent, #0066CC) 12%, transparent);
    border: 1px solid color-mix(in oklab, var(--accent, #0066CC) 24%, transparent);
}

.lib-stat .stat-num {
    display: block;
    font-size: 26px;
    line-height: 1;
    letter-spacing: -.03em;
    font-weight: 700;
    color: var(--text);
}

.lib-stat .stat-label {
    display: block;
    margin-top: 4px;
    font-weight: 600;
    font-size: 14px;
    color: var(--text);
}

.lib-stat .stat-sub {
    display: block;
    margin-top: 2px;
    color: var(--text-muted);
    font-size: 12px;
}

/* ---------- Stage (the panel that holds the tile grid) ---------- */
.lib-stage {
    position: relative;
    padding: 20px;
    border-radius: 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-low);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    z-index: 1;
}

.lib-stage-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    padding: 4px 4px 28px;  /* 28px breathing room before the tile grid */
}

.lib-stage-head h2 {
    font-size: 24px;
    letter-spacing: -.03em;
    color: var(--text);
    margin: 4px 0 0;
}

/* ---------- Subject tile grid ---------- */
.lib-subject-grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
}

/* ---------- Subject tile (compact + expanded states share this) ----------
   Tile is a <div role="button" tabindex="0"> — not a real <button> —
   because expanded markup nests <button>s (close, grade chips) and <a>s
   (homework cards), which would be invalid inside a real <button>. The
   rules below ensure the tile still feels like a button: pointer
   cursor, left-aligned text, full-width block, inherited font, and a
   :focus-visible outline. Without these, the tile would default to
   text-cursor + block layout with no focus ring. */
.subject-tile {
    position: relative;
    min-height: 150px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 12px;
    padding: 18px;
    border-radius: 16px;
    overflow: hidden;
    text-align: left;
    cursor: pointer;
    color: var(--text);
    background:
        linear-gradient(150deg,
            color-mix(in oklab, var(--accent, #0066CC) 18%, white 82%) 0%,
            color-mix(in oklab, var(--accent, #0066CC) 6%, white 94%) 100%);
    border: 1px solid color-mix(in oklab, var(--accent, #0066CC) 30%, var(--border));
    box-shadow: var(--shadow-low);
    transform: translateZ(0);
    transition: transform 240ms var(--apple-spring-open),
                box-shadow 240ms var(--apple-spring-open),
                border-color 240ms var(--apple-spring-open);
    font-family: inherit;
    width: 100%;
}

[data-theme="dark"] .subject-tile {
    background:
        linear-gradient(150deg,
            color-mix(in oklab, var(--accent, #006FE0) 32%, #1c1c1e 68%) 0%,
            color-mix(in oklab, var(--accent, #006FE0) 14%, #1c1c1e 86%) 100%);
    border: 1px solid color-mix(in oklab, var(--accent, #006FE0) 34%, var(--border));
}

.subject-tile:not(.is-expanded):hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-mid);
    border-color: color-mix(in oklab, var(--accent, #0066CC) 50%, var(--border));
}

.subject-tile:not(.is-expanded):active {
    transform: translateY(-1px);
    transition-duration: .12s;
}

.subject-tile:focus-visible {
    outline: 2px solid var(--accent, #0066CC);
    outline-offset: 3px;
}

/* Decorative orb on compact tiles only */
.subject-tile .tile-orb {
    position: absolute;
    width: 130px;
    height: 130px;
    right: -40px;
    top: -40px;
    border-radius: 999px;
    background: radial-gradient(circle,
        color-mix(in oklab, var(--accent, #0066CC) 38%, transparent),
        transparent 70%);
    opacity: .55;
    pointer-events: none;
}

[data-theme="dark"] .subject-tile .tile-orb {
    opacity: .42;
}

.subject-tile .tile-icon {
    position: relative;
    z-index: 1;
    width: 44px;
    height: 44px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    border-radius: 12px;
    background: color-mix(in oklab, var(--accent, #0066CC) 16%, white 84%);
    border: 1px solid color-mix(in oklab, var(--accent, #0066CC) 30%, var(--border));
    font-size: 20px;
    line-height: 1;
    font-weight: 700;
    color: var(--accent, #0066CC);
}

[data-theme="dark"] .subject-tile .tile-icon {
    background: color-mix(in oklab, var(--accent, #006FE0) 22%, #1c1c1e 78%);
    color: #fff;
    border-color: color-mix(in oklab, var(--accent, #006FE0) 40%, var(--border));
}

.subject-tile .tile-copy {
    position: relative;
    z-index: 1;
    width: 100%;
}

.subject-tile .tile-copy h3 {
    font-size: 17px;
    line-height: 1.1;
    letter-spacing: -.02em;
    white-space: nowrap;
    color: var(--text);
    font-weight: 700;
    margin: 0;
}

.subject-tile .tile-copy p {
    margin: 4px 0 0;
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.3;
    min-height: 16px;
}

.subject-tile .tile-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-top: auto;
    color: var(--text-muted);
    font-size: 12.5px;
    font-weight: 600;
    width: 100%;
    position: relative;
    z-index: 1;
}

/* "Open ↗" pill — clones .btn-primary from app.css */
.subject-tile .tile-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 10px;
    background: var(--accent, #0066CC);
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    border: 1px solid var(--accent, #0066CC);
    box-shadow: 0 4px 12px var(--accent-glow);
    transition: background 150ms ease,
                transform 120ms ease,
                box-shadow 150ms ease;
    white-space: nowrap;
}

.subject-tile:not(.is-expanded):hover .tile-pill {
    background: var(--accent-hover, #0056b3);
    border-color: var(--accent-hover, #0056b3);
    transform: translateY(-1px);
    box-shadow: 0 6px 16px var(--accent-glow);
}

/* ---------- EXPANDED TILE (in-grid expansion) ---------- */
.subject-tile.is-expanded {
    grid-column: 1 / -1;        /* span all 4 columns */
    min-height: 480px;
    cursor: default;
    padding: 24px;
    background:
        linear-gradient(160deg,
            color-mix(in oklab, var(--accent, #0066CC) 14%, white 86%) 0%,
            color-mix(in oklab, var(--accent, #0066CC) 4%, white 96%) 100%);
}

[data-theme="dark"] .subject-tile.is-expanded {
    background:
        linear-gradient(160deg,
            color-mix(in oklab, var(--accent, #006FE0) 26%, #1c1c1e 74%) 0%,
            color-mix(in oklab, var(--accent, #006FE0) 10%, #1c1c1e 90%) 100%);
}

/* Compact-only chrome hidden when expanded */
.subject-tile.is-expanded .tile-orb,
.subject-tile.is-expanded .tile-icon-compact,
.subject-tile.is-expanded .tile-copy-compact,
.subject-tile.is-expanded .tile-footer-compact {
    display: none;
}

/* Expanded body hidden when compact */
.subject-tile:not(.is-expanded) .tile-expanded-body {
    display: none;
}

.subject-tile .tile-expanded-body {
    display: flex;
    flex-direction: column;
    gap: 18px;
    height: 100%;
    width: 100%;
}

.subject-tile .expanded-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid var(--border);
}

.subject-tile .expanded-title {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.subject-tile .expanded-glyph {
    width: 52px;
    height: 52px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    border-radius: 14px;
    background: color-mix(in oklab, var(--accent, #0066CC) 14%, white 86%);
    border: 1px solid color-mix(in oklab, var(--accent, #0066CC) 28%, var(--border));
    font-size: 24px;
    line-height: 1;
    font-weight: 700;
    color: var(--accent, #0066CC);
    box-shadow: 0 4px 12px var(--accent-glow);
}

[data-theme="dark"] .subject-tile .expanded-glyph {
    background: color-mix(in oklab, var(--accent, #006FE0) 22%, #1c1c1e 78%);
    color: #fff;
}

.subject-tile .expanded-title h2 {
    font-size: 24px;
    line-height: 1.1;
    letter-spacing: -.03em;
    color: var(--text);
    margin: 0;
    font-weight: 700;
}

.subject-tile .expanded-title p {
    margin: 4px 0 0;
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.3;
}

.subject-tile .expanded-close {
    width: 36px;
    height: 36px;
    flex: 0 0 auto;
    display: grid;
    place-items: center;
    border-radius: 10px;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border);
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
}
.subject-tile .expanded-close:hover {
    background: var(--accent-light);
    color: var(--accent);
    border-color: var(--accent);
}
.subject-tile .expanded-close:focus-visible {
    outline: 2px solid var(--accent, #0066CC);
    outline-offset: 2px;
}

.subject-tile .expanded-actions {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    flex-wrap: wrap;
}

/* Grade-filter chip inside the expanded tile */
.lib-grade-chip {
    padding: 6px 12px;
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 180ms var(--apple-ease),
                color 180ms var(--apple-ease),
                border-color 180ms var(--apple-ease);
}
.lib-grade-chip:hover {
    color: var(--text);
    background: var(--accent-light);
    border-color: color-mix(in oklab, var(--accent) 30%, var(--border));
}
.lib-grade-chip:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}
.lib-grade-chip.is-active {
    color: #fff;
    background: var(--accent);
    border-color: var(--accent);
    box-shadow: 0 4px 12px var(--accent-glow);
}

/* The card grid INSIDE the expanded tile */
.subject-tile .expanded-cards {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
}

.subject-tile .expanded-empty {
    grid-column: 1 / -1;
    padding: 32px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
}

/* ---------- Homework cards (inside expanded tile) — dashboard parity ---------- */
.homework-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 16px;
    border-radius: 12px;
    background: var(--surface-elevated);
    border: 1px solid var(--border);
    border-top: 3px solid var(--accent, #0066CC);
    box-shadow: var(--shadow-low);
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
    overflow: hidden;
    min-width: 0;
    transition: transform 180ms ease,
                box-shadow 180ms ease,
                border-color 180ms ease;
}

.homework-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-card);
    border-color: color-mix(in oklab, var(--accent) 30%, var(--border));
    border-top-color: var(--accent, #0066CC);
}

.homework-card:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.homework-card .hw-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.homework-card .hw-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    background: color-mix(in oklab, var(--accent, #0066CC) 12%, transparent);
    color: var(--accent, #0066CC);
    border: 1px solid color-mix(in oklab, var(--accent, #0066CC) 24%, transparent);
    font-size: 15px;
    font-weight: 700;
    flex: 0 0 auto;
}

.homework-card .hw-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-muted);
}

.homework-card .hw-title {
    font-size: 15px;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: -.01em;
    color: var(--text);
    margin: 0;
    /* clamp to 2 lines */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    min-height: 38px;
}

.homework-card .hw-id {
    margin: 0;
    font-size: 11px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    color: var(--text-subtle);
}

.homework-card .hw-meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: var(--text-muted);
}

.homework-card .hw-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.homework-card .hw-pill {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-muted);
}
.homework-card .hw-pill[data-mode="hard"] {
    color: #b91d24;
    border-color: rgba(255, 59, 48, 0.28);
    background: rgba(255, 59, 48, 0.08);
}
.homework-card .hw-pill[data-mode="easy"] {
    color: #117a3d;
    border-color: rgba(52, 199, 89, 0.28);
    background: rgba(52, 199, 89, 0.08);
}

.homework-card .hw-open {
    margin-top: auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 14px;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
    box-shadow: 0 4px 12px var(--accent-glow);
    font-weight: 500;
    font-size: 13.5px;
    text-decoration: none;
    transition: background 150ms ease, transform 120ms ease, box-shadow 150ms ease;
}
.homework-card .hw-open:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

/* ---------- View Transitions API tuning ---------- */
::view-transition-old(library-tile),
::view-transition-new(library-tile) {
    animation-duration: 480ms;
    animation-timing-function: cubic-bezier(.16, 1, .3, 1);
}

/* ---------- Loading / error / empty states ---------- */
.loading-state,
.error-state,
.empty-state {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: var(--shadow-low);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    padding: 36px 28px;
    color: var(--text);
    text-align: center;
}

.empty-orb {
    width: 64px;
    height: 64px;
    border-radius: 999px;
    margin: 0 auto 14px;
    display: grid;
    place-items: center;
    font-size: 26px;
    background: var(--accent-light);
    color: var(--accent);
    border: 1px solid color-mix(in oklab, var(--accent) 24%, var(--border));
}

.spinner {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    animation: spin .8s linear infinite;
    margin: 0 auto 14px;
}

@keyframes spin { to { transform: rotate(360deg); } }

.eyebrow {
    font-size: 11px;
    letter-spacing: .13em;
    color: var(--text-muted);
    text-transform: uppercase;
    font-weight: 700;
}

/* ---------- Section-empty fallback ---------- */
.lib-section-empty {
    grid-column: 1 / -1;
    padding: 24px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
}

/* ---------- Responsive: tablet ---------- */
@media (max-width: 1030px) {
    body[data-page="library"] .topbar { grid-template-columns: 1fr; }
    .lib-toolbar { grid-template-columns: 1fr 1fr; gap: 12px; }
    .lib-stats { grid-template-columns: repeat(2, 1fr); }
    .lib-subject-grid { grid-template-columns: repeat(2, 1fr); }
    .subject-tile.is-expanded .expanded-cards { grid-template-columns: repeat(2, 1fr); }
    .lib-stage-head { flex-wrap: wrap; }
}

/* ---------- Responsive: mobile ---------- */
@media (max-width: 650px) {
    .lib-toolbar,
    .lib-stats,
    .lib-subject-grid {
        grid-template-columns: 1fr;
    }
    .subject-tile.is-expanded .expanded-cards { grid-template-columns: 1fr; }

    .lib-toolbar { padding: 12px 14px; }
    .lib-search { min-width: 0; max-width: none; }
    .lib-lang-row,
    #lib-clear-btn { width: 100%; }
    #lib-clear-btn { justify-self: stretch; }

    .lib-stage-head,
    .subject-tile .expanded-actions {
        align-items: flex-start;
        flex-direction: column;
    }

    .subject-tile.is-expanded { padding: 18px; }
    .subject-tile .tile-copy h3 { white-space: normal; }
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
    ::view-transition-old(library-tile),
    ::view-transition-new(library-tile) {
        animation-duration: 0ms;
    }
}
