:root {
	--bg: #0c1020;
	--bg-2: #151a2e;
	--bg-3: #1c2340;
	--gold: #e8c547;
	--gold-dim: #b8942e;
	--ink: #e8eaf2;
	--muted: #9aa3bd;
	--danger: #e85d5d;
	--ok: #4ecb8d;
	--cf: #c44b4b;
	--line: rgba(200, 210, 240, 0.18);
	--safe-b: max(12px, env(safe-area-inset-bottom, 0px));
	--safe-t: max(8px, env(safe-area-inset-top, 0px));
	--safe-r: max(8px, env(safe-area-inset-right, 0px));
	--safe-l: max(8px, env(safe-area-inset-left, 0px));
	--radius: 12px;
	--hit: 44px;
	--font: "Source Sans 3", "Segoe UI", "Noto Sans", "Hiragino Sans", "Noto Sans CJK JP", "Noto Sans CJK SC", "Noto Sans CJK KR", sans-serif;
}
* { box-sizing: border-box; }
html, body {
	margin: 0; padding: 0;
	background: var(--bg);
	color: var(--ink);
	font-family: var(--font);
	font-size: 16px;
	line-height: 1.45;
	min-height: 100%;
	min-height: 100dvh;
	-webkit-text-size-adjust: 100%;
	touch-action: manipulation;
}
body { padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l); }
button, input, a.button {
	font: inherit;
	touch-action: manipulation;
}
button, a.button {
	appearance: none;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-height: var(--hit);
	min-width: var(--hit);
	padding: 10px 18px;
	border: 0;
	border-radius: 10px;
	background: var(--gold);
	color: #1a1404;
	font-weight: 650;
	cursor: pointer;
	text-decoration: none;
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
button:focus-visible, a.button:focus-visible, input:focus-visible {
	outline: 2px solid var(--gold);
	outline-offset: 2px;
}
.btn-secondary, .btn-secondary:link {
	background: var(--bg-3);
	color: var(--ink);
	border: 1px solid var(--line);
}
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-outline {
	background: transparent;
	color: var(--danger);
	border: 1px solid var(--danger);
}
.btn-back {
	background: transparent;
	color: var(--muted);
	border: 1px solid var(--line);
	min-width: auto;
	margin-bottom: 8px;
}
input[type="text"], input[type="number"], input[type="time"], textarea {
	background: var(--bg-2);
	color: var(--ink);
	border: 1px solid var(--line);
	border-radius: 10px;
	padding: 10px 12px;
	min-height: var(--hit);
	width: 100%;
}
.sr-only {
	position: absolute; width: 1px; height: 1px;
	padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0;
}
h1, h2 { font-weight: 700; letter-spacing: 0.01em; margin: 0 0 0.4em; }
h1 { font-size: 2.1rem; color: var(--gold); text-align: center; }
.tagline, .hint, small { color: var(--muted); }
.tagline { text-align: center; margin: 0 0 1.2rem; }
.hint, small { font-size: 0.92rem; }
.error, .notice {
	padding: 10px 14px;
	border-radius: 10px;
	margin: 12px 0;
}
.error { background: rgba(80,16,24,0.85); border: 1px solid #e88; color: #ffd6d6; }
.notice { background: rgba(40,36,16,0.85); border: 1px solid var(--gold-dim); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: 0.08em; }
#app { max-width: 720px; margin: 0 auto; padding: 8px 16px 96px; }
.screen { animation: fade 0.2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
[hidden] { display: none !important; }

.menu-account { text-align: center; margin: 1rem 0; }
.menu-account a { color: var(--gold); }
.menu-account a.button { color: #1a1404; }
#menu-guest { display: flex; flex-direction: column; align-items: center; gap: 10px; }
#menu-local-login { display: block; max-width: 36rem; line-height: 1.6; }
.avatar-sm, .avatar {
	border-radius: 50%;
	object-fit: cover;
	background: var(--bg-3);
	vertical-align: middle;
}
.avatar-sm { width: 32px; height: 32px; margin-right: 6px; }
#menu-actions { display: flex; flex-direction: column; gap: 12px; margin: 1.4rem 0; }
.join-form { display: flex; gap: 8px; }
.join-form input { flex: 1; text-transform: uppercase; letter-spacing: 0.12em; text-align: center; }

.games-list, .player-list, .cf-ranges, #jury-list { list-style: none; padding: 0; margin: 0; }
.games-list li, .player-list li, .game-card {
	display: flex; align-items: center; gap: 12px;
	background: var(--bg-2);
	border: 1px solid var(--line);
	border-radius: var(--radius);
	padding: 10px 12px;
	margin: 8px 0;
	min-height: var(--hit);
}
.games-list button { margin-left: auto; }
.player-dot {
	width: 14px; height: 14px; border-radius: 50%;
	flex-shrink: 0;
	box-shadow: 0 0 0 2px #0006;
}
.invite-row { display: flex; gap: 8px; margin: 8px 0 16px; flex-wrap: wrap; }
.invite-row input { flex: 1; min-width: 0; font-size: 0.82rem; letter-spacing: 0; }
.actions { display: flex; flex-direction: column; gap: 10px; margin: 1.2rem 0; }

.toggle {
	display: flex; gap: 12px; align-items: flex-start;
	padding: 12px 0;
	cursor: pointer;
}
.toggle input[type="checkbox"] {
	appearance: none;
	width: 48px; height: 28px; min-height: 28px;
	border-radius: 14px;
	background: #2a3148;
	border: 1px solid var(--line);
	position: relative;
	flex-shrink: 0;
	margin-top: 2px;
}
.toggle input[type="checkbox"]::after {
	content: "";
	position: absolute; top: 2px; left: 2px;
	width: 22px; height: 22px; border-radius: 50%;
	background: #c8cde0;
	transition: transform 0.15s ease;
}
.toggle input[type="checkbox"]:checked { background: var(--gold-dim); }
.toggle input[type="checkbox"]:checked::after { transform: translateX(20px); background: #fff8e0; }
.toggle small { display: block; margin-top: 4px; }
.field { margin: 0 0 12px 60px; }
.field input[type="number"] { max-width: 6rem; }

/* Cease-fire clock */
.cf-clock { display: flex; justify-content: center; margin: 12px 0; touch-action: none; }
.cf-clock svg { width: min(86vw, 320px); height: auto; touch-action: none; user-select: none; }
.cf-ranges li {
	display: flex; gap: 8px; align-items: center;
	padding: 8px 0;
}
.cf-ranges input[type="time"] { max-width: 8rem; min-height: 40px; }

/* Top bar / countdowns */
#top-bar {
	position: sticky; top: 0; z-index: 8;
	background: linear-gradient(180deg, #0c1020 70%, #0c102000);
	padding: 4px 0 12px;
	text-align: center;
}
#top-title { font-weight: 700; color: var(--gold); letter-spacing: 0.04em; }
.countdown { margin: 4px 0; }
.cd-label { display: block; font-size: 0.78rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.cd-hms {
	font-variant-numeric: tabular-nums;
	font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
	font-size: 1.85rem;
	font-weight: 700;
	letter-spacing: 0.06em;
	color: var(--gold);
}
#cf-countdown .cd-hms { color: #f0c0c0; font-size: 1.45rem; }
#cf-countdown.active .cd-hms { color: var(--danger); }
#top-meta { font-size: 0.9rem; color: var(--muted); min-height: 1.3em; }
#shrink-banner { color: #ffcc77; margin-left: 8px; }

#game-hud {
	display: flex; align-items: center; gap: 8px;
	flex-wrap: wrap;
	margin-bottom: 8px;
}
#you-stats { flex: 1; font-size: 0.95rem; }
#you-stats strong { color: var(--gold); }
.banner {
	background: rgba(40, 36, 16, 0.9);
	border: 1px solid var(--gold-dim);
	border-radius: 10px;
	padding: 8px 12px;
	text-align: center;
}

/* Board */
#board-wrap {
	display: flex; justify-content: center;
	margin: 8px 0 16px;
	touch-action: none;
}
#board {
	display: grid;
	gap: 3px;
	width: min(96vw, 560px, calc(100dvh - 290px));
	aspect-ratio: 1;
	grid-template-rows: repeat(var(--n, 6), 1fr);
	touch-action: none;
	user-select: none;
	-webkit-user-select: none;
}
.cell {
	position: relative;
	background: #1a2140;
	border-radius: 6px;
	border: 1px solid #2a3358;
	min-width: 0; min-height: 0;
	width: auto;
	height: auto;
	padding: 0;
	aspect-ratio: 1;
	cursor: pointer;
	display: flex; align-items: center; justify-content: center;
	touch-action: none;
	color: inherit;
}
button.cell {
	min-height: 0;
	min-width: 0;
	padding: 0;
	background: #1a2140;
	border-radius: 6px;
	font-weight: 400;
}
.cell.impassable { background: #0a0c14; border-color: #111; cursor: default; }
.cell.threat { box-shadow: inset 0 0 0 2px #e8a03a; background: #2a2210; }
.cell.move-target { box-shadow: inset 0 0 0 3px var(--ok); background: #163226; }
.cell.selected { box-shadow: inset 0 0 0 3px var(--gold); }
.cell.in-range { box-shadow: inset 0 0 0 2px #e85d5d88; }
.piece {
	width: 82%;
	height: 82%;
	max-width: 82%;
	max-height: 82%;
	aspect-ratio: 1;
	border-radius: 50%;
	border: 3px solid var(--c, #fff);
	box-shadow: 0 0 0 1px #0008, 0 2px 6px #0006;
	object-fit: cover;
	background: #222;
	pointer-events: none;
	flex-shrink: 0;
}
.piece.dead { filter: grayscale(1) brightness(0.7); opacity: 0.7; border-style: dashed; }
.avatar-wrap-fixed {
	display: inline-flex;
	flex-shrink: 0;
	align-items: center;
	justify-content: center;
	width: var(--sz, 40px);
	height: var(--sz, 40px);
}
.piece-fixed {
	width: var(--sz, 40px) !important;
	height: var(--sz, 40px) !important;
	max-width: none;
	max-height: none;
	border-width: 2px;
	font-size: calc(var(--sz, 40px) * 0.38);
}
/* Token: always a visible circle in the player's colour with initials;
   a profile picture (if it loads) is layered on top. See src/avatar.js. */
.piece-token {
	position: relative;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	background-color: var(--c, #888);
	box-sizing: border-box;
	line-height: 1;
	user-select: none;
}
.cell { container-type: inline-size; }
.piece-initials {
	display: block;
	font-weight: 800;
	font-size: 0.8em;
	font-size: 30cqw;
	letter-spacing: 0.02em;
	color: inherit;
	text-shadow: 0 1px 2px #0006;
	pointer-events: none;
}
.piece-fixed .piece-initials { font-size: calc(var(--sz, 40px) * 0.38); }
.piece-img {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	object-fit: cover;
	border-radius: 50%;
	pointer-events: none;
}
.piece.has-img .piece-initials { visibility: hidden; }
#menu-avatar { display: inline-flex; background: none; }

/* Timeline */
#timeline {
	position: sticky; bottom: 0;
	display: flex; align-items: center; gap: 8px;
	background: #0c1020ee;
	padding: 8px 0 calc(8px + 48px); /* room for lang picker */
	z-index: 7;
	touch-action: none;
}
#tl-play, #tl-live { flex-shrink: 0; }
#tl-play { width: var(--hit); padding: 0; font-size: 1.1rem; }
#tl-track {
	position: relative;
	flex: 1;
	height: 44px;
	background: var(--bg-2);
	border: 1px solid var(--line);
	border-radius: 8px;
	touch-action: none;
	cursor: pointer;
}
#tl-ticks { position: absolute; inset: 0; pointer-events: none; }
.tl-tick {
	position: absolute; top: 8px; bottom: 8px;
	width: 2px; background: var(--gold);
	transform: translateX(-1px);
}
#tl-head {
	position: absolute; top: 4px; bottom: 4px;
	width: 4px; background: #fff;
	border-radius: 2px;
	transform: translateX(-2px);
	pointer-events: none;
	box-shadow: 0 0 6px #fff8;
}

/* Jury */
#jury-panel {
	background: var(--bg-2);
	border: 1px solid var(--line);
	border-radius: var(--radius);
	padding: 12px;
	margin: 8px 0 16px;
}
#jury-list li { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
#jury-list button { margin-left: auto; }

/* Modal / sheet / settings */
.modal, .sheet, .settings-panel {
	position: fixed; inset: 0;
	background: #0008;
	z-index: 20;
	display: flex; align-items: flex-end; justify-content: center;
	padding: 16px 16px var(--safe-b);
	touch-action: none;
}
.modal { align-items: center; }
.modal-card, .sheet-card, .settings-panel {
	background: var(--bg-2);
	border: 1px solid var(--line);
	border-radius: 16px;
	padding: 20px 18px 16px;
	width: min(100%, 440px);
	max-height: min(88vh, 40rem);
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
	touch-action: pan-y;
	position: relative;
}
.settings-panel {
	position: fixed; inset: auto 16px var(--safe-b) 16px;
	margin: 0 auto;
	display: flex; flex-direction: column; gap: 10px;
	top: auto;
	background: var(--bg-2);
}
.modal-close {
	position: absolute; top: 8px; right: 8px;
	background: transparent; color: var(--ink);
	min-width: var(--hit); width: var(--hit);
}
.chat-log {
	max-height: 40vh; overflow-y: auto;
	touch-action: pan-y;
	display: flex; flex-direction: column; gap: 8px;
	margin: 8px 0;
}
.chat-bubble {
	max-width: 85%;
	padding: 8px 12px;
	border-radius: 12px;
	background: var(--bg-3);
}
.chat-bubble.mine { align-self: flex-end; background: #3a3420; }
.chat-compose { display: flex; gap: 8px; }
.opt-list { display: flex; flex-direction: column; gap: 8px; }
.opt-list button { width: 100%; }

/* Language picker — same pattern as Meteors */
.lang-picker {
	position: fixed;
	right: var(--safe-r);
	bottom: var(--safe-b);
	z-index: 15;
	display: flex;
	flex-direction: column-reverse;
	align-items: flex-end;
	gap: 8px;
}
.lang-current {
	appearance: none;
	border: 1px solid rgba(200, 210, 240, 0.35);
	background: rgba(16, 20, 40, 0.92);
	border-radius: 8px;
	padding: 6px 7px;
	cursor: pointer;
	line-height: 0;
	box-shadow: 0 4px 14px rgba(0,0,0,0.4);
	min-width: 52px;
	min-height: 40px;
	color: inherit;
}
.lang-current img, .lang-option img {
	display: block; width: 36px; height: 24px;
	object-fit: cover; border-radius: 3px;
	image-rendering: auto;
	box-shadow: 0 0 0 1px rgba(0,0,0,0.35);
}
.lang-current:focus-visible, .lang-option:focus-visible,
.lang-current[aria-expanded="true"] { border-color: var(--gold); outline: none; }
.lang-list {
	display: flex; flex-direction: column; gap: 4px;
	background: rgba(16, 14, 30, 0.96);
	border: 1px solid #5a5470;
	border-radius: 12px;
	padding: 8px;
	max-height: min(70vh, 28rem);
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
	touch-action: pan-y;
	box-shadow: 0 12px 32px rgba(0,0,0,0.45);
	min-width: 11.5rem;
}
.lang-list[hidden] { display: none !important; }
.lang-option {
	appearance: none;
	display: flex; align-items: center; gap: 10px;
	width: 100%;
	border: 1px solid transparent;
	background: transparent;
	color: var(--ink);
	font: inherit; font-size: 0.95rem;
	padding: 6px 8px;
	border-radius: 8px;
	cursor: pointer;
	text-align: left;
	min-height: 40px;
	white-space: nowrap;
}
.lang-option.current { border-color: var(--gold); background: rgba(40, 32, 16, 0.7); }

@media (min-width: 700px) {
	#app { padding-bottom: 80px; }
	#menu-actions { flex-direction: row; align-items: stretch; }
	.join-form { flex: 1; }
	.actions { flex-direction: row; }
	.modal { align-items: center; }
}
@media (prefers-reduced-motion: reduce) {
	.screen { animation: none; }
	.toggle input[type="checkbox"]::after { transition: none; }
}

/* Visibility options */
.vis-panel { margin: 16px 0 8px; }
.vis-panel h3 { font-size: 1.05rem; margin: 0 0 6px; }
.vis-field {
	border: 1px solid var(--line);
	border-radius: var(--radius);
	padding: 8px 12px 10px;
	margin: 10px 0;
	background: var(--bg-2);
}
.vis-field legend {
	font-weight: 650;
	padding: 0 6px;
	color: var(--ink);
}
.vis-opt {
	display: flex;
	align-items: center;
	gap: 10px;
	min-height: var(--hit);
	padding: 4px 4px;
	cursor: pointer;
	border-radius: 8px;
	-webkit-tap-highlight-color: transparent;
}
.vis-opt:hover, .vis-opt:focus-within { background: #ffffff0c; }
.vis-opt input[type="radio"] {
	width: 22px;
	height: 22px;
	accent-color: var(--gold);
	flex-shrink: 0;
	margin: 0;
}
.vis-opt input:disabled { cursor: not-allowed; }

/* Board effects (replay arrows / pulses) */
#board-wrap { position: relative; }
#board { position: relative; z-index: 1; overflow: visible; }
.cell { overflow: visible; }
.board-fx {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	pointer-events: none;
	z-index: 4;
	overflow: visible;
}
.piece.moving {
	position: relative;
	z-index: 5;
	will-change: transform;
}
.piece.range-pulse {
	animation: range-pulse 0.55s ease-out;
}
@keyframes range-pulse {
	0% { box-shadow: 0 0 0 0 rgba(232, 197, 71, 0.95), 0 0 0 1px #0008, 0 2px 6px #0006; }
	60% { box-shadow: 0 0 0 18px rgba(232, 197, 71, 0), 0 0 0 1px #0008, 0 2px 6px #0006; }
	100% { box-shadow: 0 0 0 0 rgba(232, 197, 71, 0), 0 0 0 1px #0008, 0 2px 6px #0006; }
}
.fx-float {
	position: absolute;
	z-index: 6;
	pointer-events: none;
	font-weight: 800;
	font-size: 1.25rem;
	font-variant-numeric: tabular-nums;
	color: #4ecb8d;
	text-shadow: 0 1px 4px #000c;
	transform: translate(-50%, -50%);
	animation: fx-rise 0.55s ease-out forwards;
}
@keyframes fx-rise {
	from { opacity: 1; transform: translate(-50%, -40%) scale(1); }
	to { opacity: 0.15; transform: translate(-50%, -140%) scale(1.25); }
}

/* Timeline: large touch target, ticks aligned with playhead (idx / n) */
#timeline { touch-action: none; user-select: none; -webkit-user-select: none; }
#tl-track {
	height: var(--hit);
	min-height: var(--hit);
	touch-action: none;
	cursor: pointer;
}

#tl-back, #tl-fwd { width: var(--hit); padding: 0; font-size: 1.4rem; font-weight: 700; }
.ap-time-field { margin-left: 0; }
.ap-time-field input[type="time"] { max-width: 10rem; }
#ap-time-wrap.field { margin-left: 0; padding: 8px 0 12px; }

.grid-field { margin-left: 0; padding: 8px 0 12px; }
.grid-size-row { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
.grid-size-row input[type="number"] { max-width: 5rem; }
.grid-times { opacity: 0.7; }
#grid-use-suggested { margin-top: 6px; }

#lobby-rules.readonly .toggle, #lobby-rules.readonly .field, #lobby-rules.readonly .vis-opt { opacity: 0.75; cursor: default; }
#lobby-rules.readonly input { cursor: default; }
#cf-clock svg { touch-action: none; -webkit-user-select: none; user-select: none; }
#cf-clock svg.dragging { cursor: grabbing; }
#cf-clock svg.readonly { cursor: default; }

.tz-label { display: block; opacity: 0.65; font-size: 0.8em; }

/* Game view: exactly one viewport tall (dvh = visible area, also with the
   mobile URL bar). Header, HUD, board and timeline are stacked in a flex
   column; the board takes the remaining space (container query) so the
   page never needs to scroll and the header cannot overlap the HUD. */
html.in-game, html.in-game body {
	height: 100%;
	height: 100dvh;
	min-height: 0;
	overflow: hidden;
	overscroll-behavior: none;
}
html.in-game body { padding-bottom: 0; box-sizing: border-box; }
html.in-game #app {
	box-sizing: border-box;
	height: 100%;
	display: flex;
	flex-direction: column;
	padding: 4px 12px calc(var(--safe-b) + 4px);
	overflow: hidden;
}
html.in-game #top-bar {
	position: static;
	flex: 0 0 auto;
	background: none;
	padding: 0 0 4px;
}
html.in-game #top-bar .countdown { margin: 0; }
html.in-game #top-bar .cd-hms { font-size: 1.5rem; }
html.in-game #game {
	flex: 1 1 auto;
	min-height: 0;
	display: flex;
	flex-direction: column;
	overflow-y: auto;
	overflow-x: hidden;
}
html.in-game #game-hud { flex: 0 0 auto; margin-bottom: 4px; }
html.in-game #game-hud button { min-height: 40px; }
html.in-game #board-wrap {
	flex: 1 1 0;
	min-height: 120px;
	margin: 4px 0;
	container-type: size;
	align-items: center;
}
html.in-game #board { width: min(100cqw, 100cqh, 560px); }
html.in-game #jury-panel { flex: 0 0 auto; margin: 4px 0; }
html.in-game #timeline {
	position: static;
	flex: 0 0 auto;
	padding: 4px 0;
	min-height: 48px;
}
html.in-game #tl-caption { flex: 0 0 auto; margin: 2px 0 0; }
/* In the game the language button sits in the empty top-right corner
   (beside the header) instead of over the timeline; its list opens down. */
html.in-game .lang-picker {
	top: calc(var(--safe-t) + 4px);
	bottom: auto;
	right: calc(var(--safe-r) + 4px);
	flex-direction: column;
}
html.in-game .lang-current { min-height: 34px; min-width: 44px; padding: 4px 5px; }
html.in-game .lang-current img { width: 30px; height: 20px; }
