/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}
img, svg, video { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
ul, ol { list-style: none; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- App layout ---------- */
#root { min-height: 100dvh; display: flex; flex-direction: column; }
.app-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + var(--kb) + 16px);
}
.container { max-width: var(--content-max); margin: 0 auto; width: 100%; }
.container-wide { max-width: 1080px; margin: 0 auto; width: 100%; }

.boot-splash { display: flex; align-items: center; justify-content: center; min-height: 60dvh; }

/* ---------- Typography ---------- */
h1 { font-size: 22px; font-weight: 800; }
h2 { font-size: 18px; font-weight: 700; }
h3 { font-size: 15px; font-weight: 700; }
.text-secondary { color: var(--text-secondary); }
.text-body { color: var(--text-body); }
.text-muted { color: var(--text-muted); }
.text-brand { color: var(--brand); }
.text-success { color: var(--success); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.text-center { text-align: center; }

/* ---------- Buttons (pill) ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: var(--radius-pill);
  padding: 12px 22px; font-size: 15px; font-weight: 700;
  cursor: pointer; user-select: none; white-space: nowrap;
  background: var(--surface-alt); color: var(--text-body);
  transition: filter .12s, opacity .12s, background .12s;
}
.btn:active { filter: brightness(1.2); }
.btn:disabled { opacity: .45; cursor: default; pointer-events: none; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:active { background: var(--brand-press); }
.btn-outline { background: transparent; border-color: var(--border-alt); color: var(--text-body); }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-danger { background: transparent; border-color: var(--brand); color: var(--brand); }
.btn-danger-solid { background: var(--danger); color: #fff; }
.btn-danger-solid:active { background: var(--brand-press); }
.btn-sm { padding: 8px 16px; font-size: 13px; }
.btn-block { display: flex; width: 100%; }

/* ---------- Chips (selectable pills) ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-alt);
  background: var(--surface-alt); color: var(--text-body);
  padding: 8px 16px; font-size: 13px; font-weight: 600;
  cursor: pointer; user-select: none;
  transition: background .12s, border-color .12s, color .12s;
}
.chip.selected { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip:disabled { opacity: .4; cursor: default; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Segmented control ---------- */
.seg { display: flex; gap: 6px; background: var(--surface-alt); border: 1px solid var(--border-alt);
       border-radius: var(--radius-pill); padding: 4px; }
.seg > button {
  flex: 1; border: 0; background: transparent; color: var(--text-secondary);
  border-radius: var(--radius-pill); padding: 9px 8px; font-size: 13px; font-weight: 700;
  cursor: pointer; transition: background .12s, color .12s;
}
.seg > button.selected { background: var(--brand); color: #fff; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 16px;
}
.card + .card { margin-top: 12px; }
.card-tap { cursor: pointer; transition: border-color .12s; }
.card-tap:active { border-color: var(--text-muted); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--surface-chip); color: var(--text-body);
  border-radius: 4px; padding: 2px 7px;
  font-size: 10px; font-weight: 600;
}
.badge-pill { border-radius: var(--radius-pill); padding: 3px 10px; font-size: 11px; }
.badge-success { color: var(--success); }
.badge-brand   { color: var(--brand); }
.badge-muted   { color: var(--text-muted); }

/* ---------- Forms ---------- */
.field { margin-bottom: 16px; }
.field > label { display: block; font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.input, textarea.input, select.input {
  width: 100%;
  background: var(--surface-alt);
  border: 1px solid var(--border-alt);
  border-radius: var(--radius-input);
  color: var(--text);
  padding: 12px 14px;
  font-size: 15px;
}
.input::placeholder { color: var(--text-muted); }
.input:focus { border-color: var(--brand); outline: none; }
.input[aria-invalid="true"] { border-color: var(--danger); }
.field-error { color: var(--danger); font-size: 12px; margin-top: 5px; }
textarea.input { resize: vertical; min-height: 96px; }

/* ---------- Toggle switch ---------- */
.toggle { position: relative; display: inline-block; width: 46px; height: 26px; flex: none; }
.toggle input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.toggle .knob {
  position: absolute; inset: 0; border-radius: var(--radius-pill);
  background: var(--surface-chip); border: 1px solid var(--border-alt);
  transition: background .15s; pointer-events: none;
}
.toggle .knob::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: var(--text-secondary); transition: transform .15s, background .15s;
}
.toggle input:checked + .knob { background: var(--brand); border-color: var(--brand); }
.toggle input:checked + .knob::after { transform: translateX(20px); background: #fff; }

/* ---------- Bottom tab bar (mobile) ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; font-size: 10px; font-weight: 600; color: var(--text-muted);
}
.tabbar a svg { width: 22px; height: 22px; }
.tabbar a.active { color: var(--brand); }

/* ---------- Top nav (desktop) ---------- */
.topnav {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 24px;
  height: var(--topnav-h);
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topnav .brand { font-size: 17px; font-weight: 800; color: var(--brand); margin-right: 8px; }
.topnav a.navlink {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--text-secondary); font-size: 14px; font-weight: 600;
  padding: 6px 12px; border-radius: var(--radius-pill);
}
.topnav a.navlink svg { width: 18px; height: 18px; }
.topnav a.navlink.active { color: #fff; background: var(--brand); }
.topnav .spacer { flex: 1; }

@media (max-width: 767.98px) {
  .topnav { display: none !important; }
}
@media (min-width: 768px) {
  .tabbar { display: none !important; }
  .app-scroll { padding: 24px 24px calc(var(--kb) + 24px); }
  body { font-size: 15px; }
}

/* ---------- Grid helpers ---------- */
.grid { display: grid; gap: 12px; }
@media (min-width: 768px)  { .grid-desktop-2 { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1024px) { .grid-desktop-3 { grid-template-columns: 1fr 1fr 1fr; } }
.row { display: flex; align-items: center; gap: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.col { display: flex; flex-direction: column; gap: 4px; }
.mt-8 { margin-top: 8px; } .mt-12 { margin-top: 12px; } .mt-16 { margin-top: 16px; } .mt-24 { margin-top: 24px; }
.mb-8 { margin-bottom: 8px; } .mb-12 { margin-bottom: 12px; } .mb-16 { margin-bottom: 16px; } .mb-24 { margin-bottom: 24px; }

/* ---------- States: skeleton / empty / error ---------- */
@keyframes skeleton-pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }
.skeleton-line {
  height: 14px; border-radius: 7px; background: var(--surface-alt);
  animation: skeleton-pulse 1.4s ease-in-out infinite; margin-bottom: 10px;
}
.skeleton-card { border-radius: var(--radius-card); background: var(--surface); border: 1px solid var(--border);
  padding: 16px; margin-bottom: 12px; }
.state-box { display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 8px; padding: 48px 24px; color: var(--text-secondary); }
.state-box .state-icon { font-size: 36px; line-height: 1; }
.state-box .state-title { color: var(--text-body); font-size: 16px; font-weight: 700; }
.state-box .state-msg { font-size: 13px; max-width: 320px; }

/* ---------- Spinner ---------- */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--border-alt); border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
.spinner-sm { width: 16px; height: 16px; border-width: 2px; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; left: 0; right: 0;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + var(--kb) + 12px);
  z-index: 60; display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none; padding: 0 16px;
}
@media (min-width: 768px) { .toasts { bottom: 24px; } }
.toast {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-card); padding: 11px 18px;
  color: var(--text-body); font-size: 14px; font-weight: 600;
  max-width: 480px; box-shadow: 0 6px 24px rgba(0,0,0,.5);
  animation: toast-in .18s ease-out;
}
.toast-error { border-color: var(--brand); color: #fff; }
.toast-success { border-color: var(--success); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; padding: 24px;
  animation: toast-in .15s ease-out;
}
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-card); width: 100%; max-width: 400px;
  max-height: calc(100dvh - 48px - var(--kb)); overflow-y: auto;
  padding: 20px;
}
.modal h2 { margin-bottom: 10px; }
.modal .modal-body { color: var(--text-body); font-size: 14px; }
.modal .modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* ---------- Misc ---------- */
.divider { height: 1px; background: var(--border); margin: 16px 0; }
.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex: none;
  background: var(--surface-chip); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700; overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
