:root {
  --crimson: #8f2d28;
  --crimson-hover: #74221e;
  --cream: #faf6f0;
  --paper: #ffffff;
  --border: #e5ddd0;
  --text: #1f2937;
  --muted: #6b7280;
  --gold: #d97706;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, #fef3c7 0%, #fde68a 35%, #f5ebe0 100%);
  user-select: none;
}

.hidden {
  display: none !important;
}

.overlay-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow-y: auto;
  z-index: 50;
}

.welcome-card,
.setup-card {
  background: var(--cream);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 20px 50px rgba(143, 45, 40, 0.14);
}

.welcome-card {
  max-width: 500px;
  width: 100%;
  padding: 26px;
  text-align: center;
}

.badge-row {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.pill-crimson,
.pill-fast {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
}

.pill-crimson {
  background: var(--crimson);
  color: #ffffff;
}

.pill-fast {
  background: #d1fae5;
  color: #065f46;
}

.welcome-card h1 {
  margin: 0;
  font-size: 28px;
  color: var(--crimson);
}

.lead {
  margin: 4px 0 14px;
  font-weight: 600;
  color: #4b5563;
}

.gate-art svg {
  width: 100%;
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.intro-copy {
  margin: 14px 0 18px;
  color: #4b5563;
  font-size: 14px;
  line-height: 1.55;
}

.welcome-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* Setup Screen */
.setup-card {
  max-width: 760px;
  width: 100%;
  display: grid;
  grid-template-columns: 270px 1fr;
  gap: 22px;
  padding: 24px;
}

.setup-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

#charPreviewCanvas {
  width: 220px;
  height: 250px;
  border-radius: 10px;
  background: radial-gradient(circle at 50% 45%, #fef3c7, #e2e8f0);
}

.custom-grid {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.setup-card label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: #374151;
}

.setup-card input,
.setup-card select,
.setup-card textarea {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
  background: #ffffff;
}

.setup-right {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.setup-right h2 {
  margin: 0;
  color: var(--crimson);
}

.two-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.error-box {
  background: #fee2e2;
  color: #991b1b;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 12.5px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 14px;
  font-weight: 700;
  font-size: 13.5px;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
}

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

.btn-lg {
  padding: 11px 22px;
  font-size: 15px;
}

.btn-sm {
  padding: 6px 11px;
  font-size: 12.5px;
}

.btn-full {
  width: 100%;
}

.btn-primary {
  background: var(--crimson);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--crimson-hover);
}

.btn-outline {
  background: #ffffff;
  color: var(--crimson);
  border-color: var(--crimson);
}

/* Game Screen & HUD */
.game-screen {
  position: fixed;
  inset: 0;
}

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

.hud-top {
  position: fixed;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
  z-index: 20;
}

.hud-left {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  pointer-events: auto;
  max-width: calc(100% - 146px);
}

.hud-pill,
.hud-btn {
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 12.5px;
  font-weight: 700;
  background: rgba(250, 246, 240, 0.92);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.12);
  white-space: nowrap;
}

.hud-btn {
  cursor: pointer;
}

.hud-btn:hover {
  background: #ffffff;
}

.hud-brand,
.hud-crimson {
  background: rgba(143, 45, 40, 0.94);
  color: #ffffff;
  border-color: rgba(255, 255, 255, 0.25);
}

.hud-perf {
  background: rgba(209, 250, 229, 0.94);
  color: #065f46;
}

.hud-online {
  background: rgba(255, 255, 255, 0.92);
}

.minimap-wrap {
  pointer-events: auto;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.94);
  border: 2px solid #ffffff;
  border-radius: 12px;
  padding: 3px;
  box-shadow: 0 6px 20px rgba(15, 23, 42, 0.18);
  text-align: center;
  flex-shrink: 0;
}

#minimapCanvas {
  display: block;
  width: 120px;
  height: 120px;
  border-radius: 9px;
}

.minimap-hint {
  display: block;
  font-size: 10px;
  font-weight: 700;
  color: #475569;
  margin-top: 2px;
}

.interact-prompt {
  position: fixed;
  bottom: 58px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(143, 45, 40, 0.95);
  color: #ffffff;
  border: 2px solid #fde68a;
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
  z-index: 25;
}

.help-bar {
  position: fixed;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  background: rgba(15, 23, 42, 0.72);
  color: #f8fafc;
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 11.5px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 15;
}

.toast-container {
  position: fixed;
  top: 72px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 40;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(15, 23, 42, 0.88);
  color: #ffffff;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
  transition: opacity 0.35s;
}

.toast.fade-out {
  opacity: 0;
}

.chat-bar {
  position: fixed;
  bottom: 52px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  background: rgba(255, 255, 255, 0.96);
  padding: 6px;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 30;
}

.chat-bar input {
  width: 240px;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  padding: 6px 10px;
  font-size: 13px;
}

/* Touch Controls */
.joystick-zone {
  display: none;
  position: fixed;
  left: 16px;
  bottom: 46px;
  width: 110px;
  height: 110px;
  z-index: 22;
  touch-action: none;
}

.joystick-base {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.28);
  border: 2px solid rgba(255, 255, 255, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
}

.joystick-knob {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
}

.touch-actions {
  display: none;
  position: fixed;
  right: 16px;
  bottom: 52px;
  gap: 8px;
  z-index: 22;
}

.touch-btn {
  border: 2px solid rgba(255, 255, 255, 0.7);
  background: rgba(15, 23, 42, 0.65);
  color: #ffffff;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  font-size: 11.5px;
  font-weight: 700;
}

.touch-accent {
  background: rgba(143, 45, 40, 0.88);
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}

.modal-card {
  background: var(--cream);
  border-radius: 14px;
  width: 100%;
  max-width: 460px;
  padding: 18px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}

.modal-lg {
  max-width: 620px;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.modal-head h3 {
  margin: 0;
  color: var(--crimson);
}

.close-btn {
  border: none;
  background: #e5e7eb;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-weight: 700;
}

.story-text {
  margin: 0;
  line-height: 1.6;
  color: #374151;
}

.modal-scroll {
  max-height: 55vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.gb-form {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.gb-form input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
}

.gb-item,
.lb-row,
.class-row {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
}

.gb-head {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 4px;
}

.gb-head span {
  color: var(--muted);
}

.gb-item p {
  margin: 0;
  font-size: 13px;
}

.lb-row,
.class-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.lb-rank {
  font-weight: 800;
  color: var(--crimson);
  width: 28px;
}

.lb-info {
  flex: 1;
}

.lb-info small,
.class-row small {
  display: block;
  color: var(--muted);
  font-size: 11.5px;
}

.lb-score {
  font-weight: 700;
  color: #065f46;
}

.tp-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
}

.tp-chip {
  text-align: left;
  border: 1px solid var(--border);
  background: #ffffff;
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

.tp-chip:hover {
  border-color: var(--crimson);
  background: #fff5f5;
}

.quality-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.q-btn {
  text-align: left;
  border: 2px solid var(--border);
  background: #ffffff;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}

.q-btn strong {
  display: block;
  font-size: 13.5px;
}

.q-btn span {
  font-size: 12px;
  color: var(--muted);
}

.q-btn.active {
  border-color: var(--crimson);
  background: #fef2f2;
}

@media (max-width: 720px) {
  .setup-card {
    grid-template-columns: 1fr;
  }
  .tp-grid {
    grid-template-columns: 1fr;
  }
  .help-bar {
    display: none;
  }
  .joystick-zone,
  .touch-actions {
    display: flex;
  }
  #minimapCanvas {
    width: 92px;
    height: 92px;
  }
  .hud-left {
    max-width: calc(100% - 110px);
  }
  .hud-pill,
  .hud-btn {
    padding: 5px 8px;
    font-size: 11px;
  }
}
