:root,
:root.theme-frlg {
  /* Vivid Modern Colors */
  --bg-color: #0f172a;
  --bg-gradient: linear-gradient(135deg, #2D1414 0%, #111424 50%, #132D1D 100%);
  --blob-1: rgba(239, 68, 68, 0.4);
  --blob-2: rgba(34, 197, 94, 0.3);
  --blob-3: rgba(251, 191, 36, 0.15);

  --panel-bg: rgba(255, 255, 255, 0.05);
  --panel-border: rgba(255, 255, 255, 0.1);
  --panel-blur: 20px;

  --text-main: #f8fafc;
  --text-muted: #94a3b8;

  --primary: #ef4444;
  --primary-hover: #dc2626;
  --success: #10b981;
  --danger: #ef4444;

  --ev-hp: #34d399;
  --ev-attack: #f87171;
  --ev-defense: #fbbf24;
  --ev-spatk: #38bdf8;
  --ev-spdef: #a78bfa;
  --ev-speed: #f472b6;
}

:root.theme-rs {
  --primary: #f87171;
  --bg-gradient: linear-gradient(135deg, #2D1414 0%, #141124 50%, #141B33 100%);
  --blob-1: rgba(248, 113, 113, 0.4);
  --blob-2: rgba(56, 189, 248, 0.3);
  --blob-3: rgba(251, 191, 36, 0.2);
}

:root.theme-emerald {
  --primary: #10b981;
  --bg-gradient: linear-gradient(135deg, #112B1B 0%, #1D3627 50%, #0F383D 100%);
  --blob-1: rgba(16, 185, 129, 0.4);
  --blob-2: rgba(251, 191, 36, 0.3);
  --blob-3: rgba(56, 189, 248, 0.2);
}

:root {
  /* Typography */
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  /* Layout */
  --border-radius-lg: 16px;
  --border-radius-md: 12px;
  --border-radius-sm: 8px;

  --spacing-sm: 8px;
  --spacing-md: 16px;
  --spacing-lg: 24px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  background-color: var(--bg-color);
  /* Fallback for safe-areas */
  background: var(--bg-gradient);
  color: var(--text-main);
  font-family: var(--font-family);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: pan-x pan-y;
  /* Discourage pinch-zoom gestures while allowing scrolling */
}

#background-elements {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
  pointer-events: none;
}

.blob {
  position: absolute;
  filter: blur(80px);
  border-radius: 50%;
  animation: float 20s infinite alternate ease-in-out;
}

.blob-1 {
  width: 500px;
  height: 500px;
  background: var(--blob-1);
  top: -100px;
  left: -100px;
}

.blob-2 {
  width: 600px;
  height: 600px;
  background: var(--blob-2);
  bottom: -150px;
  right: -150px;
}

.blob-3 {
  width: 400px;
  height: 400px;
  background: var(--blob-3);
  bottom: 100px;
  left: 20%;
}

@keyframes float {
  0% {
    transform: translate(0, 0) scale(1);
  }

  100% {
    transform: translate(40px, -40px) scale(1.1);
  }
}

.app-terminal {
  display: flex;
  flex-direction: column;
  height: 100vh;
  /* iOS safe-area: respect notch at top, home bar at bottom */
  padding: calc(var(--spacing-md) + env(safe-area-inset-top)) calc(var(--spacing-md) + env(safe-area-inset-right)) calc(var(--spacing-md) + env(safe-area-inset-bottom)) calc(var(--spacing-md) + env(safe-area-inset-left));
  gap: var(--spacing-md);
}

.terminal-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(var(--panel-blur));
  -webkit-backdrop-filter: blur(var(--panel-blur));
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-md);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 1000;
}

.terminal-title {
  display: flex;
  align-items: center;
}

/* Logo wordmark replacing the old SVG */
.logo-wordmark {
  font-family: var(--font-family);
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  background: linear-gradient(135deg, #38bdf8 0%, #a78bfa 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  white-space: nowrap;
  line-height: 1;
}

/* Tiling Workspace - Preserved from Terminal fix */
.workspace {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  overflow-y: auto;
  align-content: flex-start;
  padding-bottom: 20px;
}

/* Glassmorphic Tiles */
.pane-wrapper {
  flex: 1 1 450px;
  min-width: 320px;
  height: calc(50vh - 40px);
  min-height: 550px;
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
  backdrop-filter: blur(var(--panel-blur));
  -webkit-backdrop-filter: blur(var(--panel-blur));
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-lg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  position: relative;
}

.pane-wrapper:focus-within {
  border-color: rgba(255, 255, 255, 0.25);
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.4);
  z-index: 10;
}

.pane {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.pane-header {
  padding: 12px 20px;
  background: rgba(0, 0, 0, 0.2);
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pane-header h2 {
  font-size: 1.2rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.pane-header span {
  font-size: 0.9rem;
  background: var(--primary);
  color: #fff;
  padding: 2px 8px;
  border-radius: 12px;
}

/* Internal Components */
.session-tracker {
  border-bottom: 1px solid var(--panel-border);
}

.session-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px 0 16px;
}

.session-header h2 {
  font-size: 0.95rem;
  font-weight: 600;
}

.tabs {
  display: flex;
  background: rgba(0, 0, 0, 0.25);
  padding: 4px;
  margin: 12px 16px 8px 16px;
  border-radius: var(--border-radius-md);
  border: 1px solid rgba(255, 255, 255, 0.03);
  position: relative;
  z-index: 5;
}

.tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px 4px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  border-radius: var(--border-radius-sm);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  white-space: nowrap;
}

.tab-btn:hover:not(.active) {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.08);
}

.tab-btn.active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(5px);
}

.tab-content-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  position: relative;
}

.tab-pane {
  display: none;
  animation: fadeIn 0.3s ease;
}

.tab-pane.active {
  display: block;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(5px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Forms & UI */
.glass-panel {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-md);
  padding: var(--spacing-md);
  margin: 0 auto var(--spacing-md) auto;
  max-width: 800px;
}

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text-muted);
}

.custom-input,
.custom-select {
  width: 100%;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-border);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: var(--border-radius-sm);
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  transition: all 0.2s;
}

.custom-input:focus,
.custom-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2);
  background: rgba(0, 0, 0, 0.5);
}

.btn {
  cursor: pointer;
  border: none;
  border-radius: var(--border-radius-sm);
  font-family: inherit;
  font-weight: 600;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
  padding: 10px 20px;
  font-size: 0.95rem;
}

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

.btn-primary:active {
  transform: translateY(0);
}

.btn-outline {
  background: transparent;
  border: 1px solid var(--panel-border);
  color: var(--text-main);
}

.btn-outline:hover {
  background: rgba(255, 255, 255, 0.1);
}

.btn-small {
  padding: 6px 12px;
  font-size: 0.8rem;
}

.game-btn {
  background: transparent;
  border: 1px solid var(--panel-border);
  color: var(--text-muted);
  border-radius: var(--border-radius-sm);
  padding: 4px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
}

.game-btn:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.05);
}

.game-btn.active {
  color: var(--primary);
  border-color: var(--primary);
  background: rgba(255, 255, 255, 0.1);
}

.game-dropdown {
  position: relative;
  display: inline-block;
}

.game-dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 8px;
  background: #0f172a;
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-md);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  z-index: 9999;
  overflow: hidden;
  min-width: 140px;
  display: none;
  flex-direction: column;
}

.game-dropdown-menu.active {
  display: flex;
}

.game-dropdown-menu .game-btn {
  border: none;
  border-radius: 0;
  padding: 10px 16px;
  justify-content: flex-start;
  width: 100%;
}

.game-dropdown-menu .game-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Tracked Pokemon Items */
.tracked-pokemon-list {
  display: flex !important;
  flex-direction: column !important;
}

.tracked-pkm-entry {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(0, 0, 0, 0.2);
  border-radius: var(--border-radius-sm);
  padding: 8px 12px;
  margin-bottom: 8px;
  border: 1px solid var(--panel-border);
}

/* Pokemon Entries config */
.pokemon-entry {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 1.1rem;
}

/* Yield Pills */
.yield-pill {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: 800;
  color: rgba(0, 0, 0, 0.75);
  margin-left: 4px;
}

.location-tag {
  background: rgba(255, 255, 255, 0.1);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.95rem;
  margin: 2px;
  display: inline-block;
}

/* Cards */
.result-card {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-md);
  padding: var(--spacing-md);
  margin: 0 auto var(--spacing-md) auto;
  max-width: 800px;
  transition: transform 0.2s;
}

.result-card:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* Progress */
.progress-bar-container {
  height: 8px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 4px;
  margin: 12px 0;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 4px;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.3);
}

.session-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.session-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

/* Autocomplete */
.autocomplete-wrapper {
  position: relative;
}

.autocomplete-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: #1e293b;
  /* Opaque solid background */
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-md);
  margin-top: 4px;
  max-height: 250px;
  overflow-y: auto;
  z-index: 10000;
  display: none;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}

.autocomplete-item {
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 0.95rem;
  transition: all 0.2s;
}

.autocomplete-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

/* Utilities */
.w-100 {
  width: 100%;
}

.mt-3 {
  margin-top: var(--spacing-md);
}

.text-muted {
  color: var(--text-muted);
}

/* Buttons inside header */
.session-actions-top {
  display: flex;
  gap: 4px;
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  line-height: 1;
  padding: 0 4px;
  cursor: pointer;
}

.btn-icon:hover {
  color: #fff;
  opacity: 1;
}

.btn-undo:disabled,
.btn-redo:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

/* Scrollbars */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* Loader */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.9);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s;
}

.loading-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.modal-content {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--border-radius-lg);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  max-width: 600px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  transform: translateY(20px);
  transition: transform 0.3s ease;
  margin: 0;
  /* Override generic glass-panel margin if any */
}

.modal-overlay.active .modal-content {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--panel-border);
}

.modal-header h2 {
  font-size: 1.25rem;
  margin: 0;
}

.modal-body {
  padding: 20px;
  line-height: 1.6;
}

.modal-body h3 {
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--primary);
  font-size: 1.1rem;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body p {
  margin-bottom: 1rem;
  color: var(--text-main);
  opacity: 0.9;
}

/* ============================================
   RESPONSIVE / MOBILE  (≤ 640px)
   ============================================ */
@media (max-width: 640px) {

  /* ── App shell – use env() for iOS notch + home bar ── */
  .app-terminal {
    padding: calc(8px + env(safe-area-inset-top, 0px)) calc(8px + env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) calc(8px + env(safe-area-inset-left, 0px));
    gap: 8px;
    height: 100vh;
    height: 100dvh;
  }

  /* ── Header ─────────────────────────────── */
  .terminal-toolbar {
    padding: 8px 12px;
    flex-shrink: 0;
  }

  /* Compact wordmark on small screens */
  .logo-wordmark {
    font-size: 0.9rem;
  }

  /* Tighten the game-selector left border gap */
  .terminal-title .game-selector {
    padding-left: 10px !important;
  }

  /* Hide zoom controls on mobile */
  #btn-zoom-out,
  #btn-zoom-in,
  #btn-zoom-reset {
    display: none !important;
  }

  /* Hide the "+New Research" button on mobile */
  #btn-add-pane {
    display: none !important;
  }

  /* Hide inline separator divs */
  .toolbar-actions>div[style*="width: 1px"] {
    display: none !important;
  }

  .toolbar-actions {
    gap: 6px;
  }

  /* ── Workspace: vertical, scrollable ───── */
  .workspace {
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 8px;
    padding-bottom: 8px;
    overflow-y: auto;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
  }

  /*
   * Pane height = viewport minus:
   *   - app padding top/bottom  (~16px + safe areas)
   *   - toolbar height          (~48px)
   *   - gap between toolbar and workspace (8px)
   * We subtract the safe-area insets separately because the app-terminal
   * already adds them to its padding, so we just need the raw ones here
   * for the calc to hit the right number.
   */
  .pane-wrapper {
    flex: 0 0 auto;
    width: 100%;
    min-width: 0;
    height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 72px);
    min-height: 400px;
    max-height: none;
    overflow: hidden;
  }

  @supports not (height: 100dvh) {
    .pane-wrapper {
      height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 72px);
    }
  }

  /* Internal scroll with iOS momentum */
  .tab-content-container {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Tabs: all 4 fit on one row */
  .tab-btn {
    font-size: 0.75rem;
    padding: 6px 4px;
    letter-spacing: -0.01em;
  }

  .result-card,
  .glass-panel {
    max-width: 100%;
  }

  /* Modal: bottom sheet, respects home bar */
  .modal-overlay {
    align-items: flex-end;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .modal-content {
    max-width: 100%;
    width: 100%;
    max-height: calc(92dvh - env(safe-area-inset-bottom, 0px));
    border-radius: var(--border-radius-md) var(--border-radius-md) 0 0;
    margin: 0;
    transform: translateY(100%);
  }

  .modal-overlay.active .modal-content {
    transform: translateY(0);
  }
}