:root {
  --bg: #0f2a1f;
  --panel: rgba(12, 28, 21, 0.92);
  --line: rgba(255, 255, 255, 0.14);
  --text: #f1f5f2;
  --muted: #9bb3a5;
  --accent: #f2b01e;
  --danger: #ff6b6b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  user-select: none;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }

button, input, select {
  font: inherit;
  color: inherit;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.08);
  padding: 10px 14px;
}
button { cursor: pointer; }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: #1d1500; border-color: transparent; font-weight: 700; }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; padding: 6px; }
input, select { width: 100%; user-select: text; }
select option { color: #000; }
label { display: block; font-size: 13px; color: var(--muted); }
label input, label select { margin-top: 4px; color: var(--text); }
hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); }
.row { display: flex; gap: 10px; align-items: flex-end; margin: 10px 0; }
.row > * { flex: 1; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 8px; border: 1px solid rgba(255,255,255,.5); flex: none; }
.code { font-family: ui-monospace, Menlo, monospace; letter-spacing: 4px; color: var(--accent); }

details { margin: 10px 0; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
summary { cursor: pointer; color: var(--muted); }
details label { margin-top: 10px; }

.screen, .modal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; overflow-y: auto; z-index: 20; }
.modal { background: rgba(0,0,0,.6); z-index: 40; }
.screen[hidden], .modal[hidden], [hidden] { display: none !important; }
.card {
  width: min(440px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  backdrop-filter: blur(8px);
}
.card h1 { margin: 0 0 14px; font-size: 24px; }
.card h2 { margin: 0 0 10px; font-size: 20px; }
.card > button:not(.link) { width: 100%; margin: 6px 0; }
.rules { padding-left: 18px; margin: 0 0 14px; font-size: 14px; }
.rules li { margin-bottom: 6px; }

.plist { list-style: none; padding: 0; margin: 12px 0; }
.plist li { display: flex; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.plist .pname { flex: 1; }
.plist .x { width: 36px; padding: 4px 0; flex: none; }

#banner { position: fixed; top: 0; left: 0; right: 0; background: var(--danger); color: #200; text-align: center; padding: 6px; z-index: 60; font-weight: 600; }

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; --tw: clamp(38px, 6.5vh, 52px); --hand: calc(var(--tw) * 2 + 44px); }
#hud > * { pointer-events: none; }
#hud button, #hud .chip { pointer-events: auto; }
#topbar {
  position: absolute; top: 0; left: 0; right: 0;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 6px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: linear-gradient(rgba(0,0,0,.55), transparent);
}
#info { font-size: 13px; text-shadow: 0 1px 3px #000; }
.tools { display: flex; gap: 6px; pointer-events: auto; }
.tools button { position: relative; width: 40px; height: 40px; padding: 0; background: rgba(0,0,0,.45); }
#chatBadge { position: absolute; top: -4px; right: -4px; background: var(--danger); color: #fff; font-size: 11px; border-radius: 9px; min-width: 18px; padding: 1px 4px; }

#players {
  position: absolute; top: calc(env(safe-area-inset-top) + 52px); left: 8px; right: 8px;
  display: flex; flex-wrap: wrap; gap: 6px;
}
.chip {
  position: relative; display: flex; align-items: center; gap: 4px;
  background: rgba(8, 20, 14, .78); border: 1px solid var(--line); border-radius: 20px;
  padding: 3px 10px 3px 8px; font-size: 13px;
}
.chip .dot { margin-right: 2px; width: 10px; height: 10px; }
.chip .cc { color: var(--muted); margin-left: 4px; }
.chip .cs { color: var(--accent); font-weight: 700; margin-left: 4px; }
.chip.turn { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(242,176,30,.4); }
.chip.off { opacity: .5; }
.bubble { position: absolute; top: 100%; left: 50%; transform: translateX(-50%); font-size: 28px; animation: pop .3s; z-index: 5; filter: drop-shadow(0 2px 3px #000); }
@keyframes pop { from { transform: translateX(-50%) scale(.3); opacity: 0; } }

#status { text-align: center; font-weight: 600; text-shadow: 0 1px 4px #000; padding: 0 12px; }
#status.mine { color: var(--accent); }

#bottom {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 8px 8px calc(env(safe-area-inset-bottom) + 8px);
  background: linear-gradient(transparent, rgba(0,0,0,.65) 35%);
  display: flex; flex-direction: column; gap: 6px; align-items: center;
}
#dest { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; pointer-events: auto; }
#dest button { background: rgba(125,255,138,.18); border-color: rgba(125,255,138,.6); padding: 8px 12px; display: flex; align-items: center; }
#actions { min-height: 0; }
#actions button { pointer-events: auto; }
/* власні камені: один ряд внизу екрана */
#hand {
  display: flex; gap: 6px; max-width: 100%; overflow-x: auto; padding: 14px 6px 4px;
  pointer-events: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
#hand::-webkit-scrollbar { display: none; }
.tile {
  position: relative; flex: none; width: var(--tw); height: calc(var(--tw) * 2); padding: 0; border-radius: 8px;
  background: #f6efdc; border: 2px solid #2a2a2a; overflow: hidden;
  box-shadow: 0 3px 6px rgba(0,0,0,.5); transition: transform .12s;
}
/* картинка каменя (горизонтальна) повертається на 90° і центрується в рамці: рамка 2px, тож внутрішня ширина tw-4 */
.tile img { position: absolute; width: calc(var(--tw) * 2 - 4px); height: calc(var(--tw) - 4px); left: calc(var(--tw) / -2); top: calc(var(--tw) / 2); transform: rotate(90deg); pointer-events: none; }
.tile.sel { transform: translateY(-12px); border-color: var(--accent); box-shadow: 0 0 0 3px rgba(242,176,30,.6), 0 6px 10px rgba(0,0,0,.5); z-index: 2; }
.tile.dim { opacity: .42; }

#emotes { position: absolute; right: 8px; bottom: calc(env(safe-area-inset-bottom) + var(--hand) + 8px); display: flex; flex-direction: column; align-items: flex-end; gap: 6px; pointer-events: auto; }
.emote-toggle { width: 42px; height: 42px; padding: 0; border-radius: 50%; background: rgba(0,0,0,.5); font-size: 20px; }
.emote-list { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; max-width: 190px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 6px; }
.emote-list button { width: 38px; height: 38px; padding: 0; font-size: 20px; }

#toasts { position: fixed; top: calc(env(safe-area-inset-top) + 100px); left: 0; right: 0; display: flex; justify-content: center; z-index: 30; pointer-events: none; padding: 0 12px; }
.toast { background: rgba(0,0,0,.78); border: 1px solid var(--line); border-radius: 12px; padding: 8px 14px; white-space: pre-line; text-align: center; animation: pop2 .2s; }
@keyframes pop2 { from { opacity: 0; transform: translateY(-6px); } }

#chatPanel {
  position: fixed; right: 8px; top: calc(env(safe-area-inset-top) + 52px); bottom: calc(env(safe-area-inset-bottom) + 8px);
  width: min(320px, calc(100% - 16px)); z-index: 35; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; overflow: hidden;
}
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.chat-head button { padding: 2px 10px; }
#chatLog { flex: 1; overflow-y: auto; padding: 10px; user-select: text; }
.msg { margin-bottom: 6px; word-break: break-word; font-size: 14px; }
#chatForm { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
#chatForm input { flex: 1; }
#chatForm button { width: 46px; padding: 0; flex: none; }

#overlay { position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; display: flex; justify-content: center; padding: 8px; max-height: 80vh; }
#overlay .card { max-height: 74vh; }
#overlay table { width: 100%; border-collapse: collapse; margin: 8px 0 12px; }
#overlay th { text-align: left; color: var(--muted); font-size: 12px; font-weight: 500; }
#overlay td, #overlay th { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
#overlay tr.win td { color: var(--accent); }
#overlay .rest { display: flex; flex-wrap: wrap; gap: 2px; margin-top: 4px; }
.mini { width: 34px; height: 17px; border-radius: 3px; }


.avatars { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin: 6px 0 10px; }
.avatars button { font-size: 24px; line-height: 1; padding: 6px 0; border-radius: 10px; }
.avatars button.sel { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(242,176,30,.55); background: rgba(242,176,30,.18); }
.avatars button.taken { opacity: .25; cursor: not-allowed; }
label.check { display: flex; align-items: center; gap: 8px; }
label.check input { width: auto; margin: 0; }
.chip .av { font-size: 16px; }

.chip .tm { color: var(--accent); font-weight: 700; margin-left: 4px; min-width: 22px; text-align: right; }
.chip .tm.low { color: var(--danger); }
#score { position: absolute; right: 8px; top: calc(env(safe-area-inset-top) + 96px); flex-direction: column; align-items: stretch; gap: 2px; border-radius: 12px; padding: 6px 10px; pointer-events: auto; cursor: pointer; min-width: 120px; }
#score .sh { color: var(--muted); font-size: 11px; display: flex; justify-content: space-between; gap: 8px; }
#score .sr { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; }
#score .sr b { color: var(--accent); }
.card.full { width: 100%; height: 100%; max-width: none; border-radius: 0; }
.card.full table { width: 100%; border-collapse: collapse; margin: 10px 0 14px; }
.card.full th, .card.full td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: center; }
.card.full th:first-child, .card.full td:first-child { text-align: left; }
.card.full tr.total td { font-weight: 700; color: var(--accent); }
.card.full td.win { color: var(--accent); }

#hourglass { position: absolute; left: 8px; top: 38%; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 8px; background: rgba(8,20,14,.7); border: 1px solid var(--line); border-radius: 12px; }
#hourglass #hgWho { font-size: 18px; line-height: 1; }
#hourglass #hgTime { font-weight: 700; color: var(--accent); font-size: 14px; }
#hourglass.low #hgTime { color: var(--danger); }
#hourglass.low svg { animation: hgShake .5s infinite; }
@keyframes hgShake { 50% { transform: rotate(4deg); } }

.colors { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 10px; }
.colors button { width: 36px; height: 36px; padding: 0; border-radius: 50%; border: 2px solid rgba(255,255,255,.35); }
.colors button.sel { border-color: #fff; box-shadow: 0 0 0 3px rgba(242,176,30,.8); }
.colors button.taken { opacity: .2; cursor: not-allowed; }
#status.big { font-size: 18px; color: var(--accent); background: rgba(0,0,0,.7); border-radius: 14px; padding: 8px 14px; width: fit-content; max-width: 92%; margin: 0 auto; }

.flybig { position: fixed; left: 0; top: 0; z-index: 70; pointer-events: none; font-size: min(68vw, 68vh); line-height: 1; filter: drop-shadow(0 8px 20px rgba(0,0,0,.6)); will-change: transform, opacity; }
.flybig.txt { font-size: min(34vw, 30vh); font-weight: 900; color: #ffd54f; -webkit-text-stroke: 3px #7a4a00; letter-spacing: 2px; }
input[type="range"] { padding: 0; }

/* полегшений режим */
#hint { max-width: min(640px, 94vw); background: rgba(30, 22, 0, .88); border: 1px solid #f2b01e; color: #ffe9a8; border-radius: 12px; padding: 8px 12px; font-size: 13px; pointer-events: none; text-align: center; }
.tile .badge { position: absolute; top: 2px; left: 50%; transform: translateX(-50%); font-style: normal; font-size: 11px; font-weight: 800; min-width: 18px; height: 18px; line-height: 18px; text-align: center; border-radius: 9px; background: #1d1500; color: #ffd54f; border: 1px solid #ffd54f; z-index: 3; }
.tile.hint { box-shadow: 0 0 0 3px #ffc928, 0 0 16px 4px rgba(255, 201, 40, .65); }
.tile.combo { box-shadow: 0 0 0 2px #7dff8a; }
.tile.combo .badge { background: #0d2a14; color: #7dff8a; border-color: #7dff8a; }
.tile.rel { outline: 2px dashed #8ad0ff; outline-offset: 1px; }

/* навчання */
#coach { position: absolute; top: calc(env(safe-area-inset-top) + 52px); left: 50%; transform: translateX(-50%); width: min(620px, 94vw); background: rgba(10, 24, 18, .94); border: 1px solid #f2b01e; border-radius: 14px; padding: 10px 14px; pointer-events: auto; z-index: 15; }
#coachStep { color: var(--accent); font-size: 12px; font-weight: 700; margin-bottom: 4px; }
#coachText { font-size: 14px; line-height: 1.4; }
#coach .coach-btns { display: flex; align-items: center; gap: 10px; margin-top: 8px; justify-content: space-between; }
#coach[data-kind="player"] { border-color: #7dff8a; }
body.tutorial #players, body.tutorial #score, body.tutorial #hourglass, body.tutorial #emotes { display: none !important; }
@media (min-width: 900px) { #menu { justify-content: flex-start; padding-left: 6vw; } }

body.tutorial #toasts { display: none; }

/* стартовий екран: одна головна дія, решта — у шторках */
.card.menu { width: min(380px, 100%); }
.menu-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; }
.menu-head h1 { margin: 0; font-size: 20px; }
.icon-btn { width: 34px; height: 34px; padding: 0; border-radius: 50%; flex: none; font-weight: 700; }
.profile-row { width: 100%; display: flex; align-items: center; gap: 10px; text-align: left; margin-bottom: 14px; padding: 8px 12px; }
.prof-av { width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 20px; background: rgba(255,255,255,.1); border: 3px solid var(--accent); flex: none; }
.prof-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.prof-text small { color: var(--muted); font-size: 12px; }
.chev { color: var(--muted); font-size: 22px; }
button.big { height: 50px; font-size: 16px; width: 100%; }
.summary { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 8px 2px 14px; font-size: 12px; color: var(--muted); }
.summary .link { padding: 0; font-size: 12px; color: var(--accent); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
#btnSettings { display: block; margin: 0 auto; }
.modal.sheet { align-items: flex-end; }
@media (min-width: 700px) { .modal.sheet { align-items: center; } }
.modal.sheet .card { width: min(420px, 100%); }
.card h3 { margin: 16px 0 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.srow { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.srow select { width: auto; max-width: 58%; padding: 6px 10px; }
input.sw { appearance: none; -webkit-appearance: none; width: 40px; height: 22px; padding: 0; border-radius: 11px; background: rgba(255,255,255,.2); position: relative; flex: none; cursor: pointer; border: none; }
input.sw::after { content: ""; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: left .12s; }
input.sw:checked { background: var(--accent); }
input.sw:checked::after { left: 20px; }
.modal.sheet .card > button.primary { margin-top: 14px; }
#menu { z-index: 20; }

.two button { height: 46px; margin: 0; padding: 0 10px; display: flex; align-items: center; justify-content: center; gap: 6px; line-height: 1; width: 100%; }
#coach .coach-btns { flex-wrap: wrap; } #coach .coach-btns .link { margin-left: auto; }

button.code { background: none; border: none; padding: 0 2px; font: inherit; cursor: pointer; border-bottom: 1px dashed var(--accent); border-radius: 0; }
.qr { display: flex; flex-direction: column; align-items: center; gap: 6px; margin: 12px 0; }
.qr canvas { width: 180px; height: 180px; border-radius: 10px; background: #fff; image-rendering: pixelated; }
#maintenance { z-index: 90; background: rgba(0,0,0,.82); }

#updateBar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom) + 12px); z-index: 80; display: flex; align-items: center; gap: 12px; background: rgba(10,24,18,.95); border: 1px solid var(--accent); border-radius: 14px; padding: 8px 12px; font-size: 14px; }
#updateBar button { padding: 6px 14px; }

.verline { text-align: center; margin: 6px 0 0; font-size: 11px; color: var(--muted); }
.statsbox { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 13px; padding: 8px 10px; background: rgba(255,255,255,.05); border-radius: 10px; }
.statsbox .k { color: var(--muted); }
.statsbox .v { text-align: right; font-weight: 600; }
.statsbox .sec { grid-column: 1 / -1; font-size: 11px; color: var(--accent); margin-top: 4px; text-transform: uppercase; letter-spacing: .04em; }
