:root {
  color-scheme: dark;
  font-family: Inter, Segoe UI, system-ui, sans-serif;
  --sidebar-width: 272px;
  --panel-scroll-height: 128px;
  --bg-app: #101115;
  --bg-canvas: #11131a;
  --text-primary: #f5f7fb;
  --text-secondary: #dce5f8;
  --text-muted: #9aa6b8;
  --text-dim: #aeb7c8;
  --surface: rgba(20, 22, 30, .88);
  --surface-solid: rgba(18, 21, 31, .96);
  --surface-chat: rgba(13, 16, 23, .98);
  --border: rgba(255, 255, 255, .12);
  --border-strong: rgba(255, 255, 255, .18);
  --shadow: rgba(0, 0, 0, .3);
  --input-bg: #191c25;
  --input-text: #f8fafc;
  --btn-text: #06101f;
  --btn-bg: #8ee6ff;
  --grid-line: rgba(255, 255, 255, .07);
  --grid-line-faint: rgba(255, 255, 255, .035);
  --overlay: rgba(8, 10, 15, .62);
  --minimap-bg: rgba(12, 14, 20, .88);
  background: var(--bg-app);
  color: var(--text-primary);
}

[data-theme="light"] {
  color-scheme: light;
  --bg-app: #eef2f7;
  --bg-canvas: #dbe3ef;
  --text-primary: #0f172a;
  --text-secondary: #1e293b;
  --text-muted: #64748b;
  --text-dim: #475569;
  --surface: rgba(255, 255, 255, .92);
  --surface-solid: rgba(255, 255, 255, .98);
  --surface-chat: rgba(255, 255, 255, .98);
  --border: rgba(15, 23, 42, .12);
  --border-strong: rgba(15, 23, 42, .2);
  --shadow: rgba(15, 23, 42, .12);
  --input-bg: #f8fafc;
  --input-text: #0f172a;
  --btn-text: #0f172a;
  --btn-bg: #38bdf8;
  --grid-line: rgba(15, 23, 42, .12);
  --grid-line-faint: rgba(15, 23, 42, .06);
  --overlay: rgba(241, 245, 249, .72);
  --minimap-bg: rgba(255, 255, 255, .92);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

#world {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: var(--bg-canvas);
  touch-action: none;
}

.ui-hidden {
  display: none !important;
}

.topbar {
  position: fixed;
  top: 12px;
  left: 12px;
  right: 12px;
  min-height: 52px;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  backdrop-filter: blur(14px);
  box-shadow: 0 10px 30px var(--shadow);
}

.topbar.ui-positioned {
  right: auto;
}

#sidebar.ui-sidebar-free {
  display: contents;
}

.sidebar .panel.ui-positioned,
.chat.ui-positioned {
  z-index: 7;
}

.sidebar .panel[data-ui],
.sidebar .panel.ui-positioned[data-ui] {
  max-height: var(--panel-scroll-height);
  contain: layout style paint;
}

.sidebar .panel.ui-positioned h2[data-layout-key],
.topbar[data-layout-key],
.chat-toggle[data-layout-key],
.shop-button[data-layout-key],
#minimap[data-layout-key] {
  user-select: none;
}

.mobile-menu-button {
  position: fixed;
  left: 12px;
  bottom: 172px;
  z-index: 9;
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  color: var(--text-primary);
  background: var(--surface);
  box-shadow: 0 10px 30px var(--shadow);
  font-size: 24px;
  line-height: 1;
}

#connection {
  margin-left: 10px;
  color: var(--text-dim);
  font-size: 13px;
}

.stats {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--text-secondary);
}

.stats span {
  min-width: 92px;
}

.sidebar {
  position: fixed;
  top: 12px;
  right: 12px;
  left: auto;
  width: var(--sidebar-width);
  bottom: auto;
  display: grid;
  grid-auto-rows: auto;
  gap: 10px;
}

.sidebar:not(.ui-sidebar-free) {
  z-index: 7;
}

.panel {
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-solid);
  padding: 12px;
  box-shadow: 0 10px 30px var(--shadow);
}

.panel h2 {
  margin: 0 0 8px;
  font-size: 14px;
  letter-spacing: 0;
  flex-shrink: 0;
}

.panel-hint {
  margin: -4px 0 8px;
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.35;
}

#leaderboard,
#strengthLeaderboard,
#alliances,
#pacts {
  margin: 0;
  height: calc(var(--panel-scroll-height) - 42px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  contain: strict;
}

#leaderboard,
#strengthLeaderboard {
  padding-left: 22px;
}

#leaderboard li,
#strengthLeaderboard li {
  margin: 6px 0;
  color: var(--text-secondary);
  overflow-wrap: anywhere;
}

.empty {
  color: var(--text-muted);
  font-size: 13px;
}

.alliance-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 6px 0;
}

.alliance-row button {
  padding: 5px 7px;
  color: #fff7ed;
  background: #7f1d1d;
  font-size: 12px;
}

.chat-toggle {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 11;
  min-width: 88px;
  color: var(--btn-text);
  background: var(--btn-bg);
  box-shadow: 0 10px 28px var(--shadow);
}

.chat-toggle.ui-positioned {
  bottom: auto;
}

.chat {
  position: fixed;
  left: 14px;
  bottom: 64px;
  z-index: 10;
  width: min(420px, calc(100vw - 28px));
  height: min(360px, calc(100vh - 170px));
  transform: none;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px;
  border-radius: 8px;
  background: var(--surface-chat);
  border: 1px solid var(--border);
}

.chat.ui-positioned {
  bottom: auto;
}

.chat-toggle.has-chat {
  background: #facc15;
}

.chat.hidden {
  display: none;
}

#chatLog {
  min-height: 0;
  height: 100%;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--input-bg);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text-secondary);
}

.chat-line {
  margin: 0 0 8px;
  overflow-wrap: anywhere;
}

.chat-line b {
  font-weight: 800;
  text-shadow: none;
  filter: none;
}

#chatForm {
  display: flex;
  gap: 8px;
  margin-top: 0;
}

#chatForm input {
  min-width: 0;
  flex: 1;
  height: 38px;
}

input {
  color: var(--input-text);
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 10px 11px;
  outline: none;
}

input:focus {
  border-color: #7dd3fc;
  box-shadow: 0 0 0 3px rgba(125, 211, 252, .18);
}

button {
  border: 0;
  border-radius: 7px;
  padding: 10px 12px;
  color: var(--btn-text);
  background: var(--btn-bg);
  font-weight: 700;
}

button:hover {
  filter: brightness(1.08);
}

button:disabled {
  cursor: not-allowed;
  filter: grayscale(0.6) brightness(0.7);
  opacity: 0.65;
}

button:disabled:hover {
  filter: grayscale(0.6) brightness(0.7);
}

.join-panel {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background:
    linear-gradient(rgba(11, 14, 22, .72), rgba(11, 14, 22, .78)),
    repeating-linear-gradient(0deg, transparent 0 23px, rgba(255, 255, 255, .06) 24px),
    repeating-linear-gradient(90deg, transparent 0 23px, rgba(255, 255, 255, .06) 24px);
}

.join-panel.hidden {
  display: none;
}

.join-card {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 28px 70px var(--shadow);
}

.join-card h1 {
  margin: 0;
  font-size: 34px;
  letter-spacing: 0;
}

.join-card p,
.join-card small {
  margin: 0;
  color: #b6c0d2;
}

.join-card label {
  display: grid;
  gap: 7px;
  color: var(--text-secondary);
}

#nationColor {
  width: 76px;
  height: 44px;
  padding: 4px;
}

.action-menu {
  position: fixed;
  z-index: 10;
  width: 210px;
  display: grid;
  gap: 7px;
  padding: 10px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 20px 50px var(--shadow);
}

.action-menu.hidden {
  display: none;
}

.action-menu strong {
  padding: 3px 4px 7px;
}

.action-menu button {
  text-align: left;
  color: var(--text-primary);
  background: var(--input-bg);
  font-weight: 600;
}

[data-theme="light"] .action-menu button {
  background: #e2e8f0;
}

.action-menu button:hover {
  filter: brightness(1.06);
}

#minimap {
  position: fixed;
  right: 12px;
  bottom: 16px;
  z-index: 7;
  width: 180px;
  height: 120px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--minimap-bg);
  box-shadow: 0 10px 30px var(--shadow);
}

#minimap.ui-positioned {
  right: auto;
  bottom: auto;
}

.toast-stack {
  position: fixed;
  left: 12px;
  bottom: 14px;
  z-index: 30;
  display: grid;
  gap: 8px;
  width: min(360px, calc(100vw - 24px));
}

.toast {
  padding: 11px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 12px 34px var(--shadow);
  color: var(--text-secondary);
}

.pause-menu,
.settings-menu {
  position: fixed;
  inset: 0;
  z-index: 25;
  display: grid;
  place-items: center;
  padding: 18px;
  background: var(--overlay);
  backdrop-filter: blur(8px);
}

.pause-menu.hidden,
.settings-menu.hidden {
  display: none;
}

.settings-menu {
  z-index: 26;
}

.pause-card,
.settings-card {
  width: min(460px, 100%);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 26px 70px var(--shadow);
  padding: 18px;
}

.settings-card {
  width: min(560px, 100%);
  max-height: min(90vh, 720px);
  overflow-y: auto;
}

.pause-card h2,
.settings-card h2 {
  margin: 0 0 12px;
  font-size: 24px;
  letter-spacing: 0;
}

.settings-section {
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.settings-section:last-of-type {
  border-bottom: 0;
  margin-bottom: 0;
  padding-bottom: 0;
}

.settings-section h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.settings-hint {
  margin: 0 0 10px;
  color: var(--text-muted);
  font-size: 12px;
}

.settings-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 18px;
}

.settings-checks {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
}

.settings-layout {
  display: grid;
  gap: 10px;
}

.settings-field {
  display: grid;
  gap: 6px;
}

.settings-field label,
.settings-check,
.settings-radio {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
}

.settings-field select {
  color: var(--input-text);
  background: var(--input-bg);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 9px 10px;
  font: inherit;
}

.settings-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
  flex-wrap: wrap;
}

.settings-secondary {
  color: var(--text-secondary);
  background: var(--input-bg);
}

.settings-layout-btn {
  width: 100%;
}

.layout-mode-bar {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 35;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border: 1px solid var(--btn-bg);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 8px 30px var(--shadow);
  color: var(--text-secondary);
  font-weight: 700;
  white-space: nowrap;
}

.layout-mode-bar.hidden {
  display: none;
}

.layout-mode-bar button {
  padding: 6px 12px;
  font-size: 13px;
}

body.layout-edit-mode [data-ui] {
  outline: 2px dashed rgba(142, 230, 255, .75);
  outline-offset: 2px;
}

body.layout-edit-mode .topbar[data-layout-key],
body.layout-edit-mode .chat-toggle[data-layout-key],
body.layout-edit-mode .shop-button[data-layout-key],
body.layout-edit-mode #minimap[data-layout-key],
body.layout-edit-mode [data-ui] h2[data-layout-key] {
  cursor: grab;
}

body.layout-edit-mode [data-layout-key]:active {
  cursor: grabbing;
}

body.layout-edit-mode [data-ui] h2[data-layout-key] {
  padding: 2px 4px;
  margin: -2px -4px 6px;
  border-radius: 4px;
  background: rgba(142, 230, 255, .12);
}

.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.menu-grid button {
  min-height: 42px;
}

.danger-button {
  grid-column: 1 / -1;
  color: #fff7ed;
  background: #b42335;
}

.danger-button:hover {
  background: #d12b41;
}

.toast-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.toast-actions button {
  padding: 7px 9px;
}

/* ── Sklep ────────────────────────────────── */
.shop-button {
  position: fixed;
  left: 12px;
  bottom: 116px;
  z-index: 9;
  min-width: 110px;
  color: #06101f;
  background: #fbbf24;
  box-shadow: 0 10px 28px var(--shadow);
  font-weight: 700;
}

.shop-button.ui-positioned {
  bottom: auto;
}

.shop-panel {
  position: fixed;
  left: 12px;
  bottom: 168px;
  z-index: 20;
  width: min(370px, calc(100vw - 24px));
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 20px 56px var(--shadow);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.shop-panel.ui-positioned {
  bottom: auto;
}

.shop-panel.hidden { display: none; }

.shop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.shop-header h2 { margin: 0; font-size: 15px; }

.shop-close {
  background: transparent;
  color: var(--text-dim);
  padding: 4px 8px;
  font-size: 16px;
  font-weight: 400;
}

.shop-items { display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow-y: auto; scrollbar-width: thin; padding-right: 2px; }

.shop-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: rgba(127, 127, 127, .06);
}

.mine-badge {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-weight: 900;
  font-size: 13px;
}

.mine-badge-1 { background: #14532d; color: #86efac; }
.mine-badge-2 { background: #713f12; color: #fde68a; }
.mine-badge-3 { background: #7f1d1d; color: #fca5a5; }

.missile-badge-1 { background: #1e3a5f; color: #93c5fd; }
.missile-badge-2 { background: #3b1f6e; color: #d8b4fe; }
.missile-badge-3 { background: #4a0404; color: #ff8080; border: 1px solid #ff4444; }

.expand-badge-1 { background: #0f2e4a; color: #67e8f9; }
.expand-badge-2 { background: #1a1f5e; color: #a5b4fc; }
.expand-badge-3 { background: #1f3a1f; color: #86efac; }

.shop-buy-expand {
  background: #0891b2;
  color: #fff;
}
.shop-buy-expand:hover {
  background: #0ea5e9;
}

.shop-buy-missile {
  background: #c0392b;
  color: #fff;
}
.shop-buy-missile:hover {
  background: #e74c3c;
}

.shop-divider {
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  color: #ff9944;
  padding: 6px 0 2px;
  letter-spacing: 0.03em;
  border-top: 1px solid rgba(255,255,255,0.1);
  margin-top: 2px;
}

.shop-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
  color: var(--text-secondary);
}

.shop-info strong { font-size: 14px; color: var(--text-primary); }

.shop-buy {
  flex-shrink: 0;
  padding: 8px 12px;
  font-size: 13px;
  color: #06101f;
  background: #fbbf24;
  font-weight: 700;
}

/* ── Pasek trybu miny ────────────────────── */
.mine-mode-bar {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 18;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border: 1px solid #fbbf24;
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 8px 30px var(--shadow);
  color: #ca8a04;
  font-weight: 700;
  white-space: nowrap;
}

.mine-mode-bar.hidden { display: none; }

.mine-mode-bar button {
  padding: 6px 10px;
  background: #7f1d1d;
  color: #fca5a5;
  font-size: 13px;
}

.tutorial-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
  background: var(--overlay);
  backdrop-filter: blur(6px);
}

.tutorial-overlay.hidden {
  display: none;
}

.tutorial-card {
  width: min(520px, 100%);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface-solid);
  box-shadow: 0 26px 70px var(--shadow);
  padding: 20px;
}

.tutorial-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.tutorial-header h2 {
  margin: 0;
  font-size: 22px;
}

.tutorial-step {
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 700;
}

.tutorial-card p {
  margin: 0 0 16px;
  color: var(--text-secondary);
  line-height: 1.55;
  font-size: 15px;
}

.tutorial-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tutorial-skip {
  margin-left: auto;
  color: var(--text-secondary);
  background: var(--input-bg);
}

@media (max-width: 860px) {
  :root {
    --panel-scroll-height: 108px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .sidebar:not(.ui-sidebar-free) {
    top: auto;
    right: 12px;
    bottom: 204px;
    width: 210px;
  }

  .sidebar .chat {
    display: grid;
  }

  .sidebar .chat.hidden {
    display: none;
  }

  .chat {
    left: 10px;
    right: 10px;
    bottom: 62px;
    width: auto;
    height: min(330px, calc(100vh - 150px));
    transform: none;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 6px;
    padding: 10px;
  }

  .chat-toggle {
    left: 10px;
    bottom: 10px;
  }

  #minimap {
    width: 140px;
    height: 96px;
    bottom: 204px;
  }

  .mobile-menu-button {
    bottom: 204px;
  }

  .settings-checks {
    grid-template-columns: 1fr;
  }
}