/* ==========================================================================
   app.css – byggeklosser og sidene i Kortbord (mobil først).
   ========================================================================== */
@layer components {
    .card { background: var(--bg-panel); border: 1px solid var(--border-ui); border-radius: var(--radius-l); padding: 1.25rem; }
    .card h2, .card h3 { font-size: 1.25rem; color: var(--brand-orange); margin-bottom: 0.75rem; }

    /* --- Knapper: minst 2,75rem høye, så de er lette å treffe ------------- */
    .btn { min-height: 2.75rem; padding: 0.5rem 1.1rem; border: 2px solid var(--border-ui); border-radius: var(--radius-m);
           background: var(--bg-panel); color: var(--text-main); font-weight: 700; cursor: pointer; touch-action: manipulation; }
    .btn:hover:not(:disabled) { border-color: var(--text-main); }
    .btn-primary { background: var(--brand-orange); border-color: var(--brand-orange); color: #000; }
    .btn-primary:hover:not(:disabled) { background: var(--brand-orange-hover); border-color: var(--brand-orange-hover); }
    .btn-danger { border-color: var(--action-danger-text); color: var(--action-danger-text); }
    .btn-quiet { border-color: transparent; background: none; text-decoration: underline; }
    .btn:disabled { background: none; border-color: var(--border-color); border-style: dashed; color: var(--text-muted); cursor: not-allowed; }
    .btn-row { display: flex; flex-wrap: wrap; gap: 0.6rem; }
    .btn-col { display: flex; flex-direction: column; gap: 0.6rem; }

    /* --- Skjema ---------------------------------------------------------- */
    label { display: block; font-weight: 700; margin-bottom: 0.3rem; }
    input[type="text"], input[type="number"], select {
        width: 100%; min-height: 2.75rem; padding: 0.4rem 0.75rem; margin-bottom: 1rem;
        background: var(--input-bg); border: 2px solid var(--border-ui); border-radius: var(--radius-m); color: var(--text-strong); }
    input:focus-visible, select:focus-visible { border-color: var(--text-strong); }
    input[type="radio"], input[type="checkbox"] { width: 1.35rem; height: 1.35rem; margin: 0; flex-shrink: 0; accent-color: var(--brand-orange); }
    .choice { display: flex; align-items: center; gap: 0.6rem; min-height: 2.75rem; font-weight: 400; margin: 0; cursor: pointer; }
    .help { color: var(--text-muted); font-size: 0.9rem; margin: -0.6rem 0 1rem; }
    fieldset { border: 0; padding: 0; margin: 0 0 1rem; min-width: 0; }
    legend { padding: 0; font-weight: 700; margin-bottom: 0.25rem; }
    .indent { margin-left: 1.95rem; max-width: 12rem; }
    .error-note { padding: 0.875rem 1rem; margin-bottom: 1.5rem; border: 2px solid var(--action-danger-text); border-radius: var(--radius-m); color: var(--action-danger-text); font-weight: 700; }

    /* --- Dialoger -------------------------------------------------------- */
    dialog { width: min(100% - 2rem, 28rem); padding: 1.25rem; background: var(--bg-panel); color: var(--text-main);
             border: 2px solid var(--border-ui); border-radius: var(--radius-l); }
    dialog::backdrop { background: rgba(0, 0, 0, 0.75); }
    dialog h2 { font-size: 1.25rem; color: var(--brand-orange); margin-bottom: 0.5rem; }
    dialog p { margin-bottom: 1rem; }

    /* --- Spillekort ------------------------------------------------------
       Hvert kort er en <button> med navnet i aria-label, og SVG-en er pynt.
       Valgt kort (aria-pressed) løftes, får hvit ramme og et hakemerke:
       aldri bare farge. */
    .k-sprite { position: absolute; }
    .kort { display: block; width: 100%; height: auto; }
    .k-face { fill: var(--card-face); stroke: var(--border-ui); stroke-width: 2; }
    .k-back { fill: var(--brand-blue); stroke: var(--border-ui); stroke-width: 2; }
    .k-back-line { fill: none; stroke: var(--brand-orange); stroke-width: 1.5; }
    .k-dot { fill: var(--border-ui); }
    .k-ink { fill: var(--card-ink); }
    .k-red { fill: var(--card-red); }
    .k-hole { fill: var(--card-face); }
    .k-on-back .k-hole, .k-on-back.k-red .k-hole { fill: var(--brand-blue); }
    .k-ring { fill: none; stroke-width: 3; }
    .k-ring.k-ink { stroke: var(--card-ink); }
    .k-ring.k-red { stroke: var(--card-red); }
    .k-rank { font: 800 44px/1 system-ui, sans-serif; font-variant-numeric: tabular-nums; }
    .k-joker { font: 800 21px/1 system-ui, sans-serif; }

    .card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--card-w), 1fr)); gap: 0.75rem 0.5rem;
                 list-style: none; padding: 0.5rem 0 0; margin: 0; width: 100%; }
    .card-slot { min-width: 0; text-align: center; }
    .pcard { position: relative; display: block; width: 100%; padding: 0; border: 0; background: none; border-radius: 0.5rem;
             cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: transform 0.12s ease; }
    .pcard[aria-pressed="true"] { transform: translateY(-0.4rem); outline: 3px solid var(--text-strong); outline-offset: 2px; }
    .pcard[aria-pressed="true"]:focus-visible { outline-width: 5px; }
    .pcard[aria-pressed="true"]::after {
        content: ""; position: absolute; right: -0.35rem; bottom: -0.35rem; width: 1.5rem; height: 1.5rem; border-radius: 50%;
        border: 2px solid #000; background: var(--text-strong) center / 70% no-repeat
            url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7.5'/%3E%3C/svg%3E"); }
    .card-by { display: block; margin-top: 0.2rem; font-size: 0.8rem; line-height: 1.2; color: var(--text-muted); overflow-wrap: anywhere; }

    @media (forced-colors: active) {
        .k-face, .k-back { fill: Canvas; stroke: CanvasText; }
        .k-ink, .k-red { fill: CanvasText; }
        .k-ring { stroke: CanvasText !important; }
        .k-hole { fill: Canvas !important; }
        .pcard[aria-pressed="true"] { outline-color: Highlight; }
    }
}

@layer page {
    /* ======================================================================
       1. FORSIDEN
       ====================================================================== */
    .landing-page { display: flex; flex-direction: column; align-items: center; padding: 2rem 1rem; min-height: 100dvh; }
    .landing-header { text-align: center; margin-bottom: 2rem; }
    .landing-logo { width: 6.5rem; height: auto; margin-bottom: 0.5rem; }
    .landing-title { font-size: 2rem; }
    .landing-main { width: 100%; max-width: 30rem; display: flex; flex-direction: column; gap: 1.5rem; }
    .landing-main .card { margin: 0; }
    .code-input { font-size: 1.6rem; font-weight: 800; letter-spacing: 0.3em; text-transform: uppercase; text-align: center; }

    /* ======================================================================
       2. RAMMEN RUNDT SPILLET
       ====================================================================== */
    .mobile-app { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
    .game-header { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.1rem 1rem; padding: calc(0.5rem + env(safe-area-inset-top)) 1rem 0.5rem;
                   border-bottom: 1px solid var(--border-color); }
    .game-title { font-size: 1rem; font-weight: 700; color: var(--text-muted); }
    .game-code { color: var(--brand-orange); letter-spacing: 0.15em; font-weight: 800; }
    .conn-note { color: var(--action-danger-text); font-weight: 700; font-size: 0.9rem; }
    .conn-note:empty { display: none; }
    .last-event { flex-basis: 100%; font-size: 0.9rem; color: var(--text-main); min-height: 1.35rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    #app-content { flex: 1; width: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(6.25rem + env(safe-area-inset-bottom)); }
    .view-section { padding: 1rem 1rem 0.5rem; max-width: 62.5rem; margin: 0 auto; width: 100%; }
    .view-title { font-size: 1.6rem; margin-bottom: 0.75rem; }
    .section-heading { margin: 1.75rem 0 0.75rem; font-size: 1.15rem; }
    .no-data { text-align: center; color: var(--text-muted); font-size: 1.1rem; margin: 2rem 0; }

    /* ======================================================================
       3. VENTEROMMET
       ====================================================================== */
    .lobby-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); align-items: start; }
    .invite { text-align: center; }
    .invite-code { font-size: 3rem; font-weight: 800; letter-spacing: 0.25em; margin-right: -0.25em; color: var(--brand-orange); line-height: 1.2; }
    .invite-qr { width: min(100%, 13rem); margin: 0.75rem auto 1rem; padding: 0.6rem; background: #fff; border-radius: var(--radius-m); }
    .invite-qr svg { display: block; width: 100%; height: auto; }
    .deal-summary { margin: 0.75rem 0 1rem; padding: 0.75rem 1rem; border-left: 0.3rem solid var(--brand-orange); background: var(--input-bg); font-weight: 700; }
    .deal-form .help { margin: 0.75rem 0 0; }
    .deal-form .deal-help { margin: -0.25rem 0 0 1.95rem; }

    .player-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; width: 100%; }
    .player-list li { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.25rem 1rem; padding: 0.6rem 0.9rem;
                      background: var(--bg-panel); border: 1px solid var(--border-ui); border-radius: var(--radius-m); }
    .card .player-list li { background: var(--input-bg); }
    .player-name { font-weight: 700; color: var(--text-strong); overflow-wrap: anywhere; }
    .player-meta { color: var(--text-muted); font-size: 0.9rem; }
    .player-list .is-offline .player-name { color: var(--text-muted); }
    .player-list .btn { min-height: 2.25rem; padding: 0.2rem 0.75rem; }

    /* ======================================================================
       4. HÅNDEN OG BORDET
       ====================================================================== */
    .sort { margin-bottom: 0.5rem; border: 1px solid var(--border-ui); border-radius: var(--radius-m); background: var(--bg-panel); }
    .sort summary { min-height: 2.75rem; padding: 0.55rem 1rem; font-weight: 700; cursor: pointer; }
    .sort[open] summary { border-bottom: 1px solid var(--border-color); }
    .sort-bar { display: flex; flex-wrap: wrap; align-items: end; gap: 0.5rem 1rem; padding: 0.75rem 1rem; }
    .sort-bar label { font-size: 0.9rem; color: var(--text-muted); margin: 0; }
    .sort-bar select { display: block; width: auto; margin: 0.15rem 0 0; }
    .sort-bar .choice { color: var(--text-main); }

    /* Handlingene for valgte kort står fast like over bunnmenyen */
    .action-bar { position: sticky; bottom: 0; z-index: 2; display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem;
                  margin: 1.25rem -0.5rem 0; padding: 0.4rem 0.5rem 0.5rem; background: rgba(0, 0, 0, 0.94); border: 1px solid var(--border-ui); border-radius: var(--radius-l); }
    .sel-status { flex: 1 1 50%; padding-left: 0.4rem; font-weight: 700; font-size: 0.95rem; }
    .action-bar .btn { flex: 1 1 auto; padding-inline: 0.6rem; }
    .action-bar .btn-quiet { flex: 0 0 auto; min-height: 2.25rem; padding-block: 0; }
    .action-bar .btn-quiet:disabled { visibility: hidden; }

    .player-strip { display: flex; flex-wrap: wrap; gap: 0.4rem; list-style: none; padding: 0; margin: 0 0 0.75rem; }
    .player-strip li { padding: 0.15rem 0.7rem; border: 1px solid var(--border-ui); border-radius: 1rem; background: var(--bg-panel); font-size: 0.9rem; }
    .player-strip strong { color: var(--text-strong); font-variant-numeric: tabular-nums; }
    .player-strip .is-offline { border-style: dashed; color: var(--text-muted); }

    .piles { display: grid; gap: 0.75rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr)); margin-top: 1.5rem; }
    .pile { display: flex; align-items: center; gap: 1rem; padding: 0.9rem; background: var(--bg-panel); border: 1px solid var(--border-ui); border-radius: var(--radius-m); }
    .pile-card { width: 3rem; flex-shrink: 0; }
    .pile-card.is-empty { opacity: 0.25; }
    .pile-name { font-weight: 700; color: var(--text-strong); }
    .pile-count { color: var(--text-muted); margin-bottom: 0.5rem; }
    .pile-count:last-child { margin-bottom: 0; }

    .log { margin: 0; padding-left: 2.2rem; color: var(--text-muted); }
    .log li { padding: 0.2rem 0; }
    .log li:first-child { color: var(--text-main); font-weight: 700; }
    .round-tools { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 2rem; }

    /* ======================================================================
       5. BUNNMENYEN
       ====================================================================== */
    .bottom-nav { position: fixed; bottom: calc(1rem + env(safe-area-inset-bottom)); left: 1rem; right: 1rem; max-width: 40rem; margin: 0 auto; display: flex;
                  background: rgba(12, 30, 48, 0.92); backdrop-filter: blur(15px); -webkit-backdrop-filter: blur(15px);
                  border: 1px solid var(--border-ui); border-radius: 2.5rem; box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.5); z-index: 10; overflow: hidden; }
    .bottom-nav button { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 4.5rem; gap: 0.25rem;
                         border: 0; background: none; color: var(--text-muted); font-size: 0.9rem; font-weight: 500; cursor: pointer;
                         -webkit-tap-highlight-color: transparent; transition: color 0.2s ease, background 0.2s ease; }
    .bottom-nav button:focus-visible { outline-offset: -5px; border-radius: 2.5rem; }
    .bottom-nav .nav-icon { width: 1.6rem; height: 1.6rem; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
    .bottom-nav button[aria-current="page"] { color: var(--brand-orange); background: rgba(255, 255, 255, 0.06); font-weight: 700; }
    /* Aktiv fane markeres også med en strek, ikke bare farge */
    .bottom-nav button[aria-current="page"]::after { content: ""; position: absolute; bottom: 0.35rem; width: 1.5rem; height: 0.2rem; border-radius: 0.1rem; background: currentColor; }
    .nav-count { font-variant-numeric: tabular-nums; }

    /* ======================================================================
       6. STØRRE SKJERMER OG BORDSKJERMEN
       ====================================================================== */
    @media (min-width: 40rem) { :root { --card-w: 4.75rem; } }

    /* Bordskjermen (nettbrett midt på bordet): bordet og spillerne side om side, store kort. */
    body[data-role="table"] { --card-w: clamp(4.5rem, 17vmin, 11rem); }
    body[data-role="table"] #app-content { padding-bottom: 1rem; }
    body[data-role="table"] .card-by { font-size: 1rem; }
    @media (min-width: 56rem) {
        body[data-role="table"][data-phase="playing"] #app-content { display: grid; grid-template-columns: minmax(0, 1fr) 20rem; align-items: start; }
        body[data-role="table"] .view-section { max-width: none; }
        body[data-role="table"] #player-strip { display: none; }
    }
}
