/* Mobile-first admin styles. No external dependencies.
   Goals: readable on phones (>=16px text, big tap targets), tables that
   collapse to cards on small screens, light/dark aware with a manual toggle.

   Theme resolution:
   - default     -> light values in :root
   - system dark -> dark values, UNLESS the user forced light (data-theme=light)
   - data-theme="dark"/"light" (set by the toggle) always wins. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1b1f24;
  --muted: #6b7280;
  --border: #e2e5ea;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --ok-bg: #e7f6ed;
  --ok-text: #14633a;
  --err-bg: #fdeaea;
  --err-text: #9b1c1c;
  --badge-bg: #e8eefc;
  --badge-text: #1e40af;
  --radius: 12px;
}

/* dark values, shared by system-dark (unless forced light) and explicit dark */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #181c22;
    --text: #e8eaed;
    --muted: #9aa3ad;
    --border: #2a2f37;
    --primary: #3b82f6;
    --badge-bg: #1e293b;
    --badge-text: #93c5fd;
    --ok-bg: #102a1c;
    --ok-text: #6ee7a8;
    --err-bg: #2a1414;
    --err-text: #f7a8a8;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1216;
  --surface: #181c22;
  --text: #e8eaed;
  --muted: #9aa3ad;
  --border: #2a2f37;
  --primary: #3b82f6;
  --badge-bg: #1e293b;
  --badge-text: #93c5fd;
  --ok-bg: #102a1c;
  --ok-text: #6ee7a8;
  --err-bg: #2a1414;
  --err-text: #f7a8a8;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.5rem; margin: 0 0 0.75rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 0.5rem; }
h3 { font-size: 1rem; margin: 1rem 0 0.4rem; }

.muted { color: var(--muted); font-size: 0.92rem; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; font-size: 1.05rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.navlinks {
  display: flex;
  align-items: center;
  gap: 0.25rem 1rem;
  flex-wrap: wrap;
  margin-left: auto;
}
.navlinks a {
  padding: 0.4rem 0.25rem;
  color: var(--text);
}
.navlinks a.active { color: var(--primary); font-weight: 700; }
.inline-form { display: inline; margin: 0; }

/* ---------- layout ---------- */
.container {
  max-width: 920px;
  margin: 0 auto;
  padding: 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.grid { display: grid; gap: 1rem; }
.grid.two { grid-template-columns: 1fr; }
.grid.stats { grid-template-columns: repeat(2, 1fr); }

.auth-card {
  max-width: 380px;
  margin: 2rem auto;
}

/* ---------- stat cards ---------- */
.stat { text-align: center; padding: 1rem 0.5rem; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.85rem; margin-top: 0.25rem; }

/* ---------- forms ---------- */
.stack { display: flex; flex-direction: column; gap: 0.85rem; }
label { display: block; font-weight: 600; font-size: 0.95rem; }
input, select, textarea {
  width: 100%;
  font-size: 16px; /* prevents iOS zoom on focus */
  padding: 0.65rem 0.7rem;
  margin-top: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  min-height: 44px;
}
textarea { min-height: 96px; resize: vertical; line-height: 1.4; }
label .muted { font-weight: 400; }
.field-row { display: flex; gap: 0.5rem; align-items: flex-end; flex-wrap: wrap; }

.searchbar { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.searchbar input { margin-top: 0; }

.checkbox { display: flex; align-items: center; gap: 0.5rem; font-weight: 600; }
.checkbox input { width: auto; min-height: 0; margin: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.6rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
}
.btn:hover { text-decoration: none; filter: brightness(0.98); }
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--primary); padding: 0.4rem 0.5rem; }
.btn-sm { min-height: 36px; padding: 0.35rem 0.7rem; font-size: 0.9rem; }

/* ---------- alerts ---------- */
.alert { padding: 0.75rem 1rem; border-radius: 10px; margin-bottom: 1rem; font-size: 0.95rem; }
.alert-ok { background: var(--ok-bg); color: var(--ok-text); }
.alert-error { background: var(--err-bg); color: var(--err-text); }

/* ---------- badges ---------- */
.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  background: var(--badge-bg);
  color: var(--badge-text);
}
.badge-danger { background: var(--err-bg); color: var(--err-text); }
.badge-muted { background: var(--border); color: var(--muted); }

/* ---------- key/value ---------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 0.35rem 1rem; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; word-break: break-word; }

/* ---------- pager ---------- */
.pager { display: flex; align-items: center; gap: 1rem; margin-top: 1rem; }

/* ---------- code / mono ---------- */
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9rem; }
.cred { background: var(--bg); border: 1px dashed var(--border); border-radius: 8px; padding: 0.5rem 0.7rem; word-break: break-all; }

/* ---------- responsive table ---------- */
.rtable { width: 100%; border-collapse: collapse; }
.rtable thead { display: none; }
.rtable tr {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.6rem;
}
.rtable td {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.3rem 0;
  border: none;
  text-align: right;
}
.rtable td::before {
  content: attr(data-label);
  font-weight: 600;
  color: var(--muted);
  text-align: left;
}

/* ---------- multi-select checkbox dropdown ----------
   A <details> that toggles a checkbox list on a plain click — works the same on
   desktop and mobile (unlike a native <select multiple>, which needs Ctrl-click
   on desktop). The menu floats over content so it doesn't shove the layout. */
.msel-row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.msel { position: relative; }
.msel > summary {
  cursor: pointer;
  list-style: none;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
  font-size: 0.95rem;
}
.msel > summary::-webkit-details-marker { display: none; }
.msel > summary::after { content: "▾"; color: var(--muted); margin-left: 0.4rem; }
.msel[open] > summary { border-color: var(--primary); }
.msel-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 30;
  min-width: 100%;
  max-height: 260px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.25rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
}
.msel-opt {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  font-weight: 400;
  white-space: nowrap;
  border-radius: 6px;
}
.msel-opt:hover { background: var(--bg); }
.msel-opt input { width: auto; min-height: 0; margin: 0; }

/* ---------- sortable headers ---------- */
.rtable th a { color: var(--muted); white-space: nowrap; }
.rtable th a:hover { color: var(--text); text-decoration: none; }

/* ---------- flat tables (sortable lists: stay tabular + scroll on mobile) ----------
   Unlike the default .rtable (which collapses to cards on phones), .rtable.flat
   stays a real table so the column headers remain visible and tappable for
   sorting. The wrapper scrolls horizontally on narrow screens. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
@media (max-width: 719px) {
  .rtable.flat { display: table; width: max-content; min-width: 100%; }
  .rtable.flat thead { display: table-header-group; }
  .rtable.flat tr {
    display: table-row;
    background: transparent;
    border: none;
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    padding: 0;
    margin: 0;
  }
  .rtable.flat th, .rtable.flat td {
    display: table-cell;
    text-align: left;
    padding: 0.5rem 0.6rem;
    white-space: nowrap;
  }
  .rtable.flat th { color: var(--muted); font-size: 0.85rem; border-bottom: 2px solid var(--border); }
  .rtable.flat td::before { content: none; }
}

/* ---------- theme toggle ---------- */
.theme-toggle {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
  z-index: 50;
}
.theme-toggle:hover { filter: brightness(0.97); }

/* ---------- wider screens ---------- */
@media (min-width: 720px) {
  .grid.two { grid-template-columns: 1fr 1fr; }
  .grid.stats { grid-template-columns: repeat(5, 1fr); }

  .rtable thead { display: table-header-group; }
  .rtable tr {
    display: table-row;
    border: none;
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    padding: 0;
    margin: 0;
    background: transparent;
  }
  .rtable th, .rtable td {
    display: table-cell;
    text-align: left;
    padding: 0.6rem 0.75rem;
    vertical-align: top;
  }
  .rtable th { color: var(--muted); font-size: 0.85rem; border-bottom: 2px solid var(--border); }
  .rtable td::before { content: none; }
}

/* ---------- desktop: left sidebar + wide content (landscape) ----------
   At this width the SAME top bar becomes a fixed-width vertical sidebar and the
   content area widens out so lines breathe. Phones/tablets keep the top bar. */
@media (min-width: 1024px) {
  .shell { display: flex; align-items: flex-start; min-height: 100vh; }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    flex-wrap: nowrap;
    gap: 0.15rem;
    width: 240px;
    flex: 0 0 240px;
    height: 100vh;
    overflow-y: auto;
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 1rem 0.75rem;
  }
  .topbar .brand {
    font-size: 1.2rem;
    padding: 0.25rem 0.5rem 1rem;
  }
  .navlinks {
    flex-direction: column;
    align-items: stretch;
    flex-wrap: nowrap;
    gap: 0.1rem;
    margin-left: 0;
    width: 100%;
  }
  .navlinks a {
    padding: 0.55rem 0.7rem;
    border-radius: 8px;
  }
  .navlinks a:hover { background: var(--bg); text-decoration: none; }
  .navlinks a.active { background: var(--badge-bg); color: var(--badge-text); }
  .navlinks .inline-form { display: block; margin-top: 0.75rem; }
  .navlinks .inline-form .btn { width: 100%; justify-content: flex-start; }

  .container {
    flex: 1;
    max-width: 1280px;
    margin: 0;
    padding: 1.75rem 2.5rem;
  }

  /* More room → denser, multi-column cards on the big forms. */
  .grid.three { grid-template-columns: repeat(3, 1fr); }
}
