:root {
  --tlo: #2f6b3a;
  --tlo-ciemne: #23502c;
  --papier: #fff8e6;
  --tusz: #2b2118;
  --akcent: #f2a51c;
  --cel: #ffe14d;
  --drewno: #9c6a3a;
  --karta-szer: clamp(78px, min(12vw, 15vh), 128px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--tusz);
  background: radial-gradient(circle at 50% 30%, #3c8248, var(--tlo-ciemne)) fixed;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; }

button {
  cursor: pointer;
  padding: .55em 1em;
  border: 2px solid var(--drewno);
  border-radius: 10px;
  background: var(--papier);
  color: var(--tusz);
  font-weight: 600;
  touch-action: manipulation;
}
button:disabled { opacity: .45; cursor: default; }
button.glowny { background: var(--akcent); border-color: #b87700; }
button.link { background: none; border: none; text-decoration: underline; font-weight: 400; padding: .3em; }

.rys { display: block; width: 100%; height: 100%; }

/* ---------- start i poczekalnia ---------- */
.ekran { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }

.panel {
  width: min(400px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: center;
  background: var(--papier);
  border: 6px solid var(--drewno);
  border-radius: 18px;
  padding: 24px;
  box-shadow: 0 12px 40px #0006;
}
.panel h1, .panel h2, .panel p { margin: 0; }
.panel h1 { color: var(--tlo-ciemne); }
.podtytul, .cicho { color: #776655; font-size: .9em; }
.panel label { text-align: left; font-weight: 600; }
.blad { color: #b3261e; font-weight: 600; }
.blad:empty { display: none; }
.panel input { width: 100%; padding: .55em .8em; border: 2px solid #c9b48a; border-radius: 10px; background: #fff; }
.separator { display: flex; align-items: center; gap: 10px; color: #998877; font-size: .85em; }
.separator::before, .separator::after { content: ""; flex: 1; border-top: 1px solid #cdbb98; }
#otwarte-gry { text-align: left; }
#otwarte-gry h2 { font-size: 1rem; margin-bottom: 6px; }
#lista-gier { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; }
#lista-gier li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: #f1e4c4; border-radius: 10px; }
#lista-gier li.trwa { background: #e8e2d4; }
.gra-opis { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.gra-opis b { display: block; }
.gra-opis small { color: #776655; }
#z-kodem summary { cursor: pointer; color: #776655; font-size: .9em; padding: 4px; }
#formularz-dolacz { display: flex; gap: 8px; margin-top: 6px; }
#formularz-dolacz input { text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 700; min-width: 0; }
.duzy-kod { font-size: 3rem; font-weight: 800; letter-spacing: .25em; font-family: ui-monospace, monospace; }
.adres { font-size: .9em; overflow-wrap: anywhere; }
#lista-graczy { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#lista-graczy li { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: #f1e4c4; border-radius: 10px; text-align: left; }
#lista-graczy li > span:nth-child(2) { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
#lista-graczy button { padding: .1em .6em; }
.kropka { width: 10px; height: 10px; border-radius: 50%; background: #3fae3f; flex: none; }
.kropka.offline { background: #b55; }
.rzad { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* ---------- stół ---------- */
#ekran-gra {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  user-select: none;
  -webkit-user-select: none;
}

#pasek { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; color: #fff; }
#pasek button { padding: .3em .7em; font-size: .85rem; }
#pasek nav { display: flex; gap: 6px; }
#czyja-tura { font-weight: 800; font-size: 1.1rem; }

#rywale { display: flex; justify-content: center; align-items: stretch; flex-wrap: wrap; gap: 8px; }

.gracz {
  background: #0003;
  border: 3px solid transparent;
  border-radius: 14px;
  padding: 6px 8px;
  color: #fff;
  transition: border-color .3s, background-color .3s;
}
.gracz.na-turze { border-color: var(--akcent); background: #0005; }
#rywale .gracz { flex: 1 1 250px; max-width: 390px; }

.naglowek-gracza { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.nick { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.punkty { margin-left: auto; font-weight: 800; background: var(--akcent); color: var(--tusz); border-radius: 20px; padding: 1px 10px; white-space: nowrap; }
.wachlarz { display: flex; align-items: center; min-height: 26px; padding-left: 8px; }
.wachlarz i { width: 17px; height: 25px; margin-left: -8px; border-radius: 3px; border: 1px solid #f6e7c1; background: #a5532f; }
.postep { height: 5px; border-radius: 3px; background: #0005; overflow: hidden; margin-bottom: 6px; }
.postep i { display: block; height: 100%; background: var(--cel); transition: width .5s; }

.grzadki { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 6px; }

.grzadka {
  position: relative;
  aspect-ratio: 1 / 1.12;
  display: flex;
  flex-direction: column;
  border: 4px solid var(--drewno);
  border-radius: 10px;
  background: repeating-linear-gradient(180deg, #8a5a32 0 9px, #7c4f2b 9px 18px);
  color: #fff;
  overflow: hidden;
  text-align: center;
  font-size: .72rem;
}
.grzadka .uprawa { flex: 1; min-height: 0; padding: 2px; }
.grzadka .opis-uprawy { position: relative; z-index: 2; background: #0007; padding: 1px 2px 2px; line-height: 1.25; }
.grzadka .nazwa { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.etapy { display: flex; justify-content: center; align-items: center; gap: 3px; margin-top: 1px; min-height: 1.25em; }
.etapy b { margin-left: 3px; white-space: nowrap; }
.etapy i { width: 7px; height: 7px; border-radius: 50%; border: 1px solid #fff; }
.etapy i.pelny { background: #8fe36a; border-color: #8fe36a; }
.grzadka.pusta .uprawa { display: grid; place-items: center; color: #fff7; font-size: .8rem; }
.grzadka .dojrzala { filter: drop-shadow(0 0 5px #fff6a0); }

.nakladka-plotek { position: absolute; left: 0; right: 0; bottom: 24%; height: 28%; z-index: 1; }
.nakladka-plotek svg { display: block; width: 100%; height: 100%; }
.grzadka.pusta .nakladka-plotek { bottom: 3%; }
.nakladka-mszyce { position: absolute; top: 0; right: 0; width: 48%; aspect-ratio: 1; z-index: 2; pointer-events: none; }
.nakladka-mszyce b { position: absolute; right: 1px; bottom: 0; background: #b3261e; border-radius: 10px; padding: 0 5px; }

.grzadka.cel { border-color: var(--cel); cursor: pointer; animation: pulsuj 1s ease-in-out infinite; }
@keyframes pulsuj { 50% { box-shadow: 0 0 0 5px #ffe14d77, 0 0 18px 6px #ffe14d99; } }

button.zbierz {
  position: absolute;
  z-index: 3;
  bottom: 2.9em;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  padding: .3em .55em;
  font-size: .78rem;
  background: var(--cel);
  border-color: #b89600;
  box-shadow: 0 3px 8px #0008;
}

#srodek { flex: 1; display: flex; align-items: center; justify-content: center; gap: clamp(10px, 4vw, 48px); min-height: 70px; color: #fff; }
#srodek .karta, .reka-widza .karta { width: calc(var(--karta-szer) * .62); padding: 2px; }
#srodek .karta .tekst, .reka-widza .karta .tekst { display: none; }
#srodek .karta .tytul, .reka-widza .karta .tytul { font-size: calc(var(--karta-szer) * .072); }

/* ---------- widz: bez własnej ręki, za to karty każdej osoby ---------- */
.reka-widza { display: flex; justify-content: center; gap: 4px; margin-top: 6px; min-height: calc(var(--karta-szer) * .62 * 1.4); }
#ekran-gra.widz #dol { display: none; }
#ekran-gra.widz #rywale .gracz { flex: 1 1 280px; max-width: 420px; }
#widzowie { font-size: .85rem; overflow-wrap: anywhere; }
#widzowie:empty { display: none; }
.stos { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .75rem; }
#komunikat { text-align: center; max-width: 360px; flex: 0 1 360px; font-size: .9rem; }
#komunikat .ostatni { opacity: .75; font-size: .82rem; margin-top: 4px; }
.puste-miejsce { width: calc(var(--karta-szer) * .62); aspect-ratio: 5 / 7; border: 2px dashed #fff6; border-radius: 8px; }

#ja .gracz { width: min(520px, 100%); margin: 0 auto; }
#ja .grzadki { max-width: min(500px, 66vh); margin: 0 auto; }
#ja .grzadka { aspect-ratio: 1 / 1; font-size: .82rem; }

#reka { display: flex; justify-content: center; gap: clamp(4px, 1vw, 10px); min-height: calc(var(--karta-szer) * 1.4); padding-top: 10px; }
#przyciski { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; min-height: 42px; margin-top: 8px; }

/* ---------- karty ---------- */
.karta {
  --kolor: #4f9d3a;
  width: var(--karta-szer);
  aspect-ratio: 5 / 7;
  flex: 0 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
  padding: 4px;
  border-radius: 9px;
  border: 3px solid var(--kolor);
  background: var(--papier);
  color: var(--tusz);
  box-shadow: 0 3px 8px #0006;
  text-align: center;
  overflow: hidden;
  font-weight: 400;
}
.karta.szkodnik { --kolor: #b3402e; }
.karta.ochrona { --kolor: #9c6a3a; }
.karta.pomoc { --kolor: #2f80ad; }
.karta.pogoda { --kolor: #6b5bb5; }
.karta .tytul { background: var(--kolor); color: #fff; border-radius: 4px; font-weight: 800; font-size: calc(var(--karta-szer) * .11); line-height: 1.5; white-space: nowrap; }
.karta .obraz { flex: 1; min-height: 0; }
.karta .tekst { font-size: max(9px, calc(var(--karta-szer) * .083)); line-height: 1.15; min-height: 3.45em; display: grid; place-items: center; }
.karta.tyl { --kolor: #f6e7c1; background: #a5532f; justify-content: center; }

/* touch-action: none — palec na karcie przeciąga ją, zamiast przewijać stronę */
#reka .karta { cursor: grab; touch-action: none; -webkit-touch-callout: none; transition: transform .15s, box-shadow .15s, filter .15s, opacity .15s; }
#reka .karta.w-locie { opacity: .25; }
#animacje .karta.duch { opacity: .9; box-shadow: 0 14px 28px #000a; }
.grzadka.cel.nad { animation: none; border-color: #fff; box-shadow: 0 0 0 5px var(--cel), 0 0 22px 8px #ffe14dcc; }
#srodek { border: 3px dashed transparent; border-radius: 14px; }
#ekran-gra.rzut-pogody #srodek { border-color: var(--cel); background: #ffe14d1f; }
#ekran-gra.rzut-pogody #srodek.nad { background: #ffe14d55; }
#reka .karta.nieaktywna { filter: grayscale(.75) brightness(.85); }
#reka .karta.wybrana { transform: translateY(-10px); box-shadow: 0 0 0 4px var(--cel), 0 10px 16px #0007; }
#reka .karta.do-wymiany { transform: translateY(-10px); box-shadow: 0 0 0 4px #e5533c, 0 10px 16px #0007; filter: none; }

/* ---------- dziennik, okna, dymki ---------- */
#dziennik {
  position: fixed;
  z-index: 20;
  top: 48px;
  right: 8px;
  width: min(320px, calc(100vw - 16px));
  max-height: 60vh;
  overflow: auto;
  background: var(--papier);
  border: 3px solid var(--drewno);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: .85rem;
  box-shadow: 0 8px 24px #0007;
}
#dziennik h3 { margin: 0 0 6px; }
#dziennik ol { margin: 0; padding: 0; list-style: none; }
#dziennik li { padding: 3px 0; border-top: 1px solid #e0d2ae; overflow-wrap: anywhere; }

dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: 88vh;
  border: 6px solid var(--drewno);
  border-radius: 18px;
  background: var(--papier);
  color: var(--tusz);
  padding: 18px 22px;
}
dialog::backdrop { background: #0008; }
#zasady-karty { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
#zasady-karty > div { display: grid; grid-template-columns: 44px 1fr; gap: 10px; align-items: center; }
#okno-koniec { text-align: center; }
#wyniki { padding: 0; list-style-position: inside; font-size: 1.1rem; }

#dymek {
  position: fixed;
  z-index: 60;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 24px);
  background: #fff;
  border: 3px solid #b3402e;
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
  box-shadow: 0 6px 18px #0006;
}

/* ---------- animacje ---------- */
#animacje { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 50; }
#animacje > * { position: absolute; }
#animacje .karta { margin: 0; }
.napis { font-weight: 900; font-size: 1.35rem; color: #fff; text-shadow: 0 2px 0 #000, 0 0 10px #000a; white-space: nowrap; }
.baner { left: 50%; top: 42%; background: var(--akcent); border: 4px solid #fff; border-radius: 16px; padding: 12px 34px; font-size: 1.8rem; font-weight: 900; white-space: nowrap; }
.pogoda { inset: 0; }
.pogoda.deszcz { background: #3a6f9b33; }
.pogoda.przymrozek { background: #cdeeff55; }
.pogoda i { position: absolute; top: -30px; }
.pogoda.deszcz i { width: 3px; height: 20px; border-radius: 3px; background: #a9dcff; }
.pogoda.przymrozek i { font-style: normal; color: #fff; font-size: 24px; text-shadow: 0 0 6px #4aa3d8; }
.grudka { width: 10px; height: 10px; border-radius: 50%; background: #5a3416; }

@media (max-width: 600px) {
  #rywale .gracz { flex-basis: 100%; }
  #rywale .grzadka { aspect-ratio: 1.5 / 1; }
  #rywale .gracz:not(:only-child) .wachlarz { min-height: 0; }
}

/* niskie ekrany poziome (tablet, laptop) */
@media (max-height: 820px) and (min-width: 700px) {
  :root { --karta-szer: clamp(78px, 14vh, 112px); }
  #rywale .grzadka { aspect-ratio: 1.15 / 1; }
  #ja .grzadki { max-width: min(500px, 54vh); }
}

/* ---------- 5–8 osób: zwarta siatka rywali ---------- */
#ekran-gra.duzo #rywale { display: grid; justify-content: center; grid-template-columns: repeat(var(--kolumny, 4), minmax(0, 330px)); gap: 6px; }
#ekran-gra.duzo #rywale .gracz { max-width: none; min-width: 0; padding: 3px 5px; }
#ekran-gra.duzo #rywale .naglowek-gracza { gap: 5px; margin-bottom: 2px; font-size: .85rem; }
#ekran-gra.duzo #rywale .punkty { padding: 0 7px; }
#ekran-gra.duzo #rywale .wachlarz { min-height: 18px; padding-left: 7px; }
#ekran-gra.duzo #rywale .wachlarz i { width: 11px; height: 16px; margin-left: -7px; }
#ekran-gra.duzo #rywale .postep { margin-bottom: 3px; height: 4px; }
#ekran-gra.duzo #rywale .grzadki { gap: 3px; }
#ekran-gra.duzo #rywale .grzadka { aspect-ratio: 1.35 / 1; border-width: 3px; font-size: .62rem; }
#ekran-gra.duzo #rywale .nazwa { display: none; } /* roślinę widać na rysunku */
#ekran-gra.duzo #rywale .etapy { gap: 2px; }
#ekran-gra.duzo #rywale .etapy i { width: 5px; height: 5px; }
#ekran-gra.duzo #rywale .etapy b { margin-left: 1px; }
#ekran-gra.duzo #srodek { min-height: 60px; }
#ekran-gra.duzo #ja .gracz { width: min(700px, 100%); }
#ekran-gra.duzo #ja .grzadki { max-width: min(660px, 80vh); }
#ekran-gra.duzo .reka-widza { margin-top: 3px; gap: 3px; min-height: 0; }
#ekran-gra.duzo .reka-widza .karta { width: calc(var(--karta-szer) * .42); }
#ekran-gra.duzo .reka-widza .karta .tytul { display: none; }

@media (max-width: 999px) {
  #ekran-gra.duzo #rywale { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 600px) {
  #ekran-gra.duzo #rywale { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #ekran-gra.duzo #rywale .grzadka { aspect-ratio: 1 / 1; }
  #ekran-gra.duzo #rywale .wachlarz i { width: 8px; height: 12px; margin-left: -6px; }
  #ekran-gra.duzo #rywale .punkty { font-size: .75rem; padding: 0 5px; }
}

/* 5–8 osób na niskim ekranie: wszystko razem z przyciskami musi się zmieścić bez przewijania */
@media (max-height: 820px) and (min-width: 700px) {
  #ekran-gra.duzo { --karta-szer: clamp(78px, 12.5vh, 104px); gap: 5px; }
  #ekran-gra.duzo #rywale .grzadka { aspect-ratio: 1.55 / 1; }
  #ekran-gra.duzo #ja .grzadki { max-width: min(600px, 70vh); }
}

/* w wąskiej grządce na kropki etapów nie ma miejsca — zostają punkty */
.grzadka { container-type: inline-size; }
@container (max-width: 58px) {
  .etapy i { display: none !important; }
  .etapy b { margin-left: 0 !important; }
}
/* telefon, 5 grządek rywala: dwa rzędy zamiast pięciu skrawków */
@media (max-width: 600px) {
  #ekran-gra.duzo #rywale .grzadki { grid-auto-flow: row; grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ---------- tablet: duże własne karty na dole, nad nimi pozostali; bez talii i odrzuconych ---------- */
@media (pointer: coarse) and (min-width: 600px) and (min-height: 500px) {
  #ekran-gra { gap: 5px; padding-top: 5px; }
  #ekran-gra #dol { --karta-szer: min(21vw, 19vh, 180px); }
  #ekran-gra #przyciski { margin-top: 3px; min-height: 40px; }
  /* stosy są niewidoczne, ale zostają na środku — stamtąd i tam lecą karty w animacjach */
  #ekran-gra .stos { position: absolute; visibility: hidden; }
  #ekran-gra #srodek { position: relative; min-height: 38px; }
  #ekran-gra #komunikat { max-width: 640px; flex-basis: 640px; }
  #ekran-gra #rywale .grzadka { aspect-ratio: 1.2 / 1; }
  #ekran-gra #ja .grzadki { max-width: min(500px, 44vh); }
  #ekran-gra.duzo #rywale .grzadka { aspect-ratio: 1 / 1.05; }
  #ekran-gra.duzo #ja .grzadki { max-width: min(620px, 56vh); }
}

/* wyższy tablet: przy 5–8 osobach jest miejsce, żeby rośliny rywali były lepiej widoczne */
@media (pointer: coarse) and (min-width: 600px) and (min-height: 740px) {
  #ekran-gra.duzo #rywale .grzadka { aspect-ratio: 1 / 1.35; }
}

/* małe grządki: „Zbierz” siada na podpisie, żeby nie zasłaniać rośliny */
#ekran-gra.duzo button.zbierz { bottom: 2px; padding: .25em .4em; }
@media (pointer: coarse) and (min-width: 600px) {
  #ekran-gra button.zbierz { bottom: 2px; }
}
