:root {
  --paper: #FBFCFE;
  --grid: #BCCDE6;
  --grid-faint: #E6EDF7;
  --margin: #E06B6B;
  --ink: #232840;
  --ink-soft: #5A6381;
  --line: #CDD6E5;
  --pen-0: #1F45B5;
  --pen-1: #C62D2D;
  --pen-2: #1C7D3B;
  --pen-3: #6C27A6;
  --blot: #1B2556;
  --focus: #1F45B5;
  --on-pen: #FFFFFF;
  --scrim: rgba(24, 28, 48, .42);
  --hand: "Neucha", "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
  --ui: "Golos Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #131927; --grid: #33425F; --grid-faint: #1B2435; --margin: #B9525D;
    --ink: #DCE2EF; --ink-soft: #97A3BF; --line: #2E3A52;
    --pen-0: #7FA2FF; --pen-1: #FF7C7C; --pen-2: #5ED390; --pen-3: #C99CFF;
    --blot: #33458C; --focus: #7FA2FF; --on-pen: #0E1320; --scrim: rgba(3, 6, 14, .6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #131927; --grid: #33425F; --grid-faint: #1B2435; --margin: #B9525D;
  --ink: #DCE2EF; --ink-soft: #97A3BF; --line: #2E3A52;
  --pen-0: #7FA2FF; --pen-1: #FF7C7C; --pen-2: #5ED390; --pen-3: #C99CFF;
  --blot: #33458C; --focus: #7FA2FF; --on-pen: #0E1320; --scrim: rgba(3, 6, 14, .6);
  color-scheme: dark;
}
*, *::before, *::after { box-sizing: inherit; }
html { height: 100%; scroll-padding-top: env(safe-area-inset-top, 0px); }
body {
  height: 100%; margin: 0; background: var(--paper); color: var(--ink);
  font: 400 16px/1.5 var(--ui); -webkit-font-smoothing: antialiased;
}
button { font: inherit; }
.pen-0 { --c: var(--pen-0); } .pen-1 { --c: var(--pen-1); }
.pen-2 { --c: var(--pen-2); } .pen-3 { --c: var(--pen-3); }
.pen-dot { display: inline-block; flex: none; width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.screen[hidden] { display: none !important; }

.btn {
  appearance: none; min-height: 44px; padding: 10px 16px; cursor: pointer;
  font: 500 15px/1.2 var(--ui); color: var(--ink); background: var(--paper);
  border: 1.5px solid var(--line); border-radius: 8px;
}
.btn:hover:not(:disabled) { border-color: var(--ink-soft); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--pen-0); border-color: var(--pen-0); color: var(--on-pen); }
.btn:focus-visible, .map:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.btn.small { min-height: 40px; padding: 8px 10px; font-size: 14px; }

/* ---------- Главная, лобби, настройка: лист в клетку с полями ---------- */
.paper {
  height: 100%; overflow-y: auto;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-faint) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-faint) 1px, transparent 1px);
  background-size: 24px 24px;
}
.sheet { position: relative; min-height: 100%; max-width: 960px; margin: 0 auto; padding: 12px 24px 64px 104px; }

/* Верхняя строка: назад слева, своё имя справа */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.topbar[hidden] { display: none; }
.back { display: inline-flex; align-items: center; min-height: 44px; color: var(--ink-soft); font-weight: 500; text-decoration: none; }
.back:hover { color: var(--ink); }
.back:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.me {
  appearance: none; display: inline-flex; align-items: center; gap: 8px; min-width: 0; max-width: 100%;
  min-height: 44px; margin-left: auto; padding: 6px 2px; cursor: pointer;
  font: 500 15px/1.2 var(--ui); color: var(--ink); background: none; border: 0;
}
.me-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-bottom: 2px; border-bottom: 1.5px dashed var(--ink-soft); }
.me:hover .me-name { border-bottom-style: solid; border-bottom-color: var(--ink); }
.me:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.pencil { flex: none; width: 16px; height: 16px; fill: none; stroke: var(--ink-soft); stroke-width: 1.5; stroke-linejoin: round; }
.me:hover .pencil { stroke: var(--ink); }
.sheet::before { content: ""; position: absolute; top: 0; bottom: 0; left: 76px; width: 2px; background: var(--margin); }
.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; flex-wrap: wrap; }
.hero h1 { font: 400 clamp(64px, 11vw, 96px)/.95 var(--hand); margin: 12px 0 10px; }
.lede { max-width: 54ch; margin: 0; color: var(--ink-soft); }
.hero-art { width: 168px; height: auto; flex: none; }
.hero-art .g { stroke: var(--grid); stroke-width: 1; fill: none; }
.hero-art .t { fill: var(--pen-0); fill-opacity: .16; }
.hero-art .w { fill: none; stroke: var(--pen-0); stroke-width: 2.4; stroke-linejoin: round; }

fieldset { border: 0; margin: 36px 0 0; padding: 0; min-width: 0; }
legend { font: 400 30px/1.1 var(--hand); padding: 0; margin-bottom: 12px; }

.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 4px 28px; max-width: 620px; }
.opt { position: relative; display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; cursor: pointer; }
.opt input { position: absolute; opacity: 0; pointer-events: none; }
.mark { position: relative; flex: none; width: 24px; height: 24px; }
.mark::before {
  content: ""; position: absolute; inset: 0;
  background:
    linear-gradient(var(--grid), var(--grid)) center / 100% 1px no-repeat,
    linear-gradient(var(--grid), var(--grid)) center / 1px 100% no-repeat;
}
.mark::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 12px; height: 12px; margin: -6px 0 0 -6px;
  border-radius: 50%; border: 1.5px solid var(--ink-soft); background: var(--paper);
}
.opt input:checked + .mark::after { background: var(--pen-0); border-color: var(--pen-0); }
.opt input:focus-visible + .mark { outline: 2px solid var(--focus); outline-offset: 3px; border-radius: 4px; }
.opt b { display: block; font-weight: 500; }
.opt small { display: block; color: var(--ink-soft); font-size: 14px; line-height: 1.4; }

.seats { display: flex; flex-wrap: wrap; gap: 12px 48px; }
.seat-group { display: flex; flex-direction: column; gap: 6px; }
.team-cap { margin: 0; font: 400 22px/1.2 var(--hand); color: var(--ink-soft); }
.seat { display: flex; align-items: center; gap: 12px; }
.seat input {
  width: 210px; max-width: 60vw; padding: 7px 2px; font: 400 16px/1.3 var(--ui); color: var(--ink);
  background: transparent; border: 0; border-bottom: 1.5px solid var(--line); border-radius: 0;
}
.seat input::placeholder { color: var(--ink-soft); opacity: .8; }
.seat input:focus { outline: none; border-bottom-color: var(--c); }

.maps { display: grid; grid-template-columns: repeat(auto-fill, minmax(164px, 1fr)); gap: 14px; }
.map {
  appearance: none; display: flex; flex-direction: column; gap: 2px; padding: 10px 12px 12px;
  text-align: left; color: var(--ink); background: var(--paper); cursor: pointer;
  border: 1.5px solid var(--line); border-radius: 10px;
}
.map:hover { border-color: var(--ink-soft); }
.map[aria-pressed="true"] { border-color: var(--pen-0); box-shadow: inset 0 0 0 1px var(--pen-0); }
.map svg { width: 100%; height: auto; aspect-ratio: 4 / 3; margin-bottom: 6px; }
.map .nm { font: 400 24px/1.1 var(--hand); }
.map .sz, .map .nt { font-size: 13px; line-height: 1.35; color: var(--ink-soft); }

.opt.static, .map.static { cursor: default; }
.map.static { max-width: 240px; }
.map.static:hover { border-color: var(--line); }

.start-row { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 36px; }
.hero + .start-row { margin-top: 28px; }

/* ---------- Главная: список лобби ---------- */
.join { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 14px; margin: 0 0 18px; }
.join input, .invite input {
  width: 200px; max-width: 100%; padding: 9px 2px; font: 400 16px/1.3 var(--ui); color: var(--ink);
  background: transparent; border: 0; border-bottom: 1.5px solid var(--line); border-radius: 0;
}
.join input::placeholder { color: var(--ink-soft); opacity: .8; }
.join input:focus, .invite input:focus { outline: none; border-bottom-color: var(--pen-0); }
.rooms { list-style: none; margin: 0; padding: 0; max-width: 720px; border-top: 1px solid var(--line); }
.rooms .empty { padding: 14px 0; color: var(--ink-soft); border-bottom: 1px solid var(--line); }
.room-row { display: flex; align-items: center; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.seat-dots { flex: none; display: grid; grid-template-columns: repeat(2, 12px); gap: 6px; width: 30px; }
.pen-dot.empty { background: transparent; box-shadow: inset 0 0 0 1.5px var(--c); }
.room-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.room-info b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-info span { font-size: 14px; color: var(--ink-soft); }
.room-info .status { font-size: 13px; color: var(--ink-soft); }
.room-info .status.wait { color: var(--pen-2); }
.room-row .btn { flex: none; min-width: 112px; }

/* ---------- Лобби ---------- */
.loading { margin: 48px 0 0; color: var(--ink-soft); }
.room-head h1 { font: 400 clamp(52px, 9vw, 80px)/1 var(--hand); margin: 8px 0 10px; }
.room-head .code { font-variant-numeric: tabular-nums; }
.invite { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; max-width: 640px; margin-top: 16px; }
.invite input { flex: 1 1 260px; width: auto; min-width: 0; }
.room-seats { display: flex; flex-wrap: wrap; gap: 8px 48px; }
.room-seats .seat-group { flex: 1 1 300px; max-width: 480px; gap: 0; }
.seat-row { display: flex; align-items: center; gap: 12px; min-height: 58px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.seat-row .color { flex: none; width: 96px; font-size: 14px; color: var(--ink-soft); }
.seat-row .who-name, .seat-row .free { flex: 1; min-width: 0; }
.seat-row .who-name { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.seat-row .nm { font-weight: 500; overflow-wrap: anywhere; }
.seat-row .free { color: var(--ink-soft); }
.seat-row .btn { flex: none; }
.tag {
  flex: none; padding: 1px 7px; font: 400 12px/1.4 var(--ui); color: var(--ink-soft); white-space: nowrap;
  border: 1px solid var(--line); border-radius: 999px;
}
.tag.off { border-style: dashed; }
.watchers { margin: 12px 0 0; font-size: 14px; color: var(--ink-soft); }
.check { display: flex; align-items: center; gap: 12px; margin-top: 32px; color: var(--ink-soft); cursor: pointer; }
.check[hidden] { display: none; }
.check input { flex: none; width: 18px; height: 18px; margin: 0; accent-color: var(--pen-0); }
.hint { margin: 12px 0 0; max-width: 64ch; font-size: 14px; color: var(--ink-soft); }
.start-row + .hint:empty { display: none; }
.rules { margin-top: 40px; max-width: 64ch; }
.rules summary { font: 400 26px/1.2 var(--hand); cursor: pointer; }
.rules ul { margin: 10px 0 0; padding-left: 20px; color: var(--ink-soft); }
.rules li { margin: 6px 0; }

/* Сетка тонкая и одинаковая на любом масштабе */
.grid { fill: none; stroke: var(--grid); stroke-width: 1; vector-effect: non-scaling-stroke; }
.blot { fill: var(--blot); }

/* ---------- Экран партии ---------- */
#game {
  height: 100%; display: grid;
  grid-template-columns: minmax(250px, 300px) 1fr; grid-template-areas: "side board";
}
.side {
  grid-area: side; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 24px;
  padding: 22px 20px 20px 22px; border-right: 2px solid var(--margin);
}
.head { display: flex; flex-direction: column; gap: 18px; }
.brand b { display: block; font: 400 40px/1 var(--hand); }
.brand span { font-size: 13px; color: var(--ink-soft); }
.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.players li {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 0; border-bottom: 1px solid var(--line);
}
.who { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.name { display: inline-flex; align-items: center; gap: 9px; min-width: 0; font-weight: 500; white-space: nowrap; }
.name span { overflow: hidden; text-overflow: ellipsis; }
.name.turn span {
  text-decoration-line: underline; text-decoration-style: wavy; text-decoration-color: var(--c);
  text-decoration-thickness: 2px; text-underline-offset: 6px;
}
.score { font: 400 34px/1 var(--hand); font-variant-numeric: tabular-nums; }
.turnline { display: flex; align-items: center; gap: 9px; margin: 0; font: 400 24px/1.2 var(--hand); }
.note { min-height: 2.8em; margin: -8px 0 0; font-size: 14px; line-height: 1.4; color: var(--ink-soft); }
.head .watchers { margin: -10px 0 0; }
.name .tag { margin-left: -2px; }
.ask { display: flex; flex-direction: column; gap: 8px; margin-top: -8px; }
.ask[hidden] { display: none; }
.ask-item { padding: 10px 12px; background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px; }
.ask-item p { margin: 0; font-size: 14px; line-height: 1.4; }
.ask-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.actions { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.actions .btn[hidden] { display: none; }

.board-wrap { grid-area: board; position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#board {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block;
  touch-action: none; cursor: crosshair; user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#board.idle { cursor: default; }
#board:focus { outline: none; }
#board:focus-visible { outline: 2px solid var(--focus); outline-offset: -3px; }
.zoom { position: absolute; right: max(12px, env(safe-area-inset-right, 0px)); bottom: 12px; display: flex; gap: 6px; }
.zoom .btn { min-width: 44px; padding: 10px 12px; }

.terr { fill: var(--c); fill-opacity: .16; }
.wall { fill: none; stroke: var(--c); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.wall.draw { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw .6s ease-out forwards; }
.dot { fill: var(--c); }
.dot.dead { fill-opacity: .32; }
.dot.pop { transform-box: fill-box; transform-origin: center; animation: pop .22s cubic-bezier(.3, 1.6, .6, 1); }
.last { fill: none; stroke: var(--c); stroke-width: 1.6; stroke-opacity: .6; }
.ghost { fill: var(--c); fill-opacity: .35; }
.pend { fill: none; stroke: var(--c); stroke-width: 1.6; stroke-dasharray: 3 3; }
.kbd { fill: none; stroke: var(--ink-soft); stroke-width: 1.4; }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes pop { from { transform: scale(.2); } }

/* ---------- Модальное окно ---------- */
.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 20px; background: var(--scrim); }
.modal[hidden] { display: none; }
.dialog {
  position: relative; width: min(440px, 100%); max-height: 100%; overflow-y: auto;
  padding: 22px 22px 20px 48px; background: var(--paper); border: 1.5px solid var(--line); border-radius: 12px;
}
.dialog::before { content: ""; position: absolute; top: 0; bottom: 0; left: 30px; width: 2px; background: var(--margin); }
.dialog h2 { margin: 0 0 10px; font: 400 36px/1.1 var(--hand); }
.dialog p { margin: 0; color: var(--ink-soft); }
.results { list-style: none; margin: 4px 0 0; padding: 0; }
.results li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.results .who { flex-direction: row; flex-wrap: wrap; gap: 4px 14px; }
.results .pts { font: 400 26px/1 var(--hand); white-space: nowrap; }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.name-row { display: flex; align-items: flex-end; gap: 10px; margin-top: 16px; }
.name-input {
  flex: 1; min-width: 0; padding: 8px 2px; font: 400 20px/1.3 var(--ui); color: var(--ink);
  background: transparent; border: 0; border-bottom: 1.5px solid var(--line); border-radius: 0;
}
.name-input:focus { outline: none; border-bottom-color: var(--pen-0); }
.btn.dice { flex: none; width: 44px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
.dice-icon { width: 22px; height: 22px; }
.dice-icon rect { fill: none; stroke: currentColor; stroke-width: 1.6; }
.dice-icon circle { fill: currentColor; }

/* ---------- Связь и короткие уведомления ---------- */
.conn, .toast { position: fixed; left: 50%; z-index: 20; transform: translateX(-50%); font-size: 14px; line-height: 1.4; }
.conn[hidden], .toast[hidden] { display: none; }
.conn {
  top: calc(10px + env(safe-area-inset-top, 0px)); padding: 8px 16px; white-space: nowrap;
  color: var(--ink); background: var(--paper); border: 1.5px solid var(--margin); border-radius: 999px;
  box-shadow: 0 4px 18px rgba(20, 24, 40, .14);
}
.toast {
  bottom: calc(76px + env(safe-area-inset-bottom, 0px)); width: max-content; max-width: min(92vw, 460px);
  padding: 10px 16px; color: var(--paper); background: var(--ink); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(20, 24, 40, .2);
}

@media (max-width: 760px) {
  .sheet { padding: 6px 16px 48px 52px; }
  .room-row { gap: 12px; }
  .room-row .btn { min-width: 0; }
  .seat-row .color { width: 80px; }
  .head .watchers { display: none; }
  .ask { margin-top: 2px; }
  .ask-item { padding: 8px 10px; }
  .conn { white-space: normal; width: max-content; max-width: 92vw; text-align: center; }
  .sheet::before { left: 34px; }
  .hero-art { width: 132px; }
  #game {
    grid-template-columns: 1fr; grid-template-rows: auto 1fr auto;
    grid-template-areas: "head" "board" "actions";
  }
  .side { display: contents; }
  .head { grid-area: head; gap: 6px; padding: 10px 14px 8px; border-bottom: 2px solid var(--margin); }
  .brand { display: none; }
  .players { flex-direction: row; flex-wrap: wrap; gap: 2px 18px; }
  .players li { border: 0; padding: 0; gap: 10px; }
  .who { flex-direction: row; gap: 10px; }
  .score { font-size: 28px; }
  .turnline { font-size: 20px; }
  .note { min-height: 1.4em; margin: 0; font-size: 13px; }
  .actions { grid-area: actions; flex-direction: row; gap: 6px; margin: 0; padding: 8px 10px; border-top: 1px solid var(--line); }
  .actions .btn { flex: 1; padding: 10px 6px; font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .wall.draw { animation: none; stroke-dashoffset: 0; }
  .dot.pop { animation: none; }
}
