:root {
  /* RTR pillar palette */
  --sage: #8FB397;
  --teal: #4A9999;
  --gold: #E5BD5C;
  --rust: #D67152;
  --gray: #B8AFA5;
  --rose: #DBAAA7;

  /* Premium dark base — like a high-end mobile game */
  --bg-deep: #0F1419;
  --bg-surface: #1A2128;
  --bg-glass: rgba(26, 33, 40, 0.72);
  --bg-glass-strong: rgba(26, 33, 40, 0.92);

  /* Cream tones (for home screen) */
  --cream: #FFF8F0;
  --cream-deep: #F5EDE0;
  --charcoal: #2D2A26;
  --ink: #4A453E;
  --ink-soft: #6B655C;
  --line: rgba(255, 255, 255, 0.12);
  --line-strong: rgba(255, 255, 255, 0.22);

  --shadow-soft: 0 4px 16px rgba(0, 0, 0, 0.18);
  --shadow-strong: 0 12px 40px rgba(0, 0, 0, 0.4);
  --shadow-glass: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.06) inset;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%;
  background: var(--bg-deep);
  color: var(--cream);
  font-family: 'Plus Jakarta Sans', sans-serif;
  overscroll-behavior: none;
  overflow: hidden;
}

#app {
  position: relative;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
}
.screen.active { display: block; animation: fadeIn 0.5s cubic-bezier(0.22, 1, 0.36, 1); }
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ============ HOME SCREEN ============ */
#screen-home {
  background: var(--cream);
  background-image:
    radial-gradient(circle at 20% 10%, rgba(229, 189, 92, 0.10) 0, transparent 40%),
    radial-gradient(circle at 85% 85%, rgba(143, 179, 151, 0.10) 0, transparent 45%),
    radial-gradient(circle at 50% 50%, rgba(74, 153, 153, 0.04) 0, transparent 60%);
}

.home-bg-canvas-wrap {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.85;
  z-index: 1;
}
#home-bg {
  width: 100%;
  height: 100%;
  display: block;
}

.home-content {
  position: relative;
  z-index: 2;
  height: 100%;
  width: 100%;
  padding: 32px 24px 40px;
  padding-top: calc(32px + var(--safe-top));
  padding-bottom: calc(40px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  overflow-y: auto;
}

.home-mark {
  font-family: 'Fraunces', serif;
  font-weight: 900;
  font-size: 11px;
  letter-spacing: 0.32em;
  color: var(--charcoal);
  text-transform: uppercase;
  margin-top: 12px;
  margin-bottom: 6px;
  opacity: 0.7;
}
.home-mark::before, .home-mark::after {
  content: '·';
  margin: 0 8px;
  color: var(--gold);
}

.home-title {
  font-family: 'Fraunces', serif;
  font-variation-settings: 'opsz' 144;
  font-weight: 900;
  font-size: clamp(56px, 16vw, 96px);
  line-height: 0.92;
  letter-spacing: -0.035em;
  color: var(--charcoal);
  margin: 8px 0 4px;
}
.home-title em {
  font-style: italic;
  font-weight: 400;
  color: var(--rust);
  background: linear-gradient(135deg, #D67152 0%, #E5BD5C 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.home-tag {
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-size: 14px;
  font-weight: 500;
  color: var(--ink);
  letter-spacing: 0.16em;
  margin-bottom: 36px;
  text-transform: uppercase;
}
.home-tag span { 
  color: var(--charcoal); 
  font-weight: 700;
  margin: 0 6px;
}

.home-actions {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 360px;
}

.btn-primary, .btn-secondary, .btn-ghost {
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-weight: 700;
  font-size: 16px;
  border: none;
  border-radius: 14px;
  padding: 18px 22px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.btn-primary {
  background: linear-gradient(180deg, #3D3833 0%, #2D2A26 100%);
  color: var(--cream);
  box-shadow: 0 4px 0 #1F1B17, 0 8px 24px rgba(45, 42, 38, 0.25);
}
.btn-primary:active { transform: translateY(2px); box-shadow: 0 2px 0 #1F1B17; }
.btn-secondary {
  background: var(--cream-deep);
  color: var(--charcoal);
  border: 2px solid rgba(45, 42, 38, 0.12);
}
.btn-secondary:active { transform: translateY(1px); }

.home-section {
  width: 100%;
  max-width: 360px;
  margin-top: 28px;
}
.home-section-label {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-style: italic;
  font-size: 16px;
  color: var(--ink);
  text-align: left;
  margin-bottom: 12px;
  padding-left: 4px;
}

.worlds-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.world-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(45, 42, 38, 0.08);
  border-radius: 14px;
  text-align: left;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease;
  width: 100%;
  font-family: inherit;
}
.world-card:active { transform: translateY(1px) scale(0.99); border-color: var(--gold); }
.world-card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--sage) 0%, var(--teal) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
  color: white;
  font-weight: 800;
  font-family: 'Fraunces', serif;
}
.world-card-info { flex: 1; min-width: 0; }
.world-card-name {
  font-weight: 700;
  font-size: 15px;
  color: var(--charcoal);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.world-card-meta {
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: 2px;
}
.world-card-actions { display: flex; gap: 6px; }
.world-card-btn {
  background: transparent;
  border: 1px solid rgba(45, 42, 38, 0.16);
  color: var(--ink);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.world-card-btn:active { background: rgba(45, 42, 38, 0.06); }
.world-card-btn.danger { color: var(--rust); border-color: rgba(214, 113, 82, 0.4); }

.empty-state {
  padding: 24px;
  text-align: center;
  font-family: 'Fraunces', serif;
  font-style: italic;
  color: var(--ink-soft);
  font-size: 14px;
}

.code-input-row {
  display: flex;
  gap: 8px;
}
.code-input {
  flex: 1;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.6);
  border: 1.5px solid rgba(45, 42, 38, 0.12);
  border-radius: 12px;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 18px;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
  color: var(--charcoal);
}
.code-input:focus { outline: none; border-color: var(--teal); background: white; }

.home-footer {
  margin-top: auto;
  padding-top: 32px;
  font-family: 'Fraunces', serif;
  font-style: italic;
  font-size: 12px;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
}
.home-footer strong {
  font-style: normal;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--rust);
  font-size: 10px;
}

/* ============ BUILD / PLAY SCREENS ============ */
#screen-build, #screen-play {
  background: var(--bg-deep);
}

#build-canvas, #play-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.glass {
  background: var(--bg-glass);
  backdrop-filter: blur(20px) saturate(140%);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-glass);
}

/* ============ BUILD HEADER ============ */
.build-header {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 16px;
  z-index: 10;
}

.icon-btn {
  width: 40px; height: 40px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--cream);
  flex-shrink: 0;
  transition: all 0.15s;
  font-family: inherit;
}
.icon-btn:active { transform: scale(0.92); background: rgba(255, 255, 255, 0.18); }
.icon-btn.primary { 
  background: linear-gradient(180deg, var(--rust) 0%, #B85F45 100%);
  border-color: rgba(0, 0, 0, 0.2);
  color: white;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}
.icon-btn.gold { 
  background: linear-gradient(180deg, var(--gold) 0%, #C4A050 100%);
  border-color: rgba(0, 0, 0, 0.2);
  color: var(--charcoal);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
}

.name-input {
  flex: 1;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 16px;
  color: var(--cream);
  min-width: 0;
}
.name-input::placeholder { color: rgba(255, 255, 255, 0.4); }
.name-input:focus { outline: none; border-color: var(--gold); background: rgba(255, 255, 255, 0.14); }

/* ============ BUILD MODE PILL ============ */
.build-mode-pill {
  position: absolute;
  top: calc(72px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  padding: 4px;
  border-radius: 999px;
  z-index: 10;
  gap: 2px;
}
.mode-btn {
  padding: 8px 18px;
  background: transparent;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.15s;
}
.mode-btn.active {
  background: rgba(255, 255, 255, 0.15);
  color: var(--cream);
}

/* ============ BUILD PANEL (BOTTOM) ============ */
.build-panel {
  position: absolute;
  bottom: calc(12px + var(--safe-bottom));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  border-radius: 18px;
  z-index: 10;
  overflow: hidden;
}
.build-tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
}
.build-tab {
  flex: 1;
  padding: 12px 8px;
  background: transparent;
  border: none;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
  margin-bottom: -1px;
}
.build-tab.active {
  color: var(--cream);
  border-bottom-color: var(--gold);
}
.build-tab-content {
  padding: 14px;
  max-height: 30vh;
  overflow-y: auto;
}
.build-tab-content.hidden { display: none; }

.palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
}
.palette-item {
  padding: 10px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1.5px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.palette-item:active { transform: scale(0.95); }
.palette-item.selected {
  border-color: var(--gold);
  background: rgba(229, 189, 92, 0.12);
  box-shadow: 0 0 0 1px var(--gold);
}
.palette-icon {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  background: linear-gradient(135deg, var(--icon-color, #888) 0%, var(--icon-color-2, #666) 100%);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.2);
}
.palette-label {
  font-size: 10px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.7);
  letter-spacing: 0.04em;
}
.palette-item.selected .palette-label { color: var(--cream); }

.option-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}
.option-chip {
  padding: 10px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1.5px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  text-align: center;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
  color: rgba(255, 255, 255, 0.7);
}
.option-chip.selected {
  border-color: var(--gold);
  background: rgba(229, 189, 92, 0.12);
  color: var(--cream);
  box-shadow: 0 0 0 1px var(--gold);
}
.option-chip-icon { font-size: 24px; display: block; margin-bottom: 3px; }
.option-chip-label { font-size: 11px; font-weight: 600; }

.rule-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px dashed var(--line);
  gap: 12px;
}
.rule-row:last-child { border-bottom: none; }
.rule-label {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 14px;
  color: var(--cream);
}
.rule-sub {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.5);
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-weight: 500;
  margin-top: 2px;
}
.pill-group { display: flex; gap: 4px; }
.pill {
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1.5px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  font-family: inherit;
  font-weight: 600;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: all 0.15s;
}
.pill.selected { 
  background: var(--gold);
  color: var(--charcoal); 
  border-color: var(--gold);
}

/* ============ PLAY HEADER ============ */
.play-header {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 8px;
  border-radius: 16px;
  z-index: 10;
}
.play-stat {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  font-family: 'Fraunces', serif;
  font-weight: 700;
  font-size: 15px;
  color: var(--cream);
}
.play-stat svg { color: var(--rose); }
.play-stat.gold-stat svg { color: var(--gold); }

.play-title {
  font-family: 'Fraunces', serif;
  font-weight: 700;
  font-style: italic;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.85);
  flex: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 8px;
}

/* ============ DPAD ============ */
.dpad {
  position: absolute;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: 60px 60px 60px;
  grid-template-rows: 60px 60px 60px;
  gap: 4px;
  z-index: 10;
}
.dpad-btn {
  background: var(--bg-glass);
  backdrop-filter: blur(20px) saturate(140%);
  -webkit-backdrop-filter: blur(20px) saturate(140%);
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--cream);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  touch-action: manipulation;
  font-family: inherit;
  font-weight: 700;
  transition: all 0.1s;
  box-shadow: var(--shadow-soft);
}
.dpad-btn:active { 
  background: rgba(255, 255, 255, 0.2); 
  transform: scale(0.92); 
}
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }
.dpad-center {
  grid-column: 2; grid-row: 2;
  background: linear-gradient(180deg, var(--gold) 0%, #C4A050 100%);
  border-color: rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  color: var(--charcoal);
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2), var(--shadow-soft);
}

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 25, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.modal-overlay.active { display: flex; animation: fadeIn 0.2s ease; }
.modal {
  background: var(--cream);
  border-radius: 24px;
  padding: 36px 24px 24px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,0.5);
  animation: pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes pop {
  from { transform: scale(0.85) translateY(20px); opacity: 0; }
  to { transform: scale(1) translateY(0); opacity: 1; }
}
.modal-icon { 
  display: flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  margin: 0 auto 12px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--gold) 0%, var(--rust) 100%);
  color: white;
  box-shadow: 0 8px 24px rgba(229, 189, 92, 0.4);
}
.modal-title {
  font-family: 'Fraunces', serif;
  font-weight: 900;
  font-size: 30px;
  color: var(--charcoal);
  margin-bottom: 8px;
  line-height: 1;
}
.modal-text {
  font-size: 14px;
  color: var(--ink);
  margin-bottom: 24px;
  line-height: 1.5;
}
.modal-code {
  font-family: 'Fraunces', serif;
  font-weight: 900;
  font-size: 36px;
  letter-spacing: 0.16em;
  color: var(--rust);
  background: linear-gradient(180deg, rgba(214, 113, 82, 0.06) 0%, rgba(229, 189, 92, 0.10) 100%);
  padding: 18px;
  border-radius: 14px;
  border: 2px dashed rgba(214, 113, 82, 0.35);
  margin-bottom: 16px;
}
.modal-actions { display: flex; flex-direction: column; gap: 10px; }

/* ============ TOAST ============ */
.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(120px);
  background: var(--bg-glass-strong);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--line);
  color: var(--cream);
  padding: 12px 20px;
  border-radius: 999px;
  font-family: 'Fraunces', serif;
  font-weight: 600;
  font-size: 14px;
  z-index: 200;
  transition: transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);
  pointer-events: none;
  box-shadow: var(--shadow-strong);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ============ LOADER ============ */
.loader {
  position: fixed;
  inset: 0;
  background: var(--bg-deep);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 300;
}
.loader.active { display: flex; }
.loader-content { text-align: center; }
.loader-spinner {
  width: 48px;
  height: 48px;
  margin: 0 auto 16px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--gold);
  animation: spin 1s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
.loader-text {
  font-family: 'Fraunces', serif;
  font-style: italic;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

/* ============ RESPONSIVE ============ */
@media (max-height: 700px) {
  .home-title { font-size: clamp(44px, 13vw, 72px); }
  .build-tab-content { max-height: 24vh; }
  .dpad { 
    grid-template-columns: 52px 52px 52px; 
    grid-template-rows: 52px 52px 52px;
  }
}

@media (min-width: 700px) {
  #screen-home .home-content { padding-top: 60px; }
  .home-title { font-size: 104px; }
}
