* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--fg); font-size: 13.5px; line-height: 1.5;
  -webkit-font-smoothing: antialiased; overflow: hidden; }
button { font-family: inherit; cursor: pointer; color: inherit; }
input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
:focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--radius-sm); }
::selection { background: var(--primary-50); }
[dir="rtl"], [lang="ar"] { font-family: var(--font-ar); }

.icon { width: 16px; height: 16px; flex-shrink: 0; stroke: currentColor; fill: none; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round; }
.icon.solid { fill: currentColor; stroke: none; }
.icon.sm { width: 13px; height: 13px; }
.icon.lg { width: 20px; height: 20px; }
.spacer { flex: 1; }
.empty { padding: 48px 24px; text-align: center; color: var(--muted-fg); }

#app { height: 100vh; display: flex; flex-direction: column; }
.topbar { height: var(--topbar-h); display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: var(--card); border-bottom: 1px solid var(--border); flex-shrink: 0; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; background: var(--grad); color: #fff; display: grid; place-items: center; }
.brand-name { font-weight: 600; font-size: 14px; line-height: 1.1; }
.brand-sub { font-size: 11px; color: var(--muted-fg); }
.matter { padding-left: 14px; border-left: 1px solid var(--border); min-width: 0; }
.matter-title { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.matter-sub { font-size: 11px; color: var(--muted-fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.doc-switch { display: flex; gap: 6px; margin-left: 8px; overflow-x: auto; scrollbar-width: none; }
.doc-chip { display: flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--border);
  border-radius: 999px; background: var(--card); font-size: 12px; white-space: nowrap; }
.doc-chip:hover { border-color: var(--border-strong); }
.doc-chip.on { background: var(--primary-50); border-color: var(--primary-300); color: var(--primary-700); }
.chip-type { font-size: 10px; font-weight: 600; letter-spacing: .04em; color: var(--primary); }
.chip-title { max-width: 120px; overflow: hidden; text-overflow: ellipsis; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--card); font-size: 12.5px; font-weight: 500; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--muted); border-color: var(--border-strong); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-fg); }
.btn.primary:hover { background: var(--primary-600); }
.btn.danger { color: var(--bad); border-color: hsl(0 60% 85%); }
.btn.danger:hover { background: var(--bad-bg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 8px; font-size: 11.5px; }
.btn.icon-only { padding: 7px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.mode { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--muted);
  font-size: 11.5px; color: var(--muted-fg); }
.mode i { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.mode.live i { background: var(--ok); }

.body { flex: 1; display: flex; min-height: 0; }
.nav { width: var(--nav-w); background: var(--card); border-right: 1px solid var(--border); padding: 10px 6px;
  display: flex; flex-direction: column; gap: 4px; overflow-y: auto; flex-shrink: 0; }
.nav-item { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 2px; border: 0; background: transparent;
  border-radius: var(--radius-sm); font-size: 10px; color: var(--muted-fg); text-align: center; line-height: 1.15; }
.nav-item .icon { width: 19px; height: 19px; }
.nav-item:hover { background: var(--muted); color: var(--fg); }
.nav-item.on { background: var(--primary-50); color: var(--primary); font-weight: 600; }
.view { flex: 1; min-width: 0; min-height: 0; overflow: hidden; display: flex; flex-direction: column; position: relative; }
.rail { width: var(--rail-w); border-left: 1px solid var(--border); background: var(--card); display: flex; flex-direction: column;
  min-height: 0; flex-shrink: 0; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 10.5px; font-weight: 500;
  background: var(--muted); color: var(--muted-fg); white-space: nowrap; }
.pill.ok { background: var(--ok-bg); color: var(--ok); } .pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.bad { background: var(--bad-bg); color: var(--bad); } .pill.info { background: var(--info-bg); color: var(--info); }
.pill.brand { background: var(--primary-50); color: var(--primary); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted-fg); margin-bottom: 12px; }
.field input, .field textarea, .field select, .input { padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--card); color: var(--fg); }
.check { display: flex; gap: 8px; align-items: center; font-size: 12.5px; }

.modal-back { position: fixed; inset: 0; background: hsl(0 0% 9% / .4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--card); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); width: min(560px, 100%); max-height: 88vh; overflow: auto; }
.modal.wide { width: min(900px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 15px; font-weight: 600; }
.modal-body { padding: 20px; }
.modal-actions { display: flex; gap: 8px; margin-top: 8px; }
#toast-host { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 60; }

@media (max-width: 1100px) { :root { --rail-w: 320px; } .matter { display: none; } }
@media (max-width: 820px) {
  body { overflow: auto; } #app { height: auto; min-height: 100vh; }
  .body { flex-direction: column; } .nav { width: auto; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .nav-item { flex-direction: row; gap: 6px; padding: 8px 10px; font-size: 12px; white-space: nowrap; }
  .rail { width: auto; border-left: 0; border-top: 1px solid var(--border); max-height: 60vh; }
  .brand-sub, .doc-switch { display: none; } .btn span { display: none; }
}
.toast { background: var(--fg); color: #fff; padding: 9px 16px; border-radius: 999px; font-size: 12.5px; box-shadow: var(--shadow-lg); }
.toast.bad { background: var(--bad); } .toast.ok { background: var(--ok); }
.brand, .brand > div, .btn, .mode { white-space: nowrap; flex-shrink: 0; }
.brand-sub { display: none; }
.doc-switch { flex: 1 1 0; min-width: 0; }
.spacer { flex: 0 1 0; }
@media (min-width: 1500px) { .brand-sub { display: block; } }
@media (max-width: 820px) { .topbar { overflow: hidden; gap: 8px; padding: 0 10px; } .mode, .matter { display: none; } .brand-name { font-size: 13px; } }

/* v3: two tabs + drawer */
.topbar { gap: 14px; }
.brand-mark { width: 30px; height: 30px; border-radius: 9px; }
.tabs { display: flex; gap: 2px; padding: 3px; background: var(--muted); border-radius: 10px; }
.tab { border: 0; background: transparent; padding: 6px 16px; border-radius: 8px; font-size: 13px; font-weight: 500; color: var(--muted-fg); }
.tab:hover { color: var(--fg); }
.tab.on { background: var(--card); color: var(--primary); box-shadow: var(--shadow-sm); font-weight: 600; }
.matter { border-left: 0; padding-left: 4px; }
.matter-title { font-weight: 500; color: var(--muted-fg); font-size: 12.5px; max-width: 320px; }
.doc-chip { padding: 4px 10px; }
.doc-switch { flex: 0 1 auto; }
.view { position: relative; }
.drawer { position: absolute; top: 0; right: 0; bottom: 0; width: min(760px, 92%); background: var(--bg); border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg); display: flex; flex-direction: column; z-index: 20; animation: drawer-in .16s ease-out; }
@keyframes drawer-in { from { transform: translateX(24px); opacity: .4; } to { transform: none; opacity: 1; } }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px 10px 20px; background: var(--card); border-bottom: 1px solid var(--border); }
.drawer-head h2 { font-size: 14px; font-weight: 600; }
.drawer-body { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
@media (max-width: 820px) { .tab { padding: 6px 12px; } .drawer { width: 100%; position: fixed; top: var(--topbar-h); z-index: 40; } }

/* v3: collapsible agent rail */
.rail.collapsed { width: 46px; align-items: center; padding-top: 10px; background: var(--card); }
.rail-open { position: relative; display: flex; flex-direction: column; align-items: center; gap: 8px; width: 36px; padding: 10px 0; border: 1px solid var(--border);
  border-radius: 10px; background: var(--bg); color: var(--primary); font-size: 10.5px; font-weight: 600; letter-spacing: .04em; }
.rail-open:hover { border-color: var(--primary-300); background: var(--primary-50); }
.rail-open span { writing-mode: vertical-rl; transform: rotate(180deg); text-transform: uppercase; }
.rail-badge { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--warn); color: #fff; font-size: 10.5px; display: grid; place-items: center; }
.rail-pulse { position: absolute; bottom: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); animation: rail-p 1.2s ease-in-out infinite; }
@keyframes rail-p { 50% { opacity: .25; transform: scale(1.4); } }
@media (prefers-reduced-motion: reduce) { .rail-pulse { animation: none; } .drawer { animation: none; } }
@media (max-width: 820px) { .rail.collapsed { width: auto; flex-direction: row; justify-content: center; padding: 6px; border-left: 0; border-top: 1px solid var(--border); }
  .rail-open { flex-direction: row; width: auto; padding: 8px 14px; } .rail-open span { writing-mode: horizontal-tb; transform: none; } }
