#history-wrapper {
    --history-bg: var(--menu-bg, #fffdf7);
    --history-ink: var(--menu-text, #252525);
    --history-muted: var(--secondary-text, #67645e);
    --history-line: var(--divider-color, #b9b5ab);
    position: fixed;
    inset: 50% auto auto 50%;
    z-index: 1200;
    display: flex;
    flex-direction: column;
    width: min(1200px, calc(100vw - 32px));
    max-height: min(780px, calc(100dvh - 32px));
    padding: clamp(20px, 3vw, 34px);
    overflow: hidden auto;
    box-sizing: border-box;
    color: var(--history-ink);
    background: var(--history-bg);
    border: 2px solid var(--history-line);
    border-radius: 0;
    box-shadow: none;
    opacity: 0;
    transform: translate(-50%, calc(-50% + 12px));
    transition: opacity 160ms ease, transform 160ms ease;
    pointer-events: none;
    scrollbar-color: var(--history-line) transparent;
    scrollbar-width: thin;
}

#history-wrapper.open {
    opacity: 1;
    transform: translate(-50%, -50%);
    pointer-events: auto;
}

#history-overlay {
    position: fixed;
    inset: 0;
    z-index: 1150;
    background: rgb(30 30 30 / 38%);
    opacity: 0;
    transition: opacity 160ms ease;
    pointer-events: none;
}

#history-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

#history-wrapper h3 {
    margin: 0 0 18px;
    color: var(--history-ink);
    font-size: clamp(1.35rem, 3vw, 2rem);
    font-weight: 800;
    letter-spacing: -.025em;
    text-align: left;
}

#history-tabs {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    margin-bottom: 22px;
    border: 1px solid var(--history-line);
}

#history-tabs button {
    min-height: 44px;
    padding: 8px 14px;
    color: var(--history-muted);
    background: transparent;
    border: 0;
    border-right: 1px solid var(--history-line);
    border-radius: 0;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    transition: color 120ms ease, background-color 120ms ease;
}

#history-tabs button:last-child { border-right: 0; }

#history-tabs button:hover { background: var(--surface-2, #f3f0e8); }

#history-tabs button.active,
#history-tabs button[aria-selected="true"] {
    color: var(--button-text, #fff);
    background: var(--button-bg, #3d7563);
}

#history-tabs button:focus-visible,
#history-pagination button:focus-visible,
.history-card:focus-visible {
    outline: 3px solid var(--focus-ring, #267a63);
    outline-offset: 3px;
}

.library-filters,
.public-level-filters {
    display: flex;
    align-items: end;
    flex-wrap: wrap;
    gap: 12px;
    padding: 0 0 16px;
    margin-bottom: 18px;
    border-bottom: 1px solid var(--history-line, var(--divider-color));
}

.library-filters label,
.public-level-filters label {
    display: grid;
    gap: 5px;
    min-width: 150px;
    color: var(--history-muted, var(--secondary-text));
    font-size: .72rem;
    font-weight: 750;
    letter-spacing: .04em;
    text-transform: uppercase;
}

.library-filters .library-search,
.public-level-filters .public-level-search {
    flex: 1 1 280px;
    min-width: min(280px, 100%);
}

.library-filters input,
.public-level-filters input,
.library-filters select,
.public-level-filters select {
    width: 100%;
    min-height: 44px;
    padding: 8px 34px 8px 10px;
    color: var(--history-ink, var(--menu-text));
    background: var(--menu-bg);
    border: 1px solid var(--history-line, var(--divider-color));
    border-radius: 0;
    font: inherit;
    text-transform: none;
}

.library-filters input:focus-visible,
.public-level-filters input:focus-visible,
.library-filters select:focus-visible,
.public-level-filters select:focus-visible {
    outline: 3px solid var(--state-info);
    outline-offset: 2px;
}

.public-level-stats {
    color: var(--secondary-text);
    font-variant-numeric: tabular-nums;
}

#history-grid {
    --card-width: minmax(min(220px, 100%), 1fr);
    display: grid;
    grid-template-columns: repeat(auto-fit, var(--card-width));
    gap: 12px;
    flex: 1 1 auto;
    width: 100%;
}

#public-levels-panel {
    min-height: 0;
}

.public-levels-list {
    --card-width: minmax(min(220px, 100%), 1fr);
    display: grid;
    grid-template-columns: repeat(auto-fit, var(--card-width));
    align-items: stretch;
    gap: 12px;
    width: 100%;
    margin: 0;
    padding: 0;
    list-style: none;
}

.public-level-card {
    min-width: 0;
}

.public-level-card-inner {
    height: 100%;
}

.public-level-card-inner .public-level-play {
    display: grid;
    grid-template-rows: auto auto auto 1fr;
    width: 100%;
    min-width: 0;
    height: 100%;
    padding: 0;
    color: inherit;
    text-align: left;
    background: transparent;
    border: 0;
}

.public-level-card-inner .public-level-play:hover {
    color: inherit;
    background: transparent;
}

.public-level-title {
    display: block;
    overflow: hidden;
    color: var(--history-ink);
    font-size: 1rem;
    font-weight: 850;
    line-height: 1.3;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.public-level-thumbnail {
    display: grid;
    grid-template-columns: repeat(var(--preview-columns), minmax(0, 1fr));
    grid-template-rows: repeat(var(--preview-rows), minmax(0, 1fr));
    overflow: hidden;
}

.public-level-thumbnail-cell {
    min-width: 0;
    min-height: 0;
    background: var(--cell-bg-checker-odd);
    border-right: 1px solid color-mix(in srgb, var(--cell-border-thin) 55%, transparent);
    border-bottom: 1px solid color-mix(in srgb, var(--cell-border-thin) 55%, transparent);
}

.public-level-thumbnail-cell.checker-even {
    background: var(--cell-bg-checker-even);
}

.public-level-thumbnail-cell.filled {
    background: var(--cell-fill);
}

.public-level-thumbnail-cell.major-column {
    border-right-color: var(--cell-border-thick);
}

.public-level-thumbnail-cell.major-row {
    border-bottom-color: var(--cell-border-thick);
}

.public-level-meta {
    align-self: end;
}

.public-level-cta {
    color: var(--button-bg);
    font-weight: 850;
}

.history-card {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
    gap: 10px;
    min-width: 0;
    padding: 10px;
    box-sizing: border-box;
    color: var(--history-ink);
    text-align: left;
    text-decoration: none;
    background: var(--history-block-bg, #f7f4ec);
    border: 1px solid var(--history-line);
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
    transition: border-color 120ms ease, background-color 120ms ease;
}

.history-card:hover {
    background: var(--surface-2, #efede6);
    border-color: var(--button-bg, #3d7563);
}

.history-card.current-puzzle {
    border: 2px solid var(--button-bg, #3d7563);
    padding: 9px;
}

.history-card-play {
    display: grid;
    grid-template-rows: auto auto auto 1fr;
    gap: 9px;
    min-width: 0;
    color: inherit;
    text-decoration: none;
    cursor: pointer;
}

.history-card-play:focus-visible,
.history-leaderboard-action:focus-visible {
    outline: 3px solid var(--focus-ring, #267a63);
    outline-offset: 2px;
}

.history-card-header {
    display: grid;
    grid-template-rows: auto auto;
    gap: 7px;
    min-width: 0;
    overflow: hidden;
}

.history-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    min-height: 22px;
    align-content: start;
    overflow: hidden;
}

.history-badges:empty { visibility: hidden; }

.history-puzzle-facts {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.history-fact {
    display: inline-grid;
    place-items: center;
    min-height: 28px;
    padding: 4px 8px;
    box-sizing: border-box;
    color: var(--secondary-text);
    background: var(--menu-bg);
    border: 1px solid var(--history-line);
    font-size: .72rem;
    font-weight: 850;
    letter-spacing: .05em;
    line-height: 1;
    text-transform: uppercase;
}

.history-fact-size {
    color: var(--button-text);
    background: var(--button-bg);
    border-color: var(--button-bg);
}

.history-fact-difficulty { border-left-width: 5px; }
.history-fact-easy { border-left-color: var(--state-success); }
.history-fact-medium { border-left-color: var(--state-info); }
.history-fact-hard { border-left-color: var(--cell-border-thick); }
.history-fact-expert { border-left-color: var(--state-error); }

.history-badge {
    display: inline-flex;
    align-items: center;
    min-height: 20px;
    padding: 3px 6px;
    box-sizing: border-box;
    color: var(--history-ink);
    background: #ece9e0;
    border: 1px solid var(--history-line);
    border-radius: 0;
    font-size: .58rem;
    font-weight: 850;
    line-height: 1;
    letter-spacing: .06em;
    text-transform: uppercase;
}

.history-badge-solved { color: #245c48; background: #e5f0e8; border-color: #6d9b83; }
.history-badge-current { color: #294f68; background: #e4edf2; border-color: #7292a5; }
.history-badge-custom { color: #725124; background: #f4ead8; border-color: #b28a4c; }
.history-badge-assisted { color: #70432e; background: #f4e5dc; border-color: #b67b5d; }

.history-card img,
.history-thumbnail {
    width: 100%;
    aspect-ratio: 1;
    height: auto;
    box-sizing: border-box;
    object-fit: cover;
    image-rendering: pixelated;
    background: var(--grid-bg, #fff);
    border: 1px solid var(--history-line);
    border-radius: 0;
}

.history-card-actions {
    display: flex;
    padding-top: 9px;
    border-top: 1px solid var(--history-line);
}

.history-leaderboard-action {
    width: 100%;
    min-height: 40px;
    padding: 8px 10px;
    color: var(--button-bg, #3d7563);
    background: transparent;
    border: 1px solid var(--history-line);
    border-radius: 0;
    font: inherit;
    font-size: .7rem;
    font-weight: 800;
    letter-spacing: .04em;
    text-transform: uppercase;
    cursor: pointer;
    transition: color 120ms ease, background-color 120ms ease, border-color 120ms ease;
}

.history-leaderboard-action:hover {
    color: var(--button-text, #fff);
    background: var(--button-bg, #3d7563);
    border-color: var(--button-bg, #3d7563);
}

/* socialUI owns public cards; these hooks keep its two actions legible in the library family. */
.public-level-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
}

.public-level-leaderboard {
    min-width: 112px;
    min-height: 44px;
    color: var(--button-bg, #3d7563);
    background: transparent;
    border: 1px solid var(--history-line);
    font-weight: 800;
    cursor: pointer;
}

.public-level-leaderboard:hover {
    color: var(--button-text, #fff);
    background: var(--button-bg, #3d7563);
    border-color: var(--button-bg, #3d7563);
}

.public-level-leaderboard:focus-visible {
    outline: 3px solid var(--focus-ring, #267a63);
    outline-offset: 2px;
}

.history-seed {
    width: 100%;
    margin: 0;
    overflow: hidden;
    color: var(--history-ink);
    font-size: .88rem;
    font-weight: 750;
    line-height: 1.35;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.history-meta {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: baseline;
    gap: 4px 8px;
    width: 100%;
    margin: 0;
    color: var(--history-muted);
    font-size: .72rem;
    font-variant-numeric: tabular-nums;
    line-height: 1.4;
}

.history-meta br { display: none; }
.history-meta span { min-width: 0; }
.meta-grid,
.meta-mistakes,
.meta-base,
.meta-penalty,
.meta-total {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.meta-grid { font-weight: 650; }
.meta-mistakes { justify-self: stretch; }
.meta-base,
.meta-penalty { grid-column: 1 / -1; }
.meta-penalty,
.meta-mistakes { color: #a13f35; }
.meta-total {
    grid-column: 1 / -1;
    margin-top: 3px;
    padding-top: 7px;
    color: var(--history-ink);
    font-weight: 800;
    border-top: 1px solid var(--history-line);
}

.meta-label {
    overflow: hidden;
    color: var(--history-muted);
    font-weight: 550;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.meta-value { flex: 0 0 auto; color: inherit; font-weight: 700; text-align: right; }

.history-empty {
    grid-column: 1 / -1;
    display: grid;
    place-content: center;
    min-height: 260px;
    padding: 38px 24px;
    box-sizing: border-box;
    color: var(--history-ink);
    text-align: center;
    background: var(--history-block-bg, #f7f4ec);
    border: 1px dashed var(--history-line);
    border-radius: 0;
}

.history-empty::before {
    content: '';
    width: 48px;
    height: 48px;
    margin: 0 auto 16px;
    border: 2px solid var(--history-line);
    background: var(--surface-2, #efede6);
}

.history-empty strong { font-size: 1.05rem; line-height: 1.4; }
.history-empty p { max-width: 32ch; margin: 7px auto 0; color: var(--history-muted); font-size: .86rem; line-height: 1.55; }

#history-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 22px;
}

#history-pages { display: flex; gap: 5px; }

#history-pagination button {
    display: inline-grid;
    place-items: center;
    min-width: 44px;
    min-height: 44px;
    padding: 8px 12px;
    color: var(--history-ink);
    background: transparent;
    border: 1px solid var(--history-line);
    border-radius: 0;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
}

#history-pagination button:hover:not(:disabled) { background: var(--surface-2, #efede6); }
#history-pagination .active { color: var(--button-text, #fff); background: var(--button-bg, #3d7563); border-color: var(--button-bg, #3d7563); }
#history-pagination button:disabled { opacity: .4; cursor: not-allowed; }

@media (max-width: 760px) {
    #history-wrapper {
        inset: auto 8px 8px;
        width: auto;
        max-height: calc(100dvh - 16px);
        padding: 20px;
        transform: translateY(18px);
    }

    #history-wrapper.open { transform: translateY(0); }
    #history-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

    .library-filters,
    .public-level-filters {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .library-filters .library-search,
    .public-level-filters .public-level-search {
        grid-column: 1 / -1;
        min-width: 0;
    }
}

@media (max-width: 420px) {
    #history-wrapper { padding: 16px; }
    #history-grid { gap: 8px; }
    .history-card { padding: 8px; }
    .history-card.current-puzzle { padding: 7px; }
    .history-meta { font-size: .68rem; }
    #history-tabs button { padding-inline: 6px; font-size: .78rem; }
    .public-level-actions { display: grid; grid-template-columns: 1fr; width: 100%; }
    .public-level-leaderboard { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    #history-wrapper,
    #history-overlay { transition-duration: 1ms; }
}

@media (min-width: 1100px) {
    #history-wrapper,
    #public-levels-sheet {
        width: min(1180px, calc(100vw - 48px));
        max-height: calc(100dvh - 48px);
    }

    #history-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
