*,
*::before,
*::after { box-sizing: border-box; }

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #111827;
  --primary-text: #ffffff;
  --accent: #2563eb;
  --success: #16a34a;
  --danger: #dc2626;
  --warn: #d97706;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px -12px rgba(15, 23, 42, .12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d12;
    --surface: #141821;
    --surface-2: #1b202b;
    --border: #262c38;
    --text: #e8ecf2;
    --muted: #8b95a7;
    --primary: #f1f5f9;
    --primary-text: #0b0d12;
    --accent: #60a5fa;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
svg { width: 18px; height: 18px; flex: none; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* ---------- Buttons & inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px;
  border: 1px solid transparent; border-radius: 10px;
  font: inherit; font-weight: 600; font-size: 14px;
  cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: background .15s, border-color .15s, opacity .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-primary:hover { opacity: .9; }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { border-color: var(--muted); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.btn-danger-ghost:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
.btn-block { width: 100%; }

.icon-btn {
  width: 38px; height: 38px; display: grid; place-items: center;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}

input, select, textarea {
  width: 100%; min-width: 0;
  height: 40px; padding: 0 12px;
  font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  outline: none; transition: border-color .15s, box-shadow .15s;
}
textarea { height: auto; padding: 10px 12px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }

.form-error { color: var(--danger); font-size: 13px; min-height: 0; margin: 0; }
.form-error:empty { display: none; }

/* ---------- Login ---------- */

.login { min-height: 100svh; display: grid; place-items: center; padding: 20px; }
.login-box {
  width: min(100%, 360px); background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 32px 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 16px;
}
.login-box h1 { margin: 0 0 4px; font-size: 22px; text-align: center; }
.brand-mark {
  width: 56px; height: 56px; margin: 0 auto; border-radius: 16px;
  display: grid; place-items: center; background: var(--primary); color: var(--primary-text);
}
.brand-mark svg { width: 28px; height: 28px; }
.brand-mark.small { width: 32px; height: 32px; border-radius: 9px; margin: 0; }
.brand-mark.small svg { width: 18px; height: 18px; }

/* ---------- Layout ---------- */

.topbar { position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(12px); border-bottom: 1px solid var(--border); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; min-width: 0; }
.brand span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 8px; }

.container { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; box-shadow: var(--shadow); min-width: 0; }
.stat-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.stat-value { font-size: 26px; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; }
.search { position: relative; flex: 1; min-width: 0; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { padding-left: 38px; }
.toolbar select { width: auto; }

/* ---------- Member list ---------- */

.members { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.member {
  display: flex; align-items: center; gap: 14px; padding: 14px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); cursor: pointer; text-align: left; font: inherit; color: inherit; width: 100%;
  transition: border-color .15s, transform .1s;
}
.member:hover { border-color: var(--muted); }
.member:active { transform: scale(.995); }
.avatar {
  width: 42px; height: 42px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
}
.member-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.member-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.member-meta { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.member-points { text-align: right; flex: none; }
.member-points strong { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.member-points span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot.synced { background: var(--success); }
.dot.error { background: var(--danger); }
.dot.pending { background: var(--warn); }

.empty { text-align: center; color: var(--muted); padding: 60px 0; }

/* ---------- Drawer & modal ---------- */

.drawer, .modal { position: fixed; inset: 0; z-index: 50; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(2, 6, 23, .45); animation: fade .2s ease both; }
.drawer-panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(100%, 520px);
  background: var(--bg); border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  animation: slide .25s cubic-bezier(.2, .8, .2, 1) both;
}
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 18px 20px; border-bottom: 1px solid var(--border); background: var(--surface); }
.drawer-head h2 { margin: 0; font-size: 20px; word-break: break-word; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.drawer-body { overflow-y: auto; padding: 16px 20px 40px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }

.panel, .points-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.panel h3 { margin: 0 0 12px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

.points-box { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.points-label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.points-big { font-size: 52px; font-weight: 800; line-height: 1; letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
.points-big.bump { animation: bump .35s ease; }
.quick { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; width: 100%; }
.chip { height: 38px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); font: inherit; font-weight: 700; cursor: pointer; color: var(--text); }
.chip.pos { color: var(--success); }
.chip.neg { color: var(--danger); }
.chip:hover { border-color: currentColor; }
.points-form { width: 100%; display: grid; grid-template-columns: 110px 1fr; gap: 8px; }
.points-form .seg { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.link-row { display: flex; gap: 8px; }
.link-row input { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.row-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

.wallet-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wallet-row .btn { margin-left: auto; }
.badge { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px; border-radius: 999px; font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.badge-green { background: color-mix(in srgb, var(--success) 14%, transparent); color: var(--success); }
.badge-red { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.badge-amber { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }
.badge-blue { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
#d-wallet-error { margin-top: 8px; word-break: break-word; }

.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-form .full { grid-column: 1 / -1; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.form-actions .btn-danger-ghost { margin-right: auto; }

.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.history li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.history li:last-child { border-bottom: 0; }
.history .delta { font-weight: 800; min-width: 56px; font-variant-numeric: tabular-nums; }
.history .delta.pos { color: var(--success); }
.history .delta.neg { color: var(--danger); }
.history .h-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.history .h-note { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history .h-date { font-size: 12px; color: var(--muted); }
.history .h-bal { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.history .none { color: var(--muted); justify-content: center; }

.modal { display: grid; place-items: center; padding: 16px; }
.modal-box {
  position: relative; width: min(100%, 520px); max-height: calc(100svh - 32px); overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 24px;
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, .4); animation: pop .2s cubic-bezier(.2, .8, .2, 1) both;
}
.modal-box h2 { margin: 0 0 8px; font-size: 20px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; margin: 12px 0 20px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 100; background: var(--primary); color: var(--primary-text);
  padding: 12px 18px; border-radius: 12px; font-weight: 600; box-shadow: var(--shadow);
  max-width: calc(100vw - 32px); animation: pop .2s ease both;
}
.toast.error { background: var(--danger); color: #fff; }

@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateX(40px); opacity: 0; } }
@keyframes pop { from { transform: scale(.96); opacity: 0; } }
@keyframes bump { 50% { transform: scale(1.08); } }

/* ---------- Responsive ---------- */

@media (max-width: 640px) {
  .hide-sm { display: none; }
  .topbar-actions .btn { padding: 0 10px; }
  .stats { gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat-value { font-size: 20px; }
  .stat-label { font-size: 10px; }
  .toolbar { flex-wrap: wrap; }
  .search { flex-basis: 100%; }
  .toolbar select { flex: 1; }
  .members { grid-template-columns: 1fr; }
  .drawer-panel { width: 100%; border-left: 0; }
  .drawer-body { padding: 12px 12px 40px; }
  .grid-form { grid-template-columns: 1fr; }
  .quick { grid-template-columns: repeat(3, 1fr); }
  .points-form { grid-template-columns: 1fr; }
  .modal { place-items: end center; padding: 0; }
  .modal-box { border-radius: 20px 20px 0 0; width: 100%; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dd { margin-bottom: 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
