:root {
  --bg: #0b0e17;
  --bg-2: #121626;
  --bg-3: #1a2036;
  --panel: #141a2e;
  --border: #252c44;
  --text: #e7eaf3;
  --muted: #9aa3bd;
  --accent: #7c6cff;
  --accent-2: #5a8bff;
  --accent-grad: linear-gradient(135deg, #7c6cff, #5a8bff);
  --ok: #3ecf8e;
  --warn: #ffcc66;
  --danger: #ff5c7c;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 600px at 80% -10%, #1b1140 0%, transparent 60%),
    radial-gradient(900px 500px at -10% 10%, #0d2246 0%, transparent 55%), var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
a { color: var(--accent-2); }

.app-loading { display: grid; place-items: center; height: 100vh; color: var(--muted); }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 24px; font-weight: 700; margin-bottom: 4px; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; object-fit: cover; flex: 0 0 auto; }
.brand-grad { background: var(--accent-grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.auth-sub { color: var(--muted); margin: 0 0 20px; font-size: 14px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.input, textarea.input, select.input {
  width: 100%; background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 11px 12px; outline: none; transition: border-color .15s;
}
.input:focus { border-color: var(--accent); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--bg-3); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 16px; cursor: pointer; transition: all .15s; font-weight: 500;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent-grad); border: none; color: white; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-block { width: 100%; }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.btn-ghost { background: transparent; }
.auth-switch { text-align: center; margin-top: 16px; color: var(--muted); font-size: 14px; }
.auth-switch button { background: none; border: none; color: var(--accent-2); cursor: pointer; padding: 0; }
.msg-error { background: rgba(255,92,124,.12); border: 1px solid rgba(255,92,124,.4); color: #ffb3c2; padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 14px; }
.msg-ok { background: rgba(62,207,142,.12); border: 1px solid rgba(62,207,142,.4); color: #9ff0c9; padding: 10px 12px; border-radius: 10px; font-size: 14px; margin-bottom: 14px; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 280px 1fr; height: 100vh; }
.sidebar { background: rgba(10,13,22,.6); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; backdrop-filter: blur(6px); }
.sidebar-head { padding: 16px; border-bottom: 1px solid var(--border); }
.sidebar-scroll { overflow-y: auto; flex: 1; padding: 8px; min-height: 0; }
.conv-item { padding: 9px 11px; border-radius: 9px; cursor: pointer; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; }
.conv-item:hover { background: var(--bg-2); color: var(--text); }
.conv-item.active { background: var(--bg-3); color: var(--text); }
.conv-item .title { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.conv-del { opacity: 0; border: none; background: none; color: var(--muted); cursor: pointer; }
.conv-item:hover .conv-del { opacity: 1; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.nav-row { display: flex; gap: 8px; }
.nav-row .btn { flex: 1; }

.userbox { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent-grad); display: grid; place-items: center; font-weight: 700; color: #fff; }

/* Quota bars */
.quota { font-size: 12px; color: var(--muted); }
.quota .bar { height: 6px; background: var(--bg-3); border-radius: 4px; overflow: hidden; margin: 4px 0 8px; }
.quota .bar > span { display: block; height: 100%; background: var(--accent-grad); }
.quota .bar.warn > span { background: linear-gradient(90deg,#ffcc66,#ff5c7c); }

/* ---------- Main / chat ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 18px; border-bottom: 1px solid var(--border); }
.topbar .spacer { flex: 1; }
.model-select { min-width: 190px; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 20px; background: var(--bg-3); color: var(--muted); border: 1px solid var(--border); }
.badge.mult { color: var(--accent-2); }

.chat-scroll { flex: 1; overflow-y: auto; padding: 24px 0; min-height: 0; }
.chat-inner { max-width: 820px; margin: 0 auto; padding: 0 18px; }
.empty-hint { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty-hint .big { font-size: 40px; }

.msg-row { display: flex; gap: 12px; margin-bottom: 22px; }
.msg-row .who { width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0; display: grid; place-items: center; font-size: 15px; }
.msg-row.user .who { background: var(--bg-3); }
.msg-row.assistant .who { background: var(--accent-grad); }
.bubble { min-width: 0; flex: 1; }
.bubble .name { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.content { overflow-wrap: anywhere; }
.content p { margin: 0 0 10px; }
.content pre { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; overflow-x: auto; }
.content code { background: var(--bg-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.content pre code { background: none; padding: 0; }
.content table { border-collapse: collapse; width: 100%; margin: 8px 0; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 9px; text-align: left; }
.content ul, .content ol { margin: 0 0 10px; padding-left: 22px; }

/* Tool activity monitor */
.tool-log { border: 1px solid rgba(124,108,255,.32); border-radius: 14px; background: linear-gradient(145deg, rgba(18,22,38,.98), rgba(13,17,31,.98)); margin: 10px 0 14px; font-size: 13px; overflow: hidden; box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.tool-log summary { display: flex; align-items: center; gap: 8px; padding: 10px 12px; cursor: pointer; color: var(--text); list-style: none; user-select: none; }
.tool-log summary::-webkit-details-marker { display: none; }
.activity-title { font-weight: 650; }
.activity-count { color: var(--muted); font-size: 11px; margin-left: auto; }
.activity-live { color: var(--accent-2); font-size: 12px; animation: activityPulse 1.2s ease-in-out infinite; }
.activity-live.done { color: var(--ok); animation: none; }
.tool-log .tool-body { padding: 0 12px 10px; }
.tool-step { position: relative; border-top: 1px solid rgba(37,44,68,.8); padding: 11px 0 10px 26px; }
.tool-step::before { content: ''; position: absolute; left: 7px; top: 0; bottom: -1px; width: 1px; background: var(--border); }
.tool-step:last-child::before { bottom: 18px; }
.tool-step-head { display: flex; align-items: center; gap: 7px; min-width: 0; }
.tool-icon { position: absolute; left: 0; top: 12px; width: 15px; height: 15px; display: grid; place-items: center; border-radius: 50%; background: var(--bg-3); color: var(--muted); font-weight: 700; z-index: 1; }
.tool-step.running .tool-icon { color: var(--accent-2); animation: toolSpin 1s linear infinite; }
.tool-step.complete .tool-icon { color: var(--ok); }
.tool-step.failed .tool-icon { color: var(--danger); }
.tool-step .t-name { color: var(--accent-2); font-weight: 650; }
.tool-state { margin-left: auto; color: var(--muted); font-size: 11px; white-space: nowrap; }
.tool-step.complete .tool-state { color: var(--ok); }
.tool-step.failed .tool-state { color: var(--danger); }
.tool-action { color: var(--text); margin: 3px 0 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-detail { color: var(--muted); font-size: 11px; }
.tool-detail summary { display: inline-block; padding: 0; color: var(--muted); font-size: 11px; }
.tool-detail pre, .tool-step .result { margin: 6px 0 0; max-height: 180px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; }
.tool-detail pre { padding: 8px; border-radius: 8px; background: rgba(7,10,19,.7); color: #c8d1ed; }
.tool-detail .tool-output { border-top: 1px solid var(--border); }
.tool-step .result { color: var(--muted); font-size: 11px; }
.tool-step.running .result { color: var(--accent-2); }
.tool-step.complete .result { color: #a8e8c9; }
.tool-step.failed .result { color: #ffb3c2; }
@keyframes toolSpin { to { transform: rotate(360deg); } }
@keyframes activityPulse { 50% { opacity: .35; transform: scale(.8); } }
.cursor-blink::after { content: '▋'; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer { border-top: 1px solid var(--border); padding: 14px 18px; }
.composer-inner { max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; background: var(--bg-2); border: 1px solid var(--border); border-radius: 14px; padding: 8px 8px 8px 14px; }
.composer-inner:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; background: none; border: none; outline: none; resize: none; max-height: 200px; padding: 6px 0; }
.send-btn { width: 40px; height: 40px; border-radius: 10px; border: none; background: var(--accent-grad); color: #fff; cursor: pointer; flex-shrink: 0; display: grid; place-items: center; }
.send-btn:disabled { opacity: .5; }
.composer-hint { max-width: 820px; margin: 6px auto 0; color: var(--muted); font-size: 11px; text-align: center; }

/* ---------- Modal / panels ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(5,7,12,.7); display: grid; place-items: center; padding: 20px; z-index: 50; backdrop-filter: blur(3px); }
.modal { width: 100%; max-width: 860px; max-height: 88vh; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; display: flex; flex-direction: column; overflow: hidden; box-shadow: var(--shadow); }
.modal-head { display: flex; align-items: center; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-head .spacer { flex: 1; }
.modal-close { background: none; border: none; color: var(--muted); font-size: 22px; cursor: pointer; }
.tabs { display: flex; gap: 4px; padding: 10px 14px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tab { padding: 8px 14px; border-radius: 9px 9px 0 0; cursor: pointer; color: var(--muted); border: 1px solid transparent; border-bottom: none; }
.tab.active { background: var(--bg-2); color: var(--text); border-color: var(--border); }
.modal-body { padding: 18px; overflow-y: auto; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.row-actions { display: flex; gap: 6px; }
.card { background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 20px; border: 1px solid var(--border); color: var(--muted); }
.pill.on { color: var(--ok); border-color: rgba(62,207,142,.4); }
.pill.off { color: var(--danger); border-color: rgba(255,92,124,.4); }
.hint { color: var(--muted); font-size: 13px; }
.section-title { display: flex; align-items: center; margin: 4px 0 14px; }
.section-title .spacer { flex: 1; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }

.toast-wrap { position: fixed; bottom: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--bg-3); border: 1px solid var(--border); border-radius: 10px; padding: 11px 14px; box-shadow: var(--shadow); font-size: 14px; max-width: 340px; }
.toast.err { border-color: rgba(255,92,124,.5); }
.toast.ok { border-color: rgba(62,207,142,.5); }

/* ---------- Responsif ---------- */
.menu-btn { display: none; }
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 40; width: 280px; height: 100%; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .menu-btn { display: grid; }
  .grid2 { grid-template-columns: 1fr; }
  .modal { max-height: 94vh; }
}
.scrim { display: none; }
@media (max-width: 820px) { .scrim.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 35; } }
