:root {
  color-scheme: light dark;
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1f1d1a;
  --muted: #6b665e;
  --border: #d9d5cd;
  --accent: #9c2f1f;
  --accent-text: #ffffff;
  --ok: #1d7a3a;
  --warn: #a35f00;
  --err: #b42318;
  --info: #3d5a80;
  --focus: #2f6fdb;
  --radius: 8px;
  /* faction colours taken from the in-game emblems */
  --wei: #2b5c9e;
  --shu: #2e7d43;
  --wu: #b8322a;
  --qun: #9a7414;
  --han: #8f4220;
  /* aptitude grade colours taken from the in-game troop labels */
  --g-S: #b85f12;
  --g-A: #9b45a0;
  --g-B: #4a63b0;
  --g-C: #3d7d4f;
  /* red-level pips */
  --mag-on: #e2461f;
  --mag-off: #e3a81c;
  /* rarity: 5-star orange-gold, 4-star purple (in-game name colours) */
  --r5: #c4651c;
  --r4: #7a45b0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171614;
    --surface: #211f1c;
    --surface-2: #2b2925;
    --text: #ece8e1;
    --muted: #a39d93;
    --border: #3d3a35;
    --accent: #d0563f;
    --ok: #4cb36b;
    --warn: #e0a03a;
    --err: #f06b5c;
    --info: #8fb0dc;
    --wei: #6f9ee0;
    --shu: #5fbf78;
    --wu: #ef7064;
    --qun: #d9b44a;
    --han: #dd8a5c;
    --g-S: #f0a160;
    --g-A: #d48ccc;
    --g-B: #9aaee6;
    --g-C: #86c494;
    --mag-on: #ff6a3d;
    --mag-off: #e8b53a;
    --r5: #f29a4e;
    --r4: #c39af0;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- header ---------- */
.top { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border); }
.top-row { display: flex; align-items: center; gap: 8px; padding: 8px 16px; flex-wrap: wrap; }
.top h1 { font-size: 18px; margin: 0; margin-right: auto; }
.status { display: flex; gap: 6px; }
/* Main tabs: pill buttons that read as clickable; the current one is filled with the accent colour. */
.tabs { display: flex; gap: 8px; padding: 0 16px 10px; overflow-x: auto; }
.tabs button {
  flex: none; white-space: nowrap; cursor: pointer;
  padding: 7px 16px; border-radius: 999px; font-weight: 600;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
}
.tabs button:hover:not(.on) { border-color: var(--accent); color: var(--accent); }
.tabs button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

/* Sub tabs: a segmented control, visually lighter than the main tabs. */
.subtabs {
  display: inline-flex; max-width: 100%; overflow-x: auto; margin: 0 0 14px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
}
.subtabs button {
  flex: none; white-space: nowrap; cursor: pointer;
  padding: 7px 16px; border: 0; border-radius: 0; background: none; color: var(--muted);
}
.subtabs button + button { border-left: 1px solid var(--border); }
.subtabs button:hover:not(.on) { background: var(--surface-2); color: var(--text); }
.subtabs button.on {
  background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent); font-weight: 600;
  box-shadow: inset 0 -3px 0 var(--accent);
}
.tabs button small, .subtabs button small {
  display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 999px; font-size: 11px; font-weight: 600;
  background: var(--surface-2); color: var(--muted);
}
.tabs button.on small { background: rgb(255 255 255 / .25); color: inherit; }
.subtabs button.on small { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 16px; }
.foot { max-width: 1280px; margin: 0 auto; padding: 16px; color: var(--muted); font-size: 13px; }

/* ---------- controls ---------- */
button {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 6px 12px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--err); }
button.link { border: 0; background: none; padding: 0 4px; color: var(--info); font-size: 13px; }
input[type=search], input:not([type]), select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px 8px; min-width: 0;
}
textarea { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 12px; word-break: break-all; }
.chk { display: inline-flex; align-items: center; gap: 6px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.toolbar input[type=search] { flex: 1 1 200px; }
.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 24px; }
.banner { background: var(--surface-2); border-left: 4px solid var(--warn); padding: 8px 12px; border-radius: var(--radius); }
.warn-text { color: var(--warn); font-weight: 600; }
.err-text { color: var(--err); }

/* ---------- badges ---------- */
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap; }
.s-complete { color: var(--ok); }
.s-incomplete { color: var(--muted); }
.s-warning { color: var(--warn); }
.s-error { color: var(--err); }
/* faction emblem: a small coin like the in-game badge */
.fac-wei { --fac: var(--wei); }
.fac-shu { --fac: var(--shu); }
.fac-wu { --fac: var(--wu); }
.fac-qun { --fac: var(--qun); }
.fac-han { --fac: var(--han); }
.fac {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.7em; height: 1.7em; border-radius: 50%; flex: none;
  font-size: 12px; font-weight: 700; line-height: 1;
  color: var(--fac); border: 1.5px solid var(--fac);
  background: color-mix(in srgb, var(--fac) 14%, transparent);
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--fac) 18%, transparent);
}
.fac.sm { width: 1.45em; height: 1.45em; font-size: 11px; margin-right: 4px; }
.fac-none { color: var(--muted); }
/* troop aptitude label, coloured by grade like the in-game tags (S orange, A purple, B blue, C green) */
.tr { --tr: var(--muted); }
.tr.g-S { --tr: var(--g-S); }
.tr.g-A { --tr: var(--g-A); }
.tr.g-B { --tr: var(--g-B); }
.tr.g-C { --tr: var(--g-C); }
.tr {
  display: inline-flex; align-items: center; gap: 2px; white-space: nowrap;
  font-size: 12px; line-height: 1.5; padding: 0 5px; border-radius: 4px;
  color: var(--tr); border: 1px solid color-mix(in srgb, var(--tr) 55%, transparent);
  background: color-mix(in srgb, var(--tr) 12%, transparent);
}
.tr b { font-weight: 700; }
.tr.g-S b { font-size: 13px; }
.tr.cur { background: var(--tr); border-color: var(--tr); color: var(--surface); }
.trs { display: inline-flex; gap: 3px; flex-wrap: wrap; }
.rar-5 { color: var(--r5); }
.rar-4 { color: var(--r4); }
.list td.nm.rar-5, .list td.nm.rar-4 { font-weight: 700; }
.reds { display: inline-flex; gap: 1px; vertical-align: middle; }
.mag { width: 11px; height: 15px; color: var(--mag-off); }
.mag.on { color: var(--mag-on); }
.reds.lg .mag { width: 14px; height: 19px; }
.info-reds { margin-left: auto; }
.tag { font-size: 11px; margin-left: 4px; padding: 0 5px; border-radius: 4px; background: var(--surface-2); color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.list { border-collapse: collapse; width: 100%; }
.list th, .list td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; white-space: nowrap; }
.list th { background: var(--surface-2); font-weight: 600; font-size: 13px; position: sticky; top: 0; }
.th-sort {
  border: 0; background: none; padding: 0; font: inherit; font-weight: 600; color: inherit;
  display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; border-radius: 4px;
}
.th-sort:hover:not(:disabled) { color: var(--accent); }
.sort-ind { font-size: 10px; color: var(--muted); }
.th-sort.on { color: var(--accent); }
.th-sort.on .sort-ind { color: var(--accent); }
.list tr.owned td { background: color-mix(in srgb, var(--ok) 7%, transparent); }
.list td.nm { font-weight: 600; }
.list td.desc { white-space: normal; min-width: 240px; max-width: 520px; font-size: 13px; }
.list td.desc summary { cursor: pointer; }
.list tr.click { cursor: pointer; }
.list tr.click:hover td { background: var(--surface-2); }

/* ---------- overview stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; margin-bottom: 12px; }
.stats > div { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; display: flex; flex-direction: column; }
.stats b { font-size: 22px; }
.stats small { color: var(--muted); }
.idle { margin-top: 12px; }

/* ---------- teams ---------- */
.team { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 16px; scroll-margin-top: calc(var(--top-h, 100px) + var(--tools-h, 0px) + 8px); }
.team.focus { border-color: var(--accent); }
.team-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.team-name { font-weight: 600; width: 10em; }
.team-title { font-size: 16px; margin: 0; display: flex; align-items: baseline; gap: 8px; }
.team-title small { font-size: 13px; font-weight: 400; color: var(--muted); }
.pioneer-intro p { margin: 0 0 8px; }
.pioneer-intro .summary { margin-bottom: 8px; }
.team-head .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.cost { font-size: 13px; }
.slots { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
.slot { padding: 10px 12px; border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.slot:last-child { border-right: 0; }
.slot[class*="fac-"] { box-shadow: inset 0 3px 0 var(--fac); background: linear-gradient(color-mix(in srgb, var(--fac) 6%, transparent), transparent 48px); }
.slot-head { display: flex; align-items: center; gap: 4px; }
.pos { font-weight: 700; margin-right: auto; }
.slot select { width: 100%; }
.info { font-size: 12px; color: var(--muted); display: flex; align-items: center; flex-wrap: wrap; }
.list td .fac { vertical-align: middle; }
.row { display: flex; align-items: center; gap: 6px; }
.row .lbl { flex: 0 0 3em; font-size: 13px; color: var(--muted); }
.row select { flex: 1; min-width: 0; }
.row .ro { flex: 1; }
.books { align-items: flex-start; }
.book-sels { flex: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 4px; min-width: 0; }
.issues { list-style: none; margin: 0; padding: 8px 12px; border-top: 1px solid var(--border); font-size: 13px; display: grid; gap: 2px; }
.issues span { display: inline-block; min-width: 3.5em; font-weight: 600; }
.lv-error { color: var(--err); }
.lv-warning { color: var(--warn); }
.lv-incomplete { color: var(--muted); }
.lv-hint { color: var(--info); }
.add-row { margin: 8px 0 24px; }

/* ---------- share ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px; }
.card h2 { font-size: 16px; margin: 0 0 8px; }
.field { display: grid; gap: 4px; margin: 12px 0 8px; }
.field > span { font-size: 13px; color: var(--muted); }
.summary { margin: 0; padding-left: 20px; }

/* ---------- confirm dialog ---------- */
.modal {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 20px;
  background: var(--surface); color: var(--text);
  width: min(460px, calc(100% - 32px)); box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.modal::backdrop { background: rgba(0, 0, 0, .45); }
.modal-msg { margin: 0 0 16px; white-space: pre-line; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
button.danger-fill { background: var(--err); border-color: var(--err); color: #fff; }

/* ---------- general picker ---------- */
.pick-btn {
  display: flex; align-items: center; gap: 6px; width: 100%; text-align: left; min-height: 38px;
}
.pick-btn .pb-name { font-weight: 600; }
.pick-btn small { color: var(--muted); }
.pick-btn .pb-empty { color: var(--info); }
.pick-btn .pb-caret { margin-left: auto; color: var(--muted); }
.picker {
  padding: 0; width: min(680px, calc(100% - 16px)); height: min(720px, calc(100% - 32px));
  display: none; flex-direction: column;
}
.picker[open] { display: flex; }
.picker-head { display: flex; align-items: center; padding: 12px 16px 4px; }
.picker-head h2 { font-size: 16px; margin: 0; margin-right: auto; }
.picker-head h2 small { font-weight: 400; color: var(--muted); margin-left: 8px; }
button.icon { border: 0; background: none; font-size: 18px; padding: 4px 8px; }
.picker-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px 16px; border-bottom: 1px solid var(--border); }
.picker-q { flex: 1 1 100%; font-size: 16px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 4px 10px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.picker-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.pick-row {
  display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "fac main apt" "fac notes notes";
  align-items: center; column-gap: 10px; width: 100%; text-align: left;
  border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 8px;
}
.pick-row:hover, .pick-row:focus-visible { background: var(--surface-2); }
.pick-row.sel { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.pick-row.dim .pr-main, .pick-row.dim > .fac { opacity: .6; }
.pick-row > .fac { grid-area: fac; }
.pr-main { grid-area: main; display: flex; flex-direction: column; min-width: 0; }
.pr-name { font-weight: 600; }
.pr-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pr-apt { grid-area: apt; }
.pr-notes { grid-area: notes; display: flex; flex-wrap: wrap; gap: 8px; font-size: 12px; }
.pn-cur { color: var(--ok); }
.pn-warn { color: var(--warn); }
.pn-err { color: var(--err); }
.picker-foot { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-top: 1px solid var(--border); }
.picker-foot .picker-count { margin-right: auto; }
@media (max-width: 520px) {
  .picker { width: 100%; height: 100%; max-width: none; max-height: none; border-radius: 0; }
  .pick-row { padding: 6px 4px; }
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%) translateY(150%);
  max-width: min(560px, calc(100% - 32px)); background: var(--text); color: var(--bg);
  padding: 10px 14px; border-radius: var(--radius); transition: transform .2s; z-index: 20;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.warn { border-left: 4px solid var(--warn); }

/* ---------- narrow screens ---------- */
@media (max-width: 860px) {
  .slots { grid-template-columns: 1fr; }
  .slot { border-right: 0; border-bottom: 1px solid var(--border); }
  .slot:last-child { border-bottom: 0; }
}
@media (max-width: 520px) {
  main { padding: 12px; }
  .top-row { padding: 8px 12px; }
  .tabs { padding: 0 12px; }
  .team-name { width: 100%; }
  .team-head .actions { margin-left: 0; }
}

/* ---------- skill picker ---------- */
.sk-btn { min-height: 34px; }
.sk-btn .pb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sk-btn small { white-space: nowrap; }
.row .sk-btn { flex: 1; min-width: 0; }
.sk-type {
  grid-area: fac; align-self: start; font-size: 12px; padding: 1px 6px; border-radius: 4px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.pr-desc {
  font-size: 12px; color: var(--muted); margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.rank { display: inline-block; min-width: 1.6em; text-align: center; font-weight: 700; border-radius: 4px; padding: 0 4px; border: 1px solid currentColor; }
.rank-S { color: var(--g-S); }
.rank-A { color: var(--g-A); }
.rank-B { color: var(--g-B); }
.sk-row { align-items: start; }
.seg { max-width: 100%; overflow-x: auto; }
.seg button { white-space: nowrap; flex: none; }
@media (max-width: 520px) {
  .seg button { padding: 4px 7px; }
}
.picker-tabs { margin: 0; padding: 0 16px; }
.picker-note { margin: 8px 16px 0; font-size: 13px; }
.pick-btn .pb-name.scn { color: var(--info); }
.ov-skills { font-size: 12px; color: var(--muted); white-space: normal; max-width: 16em; margin-top: 2px; }

/* ---------- tooltip ---------- */
.tip {
  position: fixed; z-index: 30; max-width: min(360px, calc(100vw - 16px));
  background: var(--text); color: var(--bg); font-size: 12px; line-height: 1.6;
  padding: 8px 10px; border-radius: var(--radius); white-space: pre-line;
  pointer-events: none; opacity: 0; transition: opacity .12s;
}
.tip.show { opacity: 1; }

/* skill chips in overview tables */
.sk-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.sk-chip {
  font-size: 12px; line-height: 1.6; padding: 0 6px; border-radius: 4px; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: help;
}
.sk-chip.scn { color: var(--info); border-color: color-mix(in srgb, var(--info) 45%, transparent); }
.sk-chip.empty { color: var(--muted); border-style: dashed; background: none; cursor: default; }

/* switchable faction (e.g. 漢 ⇄ 群) */
.fac-switch {
  display: inline-flex; align-items: center; gap: 3px; padding: 1px 6px 1px 2px;
  border-radius: 999px; background: var(--surface-2);
}
.fac-switch .fs-arrow { font-size: 12px; color: var(--muted); }
.fac-switch:hover:not(:disabled) .fs-arrow { color: var(--accent); }
@media (max-width: 520px) {
  .tabs { gap: 6px; padding: 0 12px 10px; }
  .tabs button { padding: 6px 11px; font-size: 14px; }
  .subtabs button { padding: 6px 12px; }
}

/* ---------- pinned list controls (resource management) ---------- */
.sticky-tools {
  position: sticky; top: var(--top-h, 0px); z-index: 6;
  background: var(--bg); margin: -16px -16px 0; padding: 16px 16px 4px;
  border-bottom: 1px solid transparent;
}
.sticky-tools .subtabs { margin-bottom: 10px; }
.sticky-tools .toolbar:last-child { margin-bottom: 4px; }
.table-wrap.no-scroll { overflow: visible; }
.table-wrap.no-scroll .list th { top: calc(var(--top-h, 0px) + var(--tools-h, 0px)); z-index: 5; }
@media (max-width: 520px) {
  .sticky-tools { margin: -12px -12px 0; padding: 12px 12px 4px; }
}
@media (max-width: 520px) {
  /* keep the pinned block short on phones: one scrollable line per toolbar */
  .sticky-tools .toolbar { flex-wrap: nowrap; overflow-x: auto; margin-bottom: 6px; }
  .sticky-tools .toolbar > * { flex: none; }
  .sticky-tools .toolbar input[type=search] { flex: 0 0 150px; }
  .sticky-tools .toolbar button, .sticky-tools .toolbar select { padding: 4px 8px; font-size: 13px; }
  .sticky-tools .subtabs { margin-bottom: 8px; }
}

/* aptitude column: one sort button per troop type under the column title */
.th-apt-label { display: block; }
.apt-sorts { display: inline-flex; gap: 2px; margin-top: 2px; }
.apt-sorts .th-sort { font-size: 12px; font-weight: 500; padding: 0 3px; border-radius: 4px; }
.apt-sorts .th-sort.on { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.apt-sorts .sort-ind { font-size: 9px; }
.rank-chip { justify-content: center; min-width: 1.9em; }
.sk-btn .rank-chip { flex: none; }

/* slot build tags: unlit until chosen */
.stags { display: flex; flex-wrap: wrap; gap: 4px 8px; }
.stag-grp { display: inline-flex; flex-wrap: wrap; gap: 3px; }
.stag { font-size: 12px; padding: 2px 7px; border-radius: 999px; color: var(--muted); background: transparent; }
.stag:hover:not(:disabled):not(.on) { border-color: var(--accent); color: var(--accent); }
.stag.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.stag:disabled { opacity: .35; cursor: not-allowed; }

/* clickable red level in the inventory: ⊘ resets, the n-th pip sets n reds */
.red-picker { display: inline-flex; align-items: center; gap: 1px; white-space: nowrap; }
.red-picker button { border: 0; background: none; padding: 2px 1px; min-height: 0; line-height: 1; border-radius: 4px; }
.red-picker .rp-zero { font-size: 18px; color: var(--muted); padding: 2px 4px; margin-right: 2px; }
.red-picker .rp-pip .mag { width: 14px; height: 19px; display: block; }
.red-picker button:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.red-picker .rp-zero:hover:not(:disabled) { color: var(--accent); }
.red-picker button:disabled { cursor: not-allowed; opacity: .45; }

/* Ming (serif) face after the in-game text, for titles, general / skill names and skill descriptions;
   numbers, tables and controls keep the sans-serif body font for legibility at small sizes. */
.top h1, .team-title, .card h2, .picker-head h2,
.list td.nm, .gname, .pick-btn .pb-name, .pr-name, .sk-chip,
.tip, .list td.desc, .pr-desc {
  font-family: "Noto Serif TC", "PMingLiU", "MingLiU", serif;
}
.top h1 { font-weight: 700; letter-spacing: .04em; }
.tip { font-size: 13px; font-weight: 500; }
.list td.desc, .pr-desc { font-weight: 500; }
