:root {
  color-scheme: dark;
  --ink: #edf0ff;
  --muted: #9ca7c9;
  --canvas: #080b16;
  --panel: rgba(17, 23, 45, 0.84);
  --panel-strong: #151c35;
  --line: rgba(177, 190, 237, 0.16);
  --accent: #a78bfa;
  --accent-2: #4de2c5;
  --danger: #ff7189;
  --warning: #f7c76e;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.34);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% -10%, rgba(132, 94, 247, 0.26), transparent 34rem),
    radial-gradient(circle at 96% 4%, rgba(41, 203, 173, 0.13), transparent 28rem),
    var(--canvas);
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }

.noise {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  opacity: 0.15;
  background-image: radial-gradient(rgba(255, 255, 255, 0.5) 0.5px, transparent 0.5px);
  background-size: 4px 4px;
  mask-image: linear-gradient(to bottom, rgba(0,0,0,0.6), transparent 90%);
}

.loading-shell, .auth-shell {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
}

.loading-shell { gap: 12px; color: var(--muted); }
.orbit {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(167, 139, 250, 0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.auth-grid {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(320px, 0.8fr);
  width: min(1040px, 100%);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 28px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.auth-intro {
  display: flex;
  min-height: 580px;
  flex-direction: column;
  justify-content: space-between;
  padding: clamp(30px, 5vw, 64px);
  border-right: 1px solid var(--line);
  background: linear-gradient(145deg, rgba(111, 78, 220, 0.26), rgba(15, 24, 53, 0.12));
}

.eyebrow {
  margin: 0 0 14px;
  color: var(--accent-2);
  font-size: 0.72rem;
  font-weight: 760;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1, h2, h3, p { margin-top: 0; }
h1 { max-width: 560px; margin-bottom: 16px; font-size: clamp(2.5rem, 5vw, 4.7rem); line-height: 0.98; letter-spacing: -0.06em; }
h2 { margin-bottom: 8px; font-size: 1.35rem; letter-spacing: -0.025em; }
h3 { margin-bottom: 4px; font-size: 0.98rem; }
.lede, .muted { color: var(--muted); line-height: 1.65; }
.lede { max-width: 46ch; }

.signal-list { display: grid; gap: 11px; padding: 0; margin: 28px 0 0; list-style: none; color: #d7dcf9; }
.signal-list li { display: flex; gap: 10px; align-items: center; }
.signal-list i { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 16px var(--accent-2); }

.auth-card { align-self: center; width: min(410px, 100%); padding: 34px; }
.auth-card > p { margin-bottom: 26px; color: var(--muted); }

.field { display: grid; gap: 7px; margin-bottom: 16px; }
.field label { color: #c9d0ec; font-size: 0.82rem; font-weight: 700; }
input, select, textarea {
  width: 100%;
  border: 1px solid rgba(183, 197, 243, 0.2);
  border-radius: 12px;
  outline: none;
  padding: 12px 13px;
  color: var(--ink);
  background: rgba(5, 8, 18, 0.62);
  transition: border-color 150ms ease, box-shadow 150ms ease, background 150ms ease;
}
textarea { min-height: 106px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.18); background: rgba(7, 10, 23, 0.86); }
input::placeholder, textarea::placeholder { color: #69759d; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.045);
  font-size: 0.88rem;
  font-weight: 720;
  transition: transform 150ms ease, border-color 150ms ease, background 150ms ease;
}
.button:hover { transform: translateY(-1px); border-color: rgba(189, 175, 255, 0.66); background: rgba(255, 255, 255, 0.08); }
.button.primary { border-color: transparent; color: #110d25; background: linear-gradient(135deg, #c7b3ff, #8af0d8); box-shadow: 0 12px 30px rgba(141, 112, 244, 0.22); }
.button.primary:hover { background: linear-gradient(135deg, #d7c8ff, #abf9e8); }
.button.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.button.danger { color: #ffdbe2; border-color: rgba(255, 113, 137, 0.34); }
.button.small { min-height: 32px; padding: 6px 9px; font-size: 0.75rem; }
.full { width: 100%; }

.form-error { min-height: 20px; margin: 5px 0 0; color: #ffadbb; font-size: 0.85rem; }
.form-note { margin-top: 16px; color: #7f8bb3; font-size: 0.77rem; line-height: 1.5; }

.app-shell { display: grid; grid-template-columns: 246px minmax(0, 1fr); min-height: 100vh; }
.sidebar { position: sticky; top: 0; display: flex; height: 100vh; flex-direction: column; gap: 18px; padding: 22px 15px; border-right: 1px solid var(--line); background: rgba(8, 11, 22, 0.76); backdrop-filter: blur(18px); }
.brand { display: flex; gap: 10px; align-items: center; padding: 6px 10px 16px; }
.brand-mark { display: grid; width: 32px; height: 32px; place-items: center; border-radius: 10px; color: #141025; background: linear-gradient(145deg, var(--accent), var(--accent-2)); font-size: 0.85rem; font-weight: 900; }
.brand strong { display: block; font-size: 0.93rem; letter-spacing: -0.02em; }
.brand span { display: block; margin-top: 2px; color: var(--muted); font-size: 0.7rem; }
.nav { display: grid; gap: 4px; }
.nav button { display: flex; width: 100%; align-items: center; gap: 10px; border: 0; border-radius: 10px; padding: 10px; color: #aeb8d5; background: transparent; text-align: left; font-size: 0.86rem; }
.nav button:hover { color: var(--ink); background: rgba(255, 255, 255, 0.055); }
.nav button.active { color: #171128; background: linear-gradient(135deg, #b9a6ff, #8cf0d7); font-weight: 780; }
.nav-icon { width: 17px; text-align: center; font-size: 0.94rem; }
.sidebar-footer { margin-top: auto; padding: 10px; color: var(--muted); font-size: 0.72rem; line-height: 1.45; }

.main-stage { min-width: 0; padding: 24px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 24px; }
.topbar h1 { margin: 0; font-size: clamp(1.55rem, 3vw, 2.25rem); letter-spacing: -0.045em; }
.workspace-pill, .profile-pill { display: inline-flex; align-items: center; gap: 9px; border: 1px solid var(--line); border-radius: 999px; padding: 7px 10px; color: #c7d0f0; background: rgba(255,255,255,0.035); font-size: 0.78rem; }
.workspace-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 12px var(--accent-2); }
.topbar-actions { display: flex; align-items: center; gap: 9px; }

.view { animation: rise 220ms ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 18px; }
.metric, .panel { border: 1px solid var(--line); border-radius: 18px; background: var(--panel); box-shadow: 0 14px 42px rgba(0,0,0,0.16); }
.metric { min-height: 126px; padding: 19px; }
.metric-label { color: var(--muted); font-size: 0.75rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; }
.metric-value { margin-top: 18px; font-size: 2.1rem; font-weight: 800; letter-spacing: -0.06em; }
.metric.accent .metric-value { color: #c7b6ff; }
.metric.mint .metric-value { color: #8af0d8; }

.panel { padding: 18px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 13px; }
.panel-heading h2 { margin: 0; font-size: 0.95rem; }
.panel-heading span { color: var(--muted); font-size: 0.75rem; }
.two-column { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(280px, 0.8fr); gap: 16px; }
.recent-list, .source-summary { display: grid; gap: 8px; }
.recent-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; border: 1px solid transparent; border-radius: 12px; padding: 10px; color: inherit; background: rgba(255,255,255,0.02); text-align: left; }
.recent-row:hover { border-color: var(--line); background: rgba(255,255,255,0.04); }
.recent-row strong, .recent-row span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-row span { margin-top: 3px; color: var(--muted); font-size: 0.76rem; }
.badge { display: inline-flex; align-items: center; border-radius: 999px; padding: 3px 7px; color: #d9ddff; background: rgba(167,139,250,0.14); font-size: 0.68rem; font-weight: 750; text-transform: capitalize; }
.badge.enabled { color: #baf8e8; background: rgba(77,226,197,0.13); }
.badge.off { color: #b2bbd6; background: rgba(156,167,201,0.11); }

.inbox-layout { display: grid; grid-template-columns: minmax(270px, 0.78fr) minmax(0, 1.45fr); min-height: calc(100vh - 116px); overflow: hidden; border: 1px solid var(--line); border-radius: 19px; background: var(--panel); box-shadow: var(--shadow); }
.thread-column { display: flex; min-width: 0; flex-direction: column; border-right: 1px solid var(--line); }
.thread-tools { display: grid; gap: 10px; padding: 14px; border-bottom: 1px solid var(--line); }
.filter-row { display: flex; gap: 6px; overflow-x: auto; }
.filter-row button { flex: 0 0 auto; border: 1px solid transparent; border-radius: 999px; padding: 5px 8px; color: var(--muted); background: transparent; font-size: 0.72rem; }
.filter-row button.active { border-color: rgba(167,139,250,0.46); color: #e9e5ff; background: rgba(167,139,250,0.13); }
.thread-list { overflow-y: auto; }
.thread-row { width: 100%; border: 0; border-bottom: 1px solid rgba(177,190,237,0.09); padding: 14px; color: var(--ink); background: transparent; text-align: left; }
.thread-row:hover, .thread-row.selected { background: linear-gradient(90deg, rgba(167,139,250,0.14), rgba(77,226,197,0.04)); }
.thread-row.selected { box-shadow: inset 2px 0 0 var(--accent); }
.thread-row-head { display: flex; justify-content: space-between; gap: 9px; }
.thread-row strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.84rem; }
.thread-row time { flex: 0 0 auto; color: var(--muted); font-size: 0.68rem; }
.thread-row p { display: -webkit-box; overflow: hidden; margin: 5px 0 8px; color: var(--muted); font-size: 0.76rem; line-height: 1.35; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.thread-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.unread { display: grid; min-width: 18px; height: 18px; place-items: center; border-radius: 50%; color: #08141b; background: var(--accent-2); font-size: 0.65rem; font-weight: 900; }

.conversation { display: flex; min-width: 0; flex-direction: column; }
.conversation-empty { display: grid; flex: 1; place-items: center; padding: 28px; color: var(--muted); text-align: center; }
.conversation-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.conversation-head h2 { margin: 0; font-size: 1rem; }
.conversation-head p { margin: 3px 0 0; color: var(--muted); font-size: 0.75rem; }
.conversation-actions { display: flex; gap: 7px; }
.message-list { display: flex; flex: 1; flex-direction: column; gap: 11px; overflow-y: auto; padding: 20px; }
.message { max-width: min(84%, 560px); border: 1px solid var(--line); border-radius: 15px; padding: 11px 12px; background: rgba(255,255,255,0.035); }
.message.outbound { align-self: flex-end; border-color: rgba(167,139,250,0.28); background: rgba(125, 96, 223, 0.17); }
.message-meta { display: flex; justify-content: space-between; gap: 16px; margin-bottom: 6px; color: #aeb8d5; font-size: 0.67rem; font-weight: 700; text-transform: capitalize; }
.message p { margin: 0; overflow-wrap: anywhere; color: #e6e9fb; font-size: 0.86rem; line-height: 1.5; white-space: pre-wrap; }
.attachment-chip { display: inline-flex; margin-top: 8px; border: 1px solid rgba(167,139,250,0.3); border-radius: 9px; padding: 5px 7px; color: #dad4ff; font-size: 0.72rem; text-decoration: none; }
.reply-box { padding: 14px; border-top: 1px solid var(--line); background: rgba(8,11,22,0.43); }
.reply-box textarea { min-height: 74px; }
.reply-controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.file-label { color: var(--muted); font-size: 0.76rem; }
.file-label input { width: 180px; max-width: 42vw; padding: 0; border: 0; background: transparent; }

.settings-grid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(300px, 0.8fr); gap: 16px; }
.source-table { display: grid; gap: 8px; }
.source-row { display: grid; grid-template-columns: minmax(100px, 0.65fr) minmax(120px, 1fr) auto; align-items: center; gap: 10px; border: 1px solid rgba(177,190,237,0.1); border-radius: 11px; padding: 10px; }
.source-row strong { display: block; font-size: 0.84rem; }
.source-row span { display: block; margin-top: 3px; overflow: hidden; color: var(--muted); font-size: 0.72rem; text-overflow: ellipsis; white-space: nowrap; }
.source-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.token-reveal { margin-top: 12px; border: 1px dashed rgba(138,240,216,0.45); border-radius: 12px; padding: 12px; color: #baf8e8; background: rgba(77,226,197,0.07); font-size: 0.76rem; overflow-wrap: anywhere; }
.hidden { display: none !important; }
.empty { padding: 26px 12px; color: var(--muted); text-align: center; font-size: 0.85rem; }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 20; display: grid; gap: 8px; width: min(360px, calc(100vw - 36px)); }
.toast { border: 1px solid var(--line); border-radius: 13px; padding: 12px 14px; color: #e9ebff; background: #17203c; box-shadow: var(--shadow); font-size: 0.84rem; }
.toast.error { border-color: rgba(255,113,137,0.5); color: #ffdce4; }
.toast.success { border-color: rgba(77,226,197,0.45); }

@media (max-width: 820px) {
  .auth-grid { grid-template-columns: 1fr; }
  .auth-intro { min-height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; padding: 12px; border-right: 0; border-bottom: 1px solid var(--line); }
  .brand { padding-bottom: 4px; }
  .nav { grid-template-columns: repeat(6, minmax(0, 1fr)); overflow-x: auto; }
  .nav button { justify-content: center; padding: 8px; font-size: 0; }
  .nav-icon { font-size: 1rem; }
  .sidebar-footer { display: none; }
  .metrics, .two-column, .settings-grid { grid-template-columns: 1fr; }
  .main-stage { padding: 14px; }
  .topbar { align-items: flex-start; }
  .topbar-actions { flex-wrap: wrap; justify-content: flex-end; }
  .inbox-layout { grid-template-columns: 1fr; min-height: auto; }
  .thread-column { max-height: 46vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .conversation { min-height: 58vh; }
}

@media (max-width: 520px) {
  .auth-card { padding: 25px; }
  .auth-intro { padding: 30px; }
  h1 { font-size: 2.6rem; }
  .topbar { flex-direction: column; }
  .topbar-actions { justify-content: flex-start; }
  .metrics { gap: 9px; }
  .metric { min-height: 102px; padding: 14px; }
  .metric-value { margin-top: 12px; font-size: 1.75rem; }
  .conversation-head { align-items: flex-start; flex-direction: column; }
}
