/* bgdb: minimal dark theme. Tweak the tokens in :root first. */

@font-face {
    font-family: "0xProto";
    src: url("/fonts/0xProto-Regular.woff2") format("woff2");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "0xProto";
    src: url("/fonts/0xProto-Bold.woff2") format("woff2");
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}
@font-face {
    font-family: "0xProto";
    src: url("/fonts/0xProto-Italic.woff2") format("woff2");
    font-weight: 400;
    font-style: italic;
    font-display: swap;
}

:root {
    color-scheme: dark;

    --bg: #111214;
    --surface: #17181b;
    --surface-2: #1e2024;
    --border: #2b2e34;
    --border-strong: #3a3e46;
    --text: #e4e5e7;
    --muted: #8a8f99;
    --accent: #8ab4f8;
    --accent-text: #0d1117;
    --danger: #f28b82;
    --ok: #81c995;
    --warn: #fdd663;
    --info: #c58af9;
    --gold: #e8c15a;

    --font: "0xProto", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    --radius: 6px;
    --gap: 1rem;
    --page-width: 1200px;
}

*, *::before, *::after { box-sizing: border-box; }

html {
    font-size: 14px;
    -webkit-text-size-adjust: none;
    text-size-adjust: none;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    line-height: 1.5;
    font-variant-ligatures: none;
}

h1, h2 { font-weight: 700; line-height: 1.2; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

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

.muted { color: var(--muted); }

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* ---- layout ------------------------------------------------------------- */

.topbar {
    display: flex;
    align-items: center;
    gap: 2rem;
    max-width: var(--page-width);
    margin: 0 auto;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.brand {
    color: var(--text);
    font-weight: 700;
    letter-spacing: 0.05em;
}

.topbar nav { display: flex; gap: 1.25rem; }
.topbar nav a { color: var(--muted); padding: 0.25rem 0; border-bottom: 2px solid transparent; }
.topbar nav a:hover { color: var(--text); text-decoration: none; }
.topbar nav a[aria-current="page"] { color: var(--text); border-bottom-color: var(--accent); }

body.wide main, body.wide .topbar { max-width: none; }

.topbar .sign-out { margin-left: auto; }
.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; }
.login-form { width: min(22rem, 100%); display: flex; flex-direction: column; gap: 1rem; }
.login-form h1 { margin-bottom: 0.5rem; }
.login-form input { width: 100%; }

main {
    max-width: var(--page-width);
    margin: 0 auto;
    padding: 1.5rem;
}

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--gap);
    margin-bottom: 1.25rem;
}

/* ---- form controls -------------------------------------------------------- */

input, select, textarea, button { font: inherit; color: inherit; }

input:not([type="checkbox"]), select, textarea {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.45rem 0.6rem;
    min-width: 0;
}
input:not([type="checkbox"]):hover, select:hover, textarea:hover { border-color: var(--border-strong); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; border-color: var(--accent); }
textarea { width: 100%; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.8; }
input[type="checkbox"] { accent-color: var(--accent); width: 1rem; height: 1rem; margin: 0; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.45rem 0.9rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    background: var(--surface-2);
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.5; cursor: progress; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 700; }
.btn-primary:hover { filter: brightness(1.1); border-color: var(--accent); }
.btn-small { padding: 0.2rem 0.55rem; font-size: 0.9rem; }
.btn-danger { color: var(--danger); background: transparent; border-color: transparent; }
.btn-danger:hover { border-color: var(--danger); }
.btn-link { background: none; border-color: transparent; color: var(--accent); }
.btn-link:hover { text-decoration: underline; border-color: transparent; }
.btn-icon { width: 2rem; padding-inline: 0; color: var(--muted); }
.btn.htmx-request { opacity: 0.6; }

/* ---- filters --------------------------------------------------------------- */

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}
.filters .grow { flex: 1 1 16rem; }
.filters .narrow { width: 7rem; }
.filters label.inline { display: inline-flex; align-items: center; gap: 0.4rem; color: var(--muted); }

/* ---- results table ------------------------------------------------------------ */

.summary { color: var(--muted); margin: 0 0 0.5rem; }
.empty { color: var(--muted); padding: 3rem 0; text-align: center; border: 1px dashed var(--border); border-radius: var(--radius); }

.results { transition: opacity 120ms; }
.results.htmx-request, .filters.htmx-request + .results { opacity: 0.6; }

table.data {
    width: 100%;
    border-collapse: collapse;
}
.data th {
    text-align: left;
    font-weight: 400;
    color: var(--muted);
    font-size: 0.85rem;
    text-transform: lowercase;
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border-strong);
}
.data td {
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.data tbody tr:hover { background: var(--surface); }
.data .num { text-align: right; font-variant-numeric: tabular-nums; }
.data tr.htmx-swapping { opacity: 0; transition: opacity 150ms; }
.data tr.htmx-added { animation: fade-in 300ms; }

.cell-name .name { font-weight: 700; color: var(--text); }
.cell-name .sub { display: block; color: var(--muted); font-size: 0.85rem; }
.is-expansion .cell-name { padding-left: 1.75rem; }
.is-expansion .cell-name .name { font-weight: 400; }
.cell-date { white-space: nowrap; font-variant-numeric: tabular-nums; }
.cell-notes { color: var(--muted); max-width: 28rem; white-space: pre-line; }
.cell-actions { text-align: right; white-space: nowrap; width: 1%; }
.cell-actions .btn { opacity: 0.55; }
tr:hover .cell-actions .btn, .cell-actions .btn:focus-visible { opacity: 1; }

.load-more td { text-align: center; border-bottom: 0; padding: 1rem; }
.load-more:hover { background: none !important; }

.badge {
    display: inline-block;
    padding: 0 0.45rem;
    border-radius: 999px;
    border: 1px solid currentColor;
    font-size: 0.8rem;
    line-height: 1.5;
}
.badge-owned { color: var(--ok); }
.badge-wishlist { color: var(--info); }
.badge-ordered { color: var(--warn); }
.badge-sold { color: var(--muted); }
.badge-none { color: var(--muted); border-color: transparent; }

.participants { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 0.25rem 0.9rem; }
.participants li { display: inline-flex; gap: 0.35rem; align-items: baseline; }
.participants .me .who { font-weight: 700; }
.participants .ai .who { color: var(--muted); font-style: italic; }
.participants .score { color: var(--muted); font-variant-numeric: tabular-nums; }
.result { font-size: 0.8rem; }
.result-win { color: var(--ok); }
.result-loss { color: var(--danger); }
.result-draw { color: var(--warn); }

/* ---- modal ---------------------------------------------------------------------- */

dialog#modal {
    width: min(40rem, calc(100vw - 2rem));
    max-height: calc(100dvh - 2rem);
    padding: 0;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: calc(var(--radius) * 1.5);
    box-shadow: 0 1rem 3rem rgb(0 0 0 / 0.5);
}
dialog#modal::backdrop { background: rgb(0 0 0 / 0.6); }
dialog#modal[open] { animation: fade-in 120ms ease-out; }

.modal-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1.5rem;
}
.modal-title-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }

.modal-form fieldset {
    margin: 0;
    padding: 0.75rem 1rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.modal-form legend { color: var(--muted); padding: 0 0.4rem; font-size: 0.9rem; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
    gap: 0.75rem 1rem;
}
.span-all { grid-column: 1 / -1; }
.span-2 { grid-column: span 2; }

.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field > label { color: var(--muted); font-size: 0.9rem; }
.field.check { flex-direction: row; }
.field.check label { display: inline-flex; align-items: center; gap: 0.5rem; color: var(--text); cursor: pointer; }
.field.invalid input, .field.invalid select, .field.invalid textarea, fieldset.invalid { border-color: var(--danger); }
/* add game: the priority only applies to wishlist entries */
.modal-form:not(:has(#status option[value="wishlist"]:checked)) .wishlist-only { display: none; }

.field-error, fieldset .field-error { color: var(--danger); font-size: 0.85rem; }
.form-error { color: var(--danger); margin: 0; padding: 0.5rem 0.75rem; border: 1px solid var(--danger); border-radius: var(--radius); }

.checks { display: flex; flex-wrap: wrap; gap: 0.4rem 1.25rem; }
.checks label { display: inline-flex; align-items: center; gap: 0.45rem; cursor: pointer; }

.player-rows { list-style: none; margin: 0 0 0.6rem; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.player-row { display: grid; grid-template-columns: 1fr 6.5rem 6rem 2rem; gap: 0.5rem; align-items: center; }
.ai-row input[name="ai_label"] { font-style: italic; }
.row-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }

.advanced summary { color: var(--muted); cursor: pointer; }
.advanced[open] summary { margin-bottom: 0.75rem; }

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    padding-top: 0.25rem;
}

/* ---- toasts ---------------------------------------------------------------------- */

.flash {
    position: fixed;
    inset: auto 0 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    pointer-events: none;
    z-index: 10;
}
.toast {
    margin: 0;
    padding: 0.6rem 1rem;
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-left: 3px solid var(--ok);
    border-radius: var(--radius);
    box-shadow: 0 0.5rem 1.5rem rgb(0 0 0 / 0.4);
    max-width: calc(100vw - 2rem);
    animation: toast 4s ease-in-out forwards;
}
.toast-error { border-left-color: var(--danger); animation-duration: 7s; }

@keyframes toast {
    0% { opacity: 0; translate: 0 0.5rem; }
    5%, 90% { opacity: 1; translate: 0 0; }
    100% { opacity: 0; }
}
@keyframes fade-in {
    from { opacity: 0; }
}

.error-page { text-align: center; padding-top: 20vh; }
.error-page h1 { font-size: 3rem; color: var(--muted); }

/* ---- challenges ----------------------------------------------------------------- */

.progress { display: inline-flex; align-items: center; gap: 0.5rem; }
.progress-bar { width: 6rem; height: 0.35rem; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--ok); }
.progress-text { color: var(--muted); font-size: 0.85rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
/* finished: a thin gold outline on the bar and gold numbers */
.progress.complete .progress-bar { outline: 1px solid var(--gold); outline-offset: 1px; }
.progress-text.complete { color: var(--gold); }
.overall { margin: 0 0 1rem; }
.overall .progress-bar { width: 14rem; }
.subtitle { margin: 0.25rem 0 0; }
.description { color: var(--muted); white-space: pre-line; margin: 0 0 1rem; }

.mark-done { margin-bottom: 1rem; }
.mark-done summary { color: var(--muted); cursor: pointer; }
.mark-form { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
.mark-form input:not([type="date"]) { flex: 1 1 12rem; }

.subtable { margin: 0 0 2rem; }
.subtable-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem 1rem; margin-bottom: 0.5rem; font-size: 1.05rem; }
.load-more-tables { text-align: center; padding: 1rem; }

/* a big matrix scrolls inside its own box, at most a window tall, like a spreadsheet: the page never
   scrolls sideways, and the column labels (thead) and row names stay frozen while the cells scroll */
.matrix-scroll {
    overflow: auto;
    width: fit-content;   /* only as wide as its table: beside it, the wheel scrolls the page */
    max-width: 100%;
    max-height: calc(100dvh - 1.5rem);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.matrix thead { position: sticky; top: 0; z-index: 3; background: var(--bg); }
.matrix .col-totals > * { border-bottom: 1px solid var(--border-strong); }
.matrix { --cell: 1.75rem; border-collapse: separate; border-spacing: 0; font-size: 0.85rem; }
.matrix th { font-weight: 400; }
.matrix .variant { padding-left: 1.1rem; }
.matrix th.corner { position: sticky; left: 0; z-index: 4; background: var(--bg); vertical-align: bottom; text-align: left; color: var(--muted); padding: 0 0.75rem 0.4rem 0; }
.matrix .corner-row, .matrix .corner-col { display: block; white-space: nowrap; font-size: 0.8rem; }
.matrix th.group {
    text-align: left;
    white-space: nowrap;
    font-weight: 700;
    padding: 0.2rem 0.4rem;
    border-left: 1px solid var(--border-strong);
    border-bottom: 1px solid var(--border-strong);
}
/* innermost column labels run diagonally (chart style); the th height comes from the longest label */
.matrix th.diag { position: relative; vertical-align: bottom; padding: 0; width: var(--cell); min-width: var(--cell); max-width: var(--cell); }
.matrix th.diag > div {
    position: absolute;
    left: calc(var(--cell) / 2);
    bottom: 0.3rem;
    transform-origin: left bottom;
    transform: rotate(-45deg);
    white-space: nowrap;
    text-align: left;
}
.matrix th.diag .variant { padding-left: 0; }
.matrix th.short { width: var(--cell); min-width: var(--cell); text-align: center; vertical-align: bottom; padding: 0 0 0.2rem; color: var(--muted); }
.matrix th.short .variant { padding-left: 0; }
.matrix th.diag .variant::before { content: "· "; color: var(--muted); }
.matrix th.row-head {
    text-align: left;
    white-space: nowrap;
    padding: 0 0.75rem 0 0;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    vertical-align: middle;
}
.matrix th.row-head.sticky { position: sticky; left: 0; z-index: 2; }
.matrix .col-totals th.total { position: sticky; left: 0; z-index: 4; background: var(--bg); }
.matrix td.c { width: var(--cell); height: var(--cell); padding: 0; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.matrix tr > td.c:first-of-type { border-left: 1px solid var(--border); }
.matrix tbody tr:first-child td.c { border-top: 1px solid var(--border); }
.matrix td.c button { display: block; width: 100%; height: 100%; padding: 0; border: 0; background: none; cursor: pointer; }
.matrix td.c button:hover { background: var(--surface-2); }
.matrix td.marked button, .matrix td.marked button:hover { background: color-mix(in srgb, var(--accent) 60%, transparent); }
.matrix td.played { background: color-mix(in srgb, var(--ok) 70%, transparent); }
.matrix .total { color: var(--muted); font-size: 0.75rem; white-space: nowrap; padding: 0 0.5rem; text-align: left; font-variant-numeric: tabular-nums; }
.matrix .col-totals .total { padding-top: 0.25rem; padding-bottom: 0.35rem; vertical-align: top; }
.matrix .col-totals th.total { text-align: right; }
.matrix .col-total { padding: 0.25rem 0 0.35rem; text-align: center; }
.matrix .more-rows td, .matrix .end-row td { padding: 0.9rem 0; color: var(--muted); font-size: 0.85rem; text-align: left; }
.matrix .more-rows { cursor: pointer; }
.end-of-list { text-align: center; color: var(--muted); font-size: 0.9rem; padding: 1rem 0 2rem; border-top: 1px dashed var(--border); }
.loading::before {
    content: "";
    display: inline-block;
    width: 0.8em;
    height: 0.8em;
    margin-right: 0.5em;
    vertical-align: -0.1em;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* "?" beside a search box with extra syntax; the explanation shows on hover or focus */
.search-help {
    position: relative;
    flex: none;
    align-self: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    border: 1px solid var(--border-strong);
    border-radius: 50%;
    color: var(--muted);
    font-size: 0.8rem;
    cursor: help;
}
.search-help:hover, .search-help:focus-visible { color: var(--text); border-color: var(--muted); }
.search-help-tip {
    display: none;
    position: absolute;
    top: calc(100% + 0.5rem);
    right: -0.5rem;
    z-index: 20;
    width: max-content;
    max-width: min(28rem, calc(100vw - 2rem));
    padding: 0.75rem 0.9rem;
    background: var(--surface-2);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgb(0 0 0 / 0.45);
    color: var(--text);
    font-size: 0.85rem;
    line-height: 1.45;
    text-align: left;
    cursor: default;
}
/* a bridge over the gap, so moving the pointer onto the tip doesn't close it */
.search-help-tip::before { content: ""; position: absolute; left: 0; right: 0; top: -0.6rem; height: 0.6rem; }
.search-help:hover .search-help-tip, .search-help:focus .search-help-tip { display: block; }
.search-help-tip ul { margin: 0.4rem 0 0; padding-left: 1.1rem; }
.search-help-tip li + li { margin-top: 0.3rem; }
.matrix .col-total span { writing-mode: vertical-rl; transform: rotate(180deg); }

.editor-form { display: flex; flex-direction: column; gap: 1rem; max-width: 62rem; }
.editor-form fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.75rem 1rem 1rem; margin: 0; min-width: 0; }
.editor-form legend { color: var(--muted); padding: 0 0.4rem; font-size: 0.9rem; }
.hint { margin: 0 0 1rem; font-size: 0.9rem; }
.dimension-rows { list-style: none; margin: 0 0 0.75rem; padding: 0; display: flex; flex-direction: column; gap: 1rem; }
.dimension-row { display: flex; flex-direction: column; gap: 0.4rem; padding-bottom: 1rem; border-bottom: 1px dashed var(--border); }
.dimension-fields { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: flex-end; }
.dimension-fields label { display: flex; flex-direction: column; gap: 0.25rem; color: var(--muted); font-size: 0.9rem; }
.dimension-fields label.grow { flex: 1 1 14rem; }
.dimension-fields input, .dimension-fields select { color: var(--text); }
.dimension-fields label.narrow input { width: 5rem; }
.dimension-fixed { flex: 1 1 14rem; padding: 0.45rem 0; }
.dimension-row.invalid textarea { border-color: var(--danger); }

.element-rows { list-style: none; margin: 0 0 0.5rem; padding: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.element-row { display: grid; grid-template-columns: 1fr 2rem; gap: 0.5rem; align-items: center; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; }
    .toast { animation: none; }
}

/* ---- tablets: same layout, a bit tighter ---------------------------------------------- */

@media (max-width: 1024px) {
    main, .topbar { padding-inline: 1rem; }
    .cell-notes { max-width: 18rem; }
}

/* ---- phones: tables become cards ---------------------------------------------------- */

@media (max-width: 720px) {
    html { font-size: 15px; }
    .topbar { gap: 1.25rem; }
    h1 { font-size: 1.3rem; }

    .filters > * { flex: 1 1 calc(50% - 0.5rem); }
    .filters .grow { flex-basis: 100%; }
    .filters .narrow { width: auto; }
    .filters label.inline input { flex: 1; }

    table.data, .data tbody { display: block; }
    .data thead { display: none; }
    .data tbody tr {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 0.4rem 1rem;
        padding: 0.75rem 0;
        border-bottom: 1px solid var(--border);
    }
    .data td { padding: 0; border: 0; }
    .data td[data-label]::before {
        content: attr(data-label);
        display: block;
        color: var(--muted);
        font-size: 0.75rem;
        text-transform: lowercase;
    }
    .data .num { text-align: left; }
    .data .cell-name, .data .cell-notes, .plays td[data-label="Players"] { grid-column: 1 / -1; max-width: none; }
    .data .cell-name::before { display: none; }
    .data .cell-actions { grid-column: 1 / -1; text-align: left; width: auto; display: flex; gap: 0.5rem; margin-top: 0.25rem; }
    .data .cell-actions .btn { opacity: 1; }
    .data .cell-notes:empty { display: none; }
    .is-expansion .cell-name { padding-left: 0; }
    .load-more { display: block !important; border: 0 !important; }
    .load-more td { display: block; }

    dialog#modal { width: 100vw; max-width: 100vw; max-height: 100dvh; margin: auto 0 0; border-radius: var(--radius) var(--radius) 0 0; }
    .modal-form { padding: 1.25rem 1rem; }
    .grid { grid-template-columns: 1fr 1fr; }
    .span-2 { grid-column: 1 / -1; }
    .player-row { grid-template-columns: 1fr 5.5rem 4.5rem 2rem; }
}

/* touch screens: 16px inputs keep iOS Safari from zooming on focus */
@media (pointer: coarse) {
    input:not([type="checkbox"]), select, textarea { font-size: 16px; }
    .btn-small { padding: 0.4rem 0.7rem; }
}
