:root {
  --unit-bg: #f2f4f7;
  --unit-selected: #8ec5ff;
  --unit-error: #ff9e9e;
  --unit-done: #b8f5bf;
  --app-bg: #fff;
  --text-main: #0f172a;
  --panel-bg: #e5e7eb;
  --board-bg: #eef2f7;
  --unit-font-size: 12px;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Arial, sans-serif;
  background: var(--app-bg);
  color: var(--text-main);
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: #1f2937;
  color: #fff;
  padding: 10px 14px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
}
.top-left { justify-self: start; }
.game-brand-title { font-weight: 700; margin-right: 12px; }
.top-center { justify-self: center; font-size: 20px; font-weight: 700; }
.top-right { justify-self: end; }
.stats {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}
.attempts-stat { margin-left: 20px; }

#changeLevelBtn {
  position: relative;
  z-index: 2;
  pointer-events: auto;
}
#changeLevelBtn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
button {
  border: 0;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.tabs {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--panel-bg);
}
.tabs-left { display: flex; gap: 8px; align-items: center; }
.tabs-right { display: flex; gap: 10px; align-items: center; margin-left: auto; }
.player-name-tab {
  min-width: 120px;
  text-align: right;
  font-size: 14px;
  color: #0f172a;
}
.tab { background: #cbd5e1; }
.tab.active { background: #1f2937; color: #fff; }

.selected-panel {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 4px 8px;
  max-width: min(60vw, 780px);
}
.selected-label { font-size: 12px; color: #475569; }
.selected-unit-text {
  font-size: 13px;
  font-weight: 700;
  color: #0f172a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: min(56vw, 700px);
}
.clear-selection {
  padding: 2px 8px;
  background: #ef4444;
  color: #fff;
  font-weight: 700;
}
.view { display: none; }
.view.active { display: block; }
#gameView { position: relative; }
#gameView.tutorial-mode #boardWrap { margin-left: 360px; }
.tutorial-info-panel {
  position: fixed;
  left: 10px;
  top: 118px;
  width: 330px;
  max-height: calc(100vh - 130px);
  overflow: auto;
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
  z-index: 40;
  padding: 12px;
  font-size: 13px;
  line-height: 1.35;
}
.tutorial-info-panel h3 { margin: 0 0 8px; }
.tutorial-info-panel p { margin: 0 0 10px; }
#boardWrap {
  position: relative;
  overflow: auto;
  height: calc(100vh - 108px);
  background: var(--board-bg);
  cursor: default;
}
#boardWrap.is-grabbing {
  cursor: default;
}
#board {
  position: relative;
  min-width: 3800px;
  min-height: 3800px;
  padding: 24px;
  z-index: 3;
  background-image:
    repeating-linear-gradient(to right, rgba(148, 163, 184, 0.16) 0, rgba(148, 163, 184, 0.16) 1px, transparent 1px, transparent calc(100% / 8)),
    repeating-linear-gradient(to bottom, rgba(148, 163, 184, 0.16) 0, rgba(148, 163, 184, 0.16) 1px, transparent 1px, transparent calc(100% / 8));
}

#boardGridOverlay {
  display: none;
}

.grid-top-labels,
.grid-left-labels {
  display: none;
}

.unit {
  position: absolute;
  min-width: 70px;
  min-height: 34px;
  background: var(--unit-bg);
  border: 1px solid #7f8ea3;
  border-radius: 8px;
  padding: 6px;
  font-size: var(--unit-font-size);
  line-height: 1.2;
  cursor: pointer;
  overflow: visible;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  z-index: 6;
}

.unit.selected { background: var(--unit-selected); }
.unit.error { background: var(--unit-error); }
.unit.grouped { border-width: 2px; }
.unit.done { background: var(--unit-done); border-color: #5fb96c; }
.unit .line { display: block; white-space: pre-line; word-break: normal; overflow-wrap: normal; }
.unit .line.category { font-weight: 700; color: #1f2937; }
.unit .line.sub { font-size: 11px; color: #374151; font-weight: 500; }
.unit.grouped .line.sub { font-weight: 700; }
.unit .count { color: #d11a2a; font-weight: 700; }
.unit.done .line.sub { display: none; }
.unit[title] { text-decoration: none; }

.unit-info-btn {
  position: absolute;
  left: 50%;
  bottom: -24px;
  transform: translateX(-50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #0f172a;
  background: #fff;
  color: #0f172a;
  font-size: 15px;
  font-weight: 800;
  line-height: 1;
  padding: 0;
  z-index: 40;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.25);
}

.unit-info-panel {
  position: fixed;
  top: 118px;
  right: 0;
  width: min(360px, 36vw);
  max-height: calc(100vh - 130px);
  overflow: auto;
  background: #ffffff;
  border-left: 1px solid #cbd5e1;
  box-shadow: -8px 0 20px rgba(15, 23, 42, 0.16);
  z-index: 58;
  padding: 12px 14px;
}

.merge-info-panel {
  z-index: 57;
}

.unit-info-hide {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 18px;
  font-weight: 700;
  background: #e2e8f0;
  color: #0f172a;
}

.unit-info-panel h3 { margin: 0 0 10px; }
.unit-info-image {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  margin-bottom: 10px;
}
.unit-info-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.4;
}
.unit-info-text a { color: #2563eb; }

#dashboardView { padding: 16px; gap: 14px; }
#dashboardView.view.active { display: grid; }
#lkView { padding: 16px; gap: 14px; }
#lkView.view.active { display: grid; }
.dashboard-card {
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 10px;
  padding: 12px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  border-bottom: 1px solid #e5e7eb;
  padding: 8px;
  text-align: left;
}

.rank-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  gap: 12px;
}

.rank-box {
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 10px 12px;
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rank-box-label {
  font-size: 13px;
  color: #475569;
}

.rank-box b {
  font-size: 22px;
}

.leaderboard-current-player td {
  background: #ecfeff;
  font-weight: 700;
  border-top: 2px solid #0891b2;
  border-bottom: 2px solid #0891b2;
}

body.night-mode .rank-box {
  background: #111827;
  border-color: #334155;
}

body.night-mode .rank-box-label {
  color: #94a3b8;
}

body.night-mode .leaderboard-current-player td {
  background: #082f49;
  border-top-color: #22d3ee;
  border-bottom-color: #22d3ee;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0,0,0,.6);
  z-index: 100;
}
.hidden { display: none; }
.modal-content {
  background: #fff;
  padding: 24px;
  border-radius: 12px;
  text-align: center;
  min-width: 280px;
}
.modal-content.auth {
  text-align: left;
  width: 360px;
}

.modal-title-centered {
  text-align: center;
  margin: -10px 0 14px;
}

.first-entry-title {
  margin-top: -14px;
}

.first-entry-text {
  margin: 0 0 10px;
  line-height: 1.45;
  font-size: 12px;
  text-align: justify;
}

.first-entry-recommendation {
  display: block;
  margin-top: 8px;
  line-height: 1.6;
}

.night-toggle {
  flex-direction: row !important;
  align-items: center;
  gap: 8px !important;
}

body.night-mode {
  --app-bg: #0b1020;
  --text-main: #e5e7eb;
  --panel-bg: #1f2937;
  --board-bg: #111827;
  --unit-bg: #1f2937;
  --unit-selected: #1d4ed8;
  --unit-error: #7f1d1d;
  --unit-done: #14532d;
}

body.night-mode .dashboard-card,
body.night-mode .modal-content,
body.night-mode .selected-panel,
body.night-mode .unit-info-panel,
body.night-mode .tutorial-info-panel {
  background: #111827;
  color: #e5e7eb;
  border-color: #334155;
}

body.night-mode .tab { background: #334155; color: #e5e7eb; }
body.night-mode .tab.active { background: #0f172a; }
body.night-mode .selected-label,
body.night-mode .unit .line.sub,
body.night-mode .achievement-legend-text,
body.night-mode .score-float-meta { color: #cbd5e1; }
body.night-mode .player-name-tab { color: #e5e7eb; }
body.night-mode .unit .line.category,
body.night-mode .selected-unit-text,
body.night-mode .unit-info-hide,
body.night-mode .unit-info-btn { color: #e5e7eb; }
body.night-mode .unit-info-hide,
body.night-mode .unit-info-btn { background: #1f2937; border-color: #64748b; }
body.night-mode #miniMapCanvas { background: #020617; border-color: #475569; }
#authForm {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#authForm label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}
#authForm input,
#authForm select {
  padding: 8px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}

.settings-title {
  text-align: center;
  margin: -6px 0 10px;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.setting-label {
  font-size: 14px;
}

.settings-form input[type="range"] {
  width: 100%;
}

.single-line-toggle {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

.auth-error {
  min-height: 18px;
  color: #b91c1c;
  font-size: 13px;
  margin: 0;
}


.score-fx-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 120;
}

.score-float {
  position: fixed;
  transform: translate(-50%, -50%);
  display: inline-flex;
  align-items: center;
  gap: 20px;
  text-align: center;
  opacity: 0;
  filter: contrast(0%);
  animation: scoreFloat 3s ease-out forwards;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.score-float-text {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.score-float-achievement {
  display: flex;
  align-items: center;
  justify-content: center;
}

.score-float-achievement .ach-glyph {
  font-size: 28px;
  line-height: 1;
}

.score-float.positive .score-float-value {
  color: #16a34a;
}

.score-float.negative .score-float-value {
  color: #dc2626;
}

.score-float-value {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.1;
}

.score-float-meta {
  font-size: 12px;
  font-weight: 500;
  color: #111827;
  line-height: 1.2;
  white-space: pre-line;
}

@keyframes scoreFloat {
  0% {
    opacity: 0;
    filter: contrast(0%);
    transform: translate(-50%, -50%) translateY(0);
  }
  33% {
    opacity: 1;
    filter: contrast(100%);
    transform: translate(-50%, -50%) translateY(-26px);
  }
  66% {
    opacity: 1;
    filter: contrast(100%);
    transform: translate(-50%, -50%) translateY(-50px);
  }
  100% {
    opacity: 0;
    filter: contrast(0%);
    transform: translate(-50%, -50%) translateY(-78px);
  }
}


.mini-map {
  position: fixed;
  top: 118px;
  right: 14px;
  width: 240px;
  background: rgba(17, 24, 39, 0.55);
  color: #fff;
  border-radius: 10px;
  padding: 8px;
  z-index: 60;
  user-select: none;
  transition: transform 0.25s ease;
}
.mini-map.collapsed {
  top: auto !important;
  bottom: 14px;
  width: auto;
  padding-bottom: 6px;
}
.mini-map.collapsed #miniMapCanvas { display: none; }
.mini-map-title { font-size: 12px; margin-bottom: 6px; cursor: move; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.mini-map.collapsed .mini-map-title { margin-bottom: 0; }
.mini-map-toggle {
  margin-left: 8px;
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
  padding: 2px 8px;
}
#miniMapCanvas {
  width: 220px;
  height: 150px;
  background: #111827;
  border: 1px solid #374151;
  border-radius: 6px;
  cursor: pointer;
}
.confirm-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 12px;
}


.auth-mode-toggle {
  display: flex;
  gap: 8px;
}
.auth-mode-toggle .tab { flex: 1; }
#emailField.hidden { display: none !important; }


.achievements-grid {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-start;
  gap: 5px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.achievement-card {
  padding: 4px 2px;
  background: transparent;
  border: 0;
  min-width: 74px;
}
.achievement-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 66px;
}
.achievement-count {
  font-weight: 700;
  min-width: 34px;
  text-align: left;
  margin-top: 0;
  line-height: 1;
  align-self: center;
}

.ach-glyph {
  display: inline-block;
  font-weight: 800;
  line-height: 1;
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  -webkit-text-fill-color: transparent;
}
.achievement-card .ach-glyph { font-size: 60px; transform: translateY(-8px); }
.dashboard-ach-item {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin-right: 8px;
  font-weight: 700;
}
.dashboard-ach-item .ach-glyph { font-size: 18px; }

.ach-open_category { background-image: linear-gradient(135deg,#0ea5e9,#6366f1); }
.ach-open_all_categories { background-image: linear-gradient(135deg,#f59e0b,#ef4444); }
.ach-collect_category { background-image: linear-gradient(135deg,#22c55e,#14b8a6); }
.ach-finish_game { background-image: linear-gradient(135deg,#06b6d4,#0284c7); }
.ach-streak_20 { background-image: linear-gradient(135deg,#8b5cf6,#ec4899); }
.ach-fast_5_in_60 { background-image: linear-gradient(135deg,#f43f5e,#fb7185); }

.achievements-legend {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.achievement-legend-row {
  display: flex;
  align-items: center;
  gap: 14px;
}
.achievement-legend-row .ach-glyph { font-size: 34px; display: inline-flex; align-items: center; justify-content: center; min-width: 34px; transform: translateY(-4px); }
.achievement-legend-text {
  font-size: 18px;
  line-height: 1.25;
  color: #0f172a;
}

.difficulty-progress-list {
  display: grid;
  gap: 10px;
}

.difficulty-progress-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px dashed #cbd5e1;
  padding-bottom: 6px;
}

.difficulty-progress-label {
  font-size: 14px;
  color: #334155;
}

.difficulty-progress-value {
  font-size: 18px;
  font-weight: 800;
}

.change-level-options {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.level-option {
  text-align: left;
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 8px 10px;
}

.level-option.active {
  border-color: #1d4ed8;
  background: #dbeafe;
}

.level-option-title {
  font-weight: 700;
  font-size: 14px;
}

.level-option-meta {
  font-size: 12px;
  color: #475569;
  margin-top: 3px;
}

body.night-mode .level-option {
  background: #1f2937;
  border-color: #475569;
  color: #e2e8f0;
}

body.night-mode .level-option.active {
  background: #1e3a8a;
  border-color: #60a5fa;
}

body.night-mode .level-option-meta,
body.night-mode .difficulty-progress-label {
  color: #cbd5e1;
}
