:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --border: #d9dee5;
  --accent: #1f6feb;
  --good: #1a7f37;
  --good-bg: #dcf5e3;
  --warn: #b35900;
  --warn-bg: #ffecd6;
  --bad: #c62828;
  --bad-bg: #fde2e2;
  --radius: 12px;
  --tabbar-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #222831;
    --text: #e8ebef;
    --muted: #97a3b2;
    --border: #2e3540;
    --accent: #5b9dff;
    --good: #5fd383;
    --good-bg: #173322;
    --warn: #ffb35c;
    --warn-bg: #3a2812;
    --bad: #ff7a7a;
    --bad-bg: #3d1a1a;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1216;
  --surface: #181c22;
  --surface-2: #222831;
  --text: #e8ebef;
  --muted: #97a3b2;
  --border: #2e3540;
  --accent: #5b9dff;
  --good: #5fd383;
  --good-bg: #173322;
  --warn: #ffb35c;
  --warn-bg: #3a2812;
  --bad: #ff7a7a;
  --bad-bg: #3d1a1a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  overflow-x: hidden;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}
.topbar h1 { margin: 0; font-size: 18px; }

main { padding: 16px; max-width: 720px; margin: 0 auto; }

.status { color: var(--muted); text-align: center; padding: 32px 0; }
.status.error { color: var(--bad); }

/* ---------- 底部分頁 ---------- */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 12px;
}
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 共用區塊 ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card h2 { margin: 0 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

button { font: inherit; color: inherit; }
.btn-text {
  background: none;
  border: 0;
  color: var(--accent);
  padding: 6px 4px;
  cursor: pointer;
}

input[type="search"] {
  width: 100%;
  font: inherit;
  font-size: 16px; /* 低於 16px 時 iOS 會自動放大 */
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
input[type="search"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- 屬性標籤 ---------- */
.type {
  --c: #888;
  --fg: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 3.2em;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--c);
  color: var(--fg);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  border: 2px solid transparent;
}
button.type { cursor: pointer; }
.type.dim { opacity: .45; }
.type[aria-pressed="true"] { border-color: var(--text); }

.t-normal   { --c: #9fa19f; }
.t-fire     { --c: #e62829; }
.t-water    { --c: #2980ef; }
.t-electric { --c: #f5b800; --fg: #1b1f24; }
.t-grass    { --c: #3fa129; }
.t-ice      { --c: #3dcef3; --fg: #1b1f24; }
.t-fighting { --c: #ff8000; }
.t-poison   { --c: #9141cb; }
.t-ground   { --c: #915121; }
.t-flying   { --c: #81b9ef; --fg: #1b1f24; }
.t-psychic  { --c: #ef4179; }
.t-bug      { --c: #91a119; }
.t-rock     { --c: #afa981; --fg: #1b1f24; }
.t-ghost    { --c: #704170; }
.t-dragon   { --c: #5060e1; }
.t-dark     { --c: #624d4e; }
.t-steel    { --c: #60a1b8; }
.t-fairy    { --c: #ef70ef; }

.type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}
.type-grid .type { padding: 8px 4px; min-height: 40px; }

/* ---------- 倍率 ---------- */
.mult {
  display: inline-block;
  min-width: 3.4em;
  text-align: center;
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--muted);
}
.mult.up   { background: var(--good-bg); color: var(--good); }
.mult.down { background: var(--warn-bg); color: var(--warn); }
.mult.zero { background: var(--bad-bg); color: var(--bad); }
/* 受到傷害用：低＝好、高＝壞 */
.mult.good { background: var(--good-bg); color: var(--good); }
.mult.bad  { background: var(--bad-bg); color: var(--bad); }
.pair { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); margin-right: 6px; }

.group { display: grid; grid-template-columns: 4.2em 1fr; gap: 8px; align-items: start; padding: 6px 0; }
.group + .group { border-top: 1px dashed var(--border); }
.group .row { gap: 6px; }

.empty { color: var(--muted); font-size: 14px; }

/* ---------- 完整克制表 ---------- */
details.matrix summary { cursor: pointer; color: var(--accent); padding: 4px 0; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 10px 0 6px; font-size: 13px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.sym {
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 5px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 700;
}
.sym.up, .matrix td.up     { background: var(--good-bg); color: var(--good); }
.sym.down, .matrix td.down { background: var(--warn-bg); color: var(--warn); }
.sym.zero, .matrix td.zero { background: var(--bad-bg); color: var(--bad); }

.matrix-info {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px;
  min-height: 40px; margin: 6px 0; padding: 6px 10px;
  border-radius: 8px; background: var(--surface-2); font-size: 14px;
}

/* 表頭與第一欄在兩個方向捲動時都固定 */
.matrix-scroll {
  overflow: auto;
  max-height: 72vh;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.matrix table { border-collapse: separate; border-spacing: 0; font-size: 15px; }
.matrix th, .matrix td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  text-align: center;
  padding: 0;
  min-width: 30px;
  height: 30px;
}
.matrix td { font-weight: 700; cursor: pointer; background: var(--surface); }
.matrix thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--c); color: var(--fg);
  font-size: 14px; font-weight: 700; cursor: pointer;
}
.matrix tbody th {
  position: sticky; left: 0; z-index: 1;
  background: var(--surface); padding: 0 4px; text-align: left; cursor: pointer;
}
.matrix tbody th .type { font-size: 12px; min-width: 3.6em; padding: 1px 6px; }
.matrix th.corner {
  position: sticky; top: 0; left: 0; z-index: 3;
  background: var(--surface-2); color: var(--muted);
  font-size: 11px; line-height: 1;
  background-image: linear-gradient(to top right, transparent calc(50% - .5px), var(--border) 50%, transparent calc(50% + .5px));
}
.matrix th.corner .atk { position: absolute; left: 6px; bottom: 5px; }
.matrix th.corner .def { position: absolute; right: 6px; top: 5px; }

/* 點選後的十字標亮 */
.matrix .hl-row, .matrix .hl-col { box-shadow: inset 0 0 0 999px rgba(31, 111, 235, .14); }
.matrix th.hl-row, .matrix th.hl-col { box-shadow: inset 0 0 0 3px var(--accent); }
.matrix td.hl-cell { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ---------- 對戰推薦 ---------- */
.slot { position: relative; margin-bottom: 10px; }
.slot-label { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.picked {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.picked .name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picked .remove { background: none; border: 0; color: var(--muted); font-size: 20px; line-height: 1; padding: 4px 8px; cursor: pointer; }

.suggest {
  list-style: none;
  margin: 4px 0 0;
  padding: 4px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  max-height: 320px;
  overflow-y: auto;
}
.suggest button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
}
.suggest button:hover, .suggest button:focus-visible { background: var(--surface-2); outline: none; }
.suggest .dex { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; min-width: 3.2em; }
.suggest .name { flex: 1; }
.suggest .type { font-size: 11px; min-width: 0; padding: 1px 6px; }

.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; flex-wrap: wrap; }
.rank-list li + li { border-top: 1px dashed var(--border); }
.rank-list .meta { margin-left: auto; color: var(--muted); font-size: 13px; }
.rank-list .mults { display: flex; gap: 4px; }

/* ---------- 火箭隊 ---------- */
.grunt {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 10px;
  cursor: pointer;
}
.grunt[aria-expanded="true"] { border-color: var(--accent); }
.grunt .quote { display: grid; grid-template-columns: 1.6em 1fr; gap: 4px; margin-top: 6px; font-size: 15px; }
.grunt .who { color: var(--muted); font-size: 12px; padding-top: 2px; }
.grunt-detail { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); cursor: default; }
.grunt-detail h3 { margin: 8px 0 6px; font-size: 13px; color: var(--muted); }

.sticky-search { position: sticky; top: 52px; z-index: 5; background: var(--bg); padding: 0 0 12px; margin: 0 0 4px; }

@media (min-width: 560px) {
  .type-grid { grid-template-columns: repeat(6, 1fr); }
}

/* ---------- 切換鈕（聯盟、PvP/PvE） ---------- */
.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  margin-bottom: 12px;
  background: var(--surface-2);
  border-radius: var(--radius);
}
.segmented button {
  border: 0;
  border-radius: 9px;
  background: none;
  padding: 8px 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.25;
}
.segmented button small { font-size: 11px; font-weight: 400; }
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

/* ---------- 招式標籤與徽章 ---------- */
/* 色點顏色來自 .t-<屬性> 的 --c；這裡不設預設值，否則會蓋掉屬性色 */
.move {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px 2px 6px;
  border-radius: 6px;
  background: var(--surface-2);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}
.move::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--c); flex: none; }
.badge {
  display: inline-block;
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  vertical-align: middle;
  white-space: nowrap;
}
.badge.rec    { background: var(--accent); color: #fff; }
.badge.stab   { background: var(--good-bg); color: var(--good); }
.badge.elite  { background: var(--warn-bg); color: var(--warn); }
.badge.legacy { background: var(--bad-bg); color: var(--bad); }

.poke-link {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  text-decoration: none;
  white-space: nowrap;
}
.hint .poke-link { border: 0; padding: 0; color: var(--accent); background: none; }

/* ---------- 推薦寶可夢 ---------- */
.vs-row { display: grid; grid-template-columns: 6.5em 1fr; gap: 8px; align-items: start; padding: 8px 0; }
.vs-row + .vs-row { border-top: 1px dashed var(--border); }
.vs-row > b { font-size: 14px; padding-top: 3px; overflow-wrap: anywhere; }
.vs-row .row { gap: 6px; }
.vs-row.best > b { color: var(--good); }

.ranked { list-style: none; margin: 10px 0 0; padding: 0; }
.ranked > li + li { border-top: 1px solid var(--border); }
.ranked-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 2px;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
}
.ranked-head .rank-no { min-width: 2em; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 14px; }
.ranked-head .name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranked-head .row { flex-wrap: nowrap; gap: 4px; }
.ranked-head .type { font-size: 11px; min-width: 0; padding: 1px 6px; }
.ranked-head .score { min-width: 2.6em; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; }
.ranked li[aria-expanded="true"] { background: var(--surface-2); border-radius: 8px; }
.ranked-detail { padding: 0 10px 10px; }
.ranked-detail h3 { margin: 8px 0 6px; font-size: 13px; color: var(--muted); }
.ranked-detail .row { gap: 6px; }

/* ---------- 招式表 ---------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.move-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.move-table th, .move-table td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: right; font-variant-numeric: tabular-nums; }
.move-table thead th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.move-table th[scope="row"], .move-table thead th:first-child { text-align: left; font-weight: 400; }
.move-table tbody tr:last-child > * { border-bottom: 0; }
.move-table td.empty { text-align: center; }

/* ---------- 屬性圖鑑 ---------- */
.dex-list { list-style: none; margin: 0; padding: 0; }
.dex-list li + li { border-top: 1px solid var(--border); }
.dex-list a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 2px;
  color: var(--text);
  text-decoration: none;
}
.dex-list .dex { min-width: 4.2em; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.dex-list .name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dex-list .row { flex-wrap: nowrap; gap: 4px; }
.dex-list .type { font-size: 11px; min-width: 0; padding: 1px 6px; }
