:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --text: #16181d;
  --text-muted: #5f6573;
  --border: #dde0e8;
  --primary: #4f46e5;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #b45309;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 12px rgb(0 0 0 / .06);
  --radius: 14px;
  --topbar-h: 56px;
  --ad-h: 0px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #191c22;
  --surface-2: #232730;
  --text: #eceef3;
  --text-muted: #9aa1b0;
  --border: #2d323d;
  --primary: #818cf8;
  --primary-text: #0f1115;
  --danger: #f87171;
  --success: #4ade80;
  --warning: #fbbf24;
  --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 4px 12px rgb(0 0 0 / .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
body.has-ad { --ad-h: 64px; }
[hidden] { display: none !important; }
a { color: var(--primary); }
h1 { font-size: 1.35rem; margin: 0 0 1rem; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 .5rem; }
p { margin: .5rem 0; }
.muted { color: var(--text-muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  display: flex; align-items: center; gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; }
.topnav { margin-left: auto; display: flex; gap: 4px; }
.sync-status { font-size: .75rem; color: var(--text-muted); margin-left: 8px; }
.icon-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent;
  color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.badge {
  position: absolute; top: 4px; right: 4px; min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 8px; background: var(--danger); color: #fff; font-size: 10px; line-height: 16px; text-align: center;
}
.badge:empty { display: none; }

.offline-banner {
  background: var(--surface-2); color: var(--text-muted); text-align: center; font-size: .8rem; padding: 4px;
}

main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(96px + var(--ad-h) + env(safe-area-inset-bottom));
  outline: none;
}

/* Form elements */
label { display: block; font-weight: 600; font-size: .9rem; margin: 12px 0 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--primary); outline-offset: 1px;
}
input[type="color"] { padding: 2px; height: 44px; width: 64px; }
input[type="checkbox"] { width: auto; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; padding: 10px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; min-height: 44px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.btn-row > .btn { flex: 1 1 auto; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.error { color: var(--danger); font-size: .9rem; margin-top: 8px; }
.notice { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: .9rem; margin: 8px 0; }
.notice.warn { color: var(--warning); }

/* Card list */
.list-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.list-tools input { flex: 1; }
.list-tools select { width: auto; }
.cards-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.card-tile {
  position: relative; display: flex; flex-direction: column; justify-content: space-between;
  aspect-ratio: 1.586; padding: 12px; border-radius: var(--radius);
  color: #fff; text-decoration: none; box-shadow: var(--shadow); overflow: hidden;
  background: var(--card-color, #4f46e5);
}
.card-tile::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(135deg, rgb(255 255 255 / .18), transparent 60%);
  pointer-events: none;
}
.card-tile .title { font-weight: 700; font-size: 1.05rem; text-shadow: 0 1px 2px rgb(0 0 0 / .35); word-break: break-word; }
.card-tile .meta { font-size: .75rem; opacity: .9; display: flex; gap: 6px; align-items: center; }
.card-tile.light-bg { color: #111; }
.card-tile.light-bg .title { text-shadow: none; }
.card-tile .fav { position: absolute; top: 8px; right: 10px; }
.empty { text-align: center; padding: 48px 16px; color: var(--text-muted); }
.empty svg { width: 72px; height: 72px; fill: var(--border); }
.limit-meter { font-size: .8rem; color: var(--text-muted); text-align: right; margin: 8px 0; }

.fab {
  position: fixed; right: max(16px, env(safe-area-inset-right));
  bottom: calc(20px + var(--ad-h) + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%; border: 0;
  background: var(--primary); color: var(--primary-text); box-shadow: var(--shadow);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  text-decoration: none; z-index: 5;
}
.fab svg { width: 30px; height: 30px; fill: currentColor; display: block; }

/* Add sheet */
.choice-list { display: grid; gap: 12px; }
.choice {
  display: flex; align-items: center; gap: 14px; padding: 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  text-decoration: none; font: inherit; text-align: left; cursor: pointer; width: 100%;
}
.choice svg { width: 32px; height: 32px; fill: var(--primary); flex: none; }
.choice strong { display: block; }

/* Scanner */
.scanner { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 3/4; max-height: 70dvh; margin: 0 auto; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame {
  position: absolute; inset: 25% 10%; border: 3px solid rgb(255 255 255 / .85); border-radius: 12px;
  box-shadow: 0 0 0 100vmax rgb(0 0 0 / .35);
}

/* Card detail (full screen, max brightness simulation) */
.card-view {
  position: fixed; inset: 0; z-index: 50; background: #fff; color: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  color-scheme: light;
}
.card-view .cv-top {
  position: absolute; top: env(safe-area-inset-top); left: 0; right: 0;
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
}
.card-view .cv-top .icon-btn { color: #000; }
.card-view .cv-top .icon-btn:hover { background: #eee; }
.card-view h1 { margin: 0; font-size: 1.2rem; flex: 1; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-view .barcode-wrap { width: 100%; max-width: 640px; display: flex; justify-content: center; padding: 16px; background: #fff; }
.card-view canvas { max-width: 100%; height: auto; image-rendering: pixelated; }
.card-view canvas.twod { width: min(80vw, 60dvh); }
.card-view canvas.oned { width: min(92vw, 640px); }
.card-view .code-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 1.3rem; letter-spacing: .08em; margin-top: 8px; word-break: break-all; text-align: center; }
.card-view .notes { margin-top: 16px; color: #444; max-width: 640px; white-space: pre-wrap; text-align: center; }
.card-view .hint { position: absolute; bottom: calc(12px + env(safe-area-inset-bottom)); font-size: .75rem; color: #777; text-align: center; left: 16px; right: 16px; }
.card-view .render-error { color: #b91c1c; }

/* Menus / lists */
.row-list { list-style: none; margin: 0; padding: 0; }
.row-list li { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.row-list li:last-child { border-bottom: 0; }
.row-list .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.pill { font-size: .7rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-muted); }
.pill.premium { background: linear-gradient(90deg, #f59e0b, #ec4899); color: #fff; }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmented button { flex: 1; border: 0; background: var(--surface); color: var(--text); padding: 10px; font: inherit; cursor: pointer; }
.segmented button + button { border-left: 1px solid var(--border); }
.segmented button[aria-pressed="true"] { background: var(--primary); color: var(--primary-text); }

.premium-hero { text-align: center; padding: 8px 0 16px; }
.premium-hero .price { font-size: 2rem; font-weight: 800; }
.feature-list { list-style: none; padding: 0; margin: 0; }
.feature-list li { padding: 8px 0 8px 28px; position: relative; }
.feature-list li::before { content: "✓"; position: absolute; left: 4px; color: var(--success); font-weight: 700; }

/* Ad slot */
.ad-slot {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  height: calc(var(--ad-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.ad-slot a.ad-link { display: flex; align-items: center; justify-content: center; height: 100%; max-width: 480px; width: 100%; color: var(--text); text-decoration: none; gap: 10px; padding: 0 40px 0 12px; font-size: .85rem; font-weight: 600; }
.ad-slot a.ad-link span { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.ad-slot img { max-height: 50px; max-width: 100%; }
.ad-slot .ad-label { position: absolute; top: 2px; left: 6px; font-size: .6rem; color: var(--text-muted); text-transform: uppercase; }
.ad-slot .ad-close { position: absolute; top: 0; right: 0; width: 32px; height: 32px; }
body.card-open .ad-slot { display: none !important; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + var(--ad-h) + env(safe-area-inset-bottom));
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  z-index: 100; max-width: calc(100vw - 32px); box-shadow: var(--shadow); font-size: .9rem;
}

.cf-turnstile { margin: 12px 0; min-height: 65px; }

@media (prefers-reduced-motion: no-preference) {
  .card-tile { transition: transform .12s ease; }
  .card-tile:active { transform: scale(.97); }
}

/* Social login */
.social-list { display: grid; gap: 10px; margin-bottom: 8px; }
.btn.social { gap: 10px; }
.btn.social svg { width: 20px; height: 20px; flex: none; }
.btn.social-google { background: #fff; color: #1f1f1f; border-color: #dadce0; }
.btn.social-google:hover { background: #f7f8f8; }
.btn.social-apple { background: #000; color: #fff; border-color: #000; }
.btn.social-apple:hover { background: #1a1a1a; }
:root[data-theme="dark"] .btn.social-apple { background: #fff; color: #000; border-color: #fff; }
.btn.social-facebook { background: #1877f2; color: #fff; border-color: #1877f2; }
.btn.social-facebook:hover { background: #166fe5; }
.divider { display: flex; align-items: center; gap: 12px; color: var(--text-muted); font-size: .85rem; margin: 8px 0; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
