:root {
  --bg: #050505;
  --bg-elevated: #0c0c0c;
  --bg-card: #111111;
  --bg-hover: #161616;
  --border: #1c1c1c;
  --border-strong: #2a2a2a;
  --text: #f2f2f2;
  --text-muted: #8a8a8a;
  --text-dim: #5c5c5c;
  --accent: #ffffff;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #22c55e;
  --sidebar-w: 220px;
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --mobile-nav-h: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
  -webkit-appearance: none;
  appearance: none;
}

a { color: inherit; text-decoration: none; }

.hidden { display: none !important; }

/* ── Login ─────────────────────────────────────────── */
#login-view {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(255,255,255,0.06), transparent 60%),
    var(--bg);
  padding: max(24px, var(--safe-top)) max(20px, var(--safe-right)) max(24px, var(--safe-bottom)) max(20px, var(--safe-left));
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px 28px 28px;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.02), 0 24px 80px rgba(0,0,0,0.55);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
}

.login-logo {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(145deg, #2a2a2a, #0a0a0a);
  border: 1px solid var(--border-strong);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.04em;
}

.brand-logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
  flex-shrink: 0;
  display: block;
  border-radius: 8px;
}

.brand-logo-sm {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.login-brand h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.login-brand p {
  color: var(--text-muted);
  font-size: 13px;
  margin-top: 2px;
}

.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 12px;
  color: var(--text);
  margin-bottom: 8px;
  font-weight: 500;
}

.field input, .field select, .toolbar input, .toolbar select {
  width: 100%;
  background: #0a0a0a;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 12px 14px;
  outline: none;
  transition: border-color .15s ease;
  font-size: 16px;
}

.field input:focus, .toolbar input:focus, .field select:focus {
  border-color: #555;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  transition: background .15s ease, border-color .15s ease, opacity .15s ease;
  min-height: 40px;
  touch-action: manipulation;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #000;
  width: 100%;
  padding: 14px;
  margin-top: 8px;
  font-size: 15px;
  min-height: 48px;
}

.btn-primary:hover:not(:disabled) { background: #e8e8e8; }

.btn-ghost {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) { background: var(--bg-hover); }

.btn-danger {
  background: transparent;
  border-color: #3f1d1d;
  color: #fca5a5;
}

.btn-danger:hover:not(:disabled) { background: #1a0b0b; }

.btn-sm { padding: 8px 12px; font-size: 12px; font-weight: 500; min-height: 36px; }

.login-error {
  color: #fca5a5;
  font-size: 12px;
  margin-top: 12px;
  min-height: 18px;
}

.login-discord-note {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 16px;
  line-height: 1.45;
}

#discord-login-btn {
  display: flex;
  text-decoration: none;
}

.oauth-hint {
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0a0a0a;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.45;
  text-align: left;
}

.oauth-hint code {
  display: block;
  margin: 8px 0;
  padding: 8px;
  background: #111;
  border-radius: 6px;
  color: #e8e8e8;
  word-break: break-all;
  font-size: 11px;
}

.oauth-hint .oauth-app code {
  display: inline;
  padding: 2px 6px;
  margin: 0;
}

/* ── Shell ─────────────────────────────────────────── */
.mobile-header { display: none; }

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

.sidebar {
  border-right: 1px solid var(--border);
  background: #070707;
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
}

.side-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 22px;
}

.side-brand span {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--text-muted);
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  touch-action: manipulation;
}

.nav-item svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: .85;
}

.nav-item:hover { background: var(--bg-hover); color: var(--text); }

.nav-item.active {
  background: #151515;
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--border);
}

.main {
  min-width: 0;
  padding: 28px 32px 48px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.topbar h2 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.topbar .sub {
  color: var(--text-muted);
  margin-top: 4px;
  font-size: 13px;
}

.top-user {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  background: #1a1a1a;
  border: 1px solid var(--border-strong);
  color: var(--text-muted);
  font-size: 11px;
  text-transform: lowercase;
}

/* ── Stats ─────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  min-height: 96px;
}

.stat-card .label {
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
  text-transform: uppercase;
}

.stat-card .value {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin-top: 8px;
  line-height: 1;
}

.stat-card .meta {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 12px;
}

.panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.panel-head h3 {
  font-size: 14px;
  font-weight: 600;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.toolbar input, .toolbar select {
  width: auto;
  min-width: 180px;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  text-align: left;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  vertical-align: middle;
}

th {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  font-weight: 600;
}

td { color: var(--text); font-size: 13px; }

tr:last-child td { border-bottom: none; }

tbody tr:hover td { background: rgba(255,255,255,0.015); }

.mono { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 12px; }

.muted { color: var(--text-muted); }

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

.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  border: 1px solid var(--border-strong);
  color: var(--text-muted);
}

.pill.ok { color: #86efac; border-color: #14532d; background: #052e16; }
.pill.bad { color: #fca5a5; border-color: #7f1d1d; background: #1a0b0b; }
.pill.warn { color: #fcd34d; border-color: #78350f; background: #1a1205; }

.hb-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px 22px;
  padding: 14px 18px;
  margin-bottom: 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.hb-bar .hb-title {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  font-weight: 600;
}

.hb-bar .hb-count {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  min-width: 3.2ch;
}

.hb-bar .hb-meta { color: var(--text-muted); font-size: 12px; }

.hb-bar .hb-track {
  flex: 1;
  min-width: 140px;
  height: 6px;
  background: #1a1a1a;
  border-radius: 999px;
  overflow: hidden;
}

.hb-bar .hb-fill {
  height: 100%;
  width: 0%;
  background: #e8e8e8;
  border-radius: 999px;
  transition: width .2s linear;
}

.hb-bar.late .hb-count { color: #fca5a5; }
.hb-bar.late .hb-fill { background: #ef4444; }

.hb-cell {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  min-width: 3.2ch;
  display: inline-block;
}

.hb-cell.late { color: #fca5a5; }
.hb-cell.ok { color: #86efac; }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.stub-note {
  padding: 28px 20px;
  color: var(--text-muted);
  line-height: 1.55;
  max-width: 560px;
}

/* ── Modal ─────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: max(16px, var(--safe-top)) max(16px, var(--safe-right)) max(16px, var(--safe-bottom)) max(16px, var(--safe-left));
}

.modal {
  width: 100%;
  max-width: 420px;
  max-height: min(90dvh, 720px);
  overflow: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 22px;
  -webkit-overflow-scrolling: touch;
}

.modal h3 { font-size: 16px; margin-bottom: 4px; }
.modal .sub { color: var(--text-muted); margin-bottom: 18px; font-size: 13px; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
  flex-wrap: wrap;
}

.toast {
  position: fixed;
  right: max(16px, var(--safe-right));
  bottom: max(20px, calc(var(--safe-bottom) + 12px));
  background: #151515;
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 10px;
  z-index: 60;
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
  max-width: min(320px, calc(100vw - 32px));
  font-size: 13px;
}

@media (max-width: 1100px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ── iPhone / small screens ────────────────────────── */
@media (max-width: 860px) {
  #app-view {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    padding-bottom: calc(var(--mobile-nav-h) + var(--safe-bottom) + 8px);
  }

  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 30;
    padding: calc(10px + var(--safe-top)) max(14px, var(--safe-right)) 10px max(14px, var(--safe-left));
    background: rgba(7, 7, 7, 0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
  }

  .mobile-header-brand {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
    font-size: 16px;
    letter-spacing: -0.02em;
  }

  .mobile-header-user {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    height: auto;
    z-index: 40;
    border-right: none;
    border-top: 1px solid var(--border);
    background: rgba(7, 7, 7, 0.96);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    padding: 6px max(6px, var(--safe-right)) calc(6px + var(--safe-bottom)) max(6px, var(--safe-left));
  }

  .side-brand { display: none; }

  .nav {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 2px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav::-webkit-scrollbar { display: none; }

  .nav-item {
    flex: 0 0 auto;
    width: auto;
    min-width: 64px;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    padding: 8px 10px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.01em;
  }

  .nav-item svg {
    width: 20px;
    height: 20px;
  }

  .nav-item.active {
    background: #1a1a1a;
    box-shadow: none;
  }

  .main {
    padding: 16px max(14px, var(--safe-right)) 24px max(14px, var(--safe-left));
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin-bottom: 18px;
  }

  .topbar h2 { font-size: 24px; }

  .top-user { display: none; }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .stat-card {
    padding: 14px;
    min-height: 88px;
  }

  .stat-card .value { font-size: 24px; }

  .hb-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .hb-bar .hb-track { min-width: 0; width: 100%; }

  .panel-head { padding: 14px; }

  .toolbar { width: 100%; }

  .toolbar input, .toolbar select {
    min-width: 0;
    flex: 1 1 140px;
    font-size: 16px;
  }

  .row-actions { max-width: 220px; }

  th, td { padding: 10px 12px; }

  .modal-actions { flex-direction: column-reverse; }

  .modal-actions .btn {
    width: 100%;
    min-height: 44px;
  }

  .toast {
    left: max(16px, var(--safe-left));
    right: max(16px, var(--safe-right));
    bottom: calc(var(--mobile-nav-h) + var(--safe-bottom) + 12px);
    max-width: none;
    text-align: center;
  }

  #login-view {
    align-items: flex-start;
    padding-top: max(48px, calc(var(--safe-top) + 32px));
  }

  .login-card {
    padding: 28px 20px 24px;
    border-radius: 16px;
  }
}

@media (max-width: 420px) {
  .nav-item { min-width: 56px; padding: 8px 6px; }
  .stat-card .meta { font-size: 11px; }
}
