/* TopChess — Durak prieš botą: „viewport-fit" stalas (tik durak.html).
   Principai:
   - Partijos metu puslapis NESLENKA: body = 100dvh, žaidimo laukas užpildo likusią vietą.
   - Kortų dydis skaičiuojamas iš REALIOS žaidimo lauko vietos (container query vienetai
     cqw/cqh), o ne iš viewport'o spėjimų → tilpimas visose rezoliucijose be breakpoint'ų šokinėjimo.
   - Svarbiausia info (koziris, būsena, mygtukai, ranka) gauna vietą pirma; stalas — lanksti eilutė,
     kuri sumažėja, kai trūksta vietos.
   - Trys išdėstymai: siauras (telefonas portretas), platus (šoninė juosta), žemas (telefonas gulsčias). */

@property --cw  { syntax: "<length>"; inherits: true; initial-value: 80px; }
@property --ch  { syntax: "<length>"; inherits: true; initial-value: 116px; }
@property --tcw { syntax: "<length>"; inherits: true; initial-value: 60px; }
@property --trw { syntax: "<length>"; inherits: true; initial-value: 48px; }

/* ---------- Karkasas: partijos metu jokio puslapio slinkimo ---------- */
body.durak-playing {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}
body.durak-playing .durak-container {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  max-width: 1280px;
  padding: 6px max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
}

body.durak-playing .game-area.active {
  flex: 1 1 0;
  min-height: 0;
  container-type: size;
  container-name: dk;
  display: grid;
  gap: 6px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto auto;
  grid-template-areas: "info" "status" "opps" "hint" "table" "actions" "hand";
  --chrome: 322px;
}
body.durak-playing .game-area.active > .info-bar { grid-area: info; }
body.durak-playing .game-area.active > .status-msg { grid-area: status; }
body.durak-playing .game-area.active > .opponents-area { grid-area: opps; }
body.durak-playing .game-area.active > .defense-hint { grid-area: hint; }
body.durak-playing .game-area.active > .table-area { grid-area: table; }
body.durak-playing .game-area.active > .action-buttons { grid-area: actions; }
body.durak-playing .game-area.active > .player-hand { grid-area: hand; }

/* Kortų dydis: vienas šaltinis visiems vaikams (cq vienetai skaičiuojami nuo .game-area).
   Ribos: iš aukščio (ranka + stalas turi tilpti) ir iš pločio (ranka turi tilpti su persidengimu). */
body.durak-playing .game-area.active > * {
  --ch: clamp(76px, min(calc((100cqh - var(--chrome)) / 2), calc(100cqw / 2.5)), 210px);
  --cw: calc(var(--ch) * 0.691);
  --durak-card-width: var(--cw);
  --durak-card-height: var(--ch);
}

/* Partijos metu antraštė ir raginimai nereikalingi — vieta skiriama stalui */
body.durak-playing .page-title-row,
body.durak-playing .login-hint { display: none !important; }

/* ---------- Informacijos juosta ---------- */
body.durak-playing .info-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  flex-wrap: nowrap;
  margin: 0;
  box-shadow: 0 3px 0 var(--tc-line);
}
body.durak-playing .info-bar .info-badge {
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  justify-content: center;
  flex: 1 1 0;
  min-width: 0;
  padding: 3px 2px;
  border-radius: 10px;
  background: var(--tc-surface-2);
  border: 0;
  font-size: 1.1rem;
  font-weight: 900;
  line-height: 1.1;
}
body.durak-playing .info-bar .info-badge > span:first-child {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.62rem;
  font-weight: 700;
  color: var(--tc-ink-soft);
}
body.durak-playing .info-bar .variant-badge { display: none !important; }
body.durak-playing .info-bar .variant-badge[style*="display: none"],
body.durak-playing .info-bar .variant-badge[style*="display:none"],
body.durak-playing .info-bar .variant-badge:empty { display: none !important; }

/* Koziris: kortos nuotrauka + didelis rūšies simbolis — matosi iš pirmo žvilgsnio */
body.durak-playing .trump-info {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas: "label label" "card suit";
  align-items: center;
  justify-items: center;
  column-gap: 6px;
  row-gap: 1px;
  flex-shrink: 0;
}
body.durak-playing .trump-info > span:first-child {
  grid-area: label;
  font-size: 0.62rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--tc-ink-soft);
}
body.durak-playing .trump-card-mini {
  grid-area: card;
  --trw: 60px;
  width: var(--trw);
  height: calc(var(--trw) / 0.691);
  border-radius: calc(var(--trw) * 0.07);
  border: 1px solid var(--tc-ink);
  box-shadow: 0 3px 0 var(--tc-line);
  background: #fff;
}
body.durak-playing .trump-suit {
  grid-area: suit;
  font-size: 2.1rem;
  line-height: 1;
  font-weight: 900;
  color: var(--tc-ink);
}
body.durak-playing .trump-suit.is-red { color: #D6262C; }

body.durak-playing .durak-tools { margin: 0; flex-shrink: 0; gap: 4px; }
body.durak-playing .info-bar .btn-settings {
  flex: 0 0 auto;
  width: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 0;            /* tik ikona; tekstas tik šoninėje juostoje */
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
body.durak-playing .info-bar .btn-settings.durak-custom-settings::before { margin: 0; }
body.durak-playing .durak-sound,
body.durak-playing .durak-home { width: 44px; height: 44px; }

/* ---------- Būsena ---------- */
body.durak-playing .status-msg {
  min-height: 2.6em;
  padding: 2px 6px;
  font-size: 1rem;
  line-height: 1.25;
  text-align: center;
}
body.durak-playing .defense-hint {
  display: block;            /* rezervuota vieta — kad stalas „nešokinėtų" */
  min-height: 1.35em;
  padding: 0 4px;
  font-size: 0.85rem;
  line-height: 1.35;
}
body.durak-playing .defense-hint:empty { display: block; }

/* ---------- Priešininkai ---------- */
body.durak-playing .opponents-area {
  gap: 6px;
  margin: 0;
  min-height: 40px;
  align-items: stretch;
}
body.durak-playing .opponent-section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  align-content: center;
  column-gap: 8px;
  row-gap: 0;
  padding: 4px;
  border-radius: 14px;
  min-width: 0;
  flex: 1 1 0;
}
body.durak-playing .opponent-name { font-size: 0.82rem; margin: 0; gap: 4px; flex-wrap: nowrap; white-space: nowrap; }
body.durak-playing .opponent-cards-count { font-size: 0.74rem; margin: 0; display: block; }
body.durak-playing .opponent-hand { display: none; }
body.durak-playing .player-label { font-size: 0.66rem; padding: 1px 6px; margin: 0; }

/* ---------- Stalas ---------- */
body.durak-playing .table-area {
  container-type: size;
  container-name: dkt;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  align-items: center;
  gap: 8px 10px;
  min-height: 0;
  padding: 10px;
  border-radius: 22px;
  --per: 3;
  --rows: 1;
}
body.durak-playing .table-area[data-pairs="0"] {
  flex-direction: column;
  flex-wrap: nowrap;
  gap: 2px;
}
body.durak-playing .table-area[data-pairs="4"],
body.durak-playing .table-area[data-pairs="5"],
body.durak-playing .table-area[data-pairs="6"] {
  display: flex;             /* perrašo seną grid */
  --rows: 2;
}
body.durak-playing .table-area::before { inset: 6px; border-radius: 17px; }
body.durak-playing .durak-empty-art { width: clamp(36px, 22cqh, 72px); height: clamp(36px, 22cqh, 72px); }
body.durak-playing .table-area > span { font-size: 0.9rem; text-align: center; }

/* Stalo kortų dydis: iš PAČIO stalo vietos (plotis ir aukštis), ne daugiau nei 90% rankos kortos */
body.durak-playing .table-pair {
  --tcw: min(
    calc(var(--cw) * 0.92),
    calc((100cqw - 22px) / (var(--per) * 1.5)),
    calc((100cqh - 26px - (var(--rows) - 1) * 8px) / (var(--rows) * 1.2) * 0.691)
  );
  --durak-card-width: var(--tcw);
  --durak-card-height: calc(var(--tcw) / 0.691);
  width: calc(var(--tcw) * 1.34);
  height: calc(var(--tcw) / 0.691 * 1.2);
  flex: 0 0 auto;
}
body.durak-playing .table-pair .attack-card { top: 0; left: 0; }
body.durak-playing .table-pair .defense-card {
  top: calc(var(--tcw) / 0.691 * 0.2);
  left: calc(var(--tcw) * 0.34);
  transform: rotate(6deg);
}
body.durak-playing .table-pair.undefended .attack-card { outline-width: 3px; }

/* ---------- Kortos ---------- */
body.durak-playing .card { border-radius: calc(var(--durak-card-width) * 0.07); }
body.durak-playing .card-face,
body.durak-playing .card-back { border-radius: calc(var(--durak-card-width) * 0.07); }

/* Ranka: visada tilpsta — persidengimas skaičiuojamas pagal kortų kiekį (--n iš JS) */
body.durak-playing .player-hand {
  container-type: inline-size;
  position: relative;
  display: flex;
  flex-wrap: nowrap;
  justify-content: safe center;
  align-items: flex-end;
  gap: 0;
  min-height: calc(var(--ch) + 22px);
  margin: 0;
  padding: 14px 8px 8px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}
body.durak-playing .player-hand::-webkit-scrollbar { display: none; }
/* Kortos mažėja tik tiek, kad kiekvienos matoma juosta liktų >= --strip (piršto paspaudimui);
   jei ir tada netelpa (labai daug kortų) — ranka slenka. */
body.durak-playing .player-hand { --strip: 30px; }
body.durak-playing .player-hand .card {
  --w: max(58px, min(var(--cw), calc(100cqw - 16px - (var(--n, 6) - 1) * var(--strip))));
  width: var(--w);
  height: calc(var(--w) / 0.691);
}
body.durak-playing .player-hand .card:not(:first-child) {
  --room: calc((100cqw - 16px - var(--w)) / (var(--n, 6) - 1) - var(--w));
  margin-left: max(calc(var(--strip) - var(--w)), min(8px, var(--room)));
}
body.durak-playing .player-hand .player-label { display: none; }   /* vaidmuo jau rašomas būsenos eilutėje */
body.durak-playing .card.playable:hover { transform: translateY(-10px); }
body.durak-playing .player-hand .card.selected { transform: translateY(-12px); }
body.durak-playing .player-hand .card.playable::after,
body.durak-playing .player-hand .card.transfer-card::after { width: 8px; height: 8px; bottom: 6px; margin-left: -4px; }

/* ---------- Veiksmo mygtukai: rezervuota eilutė, kad išdėstymas nešokinėtų ---------- */
body.durak-playing .action-buttons {
  min-height: 56px;
  padding: 0 0 4px;
  margin: 0;
  gap: 8px;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
}
body.durak-playing .action-btn {
  flex: 1 1 0;
  max-width: 220px;
  min-height: 48px;
  padding: 8px 12px;
  font-size: 1rem;
  white-space: normal;
  line-height: 1.15;
}
body.durak-playing .action-btn[style*="display: none"] { display: none; }

body.durak-playing .durak-feedback { top: 40%; }

/* ---------- PLATUS: šoninė juosta (planšetė gulsčia, kompiuteris) ---------- */
@media (min-width: 860px) {
  body.durak-playing .durak-container { padding: 8px 14px 12px; }
  body.durak-playing .game-area.active {
    --chrome: 238px;
    gap: 6px 16px;
    grid-template-columns: 200px minmax(0, 1fr);
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "info status"
      "info opps"
      "info hint"
      "info table"
      "info actions"
      "info hand";
  }
  body.durak-playing .game-area.active > * {
    --ch: clamp(84px, min(calc((100cqh - var(--chrome)) / 2), calc((100cqw - 220px) / 4.2)), 220px);
  }
  body.durak-playing .table-area { padding: 14px; }
  body.durak-playing .table-area[data-pairs="4"] { --per: 4; --rows: 1; }
  body.durak-playing .table-area[data-pairs="5"] { --per: 5; --rows: 1; }
  body.durak-playing .table-area[data-pairs="6"] { --per: 6; --rows: 1; }
  body.durak-playing .status-msg { min-height: 1.6em; font-size: clamp(1.1rem, 1.1vw + 0.6rem, 1.45rem); }
  body.durak-playing .defense-hint { font-size: 0.95rem; }
  body.durak-playing .opponent-name { font-size: 0.95rem; }
  body.durak-playing .opponent-cards-count { font-size: 0.85rem; }

  /* Šoninė juosta */
  body.durak-playing .info-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 12px;
    border-radius: 18px;
    overflow: hidden;
  }
  body.durak-playing .trump-info {
    grid-template-columns: auto auto;
    padding: 6px 0 4px;
    column-gap: 14px;
  }
  body.durak-playing .trump-info > span:first-child { font-size: 0.78rem; }
  body.durak-playing .trump-card-mini {
    --trw: clamp(60px, calc((100cqh - 330px) * 0.69), 112px);
    box-shadow: 0 4px 0 var(--tc-line);
  }
  body.durak-playing .trump-suit { font-size: clamp(2.6rem, 5cqh, 3.6rem); }
  body.durak-playing .info-bar .info-badge {
    flex: 0 0 auto;
    flex-direction: row;
    justify-content: space-between;
    padding: 8px 12px;
    font-size: 1.1rem;
  }
  body.durak-playing .info-bar .info-badge > span:first-child { font-size: 0.85rem; }
  body.durak-playing .info-bar .variant-badge:not(:empty):not([style*="display: none"]):not([style*="display:none"]) {
    display: flex !important;
    flex-direction: row;
    justify-content: center;
    font-size: 0.8rem;
    padding: 6px 10px;
  }
  body.durak-playing .info-bar .btn-settings {
    margin-top: auto;
    width: 100%;
    font-size: 0.95rem;
    gap: 8px;
  }
  body.durak-playing .durak-tools { width: 100%; justify-content: stretch; }
  body.durak-playing .durak-sound { width: 100%; }
  body.durak-playing .opponent-hand { min-height: 0; }
}

/* Aukšti ekranai: parodom priešininkų kortų nugarėles */
@media (min-width: 860px) and (min-height: 820px) {
  body.durak-playing .game-area.active { --chrome: 286px; }
  body.durak-playing .opponent-hand {
    display: flex;
    flex-basis: 100%;
    justify-content: center;
    padding: 4px 0 0;
  }
  body.durak-playing .opponent-hand .card { width: calc(var(--cw) * 0.34); height: calc(var(--ch) * 0.34); }
  body.durak-playing .opponent-hand .card:not(:first-child) { margin-left: calc(var(--cw) * -0.2); }
}

/* ---------- ŽEMAS (telefonas gulsčias, maži langai): šoninė juosta + kompaktiška centrinė dalis ---------- */
@media (max-height: 460px) and (min-width: 560px) {
  body.durak-playing .durak-container { padding: 4px 8px; }
  body.durak-playing .game-area.active {
    --chrome: 112px;
    gap: 4px 10px;
    grid-template-columns: minmax(0, 1fr) 176px;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas:
      "status info"
      "table  info"
      "actions opps"
      "hand   opps";
  }
  body.durak-playing .game-area.active > * {
    --ch: clamp(60px, min(calc((100cqh - var(--chrome)) / 2.1), calc((100cqw - 190px) / 4)), 150px);
  }
  body.durak-playing .defense-hint { display: none; }
  body.durak-playing .status-msg { min-height: 0; font-size: 0.95rem; padding: 0 4px; }
  body.durak-playing .table-area { padding: 6px; }
  body.durak-playing .table-area[data-pairs="4"] { --per: 4; --rows: 1; }
  body.durak-playing .table-area[data-pairs="5"] { --per: 5; --rows: 1; }
  body.durak-playing .table-area[data-pairs="6"] { --per: 6; --rows: 1; }
  body.durak-playing .info-bar { padding: 6px; gap: 5px; flex-direction: column; align-items: stretch; }
  body.durak-playing .trump-info { grid-template-areas: "card suit"; column-gap: 8px; }
  body.durak-playing .trump-info > span:first-child { display: none; }
  body.durak-playing .trump-card-mini { --trw: 40px; }
  body.durak-playing .trump-suit { font-size: 1.7rem; }
  body.durak-playing .info-bar .info-badge { flex-direction: row; justify-content: space-between; padding: 2px 8px; font-size: 0.9rem; }
  body.durak-playing .info-bar .info-badge > span:first-child { font-size: 0.72rem; }
  body.durak-playing .info-bar .info-badge:nth-child(3) { display: none; }
  body.durak-playing .durak-tools { width: 100%; }
  body.durak-playing .durak-tools .durak-sound,
  body.durak-playing .durak-tools .durak-home { flex: 1; width: auto; height: 44px; }
  body.durak-playing .info-bar .btn-settings { width: 100%; min-height: 44px; margin: 0; font-size: 0; }
  body.durak-playing .opponents-area { flex-direction: column; align-self: end; min-height: 0; gap: 4px; }
  body.durak-playing .opponent-section { flex: 0 0 auto; padding: 3px 8px; }
  body.durak-playing .opponent-name { font-size: 0.78rem; }
  body.durak-playing .action-buttons { min-height: 48px; }
  body.durak-playing .action-btn { min-height: 44px; padding: 4px 10px; font-size: 0.9rem; }
  body.durak-playing .player-hand { padding: 12px 6px 6px; min-height: calc(var(--ch) + 18px); }
  body.durak-playing .opponent-hand { display: none; }
}

/* ---------- Siauri telefonai: kompaktiškesnė juosta ---------- */
@media (max-width: 859px) and (min-height: 461px) and (max-height: 700px) {
  body.durak-playing .game-area.active { --chrome: 300px; gap: 4px; }
  body.durak-playing .trump-card-mini { --trw: 54px; }
  body.durak-playing .trump-suit { font-size: 1.9rem; }
  body.durak-playing .status-msg { min-height: 2.3em; font-size: 0.92rem; }
  body.durak-playing .action-buttons { min-height: 52px; }
}
@media (max-width: 380px) {
  body.durak-playing .info-bar { gap: 5px; padding: 5px; }
  body.durak-playing .trump-card-mini { --trw: 50px; }
  body.durak-playing .trump-suit { font-size: 1.6rem; }
  body.durak-playing .info-bar .info-badge { font-size: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  body.durak-playing .card { transition: none; }
}

/* html niekada neslenka partijos metu (apvalinimo 1px) */
html:has(body.durak-playing) { overflow: hidden; }
