:root {
  --bg: #0b0f19;
  --panel: #131b2e;
  --panel-2: #19243d;
  --border: #263554;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --gold: #fbbf24;
  --cyan: #38bdf8;
  --green: #4ade80;
  --purple: #c084fc;
  --red: #f87171;
  --accent: #f59e0b;
}

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: radial-gradient(circle at top, #172038 0%, var(--bg) 65%);
  color: var(--text);
  min-height: 100vh;
}

.app-shell {
  max-width: 780px;
  margin: 0 auto;
  padding: 12px 12px 36px;
}

/* Header */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.tier-badge {
  font-size: 30px;
  line-height: 1;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 9px;
}

.village-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.village-row h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: #fef08a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tier-line {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.top-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 6px 9px;
  cursor: pointer;
  font-size: 14px;
  transition: transform 0.1s, background 0.15s;
}

.icon-btn:hover {
  background: var(--panel-2);
}

.icon-btn:active {
  transform: scale(0.94);
}

.tier-progress {
  height: 5px;
  background: var(--panel);
  border-radius: 99px;
  overflow: hidden;
  margin-bottom: 10px;
}

.tier-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #f59e0b, #fde047);
  transition: width 0.25s ease;
}

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.stat-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 11px;
  color: var(--muted);
}

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

.stat-val.gold { color: var(--gold); }
.stat-val.cyan { color: var(--cyan); }
.stat-val.purple { color: var(--purple); }
.stat-val.ok { color: var(--green); }
.stat-val.warn { color: var(--gold); }
.stat-val.danger { color: var(--red); }

.stat-sub {
  font-size: 11px;
  color: var(--muted);
}

/* Banners */
.boost-banner {
  background: linear-gradient(90deg, rgba(245, 158, 11, 0.2), rgba(234, 179, 8, 0.35));
  border: 1px solid #f59e0b;
  color: #fef08a;
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  padding: 7px 12px;
  border-radius: 10px;
  margin-bottom: 8px;
}

.contract-bar {
  background: rgba(14, 165, 233, 0.14);
  border: 1px solid rgba(56, 189, 248, 0.5);
  border-radius: 12px;
  padding: 8px 12px;
  margin-bottom: 10px;
}

.contract-bar-top,
.contract-bar-sub {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  gap: 8px;
}

.contract-timer {
  color: var(--gold);
  font-weight: 700;
}

.mini-progress {
  height: 7px;
  background: rgba(15, 23, 42, 0.8);
  border-radius: 99px;
  overflow: hidden;
  margin: 6px 0;
}

.mini-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #38bdf8, #4ade80);
  transition: width 0.15s linear;
}

/* Pixel Canvas Stage */
.stage-wrap {
  background: #090d16;
  border: 2px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  margin-bottom: 10px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
}

#village-canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: pointer;
  touch-action: manipulation;
}

.stage-hint {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  padding: 5px 8px;
  background: rgba(19, 27, 46, 0.9);
  border-top: 1px solid var(--border);
}

/* Primary Buttons */
.action-bar {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.btn-primary,
.btn-secondary {
  border: none;
  border-radius: 14px;
  padding: 13px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: transform 0.08s, filter 0.12s;
  user-select: none;
  touch-action: manipulation;
}

.btn-primary {
  background: linear-gradient(180deg, #f59e0b 0%, #d97706 100%);
  color: #0b0f19;
  box-shadow: 0 4px 0 #92400e, 0 8px 18px rgba(245, 158, 11, 0.25);
}

.btn-primary:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #92400e;
}

.btn-secondary {
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: 0 4px 0 #020617;
}

.btn-secondary:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #020617;
}

.btn-secondary.pulse-warn {
  border-color: var(--red);
  box-shadow: 0 4px 0 #7f1d1d, 0 0 14px rgba(248, 113, 113, 0.35);
}

.btn-main {
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 0.4px;
}

.btn-sub {
  font-size: 12px;
  font-weight: 600;
  opacity: 0.88;
}

/* Tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}

.tab-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 6px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s;
}

.tab-btn.active {
  background: var(--panel-2);
  color: #fef08a;
  border-color: var(--accent);
}

/* Cards */
.tab-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.card-owned {
  border-color: rgba(74, 222, 128, 0.35);
  background: rgba(20, 83, 45, 0.16);
}

.card-locked {
  opacity: 0.6;
}

.card-left {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  min-width: 0;
}

.card-icon {
  font-size: 26px;
  line-height: 1;
  background: var(--panel-2);
  border-radius: 10px;
  padding: 7px;
  flex-shrink: 0;
}

.card-title {
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.count-pill {
  font-size: 11px;
  background: rgba(56, 189, 248, 0.18);
  color: var(--cyan);
  padding: 1px 7px;
  border-radius: 99px;
}

.card-desc {
  font-size: 12px;
  color: var(--muted);
  margin: 3px 0 5px;
  line-height: 1.35;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 11px;
  font-weight: 600;
  color: #cbd5e1;
}

.tag-ok { color: var(--green); }
.tag-warn { color: #fca5a5; }

.buy-btn {
  flex-shrink: 0;
  min-width: 96px;
  background: linear-gradient(180deg, #0ea5e9, #0284c7);
  color: #ffffff;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 12px;
  cursor: pointer;
  line-height: 1.3;
  transition: transform 0.08s, opacity 0.15s;
}

.buy-btn:active:not(:disabled) {
  transform: scale(0.96);
}

.buy-btn:disabled {
  background: #1e293b;
  color: #64748b;
  cursor: not-allowed;
}

.owned-badge {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 800;
  color: var(--green);
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid rgba(74, 222, 128, 0.35);
}

/* Leaderboard */
.lb-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 700;
  color: #fef08a;
}

.small-btn {
  background: var(--panel-2);
  border: 1px solid var(--accent);
  color: #fef08a;
  font-size: 12px;
  font-weight: 700;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}

.lb-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lb-row {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.lb-row.lb-me {
  border-color: var(--accent);
  background: rgba(245, 158, 11, 0.12);
}

.lb-rank {
  font-size: 16px;
  font-weight: 800;
  width: 30px;
  text-align: center;
}

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

.lb-name {
  font-size: 14px;
  font-weight: 700;
}

.lb-sub {
  font-size: 11px;
  color: var(--muted);
}

.lb-score {
  font-size: 14px;
  font-weight: 800;
  color: var(--gold);
  white-space: nowrap;
}

.empty-msg {
  text-align: center;
  color: var(--muted);
  padding: 24px;
  font-size: 13px;
  background: var(--panel);
  border-radius: 12px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid var(--accent);
  color: #fef08a;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  max-width: min(92vw, 520px);
  text-align: center;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  z-index: 90;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-card {
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 16px;
  padding: 20px;
  max-width: 440px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.65);
}

.modal-icon {
  font-size: 40px;
  margin-bottom: 6px;
}

.modal-card h2 {
  margin: 0 0 8px;
  font-size: 17px;
  color: #fef08a;
}

.modal-card p {
  margin: 0 0 16px;
  font-size: 13px;
  color: #cbd5e1;
  line-height: 1.5;
}

.modal-choices {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.event-choice-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 12px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.event-choice-btn:hover {
  border-color: var(--accent);
  background: #223050;
}

.footer {
  margin-top: 20px;
  text-align: center;
  font-size: 11px;
  color: #64748b;
}

.hidden {
  display: none !important;
}

@media (max-width: 560px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .tab-btn {
    font-size: 12px;
    padding: 8px 4px;
  }
  .card {
    flex-direction: column;
    align-items: stretch;
  }
  .buy-btn {
    width: 100%;
  }
}
