@layer game {
  body.is-playing {
    height: 100dvh;
    overflow: hidden;
    grid-template-rows: auto minmax(0, 1fr);
    background: var(--eiken-dark);
  }

  body.is-playing .topbar { min-height: 48px; padding-block: 2px; }
  body.is-playing .main { min-height: 0; }

  .game-table {
    --card-w: 64px;
    --trick-w: 62px;
    --seat-col: 88px;

    position: relative;
    height: 100%;
    display: grid;
    grid-template-columns: var(--seat-col) minmax(0, 1fr) var(--seat-col);
    grid-template-rows: auto auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "bar bar bar"
      "top top top"
      "left center right"
      "status status status"
      "hand hand hand";
    gap: 6px 4px;
    padding: 10px 10px calc(6px + env(safe-area-inset-bottom));
    overflow: hidden;
    color: var(--felt-text);
    background:
      var(--felt-texture),
      radial-gradient(ellipse 85% 60% at 50% 45%, var(--laken-light) 0%, var(--laken) 55%, var(--laken-deep) 100%);
    box-shadow:
      inset 0 0 0 5px var(--eiken),
      inset 0 0 0 6px rgb(201 162 74 / 0.45),
      inset 0 0 70px rgb(0 0 0 / 0.45);
  }

  .game-table :focus-visible { outline-color: var(--felt-focus); }

  .table-bar {
    grid-area: bar;
    display: flex;
    flex-wrap: wrap;
    align-items: stretch;
    border: 1px solid rgb(201 162 74 / 0.5);
    border-radius: 16px;
    background: rgb(18 61 46 / 0.94);
    box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
    overflow: hidden;
  }

  .table-bar__cell {
    flex: 1 1 0;
    min-width: 0;
    display: grid;
    align-content: center;
    gap: 2px;
    padding: 8px 10px;
  }

  .table-bar__cell--contract { flex-grow: 1.3; }
  .table-bar__cell--rule { flex-grow: 2; }

  /* Trump and called card are short and must not be squeezed; the contract cell wraps instead. */
  .table-bar__cell--trump,
  .table-bar__cell--called { flex: 0 1 auto; }

  .table-bar__cell + .table-bar__cell { border-left: 1px solid rgb(201 162 74 / 0.35); }

  .table-bar__label {
    color: var(--felt-muted);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    line-height: 1.25;
    text-transform: uppercase;
  }

  .table-bar__value {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    color: var(--schuim);
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.2;
  }

  .table-bar__value--wrap { display: block; overflow-wrap: anywhere; }

  .table-bar__suit {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 24px;
    border-radius: 6px;
    background: var(--card-face);
    font-size: 1.05rem;
    box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.2);
  }

  .table-bar__by { color: var(--felt-muted); font-weight: 400; }

  .table-bar__value--small { font-size: 0.92rem; line-height: 1.25; }

  .table-bar__rules {
    flex: none;
    display: grid;
    place-items: center;
    align-content: center;
    gap: 1px;
    min-width: 48px;
    min-height: 44px;
    padding: 6px 4px;
    border-left: 1px solid rgb(201 162 74 / 0.35);
    color: var(--messing-light);
    text-decoration: none;
  }

  .table-bar__rules:hover { background: rgb(242 210 122 / 0.12); }
  .table-bar__rules:focus-visible { outline-offset: -4px; }

  .table-bar__rules-icon { font-family: var(--font-display); font-size: 1.15rem; line-height: 1; }

  .table-bar__rules-text {
    color: var(--felt-text);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .table-bar__secret {
    flex-basis: 100%;
    padding: 6px 12px;
    background: var(--messing);
    color: var(--inkt);
    font-size: 0.9rem;
    font-weight: 700;
    text-align: center;
  }

  .seat {
    position: relative;
    display: grid;
    justify-items: center;
    align-content: center;
    gap: 3px;
    min-width: 0;
    text-align: center;
  }

  .seat--top { grid-area: top; }
  .seat--left { grid-area: left; align-self: center; }
  .seat--right { grid-area: right; align-self: center; }

  .seat--top {
    grid-template-columns: auto auto auto;
    grid-template-areas:
      "revealed revealed revealed"
      "avatar name bubble"
      "avatar facts bubble"
      "avatar counts bubble";
    justify-content: center;
    justify-items: start;
    column-gap: 10px;
    text-align: left;
  }

  .seat--top .revealed { grid-area: revealed; justify-self: center; }
  .seat--top .seat__avatar { grid-area: avatar; align-self: center; }
  .seat--top .seat__name { grid-area: name; }
  .seat--top .seat__facts { grid-area: facts; justify-content: flex-start; }
  .seat--top .seat__counts { grid-area: counts; }
  .seat--top .seat__bubble { grid-area: bubble; position: static; align-self: center; translate: none; }


  .seat__avatar {
    position: relative;
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, var(--eiken-light), var(--eiken) 65%, var(--eiken-dark));
    box-shadow: 0 0 0 2px var(--creme), 0 4px 10px rgb(0 0 0 / 0.35);
    color: var(--schuim);
  }

  .seat__avatar--bot {
    background: radial-gradient(circle at 35% 30%, #2f5c4b, var(--laken-deep) 70%);
    box-shadow: 0 0 0 2px var(--creme-muted), 0 4px 10px rgb(0 0 0 / 0.35);
  }

  .seat__initials {
    font-family: var(--font-display);
    font-size: 1.35rem;
    font-weight: 600;
  }

  .seat.team-bidder .seat__avatar {
    background: radial-gradient(circle at 35% 30%, var(--messing-light), var(--messing) 65%, #9c7a2c);
    color: var(--inkt);
  }

  .seat.is-turn .seat__avatar {
    box-shadow: 0 0 0 3px var(--laken-deep), 0 0 0 6px var(--messing-light), 0 4px 14px rgb(0 0 0 / 0.4);
  }

  .seat.is-turn .seat__avatar::after {
    content: "";
    position: absolute;
    inset: -6px;
    border-radius: 50%;
    border: 3px solid var(--messing-light);
    animation: turn-pulse 1.6s ease-out infinite;
  }

  .seat.is-away .seat__avatar { opacity: 0.6; filter: grayscale(0.6); }

  @keyframes turn-pulse {
    from { opacity: 0.9; scale: 1; }
    to { opacity: 0; scale: 1.35; }
  }

  .seat__name {
    max-width: 100%;
    overflow: hidden;
    color: var(--schuim);
    font-weight: 700;
    line-height: 1.2;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .seat__facts {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 3px;
  }

  .role {
    display: inline-flex;
    align-items: center;
    min-height: 20px;
    padding: 1px 7px;
    border-radius: 999px;
    background: var(--felt-panel);
    color: var(--felt-text);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    line-height: 1.2;
    text-align: center;
  }

  .role--bidder,
  .role--partner { background: var(--messing); color: var(--inkt); }
  .role--dealer { box-shadow: inset 0 0 0 1px rgb(244 234 213 / 0.5); }
  .role--away { background: #5b1a22; color: var(--schuim); }
  .role--blind { background: var(--eiken); color: var(--schuim); }

  .seat__counts {
    display: flex;
    gap: 8px;
    color: var(--felt-text);
    font-size: 0.85rem;
    font-weight: 700;
  }

  .seat__count { display: inline-flex; align-items: center; gap: 4px; }

  .seat__count-icon {
    display: inline-block;
    width: 10px;
    height: 14px;
    border-radius: 2px;
  }

  .seat__count-icon--cards {
    background-color: #fff;
    background-image:
      linear-gradient(90deg, rgb(200 16 46 / 0.7) 50%, transparent 50%),
      linear-gradient(rgb(200 16 46 / 0.7) 50%, transparent 50%);
    background-size: 4px 4px;
    box-shadow: 0 0 0 1px rgb(0 0 0 / 0.3);
  }

  .seat__count-icon--tricks {
    background: var(--creme);
    box-shadow: 2px -2px 0 -0.5px var(--creme-muted), 2px -2px 0 0.5px rgb(0 0 0 / 0.3), 0 0 0 1px rgb(0 0 0 / 0.3);
  }

  .seat__bubble {
    position: absolute;
    top: -4px;
    left: calc(50% + 16px);
    z-index: 2;
    padding: 3px 10px;
    border-radius: 12px 12px 12px 4px;
    background: var(--schuim);
    color: var(--inkt);
    font-size: 0.85rem;
    font-weight: 700;
    white-space: nowrap;
    box-shadow: 0 3px 8px rgb(0 0 0 / 0.3);
  }

  .seat__bubble--pass { background: var(--creme-muted); }

  .seat--right .seat__bubble {
    left: auto;
    right: calc(50% + 16px);
    border-radius: 12px 12px 4px 12px;
  }

  .revealed {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 3px;
    max-width: 100%;
    margin-bottom: 4px;
  }

  .seat--left .revealed,
  .seat--right .revealed { flex-direction: column; align-items: stretch; }

  .revealed__suit {
    display: flex;
    align-items: baseline;
    gap: 3px;
    padding: 2px 6px;
    border-radius: 6px;
    background: var(--card-face);
    box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
    color: var(--suit-ink);
    font-size: 0.85rem;
    font-weight: 800;
    line-height: 1.25;
    white-space: nowrap;
  }

  .revealed__symbol { margin-right: 1px; }

  .game-table__center {
    grid-area: center;
    position: relative;
    display: grid;
    grid-template-rows: minmax(0, 1fr);
    grid-template-columns: minmax(0, 1fr);
    place-items: center;
    min-height: 0;
  }

  .trick-area {
    position: relative;
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    min-height: calc(var(--trick-w) * 1.4375 * 2.2);
  }

  .felt-stamp {
    display: grid;
    justify-items: center;
    gap: 4px;
    padding: 14px 22px;
    border: 2px solid rgb(242 210 122 / 0.55);
    border-radius: 14px;
    background: rgb(18 61 46 / 0.7);
    text-align: center;
  }

  .felt-stamp__contract {
    color: var(--messing-light);
    font-family: var(--font-display);
    font-size: 1.8rem;
    font-weight: 600;
  }

  .felt-stamp__bidder { color: var(--felt-text); font-weight: 700; }

  .trick {
    --card-w: var(--trick-w);
    position: absolute;
    top: 50%;
    left: 50%;
    width: calc(var(--trick-w) * 2.7);
    height: calc(var(--trick-w) * 1.4375 * 2.15);
    translate: -50% -50%;
  }

  .trick__card {
    position: absolute;
    rotate: var(--tilt, 0deg);
    --from-x: 0;
    --from-y: 0;
  }

  .trick__card--top { top: 0; left: calc(50% - var(--trick-w) / 2); --from-y: -160%; }
  .trick__card--bottom { bottom: 0; left: calc(50% - var(--trick-w) / 2); --from-y: 160%; }
  .trick__card--left { left: 0; top: calc(50% - var(--trick-w) * 0.72); --from-x: -170%; }
  .trick__card--right { right: 0; top: calc(50% - var(--trick-w) * 0.72); --from-x: 170%; }

  .trick__card.is-arriving { animation: card-arrive 220ms var(--ease-overshoot) var(--delay, 0ms) both; }

  @keyframes card-arrive {
    from { translate: var(--from-x) var(--from-y); opacity: 0; rotate: 0deg; }
  }

  .trick--settled .trick__card.is-winner .card {
    box-shadow: 0 0 0 3px var(--messing-light), 0 8px 18px rgb(0 0 0 / 0.35);
  }

  .trick--settled.is-sweeping .trick__card {
    transition: translate 350ms ease-in, scale 350ms ease-in, opacity 350ms ease-in;
    scale: 0.5;
    opacity: 0;
  }

  .trick--settled.is-sweeping.to-left .trick__card { translate: -180% 0; }
  .trick--settled.is-sweeping.to-right .trick__card { translate: 180% 0; }
  .trick--settled.is-sweeping.to-top .trick__card { translate: 0 -180%; }
  .trick--settled.is-sweeping.to-bottom .trick__card { translate: 0 180%; }

  .table-status {
    grid-area: status;
    display: grid;
    justify-items: center;
    gap: 6px;
  }

  .table-status__meta {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 6px;
  }

  .chip {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    padding: 3px 12px;
    border-radius: 999px;
    background: var(--felt-panel);
    color: var(--felt-text);
    font-size: 0.82rem;
    font-weight: 700;
    white-space: nowrap;
  }

  .chip--button {
    min-height: 32px;
    border: 1px solid rgb(201 162 74 / 0.6);
    cursor: pointer;
    list-style: none;
  }

  .chip--button::-webkit-details-marker { display: none; }
  .chip--button::before { content: "↺"; margin-right: 6px; color: var(--messing-light); }
  details[open] > .chip--button { background: var(--messing); color: var(--inkt); }
  details[open] > .chip--button::before { color: var(--inkt); }

  .last-trick { position: static; }

  .last-trick__panel {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    z-index: 20;
    translate: -50% 0;
    width: max-content;
    max-width: calc(100vw - 24px);
    padding: 12px 14px;
    border: 1px solid rgb(201 162 74 / 0.6);
    border-radius: 14px;
    background: var(--laken-night);
    box-shadow: var(--shadow-2);
  }

  .last-trick__title { margin-bottom: 8px; color: var(--felt-text); font-size: 0.9rem; font-weight: 700; }

  .last-trick__cards { display: flex; gap: 10px; }

  .last-trick__cards li { display: grid; justify-items: center; gap: 4px; }
  .last-trick__cards li.is-winner .card { box-shadow: 0 0 0 3px var(--messing-light); }
  .last-trick__who { color: var(--felt-muted); font-size: 0.78rem; font-weight: 700; }

  .chip--clock {
    border: 1px solid var(--messing-light);
    background: var(--laken-night);
    color: var(--messing-light);
  }

  .chip--clock::before { content: "◷"; margin-right: 6px; }

  .chip--live { gap: 7px; background: var(--laken-night); }

  .chip__dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: #6fdc9a;
    box-shadow: 0 0 0 3px rgb(111 220 154 / 0.25);
  }

  .recap__panel {
    width: min(360px, calc(100vw - 24px));
    max-height: min(52vh, 360px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .recap__list { display: grid; gap: 2px; }

  .recap__item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 8px;
    padding: 4px 6px;
    border-radius: 8px;
    color: var(--felt-text);
    font-size: 0.9rem;
    line-height: 1.3;
  }

  .recap__item strong { color: var(--schuim); }
  .recap__item.is-auto { background: rgb(201 162 74 / 0.12); }

  .recap__auto {
    flex-basis: 100%;
    color: var(--messing-light);
    font-size: 0.78rem;
    font-weight: 700;
  }

  .turn-line { display: grid; justify-items: center; gap: 2px; text-align: center; }

  .turn-line__headline {
    color: var(--schuim);
    font-family: var(--font-display);
    font-size: 1.2rem;
    font-weight: 600;
    line-height: 1.2;
  }

  .turn-line--mine .turn-line__headline {
    padding: 2px 14px 3px;
    border-radius: 999px;
    background: var(--messing);
    color: var(--inkt);
  }

  .turn-line__hint {
    min-height: 1.4em;
    color: var(--felt-text);
    font-size: 0.95rem;
    line-height: 1.4;
  }

  .turn-line__hint.is-confirming { color: var(--messing-light); font-weight: 700; }
}

@media (max-width: 419.98px) {
  @layer game {
    /* With the rules link the bar is too narrow for "Klaveren"; the suit symbol says it, the name stays for screen readers. */
    .table-bar__suit + .table-bar__trump-name {
      position: absolute;
      width: 1px;
      height: 1px;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
    }
  }
}

@media (max-width: 719.98px) {
  @layer game {
    /* On a phone an open sheet would hide the side seats, so all three move into one row above it. */
    .game-table:has(.sheet--active) {
      grid-template-areas:
        "bar bar bar"
        "left top right"
        "center center center"
        "status status status"
        "hand hand hand";
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .game-table:has(.sheet--active) .seat--top {
      grid-template-columns: auto;
      grid-template-areas: "revealed" "avatar" "name" "facts" "counts";
      justify-items: center;
      text-align: center;
    }

    .game-table:has(.sheet--active) .seat--top .seat__facts { justify-content: center; }
    .game-table:has(.sheet--active) .seat__avatar { width: 44px; height: 44px; }
    .game-table:has(.sheet--active) .seat__bubble { display: none; }
  }
}

@media (min-width: 720px) {
  @layer game {
    .game-table {
      --card-w: 80px;
      --trick-w: 74px;
      --seat-col: 150px;
      max-width: 1180px;
      margin-inline: auto;
      padding: 14px 18px 10px;
      border-radius: 0 0 28px 28px;
    }

    .game-table { grid-template-columns: minmax(var(--seat-col), 1fr) minmax(0, 560px) minmax(var(--seat-col), 1fr); }
    .seat--left { justify-self: end; padding-right: 24px; }
    .seat--right { justify-self: start; padding-left: 24px; }
    .table-bar { justify-self: center; width: min(100%, 720px); }
    .phase-finished .seat--top { display: none; }
    .seat__avatar { width: 64px; height: 64px; }
    .seat__initials { font-size: 1.6rem; }
    .role { font-size: 0.78rem; }
    .turn-line__headline { font-size: 1.4rem; }
  }
}

@media (min-width: 720px) and (max-height: 760px) {
  @layer game {
    .game-table { --card-w: 70px; --trick-w: 62px; }
  }
}

@media (prefers-reduced-motion: reduce) {
  .seat.is-turn .seat__avatar::after { animation: none; opacity: 0.8; }
  .trick__card.is-arriving { animation: none; }
  .trick--settled.is-sweeping .trick__card { transition: none; }
}
