:root {
    --yellow: #f4c116;
    --yellow-dark: #c89400;
    --red: #c41e1e;
    --red-dark: #8d1414;
    --blue: #1f4ea8;
    --ink: #1c1c1c;
    --paper: #fffaf0;
    --paper-2: #fff3cf;
    --shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
    --radius: 14px;
    --gap: 12px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--yellow);
    color: var(--ink);
    font-family: 'Segoe UI', system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
    overscroll-behavior: none;
}

body {
    background:
        radial-gradient(circle at 0% 0%, #ffe07a 0, transparent 40%),
        radial-gradient(circle at 100% 100%, #ffd45a 0, transparent 40%),
        var(--yellow);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

button {
    font: inherit;
    cursor: pointer;
}

input[type="text"], input[type="file"] {
    font: inherit;
}

a {
    color: inherit;
}

.btn {
    display: inline-block;
    border: 2px solid var(--ink);
    border-radius: 999px;
    padding: 10px 18px;
    background: var(--paper);
    color: var(--ink);
    font-weight: 700;
    text-decoration: none;
    box-shadow: var(--shadow);
    transition: transform .05s ease;
    text-align: center;
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.18); }
.btn-primary { background: var(--red); color: #fff; border-color: var(--red-dark); }
.btn-secondary { background: var(--blue); color: #fff; border-color: #163877; }
.btn-danger { background: var(--red); color: #fff; border-color: var(--red-dark); }
.btn-ghost { background: transparent; box-shadow: none; border-color: transparent; }
.btn-tiny { padding: 4px 10px; font-size: 0.85rem; }

.muted { color: rgba(0,0,0,0.6); }
.error { color: var(--red-dark); font-weight: 700; }

.topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    padding: 10px 20px;
    background: var(--ink);
    color: var(--yellow);
    gap: 12px;
}
.topbar h1 { font-size: 1.1rem; margin: 0; text-align: center; }
.topbar .btn-ghost { color: var(--yellow); }

.topbar-game {
    background: var(--ink);
    color: var(--yellow);
    padding: 8px 16px;
}
.topbar-game .topbar-info { display: flex; align-items: center; gap: 8px; font-size: 0.95rem; flex-wrap: wrap; }
.topbar-game .game-code { letter-spacing: 0.2em; font-size: 1.1rem; }
.topbar-title { font-size: 1rem; margin: 0; text-align: center; }
.topbar-actions { text-align: right; }

@media (max-width: 640px) {
    .topbar-game {
        grid-template-columns: 1fr auto;
        grid-template-areas:
            "title title"
            "info  action";
        row-gap: 6px;
        padding: 8px 12px;
    }
    .topbar-game .topbar-title { grid-area: title; font-size: 0.95rem; }
    .topbar-game .topbar-info { grid-area: info; font-size: 0.85rem; gap: 6px; }
    .topbar-game .topbar-actions { grid-area: action; }
    .topbar-game .game-code { font-size: 1rem; }
    .topbar-game .game-code-label { display: none; }
}

/* HOME */
.home-wrap {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    gap: 18px;
    text-align: center;
}
.logo {
    font-size: clamp(2rem, 7vh, 3.5rem);
    margin: 0;
    color: var(--ink);
    text-shadow: 3px 3px 0 var(--yellow-dark);
    letter-spacing: 1px;
}
.home-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 22px;
    max-width: 1500px;
    width: 100%;
}
.home-card {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 12px;
    text-align: center;
    text-decoration: none;
}
.home-card h2 { margin: 0; font-size: 1.3rem; }
.home-card p { margin: 0; font-size: 1rem; color: rgba(0,0,0,0.7); }

@media (min-width: 1024px) {
    .home-card { padding: 32px; gap: 16px; }
    .home-card h2 { font-size: 1.6rem; }
    .home-card p { font-size: 1.05rem; }
    .home-card .btn { padding: 12px 22px; font-size: 1.05rem; }
}
@media (min-width: 1500px) {
    .home-card { padding: 40px; gap: 18px; }
    .home-card h2 { font-size: 1.8rem; }
    .home-card p { font-size: 1.15rem; }
}
.home-card input {
    padding: 10px 12px;
    border-radius: 8px;
    border: 2px solid var(--ink);
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 0.25em;
    font-size: 1.1rem;
    background: var(--paper-2);
}
.home-card-link { color: var(--ink); }

/* SETS */
.sets-wrap {
    flex: 1;
    display: grid;
    grid-template-columns: clamp(260px, 22vw, 360px) 1fr;
    gap: var(--gap);
    padding: var(--gap);
    overflow: hidden;
}
.sets-list {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 12px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}
.sets-list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.sets-list-header h2 { margin: 0; font-size: 1.1rem; }
.sets-ul { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.set-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px;
    border: 2px solid var(--ink);
    border-radius: 10px;
    background: var(--paper-2);
}
.set-item.active { background: var(--yellow); }
.set-item-name { flex: 1; font-weight: 700; }
.set-item-count { font-size: 0.8rem; color: rgba(0,0,0,0.6); }
.set-item button { background: transparent; border: none; padding: 4px; }

.set-detail {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.empty-state { color: rgba(0,0,0,0.5); padding: 20px; text-align: center; }
.set-detail-header {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}
.set-detail-header h2 { margin: 0; flex: 1; min-width: 0; }

.char-form {
    display: grid;
    grid-template-columns: 1fr 1fr auto;
    gap: 8px;
    align-items: center;
    padding: 10px;
    background: var(--paper-2);
    border: 2px dashed var(--ink);
    border-radius: 10px;
}
.char-form input[type="text"] {
    padding: 8px;
    border: 2px solid var(--ink);
    border-radius: 8px;
}

.chars-grid {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
}
@media (min-width: 1400px) {
    .chars-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
}
.char-card {
    background: var(--paper-2);
    border: 2px solid var(--ink);
    border-radius: 10px;
    padding: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    text-align: center;
}
.char-photo {
    width: 100%;
    aspect-ratio: 1 / 1;
    background: #ddd center / cover no-repeat;
    border: 2px solid var(--ink);
    border-radius: 8px;
}
.char-photo.placeholder::before {
    content: '?';
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    font-size: 2rem;
    font-weight: 700;
    color: rgba(0,0,0,0.3);
}
.char-name { font-weight: 700; font-size: 0.9rem; word-break: break-word; }
.char-actions { display: flex; gap: 4px; }

/* GAME SCREENS */
.screen {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    overflow: auto;
}
.screen-card {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 22px 26px;
    max-width: 1200px;
    width: 100%;
    text-align: center;
    box-shadow: var(--shadow);
}
.screen-card.screen-card-narrow {
    max-width: 720px;
}
.big-code {
    font-size: clamp(2rem, 8vh, 3.5rem);
    letter-spacing: 0.4em;
    font-weight: 800;
    margin: 12px 0;
    color: var(--red-dark);
}
.opponent-status {
    margin-top: 14px;
    padding: 10px;
    border-radius: 8px;
    background: var(--paper-2);
    font-size: 0.95rem;
}

.sets-picker, .secret-picker {
    list-style: none;
    margin: 12px 0 0;
    padding: 0;
    display: grid;
    gap: 10px;
}
.sets-picker { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.secret-picker { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
@media (min-width: 900px) {
    .secret-picker { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
}
.set-pick {
    border: 2px solid var(--ink);
    border-radius: 10px;
    padding: 12px;
    background: var(--paper-2);
    cursor: pointer;
    font-weight: 700;
}
.set-pick.disabled { opacity: 0.5; cursor: not-allowed; }
.set-pick.selected { background: var(--yellow); }

.secret-pick {
    border: 3px solid var(--ink);
    border-radius: 12px;
    padding: 10px;
    background: var(--paper-2);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}
.secret-pick.selected { background: var(--yellow); }
.secret-pick .char-photo { width: 100%; }
.secret-pick .char-name { font-size: 1rem; }

/* BOARD */
.screen-board {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 8px 12px 44px;
    overflow: hidden;
    position: relative;
}
.board {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 6px;
    padding: 6px;
    background: var(--ink);
    border-radius: var(--radius);
    min-height: 0;
}
@media (max-width: 720px) and (orientation: portrait) {
    .board {
        grid-template-columns: repeat(4, 1fr);
        grid-template-rows: repeat(6, 1fr);
    }
}
@media (max-width: 480px) and (orientation: portrait) {
    .board {
        grid-template-columns: repeat(3, 1fr);
        grid-template-rows: repeat(8, 1fr);
    }
}
.board-cell {
    background: var(--paper-2);
    border: 2px solid var(--yellow-dark);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4px;
    user-select: none;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: transform .08s ease;
    min-height: 0;
}
.board-cell:active { transform: scale(0.97); }
.board-cell.empty { background: rgba(255,255,255,0.05); border-style: dashed; cursor: default; }
.board-cell .cell-photo {
    flex: 1;
    width: 100%;
    background: center / cover no-repeat #ddd;
    border-radius: 6px;
    min-height: 0;
}
.board-cell .cell-photo.placeholder::before {
    content: '?';
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    font-size: 2rem;
    font-weight: 700;
    color: rgba(0,0,0,0.3);
}
.board-cell .cell-name {
    font-size: clamp(0.65rem, 1.4vmin, 1rem);
    font-weight: 700;
    margin-top: 2px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}
.board-cell.eliminated::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.55);
    border-radius: 6px;
}
.board-cell.eliminated::before {
    content: '✕';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: clamp(1.6rem, 6vh, 3rem);
    font-weight: 800;
    z-index: 1;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

/* SECRET CARD */
.secret-card {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translate(-50%, calc(100% - 28px));
    width: min(280px, 60vw);
    background: var(--paper);
    border: 3px solid var(--ink);
    border-bottom: none;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 16px rgba(0,0,0,0.25);
    transition: transform .35s ease;
    cursor: pointer;
    z-index: 10;
}
.secret-card.open {
    transform: translate(-50%, 0);
    width: min(360px, 80vw);
}
.secret-card-pull {
    background: var(--ink);
    color: var(--yellow);
    text-align: center;
    padding: 4px;
    font-size: 0.85rem;
    font-weight: 700;
    border-radius: 10px 10px 0 0;
}
.secret-card-content {
    padding: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}
.secret-photo {
    width: 100%;
    aspect-ratio: 1 / 1;
    background: center / cover no-repeat #ddd;
    border: 2px solid var(--ink);
    border-radius: 10px;
    max-height: 50vh;
}
.secret-photo.placeholder::before {
    content: '?';
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    font-size: 3rem;
    font-weight: 700;
    color: rgba(0,0,0,0.3);
}
.secret-name { font-size: 1.2rem; font-weight: 800; text-align: center; }

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
}
.overlay-content {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 20px;
    max-width: 400px;
    width: 100%;
    text-align: center;
    box-shadow: var(--shadow);
}

/* MODAL */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 16px;
    animation: modal-fade .15s ease-out;
}
.modal {
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    padding: 22px;
    max-width: 440px;
    width: 100%;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 14px;
    animation: modal-pop .2s ease-out;
}
.modal h3 { margin: 0; font-size: 1.2rem; }
.modal .modal-message { margin: 0; font-size: 1rem; line-height: 1.4; }
.modal label { display: flex; flex-direction: column; gap: 6px; font-weight: 700; font-size: 0.9rem; }
.modal input {
    padding: 10px 12px;
    border: 2px solid var(--ink);
    border-radius: 8px;
    background: var(--paper-2);
    font-size: 1rem;
}
.modal input:focus { outline: 3px solid var(--yellow); outline-offset: 1px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.modal-actions .btn { padding: 8px 16px; }
.modal-actions .btn-ghost { box-shadow: none; }

@keyframes modal-fade {
    from { opacity: 0; }
    to { opacity: 1; }
}
@keyframes modal-pop {
    from { opacity: 0; transform: translateY(8px) scale(0.96); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 820px) {
    .sets-wrap {
        grid-template-columns: 1fr;
        overflow: visible;
    }
    .sets-list { max-height: 220px; }
    .char-form { grid-template-columns: 1fr 1fr; }
    .char-form .btn { grid-column: 1 / -1; }
}
