:root {
  --bg-app: #0f241d;
  --bg-surface: #163329;
  --bg-elevated: #1f4538;
  --text-main: #f1f5f9;
  --text-muted: #b7d3c6;
  --accent: #f59e0b;
  --accent-fg: #0b1914;
  --border-subtle: rgba(255, 255, 255, 0.14);
  --ok: #10b981;
  --danger: #ef4444;
  --radius-lg: 20px;
  --radius-md: 14px;
}

body[data-theme="parchment"] {
  --bg-app: #2c1c10;
  --bg-surface: #3d2717;
  --bg-elevated: #523520;
  --text-main: #fdf6e7;
  --text-muted: #e3cca4;
  --accent: #e09f3e;
  --accent-fg: #231409;
  --border-subtle: rgba(242, 227, 194, 0.22);
}

body[data-theme="forest"] {
  --bg-app: #0b1d17;
  --bg-surface: #132e25;
  --bg-elevated: #1c4235;
  --text-main: #ecfdf5;
  --text-muted: #a7f3d0;
  --accent: #fbbf24;
  --accent-fg: #081c15;
  --border-subtle: rgba(167, 243, 208, 0.18);
}

body[data-theme="spy"] {
  --bg-app: #050d17;
  --bg-surface: #0c1929;
  --bg-elevated: #142840;
  --text-main: #e0f2fe;
  --text-muted: #7dd3fc;
  --accent: #06b6d4;
  --accent-fg: #020617;
  --border-subtle: rgba(56, 189, 248, 0.22);
}

body[data-theme="winter"] {
  --bg-app: #0f1f36;
  --bg-surface: #172e4d;
  --bg-elevated: #214069;
  --text-main: #f8fafc;
  --text-muted: #bfdbfe;
  --accent: #f43f5e;
  --accent-fg: #ffffff;
  --border-subtle: rgba(191, 219, 254, 0.22);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg-app);
  color: var(--text-main);
  line-height: 1.45;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overflow: hidden;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

/* Native Dialog Top-Layer & Scroll Containment (Modern Web Guidance) */
dialog:modal {
  overscroll-behavior: contain;
  margin: auto;
  width: min(94vw, 460px);
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--bg-surface);
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: 0 24px 50px rgba(0, 0, 0, 0.6);
}

dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgba(0, 0, 0, 0.68);
  backdrop-filter: blur(3px);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Views */
.view {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.view.is-active {
  display: flex;
}

/* --- View 1: Lobby / Offline Prep --- */
.lobby-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.card {
  width: 100%;
  max-width: 460px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
}

.brand-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 8px;
}

.lobby-title {
  font-size: 1.55rem;
  font-weight: 800;
  margin-bottom: 6px;
}

.muted {
  color: var(--text-muted);
  font-size: 0.94rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 14px;
}

.field label {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text-muted);
}

.input, .select {
  width: 100%;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 1rem;
  color: var(--text-main);
}

.input:focus, .select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.checklist {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 14px;
}

.checklist li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.22);
  font-size: 0.9rem;
}

.status-pill {
  font-size: 0.78rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  white-space: nowrap;
}

.status-pill.is-ok {
  background: rgba(16, 185, 129, 0.22);
  color: #6ee7b7;
}

.status-pill.is-warn {
  background: rgba(245, 158, 11, 0.22);
  color: #fcd34d;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 13px 18px;
  font-weight: 800;
  font-size: 1rem;
  transition: transform 0.08s ease, opacity 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: var(--accent-fg);
}

.btn-secondary {
  background: var(--bg-elevated);
  border-color: var(--border-subtle);
  color: var(--text-main);
}

.btn-ghost {
  background: transparent;
  border-color: var(--border-subtle);
  color: var(--text-muted);
}

.btn-sm {
  padding: 8px 12px;
  font-size: 0.86rem;
  border-radius: 10px;
}

/* --- View 2: Active Game Layout --- */
.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 10;
}

.header-team {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.team-label {
  font-weight: 800;
  font-size: 0.96rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.progress-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.header-stats {
  display: flex;
  align-items: center;
  gap: 8px;
}

.timer-badge {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 1.05rem;
  padding: 5px 10px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--border-subtle);
}

.net-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--ok);
  display: inline-block;
}

.net-dot.is-offline {
  background: var(--accent);
}

/* Map stage */
.map-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow: hidden;
}

#questMapCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* Floating HUD on map */
.map-top-chips {
  position: absolute;
  top: 10px;
  left: 54px;
  right: 60px;
  display: flex;
  justify-content: flex-start;
  pointer-events: none;
}

.hud-chip {
  pointer-events: auto;
  max-width: 100%;
  background: rgba(10, 20, 18, 0.84);
  backdrop-filter: blur(6px);
  color: #f8fafc;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.76rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.hud-chip-ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.map-zoom-controls {
  position: absolute;
  right: 10px;
  top: 10px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.fab-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  background: rgba(12, 24, 20, 0.84);
  color: #ffffff;
  font-weight: 800;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.fab-btn.is-active {
  background: var(--accent);
  color: var(--accent-fg);
}

/* Target Guidance Sheet above Bottom Toolbar */
.target-card {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  padding: 12px 14px;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.target-info {
  min-width: 0;
  flex: 1;
}

.target-kicker {
  font-size: 0.74rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
}

.target-name {
  font-size: 1rem;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.target-clue {
  font-size: 0.82rem;
  color: var(--text-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.target-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}

/* Bottom Toolkit Navigation */
.toolkit-bar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
  background: var(--bg-surface);
  border-top: 1px solid var(--border-subtle);
  z-index: 10;
}

.tool-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 0.73rem;
  font-weight: 700;
  color: var(--text-muted);
}

.tool-btn:active, .tool-btn.is-selected {
  background: var(--bg-elevated);
  color: var(--accent);
  border-color: var(--border-subtle);
}

.tool-icon {
  font-size: 1.25rem;
  line-height: 1;
}

/* Dialog Internals */
.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.dialog-title {
  font-size: 1.25rem;
  font-weight: 800;
}

.close-icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: rgba(0, 0, 0, 0.25);
  font-size: 1.1rem;
  line-height: 1;
  flex-shrink: 0;
}

/* Task / Checkpoint Modal */
.story-box {
  background: rgba(0, 0, 0, 0.24);
  border-left: 3px solid var(--accent);
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 0.95rem;
  margin-bottom: 14px;
}

.question-title {
  font-size: 1.05rem;
  font-weight: 800;
  margin-bottom: 10px;
}

.choices-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.choice-btn {
  text-align: left;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}

.choice-btn.is-picked {
  outline: 2px solid var(--accent);
  background: rgba(245, 158, 11, 0.18);
}

.feedback-banner {
  min-height: 24px;
  font-size: 0.9rem;
  font-weight: 700;
  margin: 8px 0;
}

.feedback-banner.is-err { color: #fca5a5; }
.feedback-banner.is-ok { color: #6ee7b7; }

.hints-section {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hint-item {
  background: rgba(0, 0, 0, 0.26);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.88rem;
}

/* Compass Dial */
.compass-dial-wrap {
  position: relative;
  width: 230px;
  height: 230px;
  margin: 12px auto 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.compass-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 4px solid var(--accent);
  background: radial-gradient(circle, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.55) 100%);
  transform: rotate(calc(var(--dial-rot, 0) * 1deg));
  transition: transform 0.12s linear;
}

.cardinal-mark {
  position: absolute;
  font-weight: 900;
  font-size: 1rem;
}
.cardinal-n { top: 8px; left: 50%; transform: translateX(-50%); color: #ef4444; }
.cardinal-e { right: 10px; top: 50%; transform: translateY(-50%); }
.cardinal-s { bottom: 8px; left: 50%; transform: translateX(-50%); }
.cardinal-w { left: 10px; top: 50%; transform: translateY(-50%); }

.target-needle {
  position: relative;
  z-index: 2;
  width: 12px;
  height: 130px;
  transform: rotate(calc(var(--target-rot, 0) * 1deg));
  transition: transform 0.15s ease-out;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.target-needle::before {
  content: '';
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 58px solid var(--accent);
}

.target-needle::after {
  content: '';
  width: 4px;
  flex: 1;
  background: rgba(255, 255, 255, 0.35);
}

/* Magnifier & QR Camera Viewport */
.camera-viewport {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: 16px;
  overflow: hidden;
  border: 2px solid var(--border-subtle);
  margin-bottom: 12px;
}

.camera-viewport video,
.camera-viewport canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(var(--lupa-scale, 1));
  transform-origin: center center;
  transition: transform 0.08s ease-out;
}

.camera-viewport[data-filter="contrast"] video,
.camera-viewport[data-filter="contrast"] canvas {
  filter: contrast(1.85) saturate(1.25);
}

.camera-viewport[data-filter="red"] video,
.camera-viewport[data-filter="red"] canvas {
  filter: grayscale(1) contrast(2.1) sepia(1) hue-rotate(-50deg) saturate(5);
}

.camera-viewport[data-filter="uv"] video,
.camera-viewport[data-filter="uv"] canvas {
  filter: invert(1) hue-rotate(180deg) contrast(1.5);
}

.qr-reticle {
  position: absolute;
  inset: 18%;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  pointer-events: none;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.32);
}

.tool-controls-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

/* --- View 3: Finale / Diploma --- */
.finale-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.verify-code-box {
  margin: 14px 0;
  padding: 16px;
  background: rgba(0, 0, 0, 0.38);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  text-align: center;
}

.verify-code-value {
  font-family: ui-monospace, 'Cascadia Code', Consolas, monospace;
  font-size: 1.55rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin: 6px 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.stat-cell {
  background: rgba(0, 0, 0, 0.24);
  padding: 10px 8px;
  border-radius: 12px;
  text-align: center;
}

.stat-val {
  font-size: 1.15rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat-lbl {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.toast {
  position: fixed;
  bottom: 76px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(10, 18, 15, 0.95);
  color: #fff;
  border: 1px solid var(--accent);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 700;
  z-index: 50;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  transition: opacity 0.2s ease;
}

.toast[hidden] {
  display: none;
}

/* --- Visual GPS Signal Alert Banner (Weak / Lost / Denied) --- */
.signal-alert-banner {
  position: absolute;
  top: 48px;
  left: 10px;
  right: 60px;
  z-index: 8;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
}

.signal-alert-banner[data-state="weak"] {
  background: rgba(180, 83, 9, 0.92);
  border: 1px solid #fbbf24;
  color: #fffbeb;
}

.signal-alert-banner[data-state="lost"] {
  background: rgba(153, 27, 27, 0.92);
  border: 1px solid #f87171;
  color: #fef2f2;
}

.signal-alert-banner[data-state="denied"] {
  background: rgba(127, 29, 29, 0.95);
  border: 1px solid #ef4444;
  color: #ffffff;
}

/* --- Debug Tables (text left, numbers right) --- */
.debug-table-wrap {
  max-height: 210px;
  overflow-y: auto;
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  margin-top: 8px;
}

.debug-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.76rem;
}

.debug-table th,
.debug-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border-subtle);
  text-align: left;
}

.debug-table th.num,
.debug-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Photo Attachment Preview inside Post Modal */
.photo-proof-box {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px dashed var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.photo-thumb {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid var(--accent);
  flex-shrink: 0;
}

/* --- Responsive & Landscape Adaptability --- */
@media (max-width: 360px) {
  .lobby-title { font-size: 1.3rem; }
  .card { padding: 14px; }
  .btn { padding: 11px 12px; font-size: 0.92rem; }
  .target-card { padding: 9px 10px; }
  .target-name { font-size: 0.92rem; }
  .fab-btn { width: 36px; height: 36px; font-size: 1rem; }
  .tool-btn { font-size: 0.68rem; padding: 5px 2px; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .game-header {
    padding: 5px 12px;
  }
  .target-card {
    left: 10px;
    right: auto;
    width: min(56vw, 360px);
    bottom: 8px;
    padding: 8px 11px;
  }
  .map-zoom-controls {
    flex-direction: row;
    top: 8px;
    right: 8px;
  }
  .signal-alert-banner {
    top: 44px;
    max-width: 60vw;
  }
  .toolkit-bar {
    padding: 4px 8px;
  }
  .tool-btn {
    flex-direction: row;
    justify-content: center;
    gap: 6px;
    padding: 5px 6px;
  }
}
