:root {
  --bg: #16161d;
  --surface: #20202a;
  --surface-2: #2a2a37;
  --line: #34344a;
  --text: #f3f3f7;
  --muted: #a3a3b8;
  --red: #e3350d;
  --red-2: #ff5a36;
  --yellow: #ffcb05;
  --ok: #4ade80;
  --warn: #fbbf24;
  --radius: 14px;
  --tabbar-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans JP", "Noto Sans TC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; letter-spacing: .3px; }
.ball {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid #111;
  background: linear-gradient(var(--red) 0 46%, #111 46% 54%, #fff 54%);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.15);
}
.offline-pill { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); color: var(--warn); }

main {
  max-width: 760px; margin: 0 auto;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  outline: none;
}
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 5;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabbar a span { font-size: 20px; filter: grayscale(1) opacity(.7); transition: filter .15s; }
.tabbar a[aria-current] { color: var(--text); }
.tabbar a[aria-current] span { filter: none; }

.screen-title { font-size: 22px; margin: 4px 0 14px; }
.screen-title small { color: var(--muted); font-weight: 600; font-size: 15px; margin-left: 6px; }
.screen-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.screen-head .screen-title { margin-bottom: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2);
  font-weight: 600; cursor: pointer; transition: transform .08s, background .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { background: var(--red-2); }
.btn.ghost { background: transparent; }
.btn.danger { color: #ff8b8b; border-color: #5a2a2a; background: #2b1a1d; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 13px; }
.btn.big { min-height: 52px; font-size: 16px; }
.btn:focus-visible, .chip:focus-visible, .icon-btn:focus-visible, a:focus-visible { outline: 2px solid var(--yellow); outline-offset: 2px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 34px; padding: 0 14px; border-radius: 99px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: 13px;
}
.chip.on { background: var(--yellow); border-color: var(--yellow); color: #1a1a1a; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface-2); font-size: 20px; cursor: pointer;
}

input[type=search], input:not([type]), textarea {
  width: 100%; padding: 11px 13px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); outline: none;
}
input:focus, textarea:focus { border-color: var(--yellow); }

/* ---------- empty state ---------- */
.empty {
  text-align: center; padding: 36px 20px; border: 1px dashed var(--line);
  border-radius: var(--radius); background: var(--surface);
}
.empty.wide { grid-column: 1 / -1; }
.empty-art { font-size: 54px; }
.empty p { color: var(--muted); max-width: 420px; margin: 8px auto 18px; }

/* ---------- QR panel ---------- */
.card-canvas {
  display: block; width: 100%; max-width: 520px; margin: 0 auto; aspect-ratio: 1;
  border-radius: 22px; box-shadow: 0 12px 40px rgba(0,0,0,.45); background: var(--surface);
}
.status-line { text-align: center; font-size: 13px; margin: 12px 0; }
.status-line .ok { color: var(--ok); }
.status-line .warn { color: var(--warn); }
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; max-width: 520px; margin: 0 auto; }
.actions .big { grid-column: 1 / -1; }
.decoded { max-width: 520px; margin: 14px auto 0; color: var(--muted); font-size: 13px; }
.decoded summary { cursor: pointer; }
.decoded code { display: block; margin-top: 8px; padding: 10px; border-radius: 10px; background: var(--surface); word-break: break-all; white-space: pre-wrap; }

.qr-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.qr-tile {
  padding: 8px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; display: flex; flex-direction: column; gap: 8px; text-align: center; font-weight: 600;
}
.qr-tile canvas { width: 100%; aspect-ratio: 1; border-radius: 10px; }

/* ---------- tags ---------- */
.search-row { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; }
.search-row .chip { white-space: nowrap; min-height: 44px; }
.tag-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.tag-card { position: relative; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); overflow: hidden; }
.tag-open { all: unset; display: flex; flex-direction: column; width: 100%; cursor: pointer; }
.tag-open:focus-visible { outline: 2px solid var(--yellow); }
.tag-open img, .no-photo { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; background: var(--surface-2); display: grid; place-items: center; font-size: 32px; color: var(--muted); }
.tag-name { padding: 8px 10px 0; font-weight: 700; }
.tag-note { padding: 0 10px; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tag-open > :last-child { padding-bottom: 10px; }
.fav {
  position: absolute; top: 6px; right: 6px; width: 34px; height: 34px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.55); color: #888; font-size: 18px; cursor: pointer;
}
.fav.on { color: var(--yellow); }

/* ---------- settings ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; }
.panel h2 { font-size: 16px; margin: 0 0 6px; }
.panel p { margin: 4px 0 12px; }
.danger-zone { border-color: #5a2a2a; }

/* ---------- sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,0);
  display: flex; align-items: flex-end; justify-content: center; transition: background .16s;
}
.sheet-backdrop.open { background: rgba(0,0,0,.6); }
.sheet {
  position: relative; width: 100%; max-width: 480px; max-height: 92vh; overflow: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; border: 1px solid var(--line);
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  transform: translateY(100%); transition: transform .18s ease-out;
}
.sheet.wide { max-width: 620px; }
.sheet-backdrop.open .sheet { transform: none; }
.sheet-backdrop.closing { background: rgba(0,0,0,0); }
.sheet-backdrop.closing .sheet { transform: translateY(100%); }
.sheet h3 { margin: 0 0 12px; }
.sheet .card-canvas.preview { max-width: 360px; margin-bottom: 14px; }
.sheet .styles { margin-bottom: 12px; }
.sheet-x { position: absolute; top: 10px; right: 10px; z-index: 1; width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--surface-2); cursor: pointer; }
.sheet-actions { display: flex; gap: 10px; justify-content: flex-end; align-items: center; margin-top: 16px; }
.detail { padding-top: 30px; }
.confirm-msg { margin: 4px 0 8px; font-size: 16px; }
.color-row { display: flex; gap: 18px; margin-bottom: 12px; }
.color-row label { display: flex; align-items: center; gap: 8px; font-weight: 600; }
input[type=color] { width: 44px; height: 34px; border: 1px solid var(--line); border-radius: 8px; background: none; padding: 2px; }

.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; color: var(--muted); }
.form label.check { flex-direction: row; align-items: center; color: var(--text); font-size: 15px; }
.form label.check input { width: 20px; height: 20px; accent-color: var(--yellow); }
.photo-pick {
  align-self: center; width: 150px; aspect-ratio: 3 / 4; border-radius: 12px; overflow: hidden; cursor: pointer;
  border: 1px dashed var(--line); background: var(--surface-2); color: var(--muted); padding: 0;
}
.photo-pick img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ---------- autocomplete ---------- */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 3; margin: 0; padding: 4px; list-style: none;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; max-height: 260px; overflow: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.ac-list li { display: flex; align-items: baseline; gap: 8px; padding: 8px 10px; border-radius: 8px; cursor: pointer; color: var(--text); }
.ac-list li.on, .ac-list li:hover { background: var(--line); }
.ac-list small { color: var(--muted); font-weight: 400; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-no { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ---------- cropper ---------- */
.cropper { position: fixed; inset: 0; z-index: 40; display: flex; flex-direction: column; background: #000; touch-action: none; }
.cropper-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px; background: #0d0d12;
}
.cropper-bar h2 { font-size: 16px; margin: 0; }
.cropper-stage { flex: 1; position: relative; overflow: hidden; }
.cropper-stage canvas { position: absolute; inset: 0; cursor: grab; touch-action: none; }
.cropper-stage canvas:active { cursor: grabbing; }
.cropper-tools { padding: 10px 12px calc(env(safe-area-inset-bottom) + 12px); background: #0d0d12; display: flex; flex-direction: column; gap: 10px; }
.cropper-tools .chips { justify-content: center; }
.cropper-tools input[type=range] { flex: 1; accent-color: var(--yellow); }

/* ---------- scan mode ---------- */
.scan {
  position: fixed; inset: 0; z-index: 50; background: #fff; color: #111;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 16px);
}
.scan canvas { width: min(92vw, 70vh); height: auto; aspect-ratio: 1; border-radius: 16px; }
.scan-label { margin: 0; font-weight: 800; font-size: 18px; }
.scan-bar { display: flex; gap: 10px; }
.scan .btn:not(.primary) { background: #eee; border-color: #ddd; color: #111; }
.scan-hint { margin: 0; font-size: 12px; color: #666; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  background: #f3f3f7; color: #111; font-weight: 600; font-size: 14px;
  transition: opacity .2s, transform .2s; box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 420px) {
  .actions { grid-template-columns: repeat(2, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ---------- settings: privacy + usage ---------- */
.privacy { border-color: #2f5a3a; }
.privacy p:first-of-type { margin-top: 8px; }
.privacy ul { padding-left: 18px; margin: 8px 0; }
.privacy li { margin-bottom: 6px; }
.usage { width: 100%; border-collapse: collapse; margin: 6px 0 10px; font-variant-numeric: tabular-nums; }
.usage td { padding: 5px 0; border-bottom: 1px solid var(--line); }
.usage td:last-child { text-align: right; }
.usage tr.total td { font-weight: 700; border-bottom-width: 2px; }
.panel code { font-size: 12px; }
.empty a { color: var(--yellow); }
.tabbar b { font-weight: inherit; }

/* ---------- multiple trainers, regions, QR size ---------- */
:root { --qr-scale: 1; }
.qr-panel > .card-canvas { max-width: calc(520px * var(--qr-scale)); width: calc(100% * var(--qr-scale)); }
.scan canvas { width: calc(min(92vw, 70vh) * var(--qr-scale)); }
.trainer-strip { max-width: 520px; margin: 28px auto 0; }
.trainer-strip h2 { font-size: 16px; margin: 0 0 10px; }
.trainer-strip h2 small { color: var(--muted); font-weight: 600; }
.qr-grid.small { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.qr-tile small { color: var(--muted); font-weight: 600; font-size: 12px; margin-top: -4px; }
.qr-tile.on { border-color: var(--yellow); box-shadow: 0 0 0 1px var(--yellow); }
.add-tile { justify-content: center; align-items: center; min-height: 140px; border-style: dashed; color: var(--muted); }
.add-tile .plus { font-size: 30px; line-height: 1; }
.region-chips { margin: -4px 0 14px; }
.form select {
  width: 100%; padding: 11px 13px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit;
}

/* ---------- tags tab: segmented + catalog ---------- */
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; margin-bottom: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.segmented button { min-height: 38px; border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 700; cursor: pointer; }
.segmented button[aria-selected="true"] { background: var(--surface-2); color: var(--text); }
.chips.scroll-x { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px 12px; padding: 0 16px; }
.chips.scroll-x::-webkit-scrollbar { display: none; }
.chips.scroll-x .chip { flex: none; }
.filter-chips { margin-bottom: 10px; }
.progress { margin-bottom: 12px; }
.progress-bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress-bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--yellow), var(--red)); transition: width .25s; }
.progress-text { margin: 6px 0 0; color: var(--muted); font-size: 13px; font-weight: 600; }
.catalog-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 4px; }
.cat-card {
  position: relative; display: flex; flex-direction: column; text-align: left; padding: 0 0 8px; overflow: hidden;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font: inherit; color: inherit;
  --g: #8b8ba7;
}
.cat-card.g-superstar { --g: #ffcb05; }
.cat-card.g-star { --g: #5aa9ff; }
.cat-card.g-regular { --g: #8b8ba7; }
.cat-card.g-special { --g: #c084fc; }
.cat-img { display: block; aspect-ratio: 1048 / 833; border-bottom: 3px solid var(--g);
  background: radial-gradient(circle at 50% 40%, color-mix(in srgb, var(--g) 30%, var(--surface-2)), var(--surface-2)); }
.cat-img img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cat-no { padding: 6px 10px 0; font: 600 12px/1.2 ui-monospace, "SFMono-Regular", Consolas, monospace; color: var(--g); }
.cat-name { padding: 2px 10px 0; font-weight: 700; font-size: 14px; }
.cat-check {
  position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(0,0,0,.55); color: transparent; border: 2px solid rgba(255,255,255,.5); font-weight: 900;
}
.cat-card.owned { border-color: var(--ok); }
.cat-card.owned .cat-check { background: var(--ok); border-color: var(--ok); color: #0b2814; }
.cat-card:not(.owned) .cat-img img { filter: saturate(.55) brightness(.8); }
.bulk { justify-content: center; margin: 16px 0 6px; }
.source a { color: var(--yellow); }

/* ---------- official support cards ---------- */
.section-title { font-size: 16px; margin: 18px 0 6px; }
.section-title small { color: var(--muted); font-weight: 600; }
.support-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin: 8px 0 8px; }
.support-tile { padding: 0; border: 0; background: none; cursor: pointer; border-radius: 14px; }
.support-tile canvas { display: block; width: 100%; aspect-ratio: 1080 / 1350; border-radius: 14px; box-shadow: 0 6px 18px rgba(0,0,0,.35); }
.support-tile:focus-visible { outline: 2px solid var(--yellow); outline-offset: 3px; }
.support-canvas { display: block; width: 100%; max-width: 420px; margin: 0 auto 14px; aspect-ratio: 1080 / 1350; border-radius: 18px; }

/* ---------- update banner ---------- */
.update-bar {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 58px); transform: translateX(-50%); z-index: 70;
  display: flex; align-items: center; gap: 12px; padding: 8px 8px 8px 16px; border-radius: 14px;
  background: var(--surface-2); border: 1px solid var(--yellow); box-shadow: 0 8px 24px rgba(0,0,0,.45);
  font-weight: 700; white-space: nowrap;
}
