:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #dde1e8;
  --text: #161a22;
  --muted: #646d7d;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --up: #1f8f4e;
  --down: #c92a2a;
  --digimon: #e8590c;
  --riftbound: #7048e8;
  --want: #d6336c;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 2px 8px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --surface: #181b22;
    --surface-2: #21252e;
    --border: #2c313c;
    --text: #e8eaef;
    --muted: #98a0ae;
    --accent: #6c8cff;
    --accent-text: #0b0e14;
    --up: #40c074;
    --down: #ff6b6b;
    --digimon: #ff8a3d;
    --riftbound: #9b7dff;
    --want: #f06595;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; color: inherit; }
h1 { font-size: 22px; margin: 0 0 4px; }
h1 .muted { font-size: 15px; font-weight: 400; }
h2 { font-size: 15px; margin: 0 0 8px; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 18px 0 8px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { display: flex; justify-content: center; margin: 24px 0; }
.up { color: var(--up); }
.down { color: var(--down); }
.grow { flex: 1 1 auto; min-width: 0; }
.mt { margin-top: 10px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.error { color: var(--down); min-height: 1em; margin: 0; font-size: 13px; }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.auth-card .stack { margin-top: 16px; }
.auth-card label, .stack > label, .field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.auth-card input { padding: 10px 12px; }
.stack > label.check { flex-direction: row; align-items: center; font-size: 14px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 20px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; letter-spacing: 0.04em; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 22px; margin-bottom: 4px; }
.logo { color: var(--accent); }
.tabs { display: flex; gap: 2px; }
.tabs a, .phone-friends {
  position: relative; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; white-space: nowrap;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.tabs a.active, .phone-friends.active { background: var(--surface-2); color: var(--text); }
.dot { position: absolute; top: 6px; right: 4px; width: 7px; height: 7px; border-radius: 50%; background: var(--want); }
.account { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.phone-friends { display: none; }

main { max-width: 1400px; margin: 0 auto; padding: 24px; }
.page-head { margin-bottom: 16px; }
.page-head p { margin: 4px 0 0; }

.bottom-nav { display: none; }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); padding: 8px 14px; border-radius: 8px;
  cursor: pointer; text-decoration: none; font-weight: 500; white-space: nowrap; color: var(--text);
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--down); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.big { padding: 14px 20px; font-size: 16px; border-radius: 12px; flex: 1; }
.btn.wanted { color: var(--want); border-color: var(--want); }
.btn:disabled { opacity: 0.5; cursor: default; }

input[type="search"], input[type="number"], input[type="text"], input[type="password"], input:not([type]), select {
  border: 1px solid var(--border); background: var(--surface); padding: 8px 10px; border-radius: 8px; min-width: 0;
  font-size: 16px; /* prevents iOS zoom on focus */
}
@media (min-width: 721px) {
  input[type="search"], input[type="number"], input[type="text"], input[type="password"], input:not([type]), select { font-size: 14px; }
}
input:focus, select:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 20px 0 12px; }
.toolbar input[type="search"] { flex: 1 1 260px; }
.toolbar select { max-width: 240px; }
.panel .toolbar { margin: 8px 0; }
.check { display: inline-flex; gap: 8px; align-items: center; color: var(--muted); cursor: pointer; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; align-self: flex-start; }
.seg.full { display: flex; margin-top: 16px; align-self: stretch; }
.seg.full button { flex: 1; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 7px; cursor: pointer; color: var(--muted); font-weight: 500; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 4px; }
.chip {
  display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); font-size: 12px; color: var(--text); cursor: pointer;
}
.chip:hover { text-decoration: none; background: var(--surface-2); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip[class*="color-"] { padding: 5px 12px; font-size: 13px; }
.chip[class*="color-"]::before { content: ""; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; background: var(--c); border: 1px solid rgba(127, 127, 127, 0.5); }
.color-red { --c: #e03131; } .color-blue { --c: #1c7ed6; } .color-yellow { --c: #fcc419; } .color-green { --c: #2f9e44; }
.color-black { --c: #212529; } .color-purple { --c: #7048e8; } .color-white { --c: #f8f9fa; }

/* ---------- Stats + panels ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-top: 12px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.chart { height: 180px; margin-top: 8px; }
.chart.chart-empty { height: 64px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.empty-chart { height: 100%; display: grid; place-items: center; color: var(--muted); font-size: 13px; }

/* ---------- Card grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 16px; }
.card {
  position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; display: flex; flex-direction: column; transition: transform 0.12s, box-shadow 0.12s;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.img { aspect-ratio: 5 / 7; background: var(--surface-2); display: grid; place-items: center; overflow: hidden; }
.img img { width: 100%; height: 100%; object-fit: contain; }
.img:empty::after { content: "No image"; color: var(--muted); font-size: 12px; }
.card .body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.name { font-weight: 600; font-size: 13px; line-height: 1.3; }
.card .name { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.meta { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .price-row { display: flex; justify-content: space-between; align-items: center; margin-top: auto; padding-top: 6px; gap: 6px; }
.card .price { font-weight: 650; font-variant-numeric: tabular-nums; font-size: 15px; }
.card .qty { font-size: 12px; color: var(--muted); }
.badge {
  position: absolute; top: 8px; left: 8px; z-index: 1; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: rgba(0, 0, 0, 0.75); color: #fff;
}
.game-dot { position: absolute; top: 10px; right: 10px; width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--surface); z-index: 1; }
.game-dot.inline { position: static; display: inline-block; margin-right: 6px; vertical-align: 1px; border: 0; width: 8px; height: 8px; }
.game-dot.digimon, .tag.digimon { background: var(--digimon); }
.game-dot.riftbound, .tag.riftbound { background: var(--riftbound); }
.quick-add {
  position: absolute; top: 8px; right: 8px; z-index: 1; width: 32px; height: 32px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); font-size: 18px; line-height: 1; cursor: pointer;
  opacity: 0; transition: opacity 0.12s; box-shadow: var(--shadow);
}
.card:hover .quick-add, .quick-add:focus { opacity: 1; }
@media (hover: none) { .quick-add { opacity: 1; } }

.empty { text-align: center; padding: 40px 0; color: var(--muted); }
.skeleton { aspect-ratio: 5 / 8.4; border-radius: var(--radius); background: var(--surface-2); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }

/* ---------- Row lists (friends, wants, decks) ---------- */
.rows { display: flex; flex-direction: column; }
.row-card {
  display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--border);
  color: var(--text); cursor: pointer;
}
.row-card:last-child { border-bottom: 0; }
a.row-card:hover, .row-card a.grow:hover { text-decoration: none; }
.row-card a.grow { color: var(--text); }
.row-card .thumb { width: 48px; flex: none; }
.row-card .thumb img { width: 100%; border-radius: 4px; display: block; }
.avatar {
  width: 38px; height: 38px; flex: none; border-radius: 50%; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 700;
}
.friend-list { list-style: none; margin: 0; padding: 0; }
.friend-list li { display: flex; flex-direction: column; padding: 6px 0; border-bottom: 1px solid var(--border); }
.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; color: #fff; font-size: 11px; font-weight: 600; vertical-align: middle; white-space: nowrap; }

/* ---------- Modal ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 0; width: min(960px, calc(100vw - 24px));
  max-height: calc(100dvh - 32px); background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.close {
  position: sticky; float: right; top: 10px; margin: 10px 10px -42px 0; border: 0; background: var(--surface-2);
  width: 32px; height: 32px; border-radius: 50%; font-size: 20px; cursor: pointer; z-index: 2;
}
.pad { padding: 24px; }
.narrow { max-width: 480px; }
.detail { display: grid; grid-template-columns: minmax(220px, 340px) 1fr; gap: 24px; padding: 24px; }
.detail .art { display: flex; flex-direction: column; gap: 12px; align-items: flex-start; }
.detail .art img { width: 100%; border-radius: 10px; display: block; background: var(--surface-2); }
.detail h2 { margin: 0 40px 4px 0; font-size: 20px; line-height: 1.25; }
.detail .sub { color: var(--muted); margin-bottom: 4px; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
td.num, th.num { text-align: right; }
td.market { font-weight: 650; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; white-space: pre-line; }

.form-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; }
.form-row label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.form-row input[type="number"] { width: 92px; }
.owned-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; padding: 10px 0; border-bottom: 1px solid var(--border); }
.owned-row .val { margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; }
.sparkline { height: 90px; }

/* ---------- Scan ---------- */
.scan-layout { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 20px; align-items: start; }
.scan-layout > div > .toolbar:first-child, #manualForm { margin-top: 0; }
.scan-panel { margin-top: 0; display: flex; flex-direction: column; gap: 12px; }
.scan-actions { display: flex; gap: 10px; }
.scan-preview img { width: 100%; max-height: 360px; object-fit: contain; border-radius: 10px; background: var(--surface-2); }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress div { height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }
.added-list { list-style: none; padding: 0; margin: 0; }
.added-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); }
#scanResults h3:first-child { margin-top: 0; }
.scan-preview:empty, #scanStatus:empty { display: none; }
#scanStatus { margin: 0; }

/* ---------- Decks ---------- */
.deck-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
.deck-stats div { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; }
.deck-stats strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.deck-stats span { font-size: 12px; color: var(--muted); }
.warnings { margin: 8px 0; padding-left: 18px; color: var(--down); font-size: 13px; }
.deck-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.deck-card { position: relative; border-radius: 8px; overflow: hidden; background: var(--surface-2); cursor: pointer; border: 2px solid transparent; }
.deck-card.concept { border: 2px dashed var(--want); }
.deck-card.concept .img { opacity: 0.6; }
.deck-card .need {
  position: absolute; top: 6px; right: 6px; z-index: 1; font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 999px;
  background: var(--want); color: #fff;
}
.deck-card .cap { padding: 6px 8px; }
.deck-card .name { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 500;
  transition: opacity 0.18s, transform 0.18s; z-index: 100; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Phone ---------- */
@media (max-width: 720px) {
  .topbar { padding: 10px 16px; gap: 12px; }
  .topbar .tabs, .sync-text { display: none; }
  .phone-friends { display: inline-block; }
  main { padding: 16px 16px calc(84px + env(safe-area-inset-bottom)); }
  .bottom-nav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
    background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); padding: 4px 0;
  }
  .bottom-nav a:hover { text-decoration: none; }
  .bottom-nav a.active { color: var(--accent); }
  .bottom-nav .ico { font-size: 18px; line-height: 1.2; }
  .bottom-nav .scan-btn .ico {
    background: var(--accent); color: var(--accent-text); width: 44px; height: 44px; border-radius: 50%;
    display: grid; place-items: center; margin-top: -18px; box-shadow: var(--shadow); font-size: 20px;
  }
  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .toolbar select { max-width: none; flex: 1 1 140px; }
  .detail { grid-template-columns: 1fr; padding: 16px; }
  .detail .art { max-width: 260px; margin: 0 auto; align-items: stretch; }
  .scan-layout { grid-template-columns: 1fr; }
  .deck-stats { grid-template-columns: repeat(2, 1fr); }
  .deck-grid { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); }
  h1 { font-size: 19px; }
}
