/* ==========================================================================
   Preview Portal — design system
   One stylesheet, served from /app.css and cached. Previously every view
   carried its own duplicated <style> block, which made consistent changes
   impossible. Tokens first, then primitives, then components.
   ========================================================================== */

:root {
  /* Neutrals — slate ramp */
  --bg:            #0b1120;
  --bg-elev:       #111a2e;
  --bg-elev-2:     #16203a;
  --bg-inset:      #060b16;
  --border:        #1f2b45;
  --border-strong: #2c3a58;

  --text:          #e8edf7;
  --text-muted:    #93a1bd;
  --text-faint:    #64748b;

  /* Accent + semantics */
  --accent:        #6366f1;
  --accent-hover:  #7c7ff5;
  --accent-soft:   rgba(99, 102, 241, .14);
  --accent-ring:   rgba(99, 102, 241, .45);

  --ok:            #34d399;
  --ok-soft:       rgba(52, 211, 153, .13);
  --warn:          #fbbf24;
  --warn-soft:     rgba(251, 191, 36, .13);
  --danger:        #f87171;
  --danger-soft:   rgba(248, 113, 113, .13);

  /* Geometry */
  --r-sm: 6px;
  --r:    10px;
  --r-lg: 14px;
  --r-pill: 999px;

  --sp-1: .25rem;  --sp-2: .5rem;   --sp-3: .75rem;
  --sp-4: 1rem;    --sp-5: 1.5rem;  --sp-6: 2rem;   --sp-7: 3rem;

  --shadow:    0 1px 2px rgba(0,0,0,.30), 0 4px 16px rgba(0,0,0,.22);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.45);

  --sidebar-w: 232px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Light theme. Dark is the default above; this only remaps the tokens, so
   every component below works in both without duplicated rules. */
@media (prefers-color-scheme: light) {
  :root {
    --bg:            #f6f8fc;
    --bg-elev:       #ffffff;
    --bg-elev-2:     #f1f5f9;
    --bg-inset:      #0f172a;   /* log panes stay dark in light mode on purpose */
    --border:        #e2e8f0;
    --border-strong: #cbd5e1;

    --text:          #0f172a;
    --text-muted:    #55637a;
    --text-faint:    #8593a8;

    --accent-soft:   rgba(99, 102, 241, .10);
    --ok:            #059669;
    --ok-soft:       rgba(5, 150, 105, .11);
    --warn:          #b45309;
    --warn-soft:     rgba(180, 83, 9, .11);
    --danger:        #dc2626;
    --danger-soft:   rgba(220, 38, 38, .10);

    --shadow:    0 1px 2px rgba(15,23,42,.06), 0 4px 14px rgba(15,23,42,.06);
    --shadow-lg: 0 10px 40px rgba(15,23,42,.13);
  }
}

/* --------------------------------------------------------------------------
   Base
   -------------------------------------------------------------------------- */

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

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

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.0625rem; }
h3 { font-size: .9375rem; }

p { margin: 0; }

code, kbd, pre { font-family: var(--mono); }

code {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  padding: .1em .4em;
  border-radius: var(--r-sm);
  font-size: .84em;
  word-break: break-all;
}

:focus-visible {
  outline: 2px solid var(--accent-ring);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

.muted  { color: var(--text-muted); }
.faint  { color: var(--text-faint); font-size: .82rem; }
.mono   { font-family: var(--mono); font-size: .84em; }
.nowrap { white-space: nowrap; }
.stack  { display: flex; flex-direction: column; gap: var(--sp-4); }
.row    { display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); flex-wrap: wrap; }
.grow   { flex: 1 1 auto; min-width: 0; }

/* --------------------------------------------------------------------------
   App shell — fixed sidebar, scrolling content
   -------------------------------------------------------------------------- */

.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  padding: var(--sp-5) var(--sp-4);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: var(--sp-3); font-weight: 700; letter-spacing: -.02em; }
.brand .logo {
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: linear-gradient(140deg, var(--accent), #8b5cf6);
  color: #fff;
  flex: none;
}
.brand .logo svg { width: 17px; height: 17px; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-label {
  font-size: .68rem; font-weight: 700; letter-spacing: .09em;
  text-transform: uppercase; color: var(--text-faint);
  padding: 0 var(--sp-3); margin-bottom: var(--sp-2);
}
.nav-item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r);
  color: var(--text-muted);
  font-weight: 550;
  transition: background .13s ease, color .13s ease;
}
.nav-item:hover { background: var(--bg-elev-2); color: var(--text); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item svg { width: 16px; height: 16px; flex: none; }

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: var(--sp-3); }
.who {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3); border-radius: var(--r);
  background: var(--bg-elev-2); border: 1px solid var(--border);
}
.avatar {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  border-radius: var(--r-pill);
  background: var(--accent-soft); color: var(--accent);
  font-weight: 700; font-size: .78rem; text-transform: uppercase;
}
.who-meta { min-width: 0; }
.who-email {
  font-weight: 550; font-size: .8rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.content { min-width: 0; padding: var(--sp-6) var(--sp-6) var(--sp-7); }
.content-head { margin-bottom: var(--sp-5); }
.content-head .sub { color: var(--text-muted); margin-top: var(--sp-1); }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto;
    flex-direction: row; align-items: center; flex-wrap: wrap;
    gap: var(--sp-3); border-right: 0; border-bottom: 1px solid var(--border);
  }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .nav-label { display: none; }
  .sidebar-foot { margin-top: 0; margin-left: auto; flex-direction: row; align-items: center; }
  .who { background: none; border: 0; padding: 0; }
  .content { padding: var(--sp-5) var(--sp-4) var(--sp-6); }
}

/* --------------------------------------------------------------------------
   Cards
   -------------------------------------------------------------------------- */

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}
.card + .card { margin-top: var(--sp-5); }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--sp-3); flex-wrap: wrap;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border);
}
.card-body { padding: var(--sp-5); }
.card-body.tight { padding: 0; }

/* Metric tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--sp-4); margin-bottom: var(--sp-5); }
.tile {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--r-lg); padding: var(--sp-4) var(--sp-5);
}
.tile .k { font-size: .72rem; font-weight: 650; letter-spacing: .07em; text-transform: uppercase; color: var(--text-faint); }
.tile .v { font-size: 1.65rem; font-weight: 680; letter-spacing: -.02em; margin-top: var(--sp-1); }
.tile .v.ok { color: var(--ok); }
.tile .v.danger { color: var(--danger); }

/* --------------------------------------------------------------------------
   Tables
   -------------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left; font-size: .7rem; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase; color: var(--text-faint);
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: var(--sp-3) var(--sp-5);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background .12s ease; }
tbody tr:hover { background: var(--bg-elev-2); }
td.shrink { width: 1%; white-space: nowrap; }

.link-strong { color: var(--text); font-weight: 600; }
.link-strong:hover { color: var(--accent); text-decoration: none; }

/* --------------------------------------------------------------------------
   Status pills
   -------------------------------------------------------------------------- */

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px 3px 7px;
  border-radius: var(--r-pill);
  font-size: .74rem; font-weight: 650;
  border: 1px solid transparent;
  white-space: nowrap;
}
.pill .dot { width: 6px; height: 6px; border-radius: var(--r-pill); background: currentColor; flex: none; }
.pill.ok      { color: var(--ok);       background: var(--ok-soft);     border-color: var(--ok-soft); }
.pill.danger  { color: var(--danger);   background: var(--danger-soft); border-color: var(--danger-soft); }
.pill.warn    { color: var(--warn);     background: var(--warn-soft);   border-color: var(--warn-soft); }
.pill.neutral { color: var(--text-muted); background: var(--bg-elev-2); border-color: var(--border); }
.pill.live .dot { animation: pulse 2s ease-in-out infinite; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .pill.live .dot { animation: none; } }

/* --------------------------------------------------------------------------
   Buttons + forms
   -------------------------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: .48rem .85rem;
  border: 1px solid transparent;
  border-radius: var(--r);
  background: var(--accent); color: #fff;
  font: inherit; font-weight: 600; font-size: .85rem;
  cursor: pointer;
  transition: background .13s ease, border-color .13s ease, color .13s ease, opacity .13s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--accent-hover); text-decoration: none; }
.btn:active { transform: translateY(.5px); }
.btn svg { width: 15px; height: 15px; }

.btn.ghost { background: transparent; border-color: var(--border-strong); color: var(--text-muted); }
.btn.ghost:hover { background: var(--bg-elev-2); color: var(--text); }

.btn.danger { background: transparent; border-color: transparent; color: var(--text-faint); }
.btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

.btn.sm { padding: .3rem .6rem; font-size: .78rem; }
.btn.block { width: 100%; }

input, textarea, select {
  font: inherit; font-size: .875rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--r);
  padding: .5rem .7rem;
  transition: border-color .13s ease, box-shadow .13s ease;
  min-width: 0;
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
textarea { width: 100%; min-height: 5.5rem; resize: vertical; font-family: var(--mono); font-size: .8rem; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: .78rem; font-weight: 650; color: var(--text-muted); }
form.inline { display: inline; }

/* --------------------------------------------------------------------------
   Banners, empty states
   -------------------------------------------------------------------------- */

.banner {
  display: flex; align-items: flex-start; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r);
  border: 1px solid var(--danger-soft);
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 550;
  margin-bottom: var(--sp-4);
}
.banner svg { width: 17px; height: 17px; flex: none; margin-top: 1px; }

.empty { padding: var(--sp-7) var(--sp-5); text-align: center; color: var(--text-muted); }
.empty svg { width: 30px; height: 30px; color: var(--text-faint); margin-bottom: var(--sp-3); }
.empty .t { font-weight: 600; color: var(--text); margin-bottom: var(--sp-1); }

/* --------------------------------------------------------------------------
   Log panes
   -------------------------------------------------------------------------- */

.logbox {
  margin: 0;
  background: var(--bg-inset);
  color: #cbd5e1;
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: var(--sp-4);
  font-size: .78rem;
  line-height: 1.6;
  max-height: 30rem;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  tab-size: 2;
}
.logbox::-webkit-scrollbar { width: 10px; height: 10px; }
.logbox::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: var(--r-pill); border: 3px solid var(--bg-inset); }

details.drawer {
  border: 1px solid var(--border);
  border-radius: var(--r);
  background: var(--bg-elev-2);
  overflow: hidden;
}
details.drawer + details.drawer { margin-top: var(--sp-3); }
details.drawer > summary {
  cursor: pointer;
  padding: var(--sp-3) var(--sp-4);
  font-weight: 600; font-size: .82rem;
  color: var(--text-muted);
  list-style: none;
  display: flex; align-items: center; gap: var(--sp-2);
}
details.drawer > summary::-webkit-details-marker { display: none; }
details.drawer > summary::before {
  content: "▸"; color: var(--text-faint); transition: transform .15s ease; display: inline-block;
}
details.drawer[open] > summary::before { transform: rotate(90deg); }
details.drawer > summary:hover { color: var(--text); }
details.drawer .logbox { border: 0; border-top: 1px solid var(--border); border-radius: 0; max-height: 34rem; }

/* --------------------------------------------------------------------------
   Auth screen (login / error) — no sidebar
   -------------------------------------------------------------------------- */

.auth {
  min-height: 100vh;
  display: grid; place-items: center;
  padding: var(--sp-5);
  background:
    radial-gradient(900px 480px at 50% -12%, var(--accent-soft), transparent 70%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 384px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--sp-6);
}
.auth-card .brand { justify-content: center; margin-bottom: var(--sp-2); }
.auth-card h1 { text-align: center; font-size: 1.15rem; }
.auth-card .sub { text-align: center; color: var(--text-muted); margin-bottom: var(--sp-5); }
.auth-card form { display: flex; flex-direction: column; gap: var(--sp-4); }

/* --------------------------------------------------------------------------
   Toolbar / filter
   -------------------------------------------------------------------------- */

.toolbar { display: flex; gap: var(--sp-2); align-items: center; flex-wrap: wrap; }
.search { position: relative; flex: 1 1 210px; max-width: 320px; }
.search svg {
  position: absolute; left: 9px; top: 50%; transform: translateY(-50%);
  width: 15px; height: 15px; color: var(--text-faint); pointer-events: none;
}
.search input { width: 100%; padding-left: 30px; }

.seg { display: inline-flex; background: var(--bg-elev-2); border: 1px solid var(--border); border-radius: var(--r); padding: 2px; }
.seg button {
  border: 0; background: transparent; color: var(--text-muted);
  font: inherit; font-size: .8rem; font-weight: 600;
  padding: .28rem .65rem; border-radius: calc(var(--r) - 3px); cursor: pointer;
  transition: background .13s ease, color .13s ease;
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--bg-elev); color: var(--text); box-shadow: var(--shadow); }

tr[hidden] { display: none; }

/* --------------------------------------------------------------------------
   Guide (onboarding steps on the tunnels page)
   -------------------------------------------------------------------------- */

.guide ol { margin: var(--sp-3) 0 0; padding-left: 1.15rem; }
.guide li { margin-bottom: var(--sp-3); }
.guide li::marker { color: var(--accent); font-weight: 700; }
.guide code { display: inline-block; margin-top: 3px; }
