:root {
  --bg: #0f1020;
  --panel: #1b1d38;
  --panel-2: #14162b;
  --text: #e6f3ff;
  --muted: #9db0c6;
  --primary: #00e0a4;
  --primary-dark: #00b381;
  --danger: #ff4d6d;
  --danger-dark: #d43a55;
  --accent: #ffd166;
  --shadow: #000000;
  /* spacing tokens */
  --space-1: 6px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --control-h: 40px;
}

/* Reset & base */
* { box-sizing: border-box; }
html, body { height: 100%; }
/* Make body respect dynamic viewport heights to avoid iOS address-bar gaps */
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Press Start 2P', 'VT323', system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  image-rendering: pixelated;
  /* Fallback order for viewport sizing: vh -> svh -> dvh */
  min-height: 100vh;
  min-height: 100svh;
  min-height: 100dvh;
}

.skip-link {
  position: absolute;
  top: -999px;
  left: 0;
  padding: 8px 16px;
  background: var(--primary);
  color: #00130b;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  z-index: 9999;
  text-decoration: none;
}
.skip-link:focus { top: 0; }

.container {
  max-width: 960px;
  margin: 0 auto;
  /* Use column flex so header and views stack naturally and the main page scrolls (single scroll container)
     Add safe-area-inset-bottom to avoid overlap with home indicator / PWA UI */
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 24px 16px 48px;
  padding-bottom: calc(48px + env(safe-area-inset-bottom, 0px));
  min-height: 100dvh;
}

.brand {
  text-align: center;
}
.brand h1 {
  margin: 8px 0 4px;
  font-size: clamp(28px, 5vw, 48px);
  letter-spacing: 4px;
  color: var(--accent);
  text-shadow: 0 0 0 var(--shadow), 2px 2px 0 var(--shadow), 4px 4px 0 #2a2a2a;
}
.brand .tagline {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.card {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 4px solid #0b0c1a;
  box-shadow: 0 0 0 4px #060714, 8px 8px 0 0 #0006;
  border-radius: 0; /* pixel look */
  padding: 16px;
}

/* Views & Tabs */
.tabs {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  margin: 8px 0 0;
}
.tab {
  background: #0b0c1a;
  color: var(--text);
  border: 3px solid #2a2d55;
  padding: 10px 12px;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  letter-spacing: 1px;
  cursor: pointer;
}
.tab.active { background: var(--primary); color: #00130b; border-color: #00130b; }
.view { display: block; opacity: 1; transition: opacity 150ms ease-in-out; }
.view.fading-out { opacity: 0; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

/* Form */
form {
  display: grid;
  gap: 12px;
}

label {
  display: block;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--accent);
}

/* Ensure consistent styling for all form elements */
input, select, textarea {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  font-family: 'VT323', 'Press Start 2P', monospace;
  font-size: 18px;
  border: 3px solid #2a2d55;
  border-radius: 0;
  background: #0b0c1a;
  color: var(--text);
  outline: none;
  box-shadow: inset -2px -2px 0 0 #0008, inset 2px 2px 0 0 #2a2a2a;
}

/* Override default browser styles for date input */
input[type="date"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  line-height: 1.5;
  height: auto;
  text-align: start;
}

input[type="date"]::placeholder {
  color: #8593a7;
}

input[type="text"], input[type="date"], textarea {
  width: 100%;
  background: #0b0c1a;
  color: var(--text);
  border: 3px solid #2a2d55;
  outline: none;
  padding: 10px 12px;
  font-family: 'VT323', 'Press Start 2P', monospace;
  font-size: 18px;
  border-radius: 0;
  box-shadow: inset -2px -2px 0 0 #0008, inset 2px 2px 0 0 #2a2a2a;
}

.with-inline {
  /* keep input and button on one row when there's space; input takes remaining width */
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

/* Make the inline input easier to use on narrow viewports: give it a sensible min width
   and stack the button below the input when space is too tight. */
#location-text { min-width: 160px; max-width: 100%; }
.hint { color: var(--muted); font-size: 10px; display: block; margin-top: 6px; }

/* Hint for species input - warning state */
.hint.warn { color: var(--danger); font-weight: 700; }
#save-success { color: var(--primary); }

/* Inline link-style buttons used inside hint banners */
.btn-inline-link { background: none; border: none; color: var(--primary); cursor: pointer; font-family: inherit; font-size: inherit; text-decoration: underline; padding: 0; }
.hint-suggest { background: none; border: none; color: var(--primary); cursor: pointer; font-family: inherit; font-size: inherit; padding: 0; text-decoration: underline; }
.hint-suggest:hover { color: var(--accent); }

/* Datalist suggestions in some browsers inherit input font; ensure they look consistent */
input[list] { caret-color: var(--primary); }

input[type="text"]::placeholder, textarea::placeholder {
  color: #8593a7;
}

.field.split {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 12px;
}

.actions { display: flex; gap: 8px; }
.actions-mobile { display: none; }

.btn {
  cursor: pointer;
  border: 3px solid #0b0c1a;
  padding: 10px 14px;
  font-family: 'Press Start 2P', system-ui, sans-serif;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #081015;
  background: var(--accent);
  border-radius: 0;
  box-shadow: 3px 3px 0 0 #0008;
  transition: transform 0.02s ease-in-out;
}
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 0 #0008; }
.btn[disabled] { opacity: 0.6; cursor: not-allowed; }

.btn.primary { background: var(--primary); color: #00130b; border-color: #00130b; }
.btn.primary:hover { background: var(--primary-dark); }

.log-sighting-btn { margin-top: 8px; width: 100%; font-size: 10px; padding: 8px; }

.btn.danger { background: var(--danger); color: #2b0006; border-color: #2b0006; }
.btn.danger:hover { background: var(--danger-dark); }

.btn.outline {
  background: transparent;
  color: var(--text);
  border-color: #2a2d55;
}
.btn.outline:hover { background: #0b0c1a; }

/* List */
.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.list-header h2 { margin: 0; font-size: 16px; }

.count-badge {
  display: inline-block;
  min-width: 28px;
  text-align: center;
  background: #0b0c1a;
  border: 3px solid #2a2d55;
  padding: 6px 8px;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
}

.sightings {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  align-items: start;
  background: #0b0c1a;
  border: 3px solid #2a2d55;
  padding: 10px;
  box-shadow: inset -2px -2px 0 0 #0008, inset 2px 2px 0 0 #2a2a2a;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  align-items: center;
}
.species { font-weight: bold; color: var(--primary); }
.date { color: var(--muted); font-size: 12px; }
.notes { grid-column: 1 / -1; color: var(--text); white-space: pre-wrap; }

.location { grid-column: 1 / -1; color: var(--muted); font-size: 12px; margin-top: 6px; }

.delete-btn {
  align-self: center;
  justify-self: end;
}

/* Compact square delete button to match spark badge size used in catalog */
.delete-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  padding: 0;
  font-size: 12px;
  line-height: 1;
  border: 2px solid var(--panel);
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
}
.delete-btn:hover { color: var(--danger); border-color: var(--danger); }

/* Responsive */
@media (max-width: 640px) {
  .field.split { grid-template-columns: 1fr; }
  .actions-desktop { display: none; }
  .actions-mobile { display: flex; }
}

/* Catalog: grid layout (header, toolbar, grid, sidebar) */
.catalog-wrap {
  display: grid;
  grid-template-columns: 1fr 280px;
  grid-template-areas:
    "header sidebar"
    "toolbar sidebar"
    "grid sidebar";
  /* tighten overall gap so header and toolbar are closer */
  gap: 12px;
  align-items: start;
  /* Do not force any fixed height here; let the grid content determine height so content starts just below header */
  height: auto;
}
@media (max-width: 900px) {
  .catalog-wrap {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "toolbar"
      "sidebar"
      "grid";
  }
}

.catalog-header { grid-area: header; display: flex; flex-direction: column; gap: 6px; }
.catalog-header h2 { margin: 0; }
.stats { color: var(--muted); font-size: 12px; display: flex; gap: 10px; align-items: center; }
.stat-label { color: var(--muted); font-size: 10px; }



.catalog-toolbar { grid-area: toolbar; }
.catalog-sidebar { grid-area: sidebar; position: relative; }
.sidebar-inner {
  position: sticky;
  top: 8px;
  /* Use safe dynamic viewport units for sticky sidebar max-height so the address bar doesn't create a huge empty area
     Fallback order keeps older browsers working */
  max-height: calc(100vh - 96px);
  max-height: calc(100svh - 96px);
  max-height: calc(100dvh - 96px);
  overflow: auto;
  padding: 8px;
  border: 2px solid var(--panel-2);
  border-radius: 12px;
  background: linear-gradient(180deg,var(--panel),var(--panel-2));
}
.sidebar-inner h3 { margin: 0 0 8px 0; font-size: 12px; color: var(--accent); }
.region-chips { display: flex; flex-wrap: wrap; gap: 8px; }

.catalog-grid { grid-area: grid; display: grid; /* cards doubled width: fewer columns */ grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; min-height: 0; }
@media (max-width: 1200px){ .catalog-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 700px){ .catalog-grid { grid-template-columns: repeat(1, minmax(0, 1fr)); } }

/* Progress */
.progress-wrap { display: flex; align-items: center; gap: 8px; }
.progress { flex: 1; margin-top: 0; background: var(--panel-2); border: 2px solid var(--panel); border-radius: 12px; height: 18px; position: relative; overflow: visible; }
.progress-marker { position: absolute; top: -2px; width: 2px; height: calc(100% + 4px); background: var(--bg); opacity: 0.6; transform: translateX(-50%); }
.progress-bar { height: 100%; width: 0%; background: var(--accent); transition: width 200ms linear; position: relative; }
.progress-bar #progress-label { display: none; }
.progress-pct-label { font-size: 12px; color: var(--muted); min-width: 36px; text-align: right; }

/* Inputs & chips
  - Ensure inputs (search) do not inherit button/chip styles
  - Scope pill/chip rules to button elements only
*/
input[type="search"] { appearance: none; -webkit-appearance: none; -moz-appearance: none; }

/* Scope visual styles to button elements with pill/chip classes so inputs are unaffected */
button.pill, button.chip { height: 36px; padding: 0 10px; display: inline-flex; align-items: center; border: 2px solid var(--panel); border-radius: 8px; background: #0b0c1a; color: var(--text); cursor: pointer; font-family: 'Press Start 2P'; font-size: 11px; }
button.pill[aria-pressed="true"], button.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #1a0f00; }
.rarity-pills { display: inline-flex; gap: 8px; }
.sort { display: inline-flex; align-items: center; gap: 8px; }
select#sort-by { height: 36px; border: 2px solid var(--panel); border-radius: 8px; background: #0b0c1a; color: var(--text); padding: 6px 8px; }

/* Cards */
.card-species { border: 2px solid var(--panel); border-radius: 12px; padding: 10px; display: grid; gap: 8px; background: linear-gradient(180deg,var(--panel),var(--panel-2)); }
.card-species.locked .sprite { filter: grayscale(1) contrast(0.6); opacity: 0.6; }
.card-species .meta { font-size: 12px; color: var(--muted); }
.sp-head { display: grid; gap: 4px; }
.sp-name { color: var(--accent); font-size: 13px; }
.sp-sci { color: var(--muted); font-size: 11px; }
.sp-media { display: grid; grid-template-columns: 1fr; gap: 6px; }
.ref-img { display: none; }
.sprite-img { width: 100%; height: 140px; object-fit: cover; border: 2px solid var(--panel); background: #05060f; image-rendering: pixelated; }
.sprite-locked { position: relative; filter: grayscale(100%) brightness(0.6); }
.sp-blurb { font-size: 12px; color: var(--text); }
.sp-regions { font-size: 12px; color: var(--muted); }
.badges { display: inline-flex; gap: 6px; margin-top: 4px; }
.badge { padding: 6px 8px; border: 2px solid var(--panel); background: #14162b; font-size: 11px; color: var(--text); }
.badge.on { background: var(--primary); color: #00130b; border-color: var(--primary); }

/* Tiny spark badge for First Sighting - shows an outlined or filled sparkle */
.badge.spark {
  padding: 4px 6px;
  font-size: 12px;
  line-height: 1;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 2px solid var(--panel);
  color: var(--muted);
  border-radius: 6px;
}
.badge.spark.on {
  background: linear-gradient(180deg, #ffd166, #ffb84d);
  color: #2a1700;
  border-color: #ffd166;
  box-shadow: 0 1px 0 rgba(0,0,0,0.2) inset;
}
.card-species:focus-within, .card-species:hover { outline: 3px solid var(--accent); outline-offset: 0; }

/* Make cards feel interactive when clickable */
.clickable-card { cursor: pointer; }
.clickable-card:focus { outline: 3px solid rgba(255,209,102,0.25); }

/* Improve header and blurb legibility */
.sp-head { line-height: 1.05; }
.sp-name { font-size: 13px; font-weight: 700; }
.sp-sci { font-size: 11px; opacity: 0.9; }
.sp-blurb { font-size: 11px; line-height: 1.2; -webkit-line-clamp: 4; line-clamp: 4; }

/* Desktop layout fixes: ensure grid items don't overlap, headings wrap, and media stacks properly */
.catalog-grid { align-items: stretch; }
.card-species { align-self: stretch; overflow: hidden; min-height: 240px; display: flex; flex-direction: column; height: 100%; }
.card-species .sp-head { position: relative; z-index: 2; flex: 0 0 auto; }
.card-species .sp-media { position: relative; z-index: 1; margin-top: 4px; flex: 0 0 auto; }
.sp-name { white-space: normal; word-break: break-word; font-size: 12px; }
.sprite-img { display: block; }

/* Contain long text: allow blurb to take remaining space and clamp lines (4 lines) */
.sp-blurb {
  flex: 1 1 auto;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  /* Standard and fallback */
  line-clamp: 4;
  overflow-wrap: break-word;
  font-size: 11px;
  line-height: 1.2;
  color: var(--text);
}
.sp-regions { flex: 0 0 auto; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 11px; color: var(--muted); }
.badges { flex: 0 0 auto; }

/* Removed detail overlay styles to disable hover popup (cards still positioned relatively) */
.card-species { position: relative; }

/* Empty state */
.catalog-empty { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 32px; text-align: center; color: var(--muted); font-size: 12px; border: 2px dashed var(--panel); border-radius: 12px; }

/* Overflow and responsive fixes */
html, body { width: 100%; overflow-x: hidden; }
*, *::before, *::after { box-sizing: border-box; }

/* Make grid columns shrinkable and avoid overflow from fixed min-widths */
.catalog-wrap { grid-template-columns: minmax(0,1fr) 280px; }
.catalog-header, .catalog-toolbar, .catalog-grid, .catalog-sidebar { min-width: 0; }

/* Toolbar: search grows and is first-class control
  - desktop: 1fr auto auto (search expands, then filters, then sort)
  - mobile: becomes a single-column toolbar with search on its own row
  Use min-width:0 on parents to prevent child shrinking.
*/
/* Toolbar: desktop uses a 3-col grid (search | filters(center) | sort(right));
   small screens switch to stacked flex layout below. */
.catalog-toolbar {
  display: grid;
  /* Row 1: search (full width)
     Row 2: filters | sort
     Use two columns on the second row so filters can be centered and sort sits to the right */
  grid-template-columns: 1fr minmax(140px, 240px);
  grid-template-rows: auto auto;
  gap: var(--space-3);
  align-items: start;
  min-width: 0;
}
.catalog-toolbar > * { position: relative; z-index: 0; }

/* Ensure search spans full width on the first row to avoid awkward empty space */
.search-wrap { grid-column: 1 / -1; grid-row: 1; min-width: 0; }

/* Search wrapper: flexible, but prefer roomy minimum so it doesn't collapse */
.search-wrap {
  /* Allow the search wrapper to shrink on narrow viewports; inputs themselves have minimum widths set separately */
  flex: 1 1 220px; /* grow, shrink, base */
  min-width: 0; /* allow shrinking to avoid forcing large gaps on mobile */
  display: flex;
  align-items: center;
  width: 100%;
  justify-self: stretch; /* fill full width */
  grid-column: 1 / -1; /* span both columns */
  grid-row: 1;
  min-height: var(--control-h);
}

/* search input: fills its wrapper */
#searchInput {
  display: block;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  height: var(--control-h);
  padding: 0 var(--space-3);
  font-family: inherit;
  border: 2px solid var(--panel);
  border-radius: 8px;
  background: #0b0c1a;
  color: var(--text);
  line-height: calc(var(--control-h) - 4px);
}

/* Ensure controls match expected ~40px height where appropriate (buttons/select) */
/* Normalize control heights */
button.pill, button.chip, select#sort-by { height: var(--control-h); }
.rarity-pills, .sort { min-width: 0; }

/* Rarity pills: own flex container, can wrap internally */
.rarity-pills {
  display: flex;
  gap: var(--space-2);
  flex: 0 1 auto;
  flex-wrap: nowrap; /* keep filters on a single line on desktop */
  overflow-x: auto; /* allow horizontal scrolling when space is tight */
  -webkit-overflow-scrolling: touch;
  align-items: center;
  justify-self: center; /* center in left column area */
  justify-content: center;
  grid-column: 1; /* left column of row 2 */
  grid-row: 2;
  margin-top: 6px;
}

/* Ensure pills don't shrink and remain a fixed size so the row stays single-line */
.rarity-pills .pill { flex: 0 0 auto; }

/* Sort wrapper: push to far right when space allows */
.sort { display: flex; align-items: center; gap: var(--space-2); justify-self: end; margin: 0; grid-column: 2; grid-row: 2; align-self: center; min-height: var(--control-h); }
.sort > * { flex-shrink: 0; }

/* Ensure select trigger has right padding so caret doesn't overlap text */
/* Keep the select compact and avoid forcing it to fill the grid column.
  Use auto width with a sensible max so the dropdown won't extend past
  the main search input or be clipped by the viewport. */
select#sort-by { padding-right: calc(var(--space-3) + 18px); max-width: 220px; width: auto; }
/* normalize native select appearance to match theme */
select#sort-by {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
}

/* Ensure select menu isn't clipped by ancestor overflow; recommend visible overflow-x only */
.catalog-wrap, .catalog-toolbar, .card { overflow-x: visible; }
/* higher stacking context for dropdowns */
select#sort-by { position: relative; z-index: 2; }

/* Placeholder text: no overflow */
#searchInput::placeholder { color: var(--muted); text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }

/* Reduce motion preference */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* Focus styles: use outline/box-shadow rather than changing border-width to avoid layout shifts */
#searchInput:focus, button.pill:focus, button.chip:focus, select#sort-by:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(255, 209, 102, 0.25); /* subtle accent ring */
}

/* On very narrow screens ensure the search remains usable (min 200px) but otherwise fills its cell */
@media (max-width: 520px) {
  #searchInput { min-width: 200px; }
}

/* On very small screens stack the location input and its button so the input keeps usable width */
@media (max-width: 520px) {
  .with-inline { grid-template-columns: 1fr; gap: 6px; }
  .with-inline .btn { width: 100%; justify-self: stretch; }
}

/* Sidebar must not force page width */
.catalog-sidebar { min-width: 0; }
.sidebar-inner { max-width: 100%; }

/* Region chips wrap; no overflow */
.region-chips { display: flex; flex-wrap: wrap; gap: 8px; max-width: 100%; }
.catalog-grid { min-width: 0; }

@media (max-width: 900px) {
  .catalog-wrap {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "toolbar"
      "sidebar"
      "grid";
  }
  /* Compact 2-row toolbar on small screens: 
     - Row 1: Search spans full width
     - Row 2: Filters (left) and Sort (right)
     This avoids large vertical gaps and prevents Sort from "floating" alone. */
  .catalog-toolbar { 
    display: grid; 
    grid-template-columns: 1fr auto; 
    grid-template-rows: auto auto; 
    gap: 8px; 
    align-items: center; 
  }
  .search-wrap { grid-column: 1 / -1; grid-row: 1; margin-top: 0; margin-bottom: 0; }
  .rarity-pills { 
    grid-column: 1; grid-row: 2; 
    display: flex; gap: 8px; 
    overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 4px; 
    flex-wrap: nowrap; justify-content: flex-start; justify-self: start; 
  }
  .rarity-pills .pill { flex: 0 0 auto; max-width: none; }
  .sort { grid-column: 2; grid-row: 2; justify-content: flex-end; justify-self: end; margin-left: 0; width: auto; }
  select#sort-by { max-width: min(320px, 100%); width: auto; box-sizing: border-box; }
  .sidebar-inner { position: static; max-height: none; }
}

/* Optional container cap */
.main { max-width: 1200px; margin: 0 auto; padding: 0 12px; }
