:root {
  --bg: #0b0f14;
  --bg-2: #0f151c;
  --panel: #131b24;
  --panel-2: #18222d;
  --border: #223040;
  --text: #e6edf3;
  --muted: #8aa0b2;
  --accent: #00c2d1;
  --accent-2: #3b82f6;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(1200px 800px at 80% -10%, #10202b 0%, var(--bg) 55%);
  color: var(--text);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

.app {
  display: grid;
  grid-template-columns: 264px 1fr;
  grid-template-rows: 1fr;
  height: 100vh;
  height: 100dvh;
}

/* Mobile chrome (hidden on desktop) */
.mobile-top, .mobile-tabs { display: none; }

/* Sidebar */
.sidebar {
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 20px 16px; gap: 24px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px;
  background: conic-gradient(from 200deg, var(--accent), var(--accent-2), var(--accent));
  box-shadow: 0 0 24px rgba(0, 194, 209, 0.45);
}
.brand-name { font-weight: 700; letter-spacing: 0.2px; font-size: 16px; }
.brand-sub { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 1.2px; }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  background: transparent; color: var(--muted); border: 0; cursor: pointer;
  padding: 11px 12px; border-radius: 10px; font-size: 14px; text-align: left;
  transition: background 0.15s, color 0.15s;
}
.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item.active { background: var(--panel-2); color: var(--text); box-shadow: inset 2px 0 0 var(--accent); }
.nav-ico { width: 18px; text-align: center; color: var(--accent); }

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 12px; }
.epigraph {
  margin: 0; padding: 12px 12px 12px 14px; border-left: 2px solid var(--accent);
  background: linear-gradient(90deg, rgba(0, 194, 209, 0.08), transparent);
  border-radius: 8px; color: var(--muted); font-size: 11.5px; font-style: italic; line-height: 1.5;
}
.epigraph cite { display: block; margin-top: 8px; font-style: normal; font-size: 10.5px; color: var(--accent); letter-spacing: 0.2px; }
.chat-hello { margin: auto; max-width: 460px; text-align: center; color: var(--muted); font-style: italic; line-height: 1.6; }
.health { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.health .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.health.ok .dot { background: var(--good); box-shadow: 0 0 10px var(--good); }
.health.down .dot { background: var(--bad); }
.docs-link { color: var(--muted); font-size: 12px; text-decoration: none; }
.docs-link:hover { color: var(--accent); }

/* Main */
.main { overflow-y: auto; padding: 28px 34px; }
.view { display: none; animation: fade 0.2s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.view-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; }
.view-head h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: -0.3px; }
.muted { color: var(--muted); margin: 0; }

/* Buttons / inputs */
.btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  padding: 9px 16px; border-radius: 10px; cursor: pointer; font-size: 13px; font-weight: 600;
  transition: transform 0.05s, border-color 0.15s, background 0.15s;
}
.btn:hover { border-color: #2f4458; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, var(--accent), #00a3b0); color: #04161a; border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn { background: transparent; border: 0; color: var(--muted); font-size: 16px; cursor: pointer; }
.icon-btn:hover { color: var(--text); }

input, select, textarea {
  width: 100%; background: var(--bg-2); border: 1px solid var(--border); color: var(--text);
  padding: 10px 12px; border-radius: 10px; font-size: 13px; font-family: inherit; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0, 194, 209, 0.15); }
textarea { resize: vertical; }
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
label input { margin-top: 6px; }

/* Stats */
.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 22px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.stat .v { font-size: 26px; font-weight: 700; letter-spacing: -0.5px; }
.stat .k { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.8px; margin-top: 2px; }

/* Grid of cards */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 18px; }
.persona-card { padding: 18px; cursor: pointer; transition: transform 0.1s, border-color 0.15s; }
.persona-card:hover { transform: translateY(-2px); border-color: #2f4458; }
.pc-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.avatar {
  width: 42px; height: 42px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-weight: 700; color: #04161a;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}
.pc-name { font-weight: 700; font-size: 15px; }
.pc-slug { color: var(--muted); font-size: 12px; }
.pc-metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 6px; }
.mini { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px; text-align: center; }
.mini .mv { font-weight: 700; }
.mini .mk { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; }

/* Badges */
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.badge { font-size: 11px; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); background: var(--bg-2); }
.badge.living { color: var(--good); border-color: rgba(52, 211, 153, 0.3); }
.badge.deceased { color: var(--muted); }
.badge.composite { color: var(--accent); border-color: rgba(0, 194, 209, 0.35); }
.badge.kind-feature { color: #c4b5fd; }
.badge.kind-rule { color: var(--bad); }
.badge.kind-skill { color: var(--accent); }
.badge.kind-playbook { color: var(--warn); }

/* Tables */
.table-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.table th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.7px; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--panel-2); }
.filters { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.filters select, .filters input { width: auto; min-width: 0; flex: 1 1 140px; }

/* Orchestrator */
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.card.pad h3 { margin: 0 0 12px; }
.card.pad textarea, .card.pad input { margin-bottom: 10px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: stretch; }
.row > * { flex: 1 1 140px; min-width: 0; }
.row .btn { flex: 0 0 auto; }
.results { margin-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.result-item {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.score-bar { height: 6px; border-radius: 4px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }
.score-track { flex: 1; height: 6px; background: var(--panel-2); border-radius: 4px; overflow: hidden; }

/* Empty */
.empty { color: var(--muted); text-align: center; padding: 60px; border: 1px dashed var(--border); border-radius: var(--radius); }

/* Modal */
.modal-backdrop, .drawer-backdrop { position: fixed; inset: 0; background: rgba(3, 7, 11, 0.6); backdrop-filter: blur(3px); display: grid; z-index: 50; }
.modal-backdrop { place-items: center; }
.modal { width: 460px; max-width: 92vw; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.modal-head, .modal-foot { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; }
.modal-head { border-bottom: 1px solid var(--border); }
.modal-foot { border-top: 1px solid var(--border); gap: 10px; justify-content: flex-end; }
.modal-body { padding: 20px; }
.modal-body h2 { margin: 0; }
.req { color: var(--bad); }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }

/* Drawer */
.drawer-backdrop { justify-items: end; }
.drawer { width: 620px; max-width: 96vw; height: 100%; background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; animation: slide 0.2s ease; }
@keyframes slide { from { transform: translateX(30px); opacity: 0.6; } to { transform: none; opacity: 1; } }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 20px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; }
.drawer-tabs { display: flex; gap: 4px; padding: 10px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tab { background: transparent; border: 0; color: var(--muted); padding: 8px 12px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600; }
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: var(--accent); background: var(--bg-2); }
.drawer-body { padding: 20px; overflow-y: auto; }
.detail-section { margin-bottom: 22px; }
.detail-section h4 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.7px; color: var(--muted); }
.kv { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.card-md { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 14px; white-space: pre-wrap; font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 12px; line-height: 1.55; max-height: 60vh; overflow: auto; }
.comp-list { display: flex; flex-direction: column; gap: 8px; }
.comp-item { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.comp-item .ct { font-weight: 600; }
.comp-item .cb { color: var(--muted); font-size: 12px; margin-top: 3px; }

/* Chat */
.chat { display: flex; flex-direction: column; height: calc(100vh - 170px); background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.chat-thread { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 78%; padding: 12px 14px; border-radius: 14px; line-height: 1.5; }
.msg.user { align-self: flex-end; background: linear-gradient(180deg, var(--accent-2), #2563eb); color: #fff; border-bottom-right-radius: 4px; }
.msg.agent { align-self: flex-start; background: var(--bg-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg .cites { margin-top: 8px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 6px; }
.chat-input { display: flex; gap: 10px; padding: 14px; border-top: 1px solid var(--border); }
.agent-select { width: 240px; }

/* Toasts */
.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast { background: var(--panel-2); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 10px; padding: 12px 16px; box-shadow: var(--shadow); min-width: 240px; animation: fade 0.2s ease; }
.toast.error { border-left-color: var(--bad); }
.toast.success { border-left-color: var(--good); }
.toast .tt { font-weight: 600; margin-bottom: 2px; }
.toast .tm { color: var(--muted); font-size: 12px; }

.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; display: inline-block; animation: spin 0.7s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 920px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .sidebar { display: none; }

  .mobile-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    background: linear-gradient(180deg, var(--bg-2), var(--bg));
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
  }

  .mobile-brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  #health-mobile { justify-content: flex-end; }
  #health-mobile .dot { width: 10px; height: 10px; }
  .mobile-page {
    color: var(--muted);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-tabs {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 2px;
    padding: 6px 8px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    background: var(--bg-2);
    border-top: 1px solid var(--border);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    pointer-events: auto;
  }

  .mobile-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 52px;
    padding: 6px 4px;
    background: transparent;
    border: 0;
    border-radius: 10px;
    color: var(--muted);
    cursor: pointer;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.2px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
  }

  .mobile-tab .nav-ico { font-size: 16px; line-height: 1; color: var(--muted); }
  .mobile-tab.active { color: var(--accent); background: rgba(0, 194, 209, 0.08); }
  .mobile-tab.active .nav-ico { color: var(--accent); }
  .mobile-tab:active { transform: scale(0.97); }

  .main {
    padding: 18px 16px calc(76px + env(safe-area-inset-bottom)) 16px;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    z-index: 1;
  }

  .view-head {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 16px;
  }

  .view-head h1 { font-size: 22px; }
  .view-head .btn, .view-head .agent-select, .view-head .filters { width: 100%; }
  .view-head .filters { flex-direction: column; align-items: stretch; }
  .view-head .filters select, .view-head .filters input { width: 100%; flex: none; }

  .cols, .stats-row { grid-template-columns: 1fr; }
  .stats-row { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .grid { grid-template-columns: 1fr; }

  .chat {
    height: calc(100dvh - 220px);
    min-height: 320px;
  }

  .msg { max-width: 92%; }
  .agent-select { width: 100%; }

  .drawer-backdrop { justify-items: stretch; align-items: stretch; }
  .drawer {
    width: 100%;
    max-width: none;
    height: 100%;
    border-left: 0;
    border-radius: 0;
  }

  .drawer-head { padding: 16px; }
  .drawer-head h2 { font-size: 18px; word-break: break-word; }
  .drawer-tabs { padding: 8px 10px; gap: 2px; overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .drawer-body { padding: 16px; }
  .tab { flex: 0 0 auto; white-space: nowrap; min-height: 40px; }

  .modal-backdrop { place-items: end center; padding: 0; }
  .modal {
    width: 100%;
    max-width: none;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 92dvh;
    overflow: auto;
  }

  .modal-foot { flex-wrap: wrap; }
  .modal-foot .btn { flex: 1 1 120px; min-height: 44px; }

  .toasts {
    left: 12px;
    right: 12px;
    bottom: max(76px, calc(68px + env(safe-area-inset-bottom)));
  }

  .toast { min-width: 0; width: 100%; }

  .btn, .nav-item, .icon-btn { min-height: 44px; }
  .icon-btn { min-width: 44px; display: grid; place-items: center; }

  .table { min-width: 560px; }
  .table th, .table td { padding: 10px 12px; font-size: 12px; }

  .kv { grid-template-columns: 1fr; }
  .score-track { width: 100%; order: 3; flex-basis: 100%; }
  .result-item > span:last-child { margin-left: auto; }

  .card-md { max-height: 50dvh; font-size: 11px; }
  .empty { padding: 36px 20px; }
}

@media (max-width: 420px) {
  .stats-row { grid-template-columns: 1fr 1fr; }
  .stat .v { font-size: 22px; }
  .mobile-tab span:not(.nav-ico) { font-size: 9px; }
  .chat { height: calc(100dvh - 210px); }
  .chat-input { flex-direction: column; }
  .chat-input .btn { width: 100%; min-height: 44px; }
}

@media (min-width: 921px) {
  .mobile-top, .mobile-tabs { display: none !important; }
}
