:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-soft: #f8fafc;
  --border: #dbe1e8;
  --text: #1f2933;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --success: #16a34a;
  --warning: #f59e0b;
  --danger: #dc2626;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
}

header {
  background: linear-gradient(135deg, #111827, #1f2937);
  color: white;
  padding: 22px 28px;
  box-shadow: var(--shadow);
}

.header-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto;
  gap: 20px;
  align-items: center;
  max-width: 1500px;
  margin: 0 auto;
}

h1 {
  margin: 0 0 6px 0;
  font-size: clamp(1.4rem, 2vw, 2rem);
  letter-spacing: -0.03em;
}

.subtitle {
  color: #cbd5e1;
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.45;
}

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
}

button {
  font: inherit;
}

.primary-btn,
.secondary-btn,
.ghost-btn,
.danger-btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 13px;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 0.12s ease,
    opacity 0.12s ease,
    background 0.12s ease;
  white-space: nowrap;
}

.primary-btn:hover,
.secondary-btn:hover,
.ghost-btn:hover,
.danger-btn:hover {
  transform: translateY(-1px);
  opacity: 0.95;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
}
.primary-btn:hover {
  background: var(--accent-dark);
}
.secondary-btn {
  background: #475569;
  color: #fff;
}
.ghost-btn {
  background: #e2e8f0;
  color: #0f172a;
}
.danger-btn {
  background: #fee2e2;
  color: #991b1b;
}

main {
  max-width: 1500px;
  width: 100%;
  margin: 0 auto;
  padding: 22px;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 20px;
}

.sidebar,
.content-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.sidebar {
  padding: 16px;
  align-self: start;
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 36px);
  overflow: auto;
}

.global-progress {
  padding: 14px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 14px;
}

.global-progress strong {
  display: block;
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.progress-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.progress-bar {
  width: 100%;
  height: 9px;
  border-radius: 999px;
  background: #e5e7eb;
  overflow: hidden;
}

.progress-bar span {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: var(--success);
  transition: width 0.2s ease;
}

.user-panel {
  padding: 14px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 14px;
}

.user-panel h3 {
  margin: 0 0 5px 0;
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.user-help {
  margin: 0 0 10px 0;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.35;
}

.user-form,
.user-select-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

.user-select-row {
  grid-template-columns: minmax(0, 1fr) auto;
}

.user-input,
.user-select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 10px;
  font: inherit;
  background: #fff;
  color: var(--text);
  min-width: 0;
}

.color-input {
  width: 42px;
  height: 39px;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  background: #fff;
  cursor: pointer;
}

.danger-mini-btn {
  border: 1px solid #fecaca;
  border-radius: 9px;
  background: #fee2e2;
  color: #991b1b;
  padding: 8px 9px;
  font-size: 0.84rem;
  font-weight: 800;
  cursor: pointer;
}

.danger-mini-btn:disabled,
.user-select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.tab-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tab-button {
  width: 100%;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  text-align: left;
}

.tab-button.active {
  background: #eff6ff;
  border-color: #bfdbfe;
  color: #1d4ed8;
}

.tab-count {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.82rem;
}

.content-card {
  min-width: 0;
  overflow: hidden;
}

.content-toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) auto auto;
  gap: 10px;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-soft);
}

.search-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  font: inherit;
  background: #fff;
  color: var(--text);
}

.continent-title {
  padding: 16px 18px 0 18px;
}

.continent-title h2 {
  margin: 0 0 6px 0;
  font-size: 1.3rem;
}

.continent-title p {
  margin: 0 0 12px 0;
  color: var(--muted);
  font-size: 0.93rem;
}

.country-list {
  padding: 0 16px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.country-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  overflow: hidden;
}

.country-header {
  width: 100%;
  border: 0;
  background: #fff;
  color: var(--text);
  padding: 14px 16px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 12px;
  align-items: center;
  cursor: pointer;
  text-align: left;
}

.country-header:hover {
  background: #f8fafc;
}

.country-main strong {
  display: block;
  font-size: 1rem;
  margin-bottom: 4px;
}

.country-sub {
  color: var(--muted);
  font-size: 0.84rem;
}

.country-progress {
  min-width: 92px;
  text-align: right;
}

.country-percent {
  font-weight: 800;
  color: var(--success);
  font-size: 1.02rem;
}

.country-count {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 3px;
}

.chevron {
  color: var(--muted);
  font-size: 1rem;
  width: 18px;
  text-align: center;
  transition: transform 0.15s ease;
}

.country-card.open .chevron {
  transform: rotate(90deg);
}

.country-body {
  border-top: 1px solid var(--border);
  background: #fbfdff;
  padding: 12px 14px 14px 14px;
}

.country-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

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

.mini-btn {
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #fff;
  color: var(--text);
  padding: 7px 9px;
  font-size: 0.84rem;
  font-weight: 700;
  cursor: pointer;
}

.mini-btn:hover {
  background: #f1f5f9;
}

.group {
  margin-bottom: 13px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  background: #fff;
  overflow: hidden;
}

.group-name {
  padding: 9px 11px;
  background: #f8fafc;
  color: #0f172a;
  font-weight: 800;
  border-bottom: 1px solid #e5e7eb;
  font-size: 0.9rem;
}

.item-list {
  padding: 8px 10px 10px 10px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(245px, 1fr));
  column-gap: 14px;
  row-gap: 4px;
}

.region-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  min-height: 28px;
  padding: 4px 2px;
  cursor: pointer;
}

.region-item input {
  margin-top: 3px;
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--success);
}

.region-item label {
  cursor: pointer;
  user-select: none;
  line-height: 1.35;
  font-size: 0.92rem;
  color: #334155;
}

.region-code {
  color: #94a3b8;
  font-size: 0.78rem;
  margin-left: 5px;
}

.region-item.assigned {
  border-left: 4px solid var(--assigned-color);
  padding-left: 8px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--assigned-color) 12%, #ffffff);
  box-shadow: inset 0 0 0 1px
    color-mix(in srgb, var(--assigned-color) 18%, transparent);
}

.assignment-chip,
.user-chip-inline {
  display: inline-flex;
  align-items: center;
  min-height: 20px;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.75rem;
  font-weight: 800;
  white-space: nowrap;
  align-self: center;
}

.assignment-chip[hidden] {
  display: none !important;
}

.stats-tab {
  margin-top: 8px;
  border-style: dashed;
}

.stats-dashboard {
  padding: 0 0 16px 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
}

.stat-card,
.stats-section {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  padding: 14px;
}

.stat-card strong {
  display: block;
  font-size: 1.55rem;
  line-height: 1;
  margin-bottom: 7px;
  color: #0f172a;
}

.stat-card span,
.stat-card small {
  display: block;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.35;
}

.stats-section h3 {
  margin: 0 0 12px 0;
  font-size: 1.05rem;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.stats-table th,
.stats-table td {
  padding: 9px 8px;
  border-bottom: 1px solid #e5e7eb;
  text-align: left;
  vertical-align: middle;
}

.stats-table th {
  color: #334155;
  background: #f8fafc;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.matrix-table {
  display: block;
  overflow-x: auto;
  white-space: nowrap;
}

.stat-bar-row {
  display: grid;
  grid-template-columns: minmax(180px, 260px) minmax(160px, 1fr);
  gap: 12px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #eef2f7;
}

.stat-bar-row:last-child {
  border-bottom: 0;
}

.stat-bar-label strong,
.leaderboard-main strong {
  display: block;
  font-size: 0.94rem;
}

.stat-bar-label span,
.leaderboard-main span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 3px;
}

.bar-track {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: #e5e7eb;
  overflow: hidden;
}

.bar-fill {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: var(--accent);
}

.leaderboard-row {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, auto);
  gap: 12px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #eef2f7;
}

.leaderboard-row:last-child {
  border-bottom: 0;
}

.leaderboard-users {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 6px;
}

.empty-note {
  padding: 14px;
  border: 1px dashed #cbd5e1;
  border-radius: 10px;
  background: #fff;
  color: var(--muted);
  line-height: 1.5;
}

.empty-note code {
  color: #334155;
  background: #f1f5f9;
  padding: 2px 5px;
  border-radius: 5px;
}

.hidden {
  display: none !important;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  background: #111827;
  color: #fff;
  padding: 12px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition:
    opacity 0.15s ease,
    transform 0.15s ease;
  max-width: min(420px, calc(100vw - 36px));
  z-index: 20;
  font-size: 0.92rem;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 900px) {
  .header-grid {
    grid-template-columns: 1fr;
  }
  .controls {
    justify-content: flex-start;
  }
  main {
    grid-template-columns: 1fr;
    padding: 14px;
  }
  .sidebar {
    position: static;
    max-height: none;
  }
  .tab-list {
    flex-direction: row;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .tab-button {
    min-width: 170px;
  }
  .content-toolbar {
    grid-template-columns: 1fr;
  }
  .country-header {
    grid-template-columns: 1fr auto;
  }
  .country-progress {
    text-align: left;
  }
  .chevron {
    display: none;
  }
}
