/* Global state helper to cleanly collapse and hide interface elements */
.hidden {
  display: none !important;
}

:root {
  --native-color-scheme: dark;
  --color-page-bg: #20232a;
  --color-app-bg: #282c34;
  --color-surface-header: #161b22;
  --color-surface-panel: #1c2025;
  --color-surface-panel-alt: #21262d;
  --color-surface-canvas: #0d1117;
  --color-surface-node-hover: #262c36;
  --color-surface-node-selected: #1f293d;
  --color-tree-line-rgb: 48, 54, 61;
  --color-suggestion-hover-bg: var(--color-surface-panel-alt);
  --color-suggestion-active-bg: #313d4f;
  --color-border-default: #30363d;
  --color-border-muted: #323842;
  --color-border-strong: #444c56;
  --color-text-primary: #f0f6fc;
  --color-text-secondary: #c9d1d9;
  --color-text-muted: #8b949e;
  --color-text-contrast: #ffffff;
  --color-accent: #58a6ff;
  --color-accent-hover: #79c0ff;
  --color-accent-active: #1f6feb;
  --color-accent-visited: #a5d6ff;
  --color-success: #238636;
  --color-success-hover: #2ea44f;
  --color-success-active: #227e3b;
  --color-danger: #da3637;
  --color-danger-hover: #b82a2b;
  --color-danger-soft: rgba(218, 54, 55, 0.15);
  --color-danger-soft-hover: rgba(218, 54, 55, 0.3);
  --color-danger-border: rgba(218, 54, 55, 0.4);
  --color-warning: #f2c744;
  --color-warning-soft: rgba(242, 199, 68, 0.15);
  --color-warning-soft-hover: rgba(242, 199, 68, 0.3);
  --color-warning-border: rgba(242, 199, 68, 0.4);
  --color-count: #ff7b72;
  --color-button-border: rgba(240, 246, 252, 0.1);
  --color-overlay: rgba(0, 0, 0, 0.75);
  --color-shadow-soft: rgba(0, 0, 0, 0.2);
  --color-shadow-medium: rgba(0, 0, 0, 0.3);
  --color-shadow-strong: rgba(0, 0, 0, 0.4);
  --color-shadow-heavy: rgba(0, 0, 0, 0.5);
  --color-glow-accent: rgba(88, 166, 255, 0.3);
}

body[data-theme="light"] {
  --native-color-scheme: light;
  --color-page-bg: #d7dee8;
  --color-app-bg: #eef3f8;
  --color-surface-header: #f8fbff;
  --color-surface-panel: #eef2f7;
  --color-surface-panel-alt: #ffffff;
  --color-surface-canvas: #f4f7fb;
  --color-surface-node-hover: #e9f2ff;
  --color-surface-node-selected: #dbeafe;
  --color-tree-line-rgb: 174, 185, 198;
  --color-suggestion-hover-bg: #e3edff;
  --color-suggestion-active-bg: #c3d9ff;
  --color-suggestion-active-text: #0b3d91;
  --color-border-default: #c7d1dc;
  --color-border-muted: #d2dae4;
  --color-border-strong: #aeb9c6;
  --color-text-primary: #18212b;
  --color-text-secondary: #334155;
  --color-text-muted: #526173;
  --color-text-contrast: #ffffff;
  --color-accent: #0969da;
  --color-accent-hover: #218bff;
  --color-accent-active: #0550ae;
  --color-accent-visited: #6f42c1;
  --color-success: #1a7f37;
  --color-success-hover: #2da44e;
  --color-success-active: #116329;
  --color-danger: #cf222e;
  --color-danger-hover: #a40e26;
  --color-danger-soft: rgba(207, 34, 46, 0.1);
  --color-danger-soft-hover: rgba(207, 34, 46, 0.18);
  --color-danger-border: rgba(207, 34, 46, 0.28);
  --color-warning: #9a6700;
  --color-warning-soft: rgba(154, 103, 0, 0.12);
  --color-warning-soft-hover: rgba(154, 103, 0, 0.2);
  --color-warning-border: rgba(154, 103, 0, 0.28);
  --color-count: #cf222e;
  --color-button-border: rgba(27, 31, 36, 0.12);
  --color-overlay: rgba(15, 23, 42, 0.35);
  --color-shadow-soft: rgba(15, 23, 42, 0.08);
  --color-shadow-medium: rgba(15, 23, 42, 0.12);
  --color-shadow-strong: rgba(15, 23, 42, 0.16);
  --color-shadow-heavy: rgba(15, 23, 42, 0.18);
  --color-glow-accent: rgba(9, 105, 218, 0.2);
}

/* Hard target for your guess input box and select menus */
input,
select {
  color-scheme: inherit !important;
}

/* --- Core Layout Configuration --- */
body {
  margin: 0;
  background: var(--color-page-bg);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--color-text-primary);
  color-scheme: var(--native-color-scheme);

  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.fullscreen {
  height: 100vh;
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  background: var(--color-app-bg);
}

.top {
  display: flex;
  flex: 1;
  min-height: 0;
  width: 100%;
}

/* --- App Master Header Styles --- */
.app-header {
  height: 50px;
  background-color: var(--color-surface-header);
  border-bottom: 1px solid var(--color-border-default);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 20px;
  box-sizing: border-box;
  z-index: 2000; /* Stays fixed above mobile bottom panels and canvas streams */

  position: relative;
}

.app-title {
  font-size: 1.5rem;
  color: var(--color-text-primary);
  margin: 0;
  font-weight: 600;

  position: absolute;
  left: 50%;
  top: 50%;

  /* Pushes the exact mathematical midpoint of the text block to match the pixel center */
  transform: translate(-50%, -50%);

  /* Prevents your text title from accidentally wrapping onto two lines on very narrow phone screens */
  white-space: nowrap;

  /* Safety precaution: ensures a click right over the title text passes through
       to any canvas or element resting behind it */
  pointer-events: none;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.nav-icon-btn {
  background: var(--color-surface-panel-alt);
  border: 1px solid var(--color-border-default);
  color: var(--color-text-secondary);
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  transition:
    background-color 0.2s,
    border-color 0.2s;
}

.nav-icon-btn:hover {
  background-color: var(--color-border-default);
  border-color: var(--color-text-muted);
  color: var(--color-text-primary);
}

/* Swaps out the old google font formatting rule for modern SVG layout logic */
.nav-icon-btn .nav-svg-icon {
  width: 20px;
  height: 20px;
  stroke: currentColor; /* Forces the SVG lines to dynamically match your hover text colors! */
  transition: transform 0.2s;
  display: block;
}

.theme-toggle-btn .theme-icon {
  display: none;
}

body[data-theme="dark"] .theme-toggle-btn .theme-icon-sun,
body[data-theme="light"] .theme-toggle-btn .theme-icon-moon {
  display: block;
}

/* --- Modal Architecture Profiles --- */
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--color-border-default);
  padding-bottom: 12px;
  margin-bottom: 16px;
}

.modal-content h2,
.modal-content .modal-header h2,
#modal-title {
  font-size: 1.4rem !important; /* !important forces the browser to prioritize this size */
  font-weight: 600 !important;
  color: var(--color-text-primary) !important;
  margin: 0;
  line-height: 1.4;
}

.close-modal-btn {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 1.75rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
}

.close-modal-btn:hover {
  color: var(--color-text-primary);
}

/* Variation A: Floating Compact Settings Modal Configuration Profile */
.modal-content.floating-window {
  background: var(--color-surface-header);
  border: 1px solid var(--color-border-default);
  border-radius: 8px;
  padding: 24px;
  max-width: 460px;
  width: 90%;
  box-shadow: 0 8px 32px var(--color-shadow-heavy);
  text-align: left;
}

/* Variation B: Screen takeover profile panel */
.modal-content.fullscreen-takeover {
  background: var(--color-surface-canvas);
  max-width: 900px;
  width: 100vw;
  height: 100vh;
  padding: 40px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  text-align: left;
}

.modal-body.scrollable {
  flex: 1;
  overflow-y: auto;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.explainer-symbol-list {
  list-style: none;
  padding: 0 0 0 0.5em;
  color: var(--color-text-muted);
}

.explainer-symbol-list li {
  display: grid;
  grid-template-columns: 1.5em minmax(0, 1fr);
  gap: 0.6em;
  margin-bottom: 0.5em;
}

.explainer-symbol-list li::before {
  content: attr(data-symbol);
  text-align: center;
}

/* --- Stats Dashboard Styles --- */
.stats-dashboard {
  margin-bottom: 20px;
}

.stats-dashboard h3,
.settings-group h3 {
  font-size: 1.05rem;
  color: var(--color-accent);
  margin: 0 0 12px 0;
}

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

/* Guess-distribution controls and chart. */
.guess-distribution {
  margin: 20px 0;
  padding-top: 16px;
  border-top: 1px solid var(--color-border-default);
}

.guess-distribution-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.guess-distribution-header h3 {
  margin: 0;
}

.stats-view-toggle {
  display: inline-flex;
  border: 1px solid var(--color-border-default);
  border-radius: 6px;
  overflow: hidden;
}

.stats-view-btn {
  border: 0;
  border-right: 1px solid var(--color-border-default);
  background: var(--color-surface-panel-alt);
  color: var(--color-text-muted);
  padding: 5px 9px;
  font-size: 0.78rem;
  cursor: pointer;
}

.stats-view-btn:last-child {
  border-right: 0;
}

.stats-view-btn.active {
  background: var(--color-accent);
  color: var(--color-text-contrast);
}

.guess-histogram-scroll {
  position: relative;
  width: 100%;
  overflow-x: hidden;
}

.guess-histogram-empty {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 1;
  transform: translate(-50%, -50%);
  padding: 6px 10px;
  border: 1px solid var(--color-border-default);
  border-radius: 4px;
  background: var(--color-surface-panel-alt);
  color: var(--color-text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
  pointer-events: none;
}

.guess-histogram {
  display: grid;
  grid-template-columns: repeat(26, minmax(0, 1fr));
  gap: 2px;
  width: 100%;
  max-width: 100%;
  height: 120px;
  box-sizing: border-box;
}

.guess-histogram-column {
  display: grid;
  grid-template-rows: 1fr 13px;
  min-width: 0;
  text-align: center;
}

.guess-histogram-column.failure .guess-histogram-bar {
  background: var(--color-danger);
}

.guess-histogram-column.median .guess-histogram-bar {
  background: var(--color-success);
}

.guess-histogram-count,
.guess-histogram-label {
  color: var(--color-text-muted);
  font-size: 0.58rem;
  line-height: 1;
}

.guess-histogram-bar-area {
  position: relative;
  display: flex;
  align-items: end;
  min-height: 0;
  border-bottom: 1px solid var(--color-border-default);
}

.guess-histogram-count {
  position: absolute;
  bottom: calc(var(--guess-histogram-bar-height) + 3px);
  width: 100%;
  overflow: hidden;
  text-overflow: clip;
  white-space: nowrap;
}

.guess-histogram-bar {
  width: 100%;
  min-height: 0;
  background: var(--color-accent);
  border-radius: 3px 3px 0 0;
}

.stat-card {
  background: var(--color-surface-panel-alt);
  border: 1px solid var(--color-border-default);
  border-radius: 6px;
  padding: 12px 6px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-text-primary);
  line-height: 1.2;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.modal-divider {
  border: 0;
  border-top: 1px solid var(--color-border-default);
  margin: 20px 0;
}

.settings-group p {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin: 0 0 12px 0;
  line-height: 1.4;
}

.settings-help {
  min-height: 2.5em;
}

.danger-btn {
  background-color: var(--color-danger);
  color: var(--color-text-primary);
  border: 1px solid var(--color-button-border);
  border-radius: 6px;
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 500;
  margin: 5px 0 0 0;
}

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

/* --- Individual Setting Component Wrapper --- */
.setting-item {
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.setting-label {
  font-size: 0.85rem;
  /*font-weight: 600;*/
  color: var(--color-text-muted);
  /*text-transform: uppercase;*/
  letter-spacing: 0.5px;
}

/* --- Segmented Radio Buttons Style --- */
.segmented-control {
  display: flex;
  background: var(--color-surface-canvas);
  border: 1px solid var(--color-border-default);
  border-radius: 6px;
  overflow: hidden;
  padding: 2px;
}

.segmented-control input[type="radio"] {
  display: none; /* Hide native ugly browser circles */
}

.segmented-control label {
  flex: 1;
  text-align: center;
  padding: 8px 12px;
  font-size: 0.9rem;
  color: var(--color-text-secondary);
  cursor: pointer;
  border-radius: 4px;
  transition:
    background-color 0.15s,
    color 0.15s;
}

.segmented-control input[type="radio"]:checked + label {
  background: var(--color-surface-panel-alt);
  color: var(--color-accent);
  font-weight: 600;
  box-shadow: 0 1px 3px var(--color-shadow-medium);
}

.segmented-control input[type="radio"]:disabled + label,
.custom-select:disabled,
.secondary-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* --- Custom Dropdown Menu Style --- */
.custom-select {
  background: var(--color-surface-canvas);
  border: 1px solid var(--color-border-default);
  color: var(--color-text-primary);
  border-radius: 6px;
  padding: 10px;
  font-size: 0.95rem;
  outline: none;
  cursor: pointer;
  width: 100%;
  box-sizing: border-box;
  transition: border-color 0.2s;
}

.custom-select:focus {
  border-color: var(--color-accent);
}

/* --- Action Control Layout Footprint --- */
.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 24px;
}

.secondary-btn {
  background-color: var(--color-surface-panel-alt);
  color: var(--color-text-secondary);
  border: 1px solid var(--color-button-border);
  border-radius: 6px;
  padding: 8px 16px;
  cursor: pointer;
  font-weight: 500;
  margin: 0px 0 0 0;
}

.secondary-btn:hover {
  background-color: var(--color-border-default);
  border-color: var(--color-text-muted);
  color: var(--color-text-primary);
}

/* Ensure the danger button matches the new layout width profile precisely */
.settings-actions .danger-btn {
  margin: 0; /* Clear out old custom legacy margins */
  width: 100%;
}

/* --- Left Panel UI Layout --- */
.left-panel {
  width: 360px;
  min-width: 360px; /* Prevents shrinking */
  min-height: 0;
  background: var(--color-surface-panel);
  border-right: 1px solid var(--color-border-muted);
  display: flex;
  flex-direction: column; /* Stacks controls neatly above the info card */
  box-sizing: border-box;
}

/* Controls Header Container */
.controls-section {
  padding: 20px;
  background: var(--color-surface-panel-alt);
  border-bottom: 1px solid var(--color-border-muted);
}

/* Add this to style the new wrapper and button layout */
.input-group {
  display: flex;
  gap: 8px; /* Space between input box and button */
  width: 100%;
}

/* relative context ensures absolute child aligns to it */
.input-wrapper {
  position: relative;
  flex: 1; /* Inherits the behavior to push the guess button right */
}

/* Force the input box to stretch to 100% of its wrapper's width */
.guess-input {
  width: 100%; /* CRITICAL: This makes the input fill the wide wrapper completely */
  padding: 12px;
  border-radius: 6px;
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface-canvas);
  color: var(--color-text-primary);
  font-size: 1rem;
  box-sizing: border-box;
  outline: none;
}

.guess-input:focus {
  border-color: var(--color-accent);
}

/* The floating suggestion panel */
.suggestions-dropdown {
  position: absolute;
  top: 100%; /* Sits exactly below the input field */
  left: 0;
  width: 100%;
  max-height: 200px; /* Limits size to force scrollbars if many matches */
  overflow-y: auto; /* Vertical scroll enabled */
  overflow-x: hidden;
  background: var(--color-surface-panel);
  border: 1px solid var(--color-border-strong);
  border-radius: 6px;
  margin-top: 4px;
  box-shadow: 0 4px 12px var(--color-shadow-medium);
  z-index: 10; /* Keeps it layered above images and text */
  box-sizing: border-box;
}

/* Custom scrollbar styling specifically for the suggestions dropdown */
.suggestions-dropdown::-webkit-scrollbar {
  width: 6px;
}

.suggestions-dropdown::-webkit-scrollbar-track {
  background: var(--color-surface-panel);
  border-radius: 0 6px 6px 0; /* Smoothly rounds the corners matching the container */
}

.suggestions-dropdown::-webkit-scrollbar-thumb {
  background-color: var(--color-border-muted);
  border-radius: 3px;
}

.suggestions-dropdown::-webkit-scrollbar-thumb:hover {
  background-color: var(--color-border-strong);
}

/* State utility class to hide the dropdown when empty */
.suggestions-dropdown.hidden {
  display: none;
}

/* Individual list entries */
.suggestion-item {
  padding: 10px 12px;
  cursor: pointer;
  color: var(--color-text-secondary);
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Hover state for mouse navigation */
.suggestion-item:hover {
  background: var(--color-suggestion-hover-bg);
  color: var(--color-accent);
}

/* Keyboard active state tracked via JavaScript */
.suggestion-item.active {
  background: var(--color-suggestion-active-bg);
  color: var(--color-suggestion-active-text, var(--color-text-contrast));
  outline: none;
}

/* New Button Styling */
.submit-btn {
  padding: 0 16px;
  background-color: var(--color-success);
  color: var(--color-text-contrast);
  border: 1px solid var(--color-button-border);
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.1s ease;
}

.submit-btn:hover {
  background-color: var(--color-success-hover);
}

.submit-btn:active {
  background-color: var(--color-success-active);
}

.guesses-left-box {
  margin-top: 12px;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--color-text-muted);
}

.game-mode-label {
  font-size: 0.72rem;
  line-height: 1.2;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-accent);
  margin-bottom: 2px;
}

.game-mode-label[data-ready="false"] {
  visibility: hidden;
}

.guesses-left-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}

.count-highlight {
  color: var(--color-count);
  font-size: 1.1rem;
}

/* --- Controls Master Row Framework --- */
.game-controls-bar {
  display: flex;
  align-items: center;
  gap: 8px; /* Perfectly spaces out container boxes and action items */
  margin-bottom: 0px;
}

/* Ensure your legacy container box strips away accidental display margin blocks */
.guesses-left-box {
  margin: 0;
  flex-grow: 1; /* Lets the counter stretch to fill remaining room, pushing buttons to the right */
}

/* --- Shared Game Control Button Foundation --- */
.control-btn {
  height: 38px; /* Matches the vertical height profile of your guesses counter box */
  padding: 0 16px;
  margin: 8px 0 0 0;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  transition:
    background-color 0.15s,
    border-color 0.15s,
    opacity 0.2s;
}

/* --- 1. Yellow Hint Button Configuration --- */
.hint-btn {
  background-color: var(--color-warning-soft);
  border: 1px solid var(--color-warning-border);
  color: var(--color-warning);
}

.hint-btn:hover:not(:disabled) {
  background-color: var(--color-warning-soft-hover);
  border-color: var(--color-warning);
}

/* Disabled State: Grays out cleanly when game.canHint() evaluates false */
.hint-btn:disabled {
  background-color: var(--color-surface-panel-alt);
  border-color: var(--color-border-default);
  color: var(--color-text-muted);
  cursor: not-allowed;
  opacity: 0.5;
}

/* --- 2. Red Restart Button Configuration --- */
.restart-btn {
  background-color: var(--color-danger-soft);
  border: 1px solid var(--color-danger-border);
  color: var(--color-danger);
  padding: 0 12px; /* Narrower square padding footprint for standalone icons */
}

.restart-btn:hover:not(:disabled) {
  background-color: var(--color-danger-soft-hover);
  border-color: var(--color-danger);
  color: var(--color-text-primary);
}

/* Daily results can be viewed after completion, while unfinished dailies stay locked. */
.restart-btn:disabled,
.daily-result-btn:disabled {
  background-color: var(--color-surface-panel-alt);
  border-color: var(--color-border-default);
  color: var(--color-text-muted);
  cursor: not-allowed;
  opacity: 0.5;
}

.restart-btn .control-svg-icon {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  display: block;
  transition: transform 0.25s ease;
}

/* Blue history control for reopening the completed daily board. */
.daily-result-btn {
  background-color: var(--color-accent);
  border: 1px solid var(--color-accent);
  color: var(--color-text-contrast);
  padding: 0 10px;
}

.daily-result-btn:hover:not(:disabled) {
  background-color: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
}

/* --- Clade Info Card Component --- */
.clade-card {
  flex: 1;
  min-height: 0; /* CRITICAL: Allows flex items to shrink below content size in Firefox/Chrome */
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sci-name {
  margin: 0;
  font-size: 1.8rem;
  font-style: italic;
  color: var(--color-accent);
}

.com-name {
  margin: 4px 0 0 0;
  font-size: 1.1rem;
  color: var(--color-text-muted);
  font-weight: 500;
}

/* Ensure the header text doesn't overflow horizontally if the name is massive */
.card-header {
  overflow-x: hidden;
  text-overflow: ellipsis;
}

/* Add this brand new class to drive the vertical scroll layout */
.clade-scroll-content {
  flex: 1;
  min-height: 0;
  overflow-y: auto; /* Enables smooth vertical scrolling */
  overflow-x: hidden; /* Hard blocks horizontal scrolling */
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-right: 4px; /* Tiny buffer so the scrollbar doesn't hug the text tightly */
}

/* Optional: Clean look for Webkit browsers (Chrome/Safari/Edge) to make the scrollbar minimal */
.clade-scroll-content::-webkit-scrollbar {
  width: 6px;
}

.clade-scroll-content::-webkit-scrollbar-track {
  background: transparent;
}

.clade-scroll-content::-webkit-scrollbar-thumb {
  background-color: var(--color-border-muted);
  border-radius: 3px;
}

/* 1. Modify the wrapper to let it expand naturally */
.card-image-wrapper {
  width: 100%;
  background: var(--color-surface-canvas);
  border-radius: 8px;
  border: 1px solid var(--color-border-muted);
  overflow: hidden;
  display: block; /* Changed from flex to block for standard document flow */
  flex-shrink: 0;
}

/* 2. Update the image rules to prevent vertical clipping */
.clade-img {
  width: 100%; /* Forces it to match the left panel's width */
  height: auto; /* Automatically calculates correct proportional height */
  max-height: none; /* REMOVE or override the previous 220px restriction */
  object-fit: contain; /* Ensures the entire image canvas displays without truncation */
  display: block; /* Eliminates unwanted inline bottom margins */
}

.card-body-text {
  line-height: 1.6;
  font-size: 0.95rem;
  color: var(--color-text-secondary);
}

/* --- Right Panel UI Layout --- */
.right-panel {
  flex: 1;
  background: var(--color-surface-canvas);
  overflow-y: auto; /* Allow vertical scrolling if the tree gets deep */
  overflow-x: hidden; /* Lock the horizontal plane to your screen width */
  position: relative;
}

.right-panel::-webkit-scrollbar {
  width: 8px; /* Slightly wider track for easy clicking */
}

.right-panel::-webkit-scrollbar-track {
  background: var(--color-surface-canvas);
}

.right-panel::-webkit-scrollbar-thumb {
  background-color: var(--color-surface-panel-alt);
  border-radius: 4px;
}

.right-panel::-webkit-scrollbar-thumb:hover {
  background-color: var(--color-border-default);
}

.tree-container {
  position: relative;
  width: 100%; /* Snaps perfectly to the remaining game panel space */
  min-height: 100%;
  height: auto;
}

.tree-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.node-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

/* --- Optimized Small Node Component --- */
.tree-node {
  --node-proximity-hue: 60deg;
  position: absolute;
  border: 1px solid hsl(var(--node-proximity-hue) 45% 34%);
  border-radius: 4px;
  padding: 6px 10px;
  transform: translate(
    -50%,
    -50%
  ); /* Keeps node centered on its physics particle */
  text-align: center;
  cursor: pointer;
  user-select: none;
  box-shadow: 0 2px 4px var(--color-shadow-soft);

  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  background: hsl(var(--node-proximity-hue) 38% 21%);
  color: var(--color-text-primary);
}

.tree-node-label {
  display: -webkit-box;
  width: fit-content;
  max-width: 92px;
  line-height: 1.12;
  white-space: normal;
  overflow-wrap: normal;
  word-break: normal;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.tree-node:hover {
  border-color: hsl(var(--node-proximity-hue) 60% 52%);
  background: hsl(var(--node-proximity-hue) 42% 28%);
}

.tree-node.selected {
  border-color: hsl(var(--node-proximity-hue) 70% 58%);
  background: hsl(var(--node-proximity-hue) 46% 34%);
  box-shadow: 0 0 8px hsl(var(--node-proximity-hue) 70% 58% / 0.28);
}

.clade-node {
  font-style: italic;
}

.species-node {
  background: var(--color-surface-panel);
  border-color: hsl(var(--node-proximity-hue) 62% 54%);
  color: hsl(var(--node-proximity-hue) 68% 62%);
}

.species-node:hover {
  background: var(--color-surface-panel-alt);
  border-color: hsl(var(--node-proximity-hue) 70% 60%);
  color: hsl(var(--node-proximity-hue) 74% 68%);
}

.species-node.selected {
  background: var(--color-surface-panel-alt);
  border-color: hsl(var(--node-proximity-hue) 78% 66%);
  color: hsl(var(--node-proximity-hue) 82% 72%);
  box-shadow: 0 0 8px hsl(var(--node-proximity-hue) 78% 66% / 0.3);
}

body[data-theme="light"] .tree-node {
  border-color: hsl(var(--node-proximity-hue) 42% 68%);
  background: hsl(var(--node-proximity-hue) 70% 88%);
}

body[data-theme="light"] .tree-node:hover {
  border-color: hsl(var(--node-proximity-hue) 55% 58%);
  background: hsl(var(--node-proximity-hue) 72% 82%);
}

body[data-theme="light"] .tree-node.selected {
  border-color: hsl(var(--node-proximity-hue) 62% 50%);
  background: hsl(var(--node-proximity-hue) 74% 76%);
  box-shadow: 0 0 8px hsl(var(--node-proximity-hue) 62% 50% / 0.2);
}

body[data-theme="light"] .species-node {
  background: var(--color-surface-panel-alt);
  border-color: hsl(var(--node-proximity-hue) 56% 52%);
  color: hsl(var(--node-proximity-hue) 70% 34%);
}

body[data-theme="light"] .species-node:hover {
  background: var(--color-surface-header);
  border-color: hsl(var(--node-proximity-hue) 62% 46%);
  color: hsl(var(--node-proximity-hue) 76% 30%);
}

body[data-theme="light"] .species-node.selected {
  background: var(--color-surface-header);
  border-color: hsl(var(--node-proximity-hue) 68% 40%);
  color: hsl(var(--node-proximity-hue) 82% 26%);
  box-shadow: 0 0 8px hsl(var(--node-proximity-hue) 68% 40% / 0.18);
}

.tree-node {
  box-sizing: border-box;
}

/* --- Game Over Modal Overlay --- */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: var(--color-overlay);
  z-index: 9999; /* Forces it above all navigation, canvas nodes, and cards */
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 1;
  transition: opacity 0.3s ease;
}

/* Structural utility class to toggle state */
.modal-overlay.hidden {
  display: none;
  pointer-events: none;
  opacity: 0;
}

.modal-content {
  background: var(--color-surface-header);
  border: 1px solid var(--color-border-default);
  border-radius: 8px;
  padding: 30px 40px;
  text-align: center;
  max-width: 400px;
  width: 90%;
  box-shadow: 0 8px 24px var(--color-shadow-heavy);
  position: relative;
}

#game-over-modal .modal-content {
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

.game-over-close-btn {
  position: absolute;
  top: 14px;
  right: 16px;
}

.modal-content h2 {
  font-size: 1.8rem;
  margin-top: 0;
  margin-bottom: 10px;
  color: var(--color-text-primary);
}

.modal-content p {
  color: var(--color-text-muted);
  font-size: 1rem;
  margin-bottom: 24px;
}

.game-over-stats {
  display: grid;
  gap: 10px;
  padding-top: 16px;
  border-top: 1px solid var(--color-border-default);
  text-align: left;
}

.game-over-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.share-button {
  background-color: var(--color-accent);
  border: 1px solid var(--color-accent);
  border-radius: 6px;
  color: var(--color-text-contrast);
  padding: 9px 16px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.share-button:hover {
  background-color: var(--color-accent-hover);
  border-color: var(--color-accent-hover);
}

.game-over-stat-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.game-over-stat-label {
  color: var(--color-text-secondary);
}

.game-over-stat-value {
  color: var(--color-text-primary);
}

.game-over-guess-scores {
  margin-top: 18px;
  margin-bottom: 18px;
  text-align: left;
}

.game-over-guess-scores summary {
  color: var(--color-text-secondary);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.game-over-guess-scores .game-over-guess-score-explainer {
  margin: 10px 0 0;
  color: var(--color-text-muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.game-over-guess-score-table-wrap {
  max-height: 180px;
  overflow-y: auto;
}

.game-over-guess-score-table {
  width: 100%;
  margin-top: 12px;
  border-collapse: collapse;
  table-layout: fixed;
  color: var(--color-text-muted);
}

.game-over-guess-score-table th,
.game-over-guess-score-table td {
  padding: 6px 0;
  border-bottom: 1px solid var(--color-border-default);
}

.game-over-guess-score-table th {
  text-align: left;
  font-weight: 400;
}

.game-over-guess-score-table thead th {
  position: sticky;
  top: 0;
  background: var(--color-surface-header);
  color: var(--color-text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.game-over-guess-score-table th:first-child {
  width: 56%;
  overflow-wrap: anywhere;
}

.game-over-guess-score-table th:not(:first-child),
.game-over-guess-score-table td {
  width: 22%;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.game-over-next-daily {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 18px;
  margin-bottom: 12px;
  padding-top: 16px;
  border-top: 1px solid var(--color-border-default);
}

.game-over-next-daily-label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-secondary);
}

.game-over-next-daily-value {
  font-size: 1.2rem;
  color: var(--color-text-primary);
}

.restart-button {
  background-color: var(--color-success);
  color: var(--color-text-contrast);
  border: 1px solid var(--color-button-border);
  border-radius: 6px;
  padding: 10px 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.2s;
}

.restart-button:hover {
  background-color: var(--color-success-hover);
}

.pwa-instructions-card {
  background: var(--color-surface-panel-alt);
  border: 1px solid var(--color-border-default);
  border-radius: 6px;
  margin-top: 10px;
  padding: 16px;
}

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

.pwa-instructions-list {
  margin: 0;
  padding-left: 20px;
  color: var(--color-text-secondary);
  line-height: 1.8;
}

.pwa-instructions-list li {
  margin-bottom: 10px;
}

.pwa-instructions-list li:last-child {
  margin-bottom: 0;
}

.pwa-inline-icon {
  width: 20px;
  height: 20px;
  margin: 0 4px;
  vertical-align: middle;
  display: inline-block;
  fill: currentColor;
}

.pwa-install-btn {
  width: 100%;
  margin-top: 15px;
  padding: 12px;
}

/* ============================================================================
   MOBILE RESPONSIVE BREAKPOINT (< 600px)
   ============================================================================ */

/* --- Global Link Styling (Targeting FAQ and Modals) --- */
.modal-content a {
  color: var(--color-accent);
  text-decoration: none;

  /* Adds a subtle underline transition for better UX */
  border-bottom: 1px solid transparent;
  transition:
    border-bottom-color 0.2s,
    color 0.2s;

  /* Ensures the tap target is comfortable on mobile */
  padding: 2px 0;
}

.modal-content a:hover {
  color: var(--color-accent-hover);
  border-bottom-color: var(--color-accent-hover);
  text-decoration: none;
}

.modal-content a:active {
  color: var(--color-accent-active);
}

.modal-content a:visited {
  color: var(--color-accent-visited);
}

* {
  -webkit-tap-highlight-color: transparent;
}

/* --- Responsive Adjustments (< 600px) --- */
@media (max-width: 600px) {
  /* 1. Prevent the root document body from dragging/bouncing on swipe */
  html,
  body {
    overflow: hidden !important;
    position: fixed !important;
    width: 100% !important;
    height: 100% !important;
  }

  /* 1. Turn the main parent container vertical */
  .top {
    flex-direction: column;
    height: 100vh;
    /*height: calc(100vh - 50px);*/
    width: 100vw;
    overflow: hidden;
  }

  .app-header {
    justify-content: space-between;
    padding: 0 12px; /* Marginally tighten outer margins */
  }

  .app-title {
    position: static; /* Returns the title to the normal layout grid flow */
    transform: none; /* Discards the desktop offset alignment math */
    font-size: 1.5rem; /* Slightly reduce font size so it has more room to breathe */
    pointer-events: auto; /* Re-enable tap interactions if needed */
  }

  .header-actions {
    gap: 6px; /* Tighten the gap between the Settings and FAQ buttons */
  }

  .nav-icon-btn {
    padding: 5px 10px; /* Slightly compress button sizes */
    font-size: 0.85rem;
  }

  .modal-content.fullscreen-takeover {
    padding: 20px;
  }

  /* 2. Make the Left Panel act as a fixed container on mobile */
  .left-panel {
    width: 100% !important;
    min-width: 100% !important;
    height: auto !important; /* Lets content dictate height block profiles */
    flex-shrink: 0;
    border-right: none;
    border-bottom: 1px solid var(--color-border-muted);
  }

  /* 3. Style the input controls to stick firmly to the top of the mobile device */
  .controls-section {
    background: var(--color-surface-header);
    padding: 12px 16px;
    border-bottom: 1px solid var(--color-border-default);
    position: sticky;
    top: 0;
    z-index: 100;
    box-sizing: border-box;
  }

  /* 4. Force the interactive tree container to occupy the middle viewport space */
  .right-panel {
    flex: 1;
    width: 100%;
    position: relative;

    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    height: auto;
  }

  .tree-container {
    width: 100%;
    min-height: 100%;
    height: auto;
  }

  .tree-node {
    padding: 4px 6px;
    width: auto;
    min-width: 0;
    box-sizing: border-box;
  }

  .tree-node-label {
    font-size: 0.72rem;
  }

  /* 1. When the sheet is open, block all touch interactions on the tree panel */
  .clade-card-active .right-panel {
    pointer-events: none !important;
    user-select: none !important;
  }

  /* 5. Transform the Clade Card into the Bottom Sheet Drawer */
  .clade-card {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100vw;
    /*height: 420px;*/

    /* FIX 1: Make the sheet fill the entire viewport screen height */
    height: calc(100vh - var(--header-height, 100px));

    background: var(--color-surface-header);
    border-top: 1px solid var(--color-border-default);
    border-left: none; /* Strip legacy desktop borders */
    border-radius: 16px 16px 0 0;
    z-index: 9; /* Float above the canvas lines and node dots */
    box-shadow: 0 -4px 20px var(--color-shadow-strong);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;

    /* Hardware accelerated transition setup */
    /*
    transform: translateY(
      350px
    );
    transition: transform 0.35s cubic-bezier(0.25, 1, 0.5, 1); */

    transform: translateY(var(--sheet-peek, 100vh - 170px));
    transition: transform 0.35s cubic-bezier(0.25, 1, 0.5, 1);

    /*touch-action: none;*/
    pointer-events: auto !important;
  }

  /* When this class is active, the drawer slides up smoothly */
  .clade-card.expanded {
    transform: translateY(0px);
  }

  /* Setup a visual dragging anchor area out of the existing header element */
  .card-header {
    padding: 12px 20px;
    cursor: grab;
    position: relative;
    background: var(--color-surface-panel);
    border-radius: 16px 16px 0 0;
  }

  /* Generate a decorative modern pill handle bar inside the header via CSS */
  .card-header::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    background: var(--color-border-default);
    border-radius: 2px;
    margin: -4px auto 8px auto;
  }

  /* Ensure scrollable text works comfortably within the sheet constraints */
  .clade-scroll-content {
    flex: 1;
    padding: 16px 20px;

    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important; /* Re-enables iOS momentum tracking */
    touch-action: pan-y; /* Tells iOS to allow scrolling exclusively on this axis */
    pointer-events: auto !important; /* Force touch engine assignment */
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .stat-value {
    font-size: 1.3rem;
  }
}

/* Use readable horizontal rows when 26 vertical labels no longer fit. */
@media (max-width: 550px) {
  #stats-modal .modal-content.floating-window {
    max-height: calc(100vh - 32px);
    display: flex;
    flex-direction: column;
  }

  #stats-modal .modal-body {
    min-height: 0;
    overflow-y: auto;
  }

  .guess-histogram {
    display: flex;
    flex-direction: column;
    gap: 5px;
    height: auto;
  }

  .guess-histogram-column {
    display: grid;
    grid-template-columns: 24px minmax(0, 1fr);
    grid-template-rows: 20px;
    gap: 6px;
    height: 20px;
    align-items: center;
  }

  .guess-histogram-label {
    grid-column: 1;
    grid-row: 1;
  }

  .guess-histogram-bar-area {
    grid-column: 2;
    grid-row: 1;
    height: 20px;
    border-bottom: 0;
    border-left: 1px solid var(--color-border-default);
  }

  .guess-histogram-bar {
    width: var(--guess-histogram-bar-height);
    height: 100% !important;
    border-radius: 0 3px 3px 0;
  }

  .guess-histogram-count {
    top: 50%;
    bottom: auto;
    left: calc(var(--guess-histogram-bar-height) + 4px);
    width: auto;
    transform: translateY(-50%);
  }
}
