:root {
  --bg: #0b1322;
  --bg-glow: #12203a;
  --panel: #111b2e;
  --panel-border: #1f2c45;
  --card: #141f34;
  --card-border: #22314d;
  --band: #18243b;
  --row-hover: #16233a;
  --row-border: #1a2740;
  --text: #e6ebf3;
  --text-soft: #c9d2e0;
  --muted: #8592a8;
  --faint: #5f6c83;
  --accent: #2f73e0;
  --accent-hover: #3b82f6;
  --user: #1d3557;
  --user-border: #2b4a75;
  --ok: #4ade80;
  --ok-bg: rgba(74, 222, 128, .08);
  --err: #f87171;
  --err-bg: rgba(248, 113, 113, .08);
  --amber: #fbbf24;
  --folder: #f6c343;
  --radius: 16px;
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 600px at 50% -10%, var(--bg-glow), var(--bg)) fixed, var(--bg);
  color: var(--text);
  font: 14px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ layout */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
  gap: 20px;
  padding: 24px;
  max-width: 1500px;
  margin: 0 auto;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  overflow: hidden;
  box-shadow: 0 20px 50px -20px rgba(0, 0, 0, .55);
}
.tabs { display: none; }

/* ------------------------------------------------------------ chat header */
.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--panel-border);
  position: relative;
}
.logo {
  width: 36px; height: 36px;
  border-radius: 10px;
  border: 0;
  background: #f4f6fa;
  color: #3b4659;
  display: grid; place-items: center;
  flex: none;
  transition: transform .15s;
}
.logo:hover { transform: rotate(30deg); }
.brand { flex: 1; min-width: 0; }
.brand-name { font-family: Georgia, "Times New Roman", serif; font-weight: 700; font-size: 19px; line-height: 1.1; letter-spacing: -.2px; }
.brand-model { font-size: 11.5px; color: var(--muted); }
.status { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--muted); }
.status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); }
.status.online .dot { background: #22c55e; box-shadow: 0 0 0 3px rgba(34, 197, 94, .15); }
.status.connecting .dot { background: var(--amber); animation: pulse 1s infinite; }
.status.offline .dot { background: var(--err); }
.help {
  width: 22px; height: 22px; border-radius: 50%;
  border: 1.5px solid var(--muted); background: none; color: var(--muted);
  font-size: 12px; font-weight: 700; display: grid; place-items: center; padding: 0;
}
.help:hover { color: var(--text); border-color: var(--text); }

.menu {
  position: absolute; top: 60px; left: 16px; z-index: 20;
  background: #18243b; border: 1px solid var(--card-border); border-radius: 12px;
  padding: 6px; min-width: 230px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .5);
}
.menu button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  background: none; border: 0; text-align: left; padding: 9px 12px; border-radius: 8px;
}
.menu button span { width: 16px; text-align: center; color: var(--muted); }
.menu button:hover { background: #22314d; }
.menu-sep { height: 1px; background: var(--card-border); margin: 6px 4px; }

/* ------------------------------------------------------------ messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 22px 12px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}
.messages::-webkit-scrollbar, .rows::-webkit-scrollbar, .modal-body::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb, .rows::-webkit-scrollbar-thumb, .modal-body::-webkit-scrollbar-thumb { background: #26344f; border-radius: 8px; }

.msg { margin: 0 0 18px; }
.who { font-size: 10.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: 8px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.bubble {
  background: var(--user);
  border: 1px solid var(--user-border);
  border-radius: 14px;
  padding: 10px 15px;
  max-width: 85%;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 6px 18px -8px rgba(47, 115, 224, .35);
}
.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 14px 18px;
  color: var(--text-soft);
  overflow-wrap: anywhere;
}
.card > * + * { margin-top: 8px; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 8px; }
.md strong { color: var(--text); font-weight: 650; }
.md h1, .md h2, .md h3, .md h4 { color: var(--text); font-size: 14.5px; margin: 14px 0 6px; }
.md h1 { font-size: 17px; } .md h2 { font-size: 15.5px; }
.md ul, .md ol { margin: 4px 0 8px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md a { color: #7fb0ff; }
.md code { font-family: var(--mono); font-size: 12px; background: #0d1626; border: 1px solid var(--card-border); border-radius: 5px; padding: 1px 5px; color: #dbe4f3; }
.md pre { background: #0b1322; border: 1px solid var(--card-border); border-radius: 10px; padding: 12px; overflow-x: auto; }
.md pre code { background: none; border: 0; padding: 0; font-size: 12px; }
.md blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--card-border); color: var(--muted); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; font-size: 13px; }
.md th, .md td { border: 1px solid var(--card-border); padding: 5px 9px; text-align: left; }
.md th { background: var(--band); color: var(--text); }
.md hr { border: 0; border-top: 1px solid var(--card-border); margin: 12px 0; }

.tool {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  font-family: var(--mono); font-size: 11.5px;
  background: var(--ok-bg); color: var(--ok);
  border: 1px solid rgba(74, 222, 128, .18);
  border-radius: 6px; padding: 3px 9px;
  cursor: pointer; user-select: none;
}
.tool .tn { font-weight: 700; flex: none; }
.tool .ts { color: rgba(74, 222, 128, .75); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool.running { background: rgba(251, 191, 36, .07); color: var(--amber); border-color: rgba(251, 191, 36, .2); }
.tool.running .ts { color: rgba(251, 191, 36, .75); }
.tool.error, .tool.stopped { background: var(--err-bg); color: var(--err); border-color: rgba(248, 113, 113, .2); }
.tool.error .ts, .tool.stopped .ts { color: rgba(248, 113, 113, .75); }
.tool .spin { width: 10px; height: 10px; border: 1.5px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
.tool-result {
  font-family: var(--mono); font-size: 11.5px; white-space: pre-wrap;
  background: #0b1322; border: 1px solid var(--card-border); border-radius: 8px;
  padding: 10px; max-height: 260px; overflow: auto; color: var(--muted);
}
.err-block { color: var(--err); background: var(--err-bg); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.meta { font-size: 11px; color: var(--faint); }

.typing { display: flex; gap: 5px; padding: 6px 2px 14px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }

.empty { text-align: center; padding: 12vh 12px 20px; color: var(--muted); }
.empty-logo { font-family: Georgia, serif; font-size: 34px; font-weight: 700; color: var(--text); margin-bottom: 8px; }
.empty p { max-width: 420px; margin: 0 auto 22px; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.chips button {
  background: var(--card); border: 1px solid var(--card-border); color: var(--text-soft);
  border-radius: 999px; padding: 8px 14px; font-size: 13px;
}
.chips button:hover { border-color: var(--accent); color: var(--text); }

/* permission / question cards */
.request {
  border: 1px solid rgba(251, 191, 36, .35);
  background: linear-gradient(180deg, rgba(251, 191, 36, .06), rgba(251, 191, 36, .02));
  border-radius: 14px; padding: 14px 16px; margin: 0 0 18px;
}
.request.question { border-color: rgba(59, 130, 246, .4); background: linear-gradient(180deg, rgba(59, 130, 246, .08), rgba(59, 130, 246, .02)); }
.rq-title { font-weight: 600; margin-bottom: 8px; }
.rq-title small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; margin-top: 2px; }
.rq-detail { font-family: var(--mono); font-size: 11.5px; white-space: pre-wrap; background: #0b1322; border: 1px solid var(--card-border); border-radius: 8px; padding: 10px; max-height: 220px; overflow: auto; margin: 0 0 12px; color: var(--text-soft); }
.rq-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.rq-q { margin-bottom: 12px; }
.rq-q-text { font-weight: 600; margin-bottom: 8px; color: var(--text); }
.rq-options { display: grid; gap: 6px; }
.rq-opt { text-align: left; background: var(--card); border: 1px solid var(--card-border); border-radius: 10px; padding: 9px 12px; }
.rq-opt b { display: block; font-weight: 600; color: var(--text); }
.rq-opt span { font-size: 12.5px; color: var(--muted); }
.rq-opt.selected { border-color: var(--accent-hover); background: rgba(59, 130, 246, .12); }
.rq-other { width: 100%; margin-top: 6px; background: #0d1626; border: 1px solid var(--card-border); border-radius: 10px; color: var(--text); padding: 8px 12px; font: inherit; }

/* ------------------------------------------------------------ composer */
.scroll-down {
  position: absolute; right: 50%; transform: translateX(50%); bottom: 112px;
  width: 32px; height: 32px; border-radius: 50%;
  background: #e8edf5; color: #1d2638; border: 0; display: grid; place-items: center;
  box-shadow: 0 6px 16px rgba(0, 0, 0, .4); z-index: 5;
}
.composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 12px 18px 6px;
  border-top: 1px solid var(--panel-border);
}
.composer textarea {
  flex: 1; resize: none; min-height: 46px; max-height: 200px;
  background: var(--card); border: 1px solid var(--card-border); border-radius: 14px;
  color: var(--text); font: inherit; padding: 12px 16px; outline: none;
  overflow-y: hidden;
}
.composer textarea:focus { border-color: #34507d; }
.composer textarea::placeholder { color: var(--faint); }
.composer.drop textarea { border-color: var(--accent-hover); border-style: dashed; }
.round { width: 42px; height: 42px; border-radius: 50%; border: 0; display: grid; place-items: center; flex: none; margin-bottom: 2px; transition: transform .1s, background .15s; }
.round:active { transform: scale(.94); }
.mic { background: #f4f6fa; color: #1d2638; }
.attach { background: var(--card); color: var(--text-soft); border: 1px solid var(--card-border); }
.attach:hover { color: var(--text); border-color: #34507d; }

/* attachments: tray above the composer and thumbnails in sent messages */
.tray { display: flex; gap: 8px; overflow-x: auto; padding: 10px 18px 0; border-top: 1px solid var(--panel-border); }
.tray + .composer { border-top: 0; }
.att {
  position: relative; flex: none; display: flex; align-items: center; gap: 8px;
  height: 58px; max-width: 200px; padding: 0 10px 0 0; overflow: hidden;
  background: var(--card); border: 1px solid var(--card-border); border-radius: 10px;
  font-size: 12px; color: var(--text-soft);
}
.att img, .att .att-ico { width: 56px; height: 56px; flex: none; object-fit: cover; }
.att .att-ico { display: grid; place-items: center; background: #0d1626; color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; }
.att span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att.loading img, .att.loading .att-ico { opacity: .4; }
.att.loading::after {
  content: ""; position: absolute; left: 19px; top: 19px; width: 18px; height: 18px;
  border: 2px solid #fff; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite;
}
.att.failed { border-color: var(--err); }
.att-x {
  position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  border: 0; background: rgba(0, 0, 0, .65); color: #fff; font-size: 11px; line-height: 20px; padding: 0;
}
.bubble-atts { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; max-width: 85%; margin-bottom: 6px; }
.bubble-atts img { width: 120px; height: 120px; object-fit: cover; border-radius: 12px; border: 1px solid var(--user-border); cursor: pointer; }
.bubble-atts .att { cursor: pointer; }
.mic.rec { background: var(--err); color: #fff; animation: pulse 1.2s infinite; }
.send { background: var(--accent); color: #fff; opacity: .55; }
.send.ready { opacity: 1; }
.send:hover { background: var(--accent-hover); }
.send .ic-stop { display: none; }
.send.stop { background: #e8edf5; color: #1d2638; opacity: 1; }
.send.stop .ic-send { display: none; }
.send.stop .ic-stop { display: block; }
.composer-meta { display: flex; justify-content: space-between; gap: 10px; padding: 0 20px 12px; font-size: 11.5px; color: var(--faint); }
.composer-meta button { background: none; border: 0; padding: 2px 0; color: var(--muted); white-space: nowrap; flex: none; }
.composer-meta button:hover { color: var(--text); }
.composer-meta #convTitle { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ------------------------------------------------------------ files */
.files-head { padding: 18px 20px 14px; }
.crumbs { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 700; margin-bottom: 14px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.crumbs button { background: none; border: 0; padding: 0; color: var(--text); font-weight: 700; text-transform: none; letter-spacing: 0; font-size: 12.5px; }
.crumbs button:hover { text-decoration: underline; }
.crumbs .sep { color: var(--faint); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  border-radius: 8px; padding: 8px 13px; font-size: 13px; font-weight: 600; border: 1px solid transparent;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: var(--card-border); color: var(--text-soft); }
.btn.ghost:hover { border-color: #34507d; color: var(--text); }
.btn.danger { background: transparent; border-color: rgba(248, 113, 113, .4); color: var(--err); }
.btn.danger:hover { background: var(--err-bg); }
.search {
  display: flex; align-items: center; gap: 10px;
  background: var(--band); border: 1px solid var(--card-border); border-radius: 10px;
  padding: 0 14px; color: var(--muted);
}
.search input { flex: 1; background: none; border: 0; outline: none; color: var(--text); font: inherit; padding: 11px 0; }
.search input::placeholder { color: var(--muted); }
.search input::-webkit-search-cancel-button { filter: invert(.6); }

.table-head, .row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 80px 128px;
  align-items: center;
  padding: 0 20px;
}
.table-head { background: var(--band); border-top: 1px solid var(--row-border); border-bottom: 1px solid var(--row-border); }
.table-head button {
  background: none; border: 0; padding: 10px 0; text-align: left;
  font-size: 10.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
}
.table-head .col-name { padding-left: 30px; }
.table-head .col-size, .table-head .col-date { text-align: right; }
.table-head button.active { color: var(--text-soft); }
.table-head i { font-style: normal; }

.rows { flex: 1; overflow-y: auto; }
.row { border-bottom: 1px solid var(--row-border); min-height: 41px; cursor: pointer; position: relative; }
.row:hover { background: var(--row-hover); }
.row .name { display: flex; align-items: center; gap: 12px; min-width: 0; font-weight: 550; color: var(--text); padding: 9px 0; }
.row .name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .name small { color: var(--faint); font-weight: 400; margin-left: 6px; }
.row .size, .row .date { text-align: right; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row .ico { flex: none; width: 18px; height: 18px; }
.row .more {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  background: var(--card); border: 1px solid var(--card-border); border-radius: 6px; width: 26px; height: 24px;
  display: none; place-items: center; color: var(--muted); padding: 0;
}
.row:hover .more { display: grid; }
.row.up .name { color: var(--muted); }
.files-foot { padding: 8px 20px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--row-border); }
.files-empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.drop-hint {
  position: absolute; inset: 8px; border: 2px dashed var(--accent-hover); border-radius: 12px;
  background: rgba(17, 27, 46, .92); display: none; place-items: center; font-weight: 600; z-index: 10; pointer-events: none;
}
.files.dragging .drop-hint { display: grid; }

/* ------------------------------------------------------------ modal / toast */
.modal {
  width: min(760px, calc(100vw - 32px)); max-height: min(86vh, 900px);
  background: var(--panel); color: var(--text);
  border: 1px solid var(--card-border); border-radius: var(--radius); padding: 0;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6);
}
.modal[open] { display: flex; flex-direction: column; }
.modal::backdrop { background: rgba(4, 8, 16, .65); backdrop-filter: blur(3px); }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--panel-border); }
.modal-title { flex: 1; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal-close { background: none; border: 0; color: var(--muted); font-size: 16px; padding: 4px 8px; }
.modal-body { padding: 16px 18px; overflow: auto; flex: 1; min-height: 0; }
.modal-body pre.code { margin: 0; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; color: var(--text-soft); }
.modal-body img { max-width: 100%; border-radius: 8px; display: block; margin: 0 auto; }
.modal-body iframe { width: 100%; height: 70vh; border: 0; border-radius: 8px; background: #fff; }
.modal-body input[type=text], .modal-body input[type=password] {
  width: 100%; background: var(--card); border: 1px solid var(--card-border); border-radius: 10px;
  color: var(--text); font: inherit; padding: 10px 12px; outline: none;
}
.modal-body input:focus { border-color: var(--accent-hover); }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--panel-border); }
.modal-actions:empty { display: none; }
.conv-list { display: grid; gap: 6px; }
.conv-list button { text-align: left; background: var(--card); border: 1px solid var(--card-border); border-radius: 10px; padding: 10px 14px; }
.conv-list button:hover, .conv-list button.current { border-color: var(--accent-hover); }
.conv-list small { display: block; color: var(--muted); font-size: 11.5px; }
.help-list { margin: 0; padding-left: 18px; color: var(--text-soft); }
.help-list li { margin: 6px 0; }
kbd { font-family: var(--mono); font-size: 11px; background: var(--card); border: 1px solid var(--card-border); border-radius: 4px; padding: 1px 5px; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #e8edf5; color: #111827; padding: 10px 16px; border-radius: 10px; font-weight: 550; font-size: 13px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .45); z-index: 100; max-width: calc(100vw - 32px);
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@keyframes pulse { 50% { opacity: .55; } }

/* ------------------------------------------------------------ mobile */
@media (max-width: 900px) {
  .app { display: block; padding: 0; height: calc(100dvh - 60px - env(safe-area-inset-bottom)); }
  .panel { height: 100%; border-radius: 0; border: 0; box-shadow: none; }
  .app[data-view="chat"] .files, .app[data-view="files"] .chat { display: none; }
  .chat-head { padding: calc(12px + env(safe-area-inset-top)) 16px 12px; }
  .menu { top: calc(58px + env(safe-area-inset-top)); }
  .messages { padding: 16px 14px 8px; }
  .bubble { max-width: 92%; }
  .card { padding: 12px 14px; }
  .composer { padding: 10px 12px 4px; gap: 8px; }
  .tray { padding: 10px 12px 0; }
  .bubble-atts { max-width: 92%; }
  .composer textarea { font-size: 16px; } /* prevents iOS zoom on focus */
  .composer-meta { padding: 0 14px 8px; }
  .scroll-down { bottom: 100px; }
  .files-head { padding: calc(14px + env(safe-area-inset-top)) 14px 12px; }
  .table-head, .row { grid-template-columns: minmax(0, 1fr) 62px 92px; padding: 0 14px; }
  .row .date { font-size: 11px; }
  .row .more { display: grid; position: static; transform: none; margin-left: auto; }
  .row .name { padding-right: 4px; }
  .search input { font-size: 16px; }
  .tabs {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0;
    height: calc(60px + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--panel); border-top: 1px solid var(--panel-border); z-index: 30;
  }
  .tabs button { flex: 1; background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; color: var(--muted); }
  .tabs button.active { color: var(--text); }
  .tabs button.active svg { color: var(--accent-hover); }
  .toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
}
@media (max-width: 420px) {
  .table-head .col-size, .row .size { display: none; }
  .table-head, .row { grid-template-columns: minmax(0, 1fr) 92px; }
  .status #statusText { display: none; }
}
