:root {
  --bg: #12131a;
  --bg2: #1c1e29;
  --card: #252838;
  --text: #f3f4f8;
  --muted: #9aa0b8;
  --x: #ff5d73;
  --o: #3fc1ff;
  --accent: #ffd34d;
  --radius: 20px;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

a { color: inherit; text-decoration: none; }

/* ---------- Портал ---------- */
.portal-header {
  text-align: center;
  padding: 28px 16px 8px;
}
.portal-header h1 {
  margin: 0;
  font-size: clamp(28px, 7vw, 44px);
  letter-spacing: .5px;
}
.portal-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 16px;
}

.games {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  padding: 20px 16px 32px;
  max-width: 1000px;
  margin: 0 auto;
}
@media (min-width: 700px) { .games { grid-template-columns: repeat(3, 1fr); gap: 24px; padding: 28px 24px; } }
@media (min-width: 1100px) { .games { grid-template-columns: repeat(4, 1fr); } }

.tile {
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 6px 20px rgba(0,0,0,.35);
  transition: transform .12s ease;
}
.tile:active { transform: scale(.96); }
.tile img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: var(--bg2);
}
.tile .title {
  padding: 12px 10px 14px;
  text-align: center;
  font-weight: 700;
  font-size: clamp(15px, 4vw, 20px);
}
.tile .tag {
  display: block;
  margin-top: 2px;
  font-weight: 500;
  font-size: 12px;
  color: var(--muted);
}
.tile.soon { opacity: .45; pointer-events: none; }

/* ---------- Спільне для ігор ---------- */
.game-page {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
}
.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  font-size: 16px;
  min-height: 48px;
  padding: 0 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:active { transform: scale(.95); background: #31354a; }
.btn.primary { background: var(--accent); color: #1a1a1a; }
.btn.primary:active { background: #e6bd3f; }

/* ---------- Перемикач режиму ---------- */
.modebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 14px 16px 0;
}
.segmented {
  display: flex;
  background: var(--bg2);
  border-radius: 16px;
  padding: 4px;
  width: 100%;
  max-width: 520px;
}
.segmented button {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  font-size: clamp(14px, 3.8vw, 17px);
  min-height: 48px;
  border-radius: 12px;
  padding: 0 8px;
}
.segmented button.on { background: var(--card); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.3); }
.whoami { color: var(--muted); font-size: 15px; min-height: 22px; }
.whoami b { color: var(--text); }
.whoami button {
  appearance: none; border: 0; background: none; color: var(--accent);
  font: inherit; font-weight: 700; padding: 6px 4px;
}

/* ---------- Статус під плиткою ---------- */
.tile .status-line {
  display: block;
  margin-top: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile .status-line.waiting { color: #4ade80; }
.tile .status-line:empty { display: none; }

/* ---------- Модальне вікно / оверлей ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(10, 11, 16, .82);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.overlay[hidden] { display: none; }
.dialog {
  background: var(--card);
  border-radius: 24px;
  padding: 24px 20px 20px;
  width: 100%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.dialog h2 { margin: 0 0 8px; font-size: 22px; }
.dialog p { margin: 0 0 16px; color: var(--muted); }
.dialog input {
  width: 100%;
  font: inherit;
  font-size: 20px; /* ≥16px, щоб iOS не збільшувала сторінку */
  padding: 14px 16px;
  border-radius: 14px;
  border: 2px solid #3a3f58;
  background: var(--bg2);
  color: var(--text);
  outline: none;
  -webkit-user-select: text;
  user-select: text;
  margin-bottom: 14px;
}
.dialog input:focus { border-color: var(--accent); }
.dialog .row { display: flex; gap: 10px; }
.dialog .row .btn { flex: 1; }
.spinner {
  width: 44px; height: 44px; margin: 4px auto 16px;
  border: 5px solid #3a3f58; border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- PWA ---------- */
.pwa-banner {
  position: fixed; z-index: 100;
  left: 50%; transform: translateX(-50%);
  bottom: calc(12px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 2px solid var(--accent);
  border-radius: 18px; padding: 8px 8px 8px 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
  font-weight: 700; white-space: nowrap;
  max-width: calc(100vw - 24px);
}
.pwa-banner .btn { min-height: 42px; }
.pwa-banner .close { background: transparent; padding: 0 10px; color: var(--muted); }
.install-btn { margin-top: 12px; }
.install-btn[hidden] { display: none; }
.steps { text-align: left; margin: 0 0 16px; padding-left: 22px; color: var(--text); line-height: 1.6; }
