:root {
  --page-bg: #171412;
  --surface: #241f1c;
  --accent: #f59e0b;
  --accent-text: #14110f;
  --board-bg: #3b332c;
  --slot-bg: #4c4239;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: rgba(255, 255, 255, 0.11);
}

[hidden] {
  display: none !important;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(circle at 50% 0%, color-mix(in srgb, var(--accent) 16%, var(--page-bg)), var(--page-bg) 62%);
  color: var(--text);
  font-family: system-ui, -apple-system, "PingFang SC", "Noto Sans SC", "Segoe UI", sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 240ms ease;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Masthead / Shared Portal Header Host */
.masthead {
  width: 100%;
  max-width: 560px;
  min-height: 46px;
  padding: 8px 14px 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

[data-xgh-host] .masthead-left {
  display: none !important;
}

.masthead-left h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.masthead-right {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.header-btn {
  height: 32px;
  padding: 0 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.header-btn:active {
  transform: scale(0.95);
}

/* Main Portrait-First Single-Screen Shell */
.shell {
  width: 100%;
  max-width: 520px;
  flex: 1 1 auto;
  min-height: 0;
  padding: 6px 14px max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.top-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.theme-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border));
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.theme-pill:hover {
  border-color: var(--accent);
}

.theme-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  flex-shrink: 0;
}

.pill-caret {
  opacity: 0.7;
  font-size: 0.78rem;
}

.mode-meta {
  font-size: 0.72rem;
  color: var(--muted);
  padding-left: 6px;
  font-weight: 600;
}

.scores-group {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-shrink: 0;
}

.score-box {
  min-width: 76px;
  padding: 6px 11px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.score-label {
  font-size: 0.67rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.score-val {
  font-size: 1.12rem;
  font-weight: 800;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}

.pure-badge {
  background: color-mix(in srgb, var(--accent) 25%, transparent);
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 55%, transparent);
  border-radius: 999px;
  padding: 0 5px;
  font-size: 0.6rem;
  font-weight: 800;
}

/* Mode Selector Tabs */
.mode-tabs {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 4px;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.mode-tab {
  border: 0;
  border-radius: 9px;
  padding: 7px 6px;
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease;
}

.mode-tab.active {
  background: var(--accent);
  color: var(--accent-text);
}

/* Square Board Stage */
.board-stage {
  width: 100%;
  flex: 1 1 auto;
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

#board-canvas {
  display: block;
  border-radius: 18px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.38);
  touch-action: none;
  cursor: grab;
}

#board-canvas:active {
  cursor: grabbing;
}

/* Bottom Single-Row Thumb Bar */
.thumb-bar {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  flex-shrink: 0;
}

.thumb-btn {
  min-height: 44px;
  padding: 8px 6px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  white-space: nowrap;
}

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

.thumb-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.thumb-btn.accent-btn {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}

.btn-ico {
  font-size: 0.92rem;
  line-height: 1;
}

.swipe-hint {
  margin: 0;
  font-size: 0.74rem;
  color: var(--muted);
  text-align: center;
  flex-shrink: 0;
}

/* Modal Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(5, 8, 14, 0.74);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  width: 100%;
  max-width: 500px;
  max-height: min(86vh, 680px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
}

.compact-card {
  max-width: 360px;
  text-align: center;
  padding: 24px 20px;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.modal-head h2,
.compact-card h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
}

.modal-sub {
  margin: 4px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.45;
}

.close-btn {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
  flex-shrink: 0;
}

.badge-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
}

.toggle-wrap {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  font-weight: 700;
  cursor: pointer;
}

.toggle-wrap input {
  width: 17px;
  height: 17px;
  accent-color: var(--accent);
  cursor: pointer;
}

.scrollable-panel {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

/* Themes Selector Grid */
.themes-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding-right: 2px;
}

.theme-card {
  text-align: left;
  padding: 12px;
  border-radius: 14px;
  border: 1.5px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 7px;
  transition: border-color 140ms ease, transform 140ms ease;
}

.theme-card:hover {
  border-color: rgba(255, 255, 255, 0.35);
}

.theme-card.active {
  border-color: var(--card-accent, var(--accent));
  background: color-mix(in srgb, var(--card-accent, var(--accent)) 14%, transparent);
}

.theme-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.theme-card-name {
  font-size: 0.92rem;
  font-weight: 800;
}

.theme-card-count {
  font-size: 0.7rem;
  color: var(--muted);
  font-weight: 700;
}

.theme-card-desc {
  font-size: 0.73rem;
  color: var(--muted);
  line-height: 1.35;
}

.theme-preview-row {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.mini-chip {
  font-size: 0.68rem;
  font-weight: 800;
  padding: 3px 6px;
  border-radius: 6px;
  line-height: 1.2;
}

/* 16-Tier Almanac Grid */
.almanac-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding-right: 2px;
}

.almanac-cell {
  border-radius: 12px;
  padding: 10px 6px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 76px;
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.almanac-cell.locked {
  background: rgba(255, 255, 255, 0.04) !important;
  color: var(--muted) !important;
  border-style: dashed;
}

.almanac-val {
  font-size: 0.68rem;
  font-weight: 700;
  opacity: 0.78;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.almanac-name {
  font-size: 0.9rem;
  font-weight: 800;
  line-height: 1.2;
}

.almanac-tag {
  font-size: 0.6rem;
  opacity: 0.78;
  font-weight: 700;
}

/* Guide Body */
.guide-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 0.86rem;
  line-height: 1.55;
  color: #e2e8f0;
}

.guide-block h3 {
  margin: 0 0 4px;
  font-size: 0.92rem;
  color: var(--accent);
}

.guide-block p {
  margin: 0;
}

/* Action Buttons in Compact Modals */
.victory-crown {
  font-size: 2.3rem;
  color: #fde047;
  line-height: 1;
}

.stack-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}

.primary-btn,
.secondary-btn {
  width: 100%;
  min-height: 44px;
  border-radius: 12px;
  font-size: 0.94rem;
  font-weight: 800;
  cursor: pointer;
  border: 1px solid transparent;
}

.primary-btn {
  background: var(--accent);
  color: var(--accent-text);
}

.secondary-btn {
  background: rgba(255, 255, 255, 0.07);
  border-color: var(--border);
  color: var(--text);
}

@media (max-width: 440px) {
  .shell {
    padding: 4px 10px max(10px, env(safe-area-inset-bottom));
    gap: 8px;
  }
  .themes-grid {
    grid-template-columns: 1fr;
  }
  .almanac-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  .score-box {
    min-width: 68px;
    padding: 5px 8px;
  }
  .thumb-btn {
    font-size: 0.76rem;
    padding: 8px 4px;
    gap: 3px;
  }
}
