:root {
    --bg: #f3f4f6;
    --bg-2: #ffffff;
    --panel: #ffffff;
    --line: #d7dbe2;
    --text: #111111;
    --muted: #5c6570;
    --accent: #111111;
    --named: #000000;
    --danger: #c0392b;
    --danger-bg: #f8e8e6;
    --shadow: 0 18px 50px rgba(17, 17, 17, 0.12);
    --radius: 16px;
    --safe-top: env(safe-area-inset-top);
    --safe-bottom: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.app {
    width: min(1400px, calc(100% - 1.25rem));
    margin: 0 auto;
    padding: calc(0.9rem + var(--safe-top)) 0 calc(1.5rem + var(--safe-bottom));
}

.top {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 1rem;
    margin-bottom: 0.9rem;
}

.eyebrow {
    margin: 0 0 0.15rem;
    color: var(--muted);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    font-size: 0.72rem;
    font-weight: 700;
}

h1 {
    margin: 0;
    font-size: clamp(1.45rem, 4vw, 2rem);
    letter-spacing: -0.03em;
}

.stats {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.45rem 0.85rem;
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    white-space: nowrap;
}

.stats strong { color: var(--text); font-size: 1.15rem; }
.stats span { color: var(--muted); font-size: 0.8rem; }

.chrome {
    position: sticky;
    top: 0;
    z-index: 20;
    padding: 0.15rem 0 0.55rem;
    background: var(--bg);
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.4rem;
    background: var(--panel);
    padding: 0.35rem;
    border-radius: 999px;
    border: 1px solid var(--line);
}

.tab, .chip, .btn, .cell {
    font: inherit;
    cursor: pointer;
    touch-action: manipulation;
}

.tab {
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 999px;
    padding: 0.7rem 0.8rem;
    font-weight: 700;
}

.tab.is-active {
    background: var(--accent);
    color: #ffffff;
}

.page { margin-top: 1rem; }
.page[hidden] { display: none !important; }

.toolbar {
    display: grid;
    gap: 0.7rem;
    margin-bottom: 0.9rem;
}

.search input,
.field input {
    width: 100%;
    border: 1px solid var(--line);
    background: var(--bg-2);
    color: var(--text);
    border-radius: 12px;
    padding: 0.8rem 0.9rem;
    font: inherit;
}

.search input:focus,
.field input:focus {
    outline: 2px solid var(--accent);
    border-color: transparent;
}

.filters {
    display: flex;
    gap: 0.4rem;
    overflow-x: auto;
}

.chip {
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--muted);
    border-radius: 999px;
    padding: 0.45rem 0.8rem;
    white-space: nowrap;
}

.chip.is-active {
    background: var(--accent);
    color: #ffffff;
    border-color: var(--accent);
}

.btn {
    border: 0;
    border-radius: 12px;
    padding: 0.75rem 1rem;
    font-weight: 700;
}

.btn-primary { background: var(--accent); color: #ffffff; }
.btn-danger { background: var(--danger-bg); color: var(--danger); border: 1px solid #e8c4bf; }
.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
.btn-ghost.danger { color: var(--danger); border-color: #e8c4bf; }

.board {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(3.35rem, 1fr));
    gap: 0.38rem;
}

.cell {
    min-height: 3.35rem;
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--text);
    border-radius: 10px;
    padding: 0.28rem 0.2rem 0.32rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.08rem;
}

.cell-num {
    font-variant-numeric: tabular-nums;
    font-weight: 700;
    letter-spacing: 0.04em;
    font-size: 0.86rem;
}

.fields {
    display: grid;
    gap: 0.75rem;
}

.cell-name,
.cell-phone {
    display: none;
}

.cell.named {
    background: #000000;
    border-color: #000000;
    color: #000000;
}

.cell.named .cell-num,
.cell.named .cell-name,
.cell.named .cell-phone {
    display: none;
}

.cell.is-hidden { display: none; }

.empty-filter {
    color: var(--muted);
    text-align: center;
    padding: 1.5rem 0.5rem;
}

.history { display: grid; gap: 0.8rem; }

.round {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
}

.round-head {
    width: 100%;
    border: 0;
    background: transparent;
    color: inherit;
    text-align: left;
    padding: 0.95rem 1rem;
    display: flex;
    justify-content: space-between;
    gap: 0.8rem;
    align-items: center;
}

.round-head strong { display: block; }
.round-head span { color: var(--muted); font-size: 0.85rem; }
.round-count {
    background: #111111;
    color: #ffffff;
    border-radius: 999px;
    padding: 0.3rem 0.65rem;
    font-size: 0.8rem;
    white-space: nowrap;
}

.round-body {
    border-top: 1px solid var(--line);
    padding: 0.8rem;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr));
    gap: 0.45rem;
}

.round-item {
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0.5rem 0.6rem;
}

.round-item b {
    display: block;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.round-item span,
.round-item small {
    display: block;
}

.round-item small {
    color: var(--muted);
    font-size: 0.78rem;
    font-variant-numeric: tabular-nums;
    margin-top: 0.12rem;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(17, 17, 17, 0.45);
    display: none;
    place-items: end center;
    padding: 0.75rem;
    z-index: 50;
}

.modal.is-open {
    display: grid;
}

.modal[hidden],
.toast[hidden] {
    display: none !important;
}

.modal-card {
    width: min(32rem, 100%);
    background: var(--bg-2);
    border: 1px solid var(--line);
    border-radius: 20px 20px 16px 16px;
    padding: 1.1rem 1rem 1rem;
    box-shadow: var(--shadow);
    margin-bottom: var(--safe-bottom);
}

.modal-number {
    margin: 0 0 0.9rem;
    font-size: 2.4rem;
    letter-spacing: 0.08em;
}

.field { display: grid; gap: 0.35rem; font-weight: 600; }
.modal-error { color: var(--danger); margin: 0.6rem 0 0; }
.modal-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1rem;
}

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(1rem + var(--safe-bottom));
    transform: translateX(-50%);
    background: #111111;
    color: #ffffff;
    border: 1px solid #111111;
    border-radius: 999px;
    padding: 0.7rem 1rem;
    z-index: 60;
    max-width: calc(100% - 2rem);
}

.toast.is-error {
    background: var(--danger-bg);
    color: var(--danger);
    border-color: #e8c4bf;
}

@media (min-width: 480px) {
    .board { grid-template-columns: repeat(auto-fill, minmax(3.5rem, 1fr)); }
}

@media (min-width: 640px) {
    .toolbar {
        grid-template-columns: 1fr auto auto;
        align-items: center;
    }
    .modal { place-items: center; }
    .modal-card { border-radius: var(--radius); }
}

@media (min-width: 768px) {
    .board { grid-template-columns: repeat(auto-fill, minmax(3.7rem, 1fr)); }
    .cell { min-height: 3.6rem; }
}

@media (min-width: 1280px) {
    .board { gap: 0.42rem; }
}

/* iPad / iPad Pro portrait: all 1000 numbers on one screen, no board scroll */
@media (orientation: portrait) and (min-width: 744px) and (max-width: 1100px) {
    html, body {
        height: 100dvh;
        overflow: hidden;
        overscroll-behavior: none;
        -webkit-text-size-adjust: 100%;
    }

    .app {
        width: 100%;
        max-width: none;
        height: 100dvh;
        margin: 0;
        padding: calc(0.28rem + var(--safe-top)) 0.4rem calc(0.28rem + var(--safe-bottom));
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    .chrome {
        position: static;
        padding: 0 0 0.28rem;
        flex-shrink: 0;
    }

    .top {
        margin-bottom: 0.28rem;
        align-items: center;
    }

    .eyebrow { display: none; }

    h1 { font-size: 1.15rem; }

    .stats { padding: 0.28rem 0.65rem; }
    .stats strong { font-size: 1rem; }

    .tab { padding: 0.42rem 0.6rem; }

    #page-board.is-active {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;
        margin-top: 0.28rem;
    }

    #page-board .toolbar {
        grid-template-columns: minmax(0, 1fr) auto auto;
        align-items: center;
        gap: 0.35rem;
        margin-bottom: 0.28rem;
        flex-shrink: 0;
    }

    .search input,
    .chip,
    .btn {
        padding: 0.38rem 0.55rem;
        border-radius: 9px;
    }

    .board {
        flex: 1;
        min-height: 0;
        width: 100%;
        overflow: hidden;
        gap: 2px;
        grid-template-columns: repeat(25, minmax(0, 1fr)) !important;
        grid-template-rows: repeat(40, minmax(0, 1fr));
    }

    .cell {
        min-height: 0 !important;
        height: auto;
        width: auto;
        padding: 0;
        border-radius: 3px;
    }

    .cell-num {
        font-size: clamp(0.48rem, 1.05vw, 0.72rem);
        letter-spacing: 0;
        line-height: 1;
    }

    #page-history:not([hidden]) {
        flex: 1;
        min-height: 0;
        overflow: auto;
        margin-top: 0.28rem;
    }

    .empty-filter {
        padding: 0.4rem 0;
        margin: 0;
        flex-shrink: 0;
    }
}
