:root {
  --bg: #eef1f6; --surface: #fff; --ink: #1b2433; --muted: #5d6b82; --line: #d9dfea;
  --accent: #4b3fd1; --accent-soft: #e8e6fb;
  --gem: #0d7f91; --gold: #a76d00;
  --ok: #1d7a4d; --ok-bg: #dff3e8; --warn: #9a5a00; --warn-bg: #fdecc8; --bad: #b3261e; --bad-bg: #fbe1de;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151f; --surface: #1a2030; --ink: #e8ecf5; --muted: #9aa7bf; --line: #2b3449;
    --accent: #9f94ff; --accent-soft: #2a2750;
    --gem: #4cc9dc; --gold: #f0b94a;
    --ok: #6fd6a0; --ok-bg: #173526; --warn: #f0b94a; --warn-bg: #3a2c10; --bad: #ff9a92; --bad-bg: #3d1a18;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 16px/1.45 "Bricolage Grotesque", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top, 0)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.topbar-row { display: flex; align-items: center; justify-content: space-between; }
h1 { margin: 0; font-size: 1.5rem; font-weight: 700; letter-spacing: -.02em; }
.icon-btn {
  display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line);
}
.icon-btn.small { width: 40px; height: 40px; flex: none; }
.search {
  width: 100%; height: 46px; margin: 8px 0 10px; padding: 0 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
}
.search::placeholder { color: var(--muted); }
.filters { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.filters::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 38px; padding: 0 14px; border-radius: 999px; background: var(--surface);
  border: 1px solid var(--line); white-space: nowrap;
}
.chip span { color: var(--muted); margin-left: 4px; font-size: .85em; }
.chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip.is-active span { color: inherit; opacity: .85; }

/* List */
main { max-width: 1100px; margin: 0 auto; padding: 14px 16px 40px; }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; gap: 10px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.code-wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-width: 0; }
.code { font-size: 1.2rem; font-weight: 700; letter-spacing: .01em; overflow-wrap: anywhere; }
.badge { font-size: .78rem; font-weight: 600; padding: 2px 9px; border-radius: 999px; }
.badge.active { color: var(--ok); background: var(--ok-bg); }
.badge.used-up { color: var(--warn); background: var(--warn-bg); }
.badge.expired { color: var(--bad); background: var(--bad-bg); }
.desc { margin: 0; color: var(--muted); }
.currency { display: flex; gap: 8px; }
.cur { flex: 1; padding: 8px 10px; border-radius: 10px; background: var(--bg); font-size: .9rem; color: var(--muted); }
.cur b { display: block; font-size: 1.15rem; }
.cur.gem b { color: var(--gem); } .cur.gold b { color: var(--gold); }
.usage-text { display: flex; justify-content: space-between; font-size: .88rem; color: var(--muted); margin-bottom: 4px; }
.usage-text b { color: var(--ink); }
.bar { height: 8px; border-radius: 99px; background: var(--bg); overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: inherit; }
.expiry { margin: 0; font-size: .85rem; color: var(--muted); }
.toggle {
  min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--accent-soft);
  color: var(--accent); font-weight: 600; text-align: left; padding: 0 12px; position: relative;
}
.toggle::after { content: "▾"; position: absolute; right: 14px; transition: transform .2s; }
.toggle[aria-expanded="true"]::after { transform: rotate(180deg); }
.toggle:disabled { background: transparent; color: var(--muted); cursor: default; }
.toggle:disabled::after { content: none; }
.items { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.items[hidden] { display: none; }
.item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
}
.item.muted { color: var(--muted); justify-content: flex-start; border-style: dashed; }
.item-name { display: block; font-weight: 600; }
.item-meta { display: block; font-size: .8rem; color: var(--muted); }
.qty { font-weight: 700; color: var(--accent); }

/* States */
.status { text-align: center; color: var(--muted); padding: 40px 16px; }
.status.error { color: var(--bad); }
.retry { display: block; margin: 12px auto 0; min-height: 44px; padding: 0 18px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; font-weight: 600; }
.skeleton i { display: block; height: 14px; border-radius: 6px; background: var(--bg); animation: pulse 1.2s ease-in-out infinite; }
.skeleton i:nth-child(1) { width: 40%; height: 20px; } .skeleton i:nth-child(2) { width: 75%; } .skeleton i:nth-child(3) { width: 55%; }
@keyframes pulse { 50% { opacity: .4; } }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(20px + env(safe-area-inset-bottom, 0));
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: .9rem; z-index: 10;
}
.toast[hidden] { display: none; }

/* Larger screens */
@media (min-width: 640px) { .list { grid-template-columns: repeat(2, 1fr); align-items: start; } }
@media (min-width: 980px) { .list { grid-template-columns: repeat(3, 1fr); } .topbar { padding-inline: max(16px, calc((100% - 1068px) / 2)); } .filters { margin: 0; padding: 0; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---- Management UI ---- */
.row-gap { display: flex; gap: 8px; align-items: center; }
.btn {
  min-height: 44px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary, .chip.is-active, .retry { color: #14112e; } }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.danger { color: #2a0b09; } }
.btn.danger-outline { color: var(--bad); }
.btn:disabled { opacity: .6; cursor: default; }
.card-actions { display: flex; gap: 8px; }
.card-actions .btn { flex: 1; }

/* Sheets (dialogs): bottom sheet on phones, centered on larger screens */
dialog.sheet {
  border: 0; padding: 0; background: var(--surface); color: var(--ink);
  width: 100%; max-width: 100%; max-height: 92dvh; margin: auto 0 0; border-radius: 18px 18px 0 0;
  flex-direction: column; overflow: hidden;
}
dialog.sheet[open] { display: flex; }
dialog.sheet::backdrop { background: rgba(10, 14, 24, .55); }
.sheet-head, .sheet-foot { display: flex; align-items: center; gap: 8px; padding: 14px 16px; flex: none; }
.sheet-head { justify-content: space-between; border-bottom: 1px solid var(--line); }
.sheet-head h2, .sheet-body h2 { margin: 0; font-size: 1.2rem; }
.sheet-foot {
  justify-content: flex-end; border-top: 1px solid var(--line);
  padding-bottom: calc(14px + env(safe-area-inset-bottom, 0));
}
.sheet-foot .btn { flex: 1; }
.sheet-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; flex: 1; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: .92rem; }
.field input {
  height: 46px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); font-weight: 400; width: 100%; min-width: 0;
}
.field input[readonly] { opacity: .65; }
.field small { font-weight: 400; color: var(--muted); font-size: .8rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.items-head { display: flex; align-items: center; justify-content: space-between; }
.items-head h3 { margin: 0; font-size: 1.05rem; }
.items.edit { margin: 0; }
.grow { flex: 1; min-width: 0; }
.qty-input { width: 72px; height: 40px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); padding: 0 8px; text-align: center; }
.form-error { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--bad-bg); color: var(--bad); font-weight: 600; }
.form-error[hidden] { display: none; }
.picker-tools { padding: 10px 16px 0; flex: none; }
.picker-tools .filters { margin: 0; padding: 0; }
.picker-list { list-style: none; margin: 0; padding: 4px 16px 16px; overflow-y: auto; display: grid; gap: 6px; flex: 1; align-content: start; }
.pick {
  width: 100%; text-align: left; padding: 10px 12px; border-radius: 10px; min-height: 52px;
  border: 1px solid var(--line); background: var(--surface);
}
.pick:hover { background: var(--accent-soft); }
.pick .item-meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (min-width: 640px) {
  dialog.sheet { width: 560px; margin: auto; border-radius: 18px; max-height: 86dvh; }
  dialog.sheet.small { width: 420px; }
}
