/* =========================================
   CSS Variables & Reset
   ========================================= */
:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface2: #f0f2f7;
  --border: #e3e6ef;
  --text: #1f2330;
  --muted: #6b7280;
  --red: #e3350d;      /* Pokéball red */
  --blue: #2a75bb;     /* Pokémon blue */
  --yellow: #ffcb05;
  --target-bg: #fff4c2;
  --target-text: #7a5600;
  --success: #2f9e44;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 4px 14px rgba(20, 30, 60, .05);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --transition: .15s ease;
  color-scheme: light;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  font-variant-numeric: tabular-nums;
}

button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }
input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* =========================================
   Header
   ========================================= */
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.brand h1 { font-size: 20px; font-weight: 800; letter-spacing: -.01em; }
.brand h1 span { color: var(--red); }

.header-actions { margin-left: auto; display: flex; gap: 4px; }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  color: var(--muted);
  display: grid; place-items: center;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--surface2); color: var(--text); }

/* Segmented toggle */
.segmented {
  display: inline-flex;
  background: var(--surface2);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.segmented button {
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
  transition: all var(--transition);
}
.segmented button.on {
  background: var(--surface);
  color: var(--red);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
}
.segmented.small button { padding: 4px 10px; font-size: 13px; }

/* =========================================
   Layout
   ========================================= */
.main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 16px calc(80px + env(safe-area-inset-bottom));
}

/* =========================================
   Filters
   ========================================= */
.filters {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  margin-bottom: 14px;
}

.filter-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--muted);
  text-transform: uppercase;
}

.filter-block { display: grid; gap: 6px; }

/* Chain search */
.search {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.search:focus-within { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(42, 117, 187, .15); }
.search .chips { display: contents; }
#chain-input {
  flex: 1;
  min-width: 160px;
  border: none;
  outline: none;
  padding: 5px 2px;
  font-size: 16px; /* ≥16px stops iOS zoom-on-focus */
  background: transparent;
}

.suggest {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  max-height: 280px;
  overflow-y: auto;
  z-index: 20;
}
.suggest li { padding: 9px 12px; cursor: pointer; }
.suggest li.active, .suggest li:hover { background: var(--surface2); color: var(--blue); }

/* Chips */
.chip {
  min-width: 32px;
  height: 26px;
  padding: 0 6px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 14px;
  transition: all var(--transition);
}
.chip:hover { border-color: var(--blue); }
.chip.on { background: var(--blue); border-color: var(--blue); color: #fff; }
.chip.any { padding: 0 10px; }
.chip.removable { height: 26px; font-size: 13px; border-radius: 999px; }

/* IV picker */
.iv-block { gap: 8px; }
.iv-row { display: flex; align-items: center; gap: 10px; }
.iv-row .filter-label { width: 34px; flex-shrink: 0; }
.iv-grid { display: flex; flex-wrap: wrap; gap: 4px; }

/* Level */
.level-block summary { cursor: pointer; list-style: revert; }
.level-range { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.level-range select {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  background: var(--surface);
}

.filter-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
.data-info { font-size: 12px; color: var(--muted); }
.filter-footer .btn { margin-left: auto; }
.filter-footer .data-info { margin-right: auto; }

/* Buttons */
.btn {
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-weight: 600;
  font-size: 14px;
  transition: all var(--transition);
}
.btn-ghost { color: var(--muted); border: 1px solid var(--border); background: var(--surface); }
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }
.btn-primary { background: var(--red); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }

/* =========================================
   Status
   ========================================= */
.status { text-align: center; color: var(--muted); padding: 8px 0; }
.status:empty { display: none; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--red);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 24px auto 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* =========================================
   Results — table on desktop, cards on mobile
   ========================================= */
.results {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.results:not(:has(.res-row)) { display: none; }

.res-head, .res-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 76px 52px 52px 52px;
  align-items: center;
  gap: 8px;
  padding: 9px 16px;
}
.results.is-shadow .res-head,
.results.is-shadow .res-row {
  grid-template-columns: minmax(0, .9fr) 44px 44px 44px minmax(0, 1.3fr) minmax(0, 1.3fr);
  align-items: start;
}

.res-head {
  background: var(--surface2);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .05em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}
.res-head span:nth-child(n+2):nth-child(-n+5) { text-align: center; }
.results.is-shadow .res-head span:nth-child(n+5) { text-align: left; }

.res-row { border-bottom: 1px solid var(--border); transition: opacity var(--transition); }
.res-row:last-child { border-bottom: none; }
.res-row:hover { background: #fafbfe; }
.res-row.both { background: linear-gradient(90deg, rgba(42, 117, 187, .05), transparent 40%); }

.lv { text-align: center; color: var(--blue); font-weight: 600; }
.ivs { display: contents; }
.iv { text-align: center; font-weight: 700; font-size: 15px; color: var(--text); }


/* Evolution chain */
.evo { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; min-width: 0; }
.evo-m {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  padding: 2px 6px;
  border-radius: 6px;
  white-space: nowrap;
}
.evo-name { font-weight: 600; }
.evo-cp { color: var(--muted); font-size: 13px; }
.evo-arrow { color: #c3c8d4; font-size: 12px; }
.evo-m.is-col .evo-name::after { content: " ✓"; color: var(--success); font-size: 12px; }

.evo-m.target { background: var(--target-bg); }
.evo-m.target .evo-name, .evo-m.target .evo-cp { color: var(--target-text); font-weight: 700; }


/* Shadow / Purified */
.evo-names { font-weight: 600; }
.sp-col { display: grid; gap: 6px; min-width: 0; }
.hit { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; }
.hit-lv { font-size: 12px; font-weight: 700; letter-spacing: .03em; }
.sp-shadow .hit-lv { color: #7b3fbf; }
.sp-purified .hit-lv { color: var(--blue); }
.hit .evo { font-size: 13px; }

.sentinel { height: 1px; }

/* Floating back-to-filters */
.top-btn {
  position: fixed;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 40;
  background: var(--red);
  color: #fff;
  font-weight: 700;
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: 0 4px 14px rgba(227, 53, 13, .35);
}

/* =========================================
   Dialogs & toast
   ========================================= */
.dialog {
  margin: auto;
  border: none;
  border-radius: var(--radius);
  padding: 20px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .2);
  color: var(--text);
  background: var(--surface);
}
.dialog::backdrop { background: rgba(20, 25, 40, .35); }
.dialog h2 { font-size: 18px; margin-bottom: 8px; }
.dialog p { font-size: 14px; margin-bottom: 12px; }
.dialog input[type="password"], .dialog input[type="search"] {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 16px;
  outline: none;
}
.dialog input:focus { border-color: var(--blue); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

.dialog-wide {
  width: min(640px, calc(100vw - 32px));
  height: min(760px, calc(100vh - 48px));
  padding: 0;
  display: none;
  flex-direction: column;
}
.dialog-wide[open] { display: flex; }
.dex-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.dex-head h2 { margin: 0; }
.dex-tools { display: flex; gap: 8px; align-items: center; padding: 0 16px 12px; flex-wrap: wrap; }
.dex-tools input { flex: 1; min-width: 160px; }
.dex-list { list-style: none; overflow-y: auto; flex: 1; border-top: 1px solid var(--border); }
.dex-item label {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.dex-item input { width: 18px; height: 18px; accent-color: var(--success); }
.tag {
  margin-left: auto;
  font-size: 11px;
  color: #7b3fbf;
  background: #f3ecfb;
  padding: 1px 6px;
  border-radius: 999px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #1f2330;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 100;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .2);
}

/* IV picker wraps as a 9×2 grid: 不限 + 0–7, then 8–15 aligned under 0 */
@media (max-width: 960px) {
  .iv-grid { display: grid; grid-template-columns: repeat(9, minmax(0, 40px)); gap: 4px; }
  .iv-grid .chip { min-width: 0; padding: 0; }
  .iv-grid .chip.any { font-size: 12px; }
  .iv-grid .chip[data-v="8"] { grid-column-start: 2; }
}

/* =========================================
   Mobile
   ========================================= */
@media (max-width: 720px) {
  .header-inner { flex-wrap: wrap; gap: 8px 12px; padding: 8px 12px; }
  .brand h1 { font-size: 18px; }
  .site-header .segmented { order: 3; width: 100%; }
  .site-header .segmented button { flex: 1; padding: 7px 4px; font-size: 13px; }

  .main { padding: 12px 12px calc(80px + env(safe-area-inset-bottom)); }
  .filters { padding: 12px; }

  .iv-row { display: grid; gap: 4px; }
  .iv-grid { grid-template-columns: repeat(9, 1fr); }
  .iv-grid .chip { height: 30px; }

  /* Results become cards */
  .results { background: none; border: none; box-shadow: none; overflow: visible; display: grid; gap: 8px; }
  .res-head { display: none; }

  .res-row,
  .results.is-shadow .res-row {
    grid-template-columns: auto 1fr;
    grid-template-areas: "evo evo" "lv ivs";
    gap: 8px 12px;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
  }
  .res-row:last-child { border-bottom: 1px solid var(--border); }
  .res-row .evo { grid-area: evo; }
  .res-row .lv { grid-area: lv; text-align: left; }
  .res-row .ivs { grid-area: ivs; display: flex; justify-content: flex-end; gap: 14px; }

  .iv::before {
    content: attr(data-k);
    font-size: 10px;
    font-weight: 700;
    color: var(--muted);
    margin-right: 3px;
    letter-spacing: .04em;
  }

  .results.is-shadow .res-row {
    grid-template-areas: "names ivs" "sp sp" "pu pu";
  }
  .results.is-shadow .evo-names { grid-area: names; }
  .results.is-shadow .ivs { grid-area: ivs; }
  .results.is-shadow .sp-shadow { grid-area: sp; }
  .results.is-shadow .sp-purified { grid-area: pu; border-top: 1px dashed var(--border); padding-top: 6px; }
  .results.is-shadow .sp-purified:has(.none) { display: none; }

}
