/* FIX: elements with an explicit display rule ignore the hidden attribute */
[hidden] { display: none !important; }
/* ==========================================================================
   AFINA v4 — minimalist UI (DeepSeek / ChatGPT / Claude feel)
   Plain CSS, no framework. Light + dark via prefers-color-scheme.
   Accent orange is reserved for the primary action only.
   ========================================================================== */

:root {
  --accent: #e8590c;
  --accent-hover: #d94e00;
  --accent-soft: rgba(232, 89, 12, .10);

  --bg: #ffffff;
  --bg-side: #f7f7f8;
  --bg-elev: #ffffff;
  --bg-hover: #f0f0f1;
  --bg-code: #f6f6f7;
  --border: #e5e5e7;
  --border-strong: #d4d4d8;
  --text: #17171a;
  --text-soft: #6b6b73;
  --text-faint: #9a9aa2;
  --danger: #d64545;
  --ok: #2f9e44;
  --shadow: 0 8px 30px rgba(0, 0, 0, .12);
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 268px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
          "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1c;
    --bg-side: #141416;
    --bg-elev: #232326;
    --bg-hover: #2a2a2e;
    --bg-code: #0f0f10;
    --border: #2e2e33;
    --border-strong: #3d3d44;
    --text: #ececee;
    --text-soft: #9d9da6;
    --text-faint: #6e6e78;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; padding: 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
::selection { background: var(--accent-soft); }

/* thin scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

.ico { width: 20px; height: 20px; fill: none; stroke: currentColor;
       stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ico.mini { width: 16px; height: 16px; }

/* ============================ layout ============================ */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
  transition: margin-left .18s ease;
}
.app.side-collapsed .sidebar { margin-left: calc(-1 * var(--sidebar-w)); }

.brand { display: flex; align-items: center; gap: 9px; padding: 14px 14px 8px; }
.brand-logo { width: 26px; height: 26px; }
.brand-name { font-weight: 600; letter-spacing: .04em; flex: 1; }
.collapse-btn { color: var(--text-faint); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: var(--radius-sm);
  color: var(--text-soft); transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.new-btn {
  display: flex; align-items: center; gap: 9px;
  margin: 8px 12px; padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent); color: #fff;
  font-weight: 500;
  transition: background .12s;
}
.new-btn:hover { background: var(--accent-hover); }

.side-nav { display: flex; flex-direction: column; padding: 4px 8px; gap: 1px; }
.side-link {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 10px; border-radius: var(--radius-sm);
  color: var(--text-soft); font-size: 14px; text-align: left;
  transition: background .12s, color .12s;
}
.side-link:hover { background: var(--bg-hover); color: var(--text); }
.side-link.active { background: var(--bg-hover); color: var(--text); font-weight: 500; }

.search-wrap {
  display: flex; align-items: center; gap: 7px;
  margin: 8px 12px; padding: 7px 10px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.search-ico { color: var(--text-faint); }
.search-input { border: none; background: none; outline: none; width: 100%; }

.hist-section { padding: 4px 8px; display: flex; flex-direction: column; min-height: 0; }
.hist-section.grow { flex: 1; }
.proj-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 10px 4px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint);
}
.proj-add { color: var(--text-faint); padding: 2px; border-radius: 6px; }
.proj-add:hover { background: var(--bg-hover); color: var(--text); }
.history { overflow-y: auto; min-height: 0; }

.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-soft); font-size: 14px; text-align: left;
  width: 100%; transition: background .12s, color .12s;
}
.chat-item:hover { background: var(--bg-hover); color: var(--text); }
.chat-item.active { background: var(--bg-hover); color: var(--text); font-weight: 500; }
.chat-item .ct { flex: 1; white-space: nowrap; overflow: hidden;
                 text-overflow: ellipsis; min-width: 0; }
.chat-item .gen-dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: pulse 1.4s infinite; flex: 0 0 7px;
}
.chat-item .row-act { opacity: 0; color: var(--text-faint); }
.chat-item:hover .row-act { opacity: 1; }
.chat-item .row-act:hover { color: var(--danger); }
@keyframes pulse { 0%,100% { opacity: .35 } 50% { opacity: 1 } }

.sidebar-bottom { padding: 8px; border-top: 1px solid var(--border); }
.account {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 8px 10px; border-radius: var(--radius-sm); color: var(--text);
}
.account:hover { background: var(--bg-hover); }
.acc-avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; display: flex;
  align-items: center; justify-content: center; font-size: 13px; font-weight: 600;
}
.acc-text { flex: 1; text-align: left; font-size: 14px; }
.acc-caret { color: var(--text-faint); }

.scrim { display: none; }

/* ============================ main ============================ */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

.topbar {
  display: flex; align-items: center; gap: 6px;
  height: 52px; padding: 0 10px; border-bottom: 1px solid var(--border);
  flex: 0 0 52px;
}
.menu-btn { display: none; }
.desktop-menu-btn { color: var(--text-faint); }
.session-title {
  flex: 1; text-align: left; font-weight: 600; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding: 6px 8px; border-radius: var(--radius-sm); color: var(--text);
  max-width: 100%;
}
.session-title:hover { background: var(--bg-hover); }
.topbar-right { display: flex; align-items: center; gap: 4px; }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); }
.conn-dot.on { background: var(--ok); }
.conn-dot.off { background: var(--danger); }

.chat {
  flex: 1; overflow-y: auto; min-height: 0;
  scroll-behavior: smooth;
}
.empty-state {
  max-width: 720px; margin: 0 auto; padding: 12vh 22px 40px;
  text-align: center; display: flex; flex-direction: column;
  align-items: center; gap: 18px;
}
.empty-logo { width: 56px; height: 56px; opacity: .9; }
.greeting { font-size: 26px; font-weight: 600; margin: 0; }
.suggest-row { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.suggest {
  padding: 9px 14px; border: 1px solid var(--border);
  border-radius: 999px; color: var(--text-soft); font-size: 13.5px;
  transition: background .12s, border-color .12s;
}
.suggest:hover { background: var(--bg-hover); border-color: var(--border-strong); }

.thread { max-width: 760px; margin: 0 auto; padding: 22px 20px 40px; }
.msg { display: flex; gap: 14px; margin-bottom: 26px; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 50%; flex: 0 0 30px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
}
.msg.user .avatar { background: var(--border-strong); color: var(--text); }
.msg.assistant .avatar { background: var(--accent); color: #fff; }
.msg .body { flex: 1; min-width: 0; }
.msg .name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.msg .role-label { font-size: 11px; color: var(--text-faint); font-weight: 400; margin-left: 6px; }

/* reasoning block */
.reasoning {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-code); margin-bottom: 10px; overflow: hidden;
}
.reasoning summary {
  cursor: pointer; padding: 7px 11px; font-size: 12.5px;
  color: var(--text-soft); user-select: none;
}
.reasoning .rbody {
  padding: 4px 12px 11px; font-size: 13px; color: var(--text-soft);
  white-space: pre-wrap; max-height: 320px; overflow-y: auto;
  border-top: 1px solid var(--border);
}

/* markdown content */
.content { word-wrap: break-word; overflow-wrap: anywhere; }
.content > *:first-child { margin-top: 0; }
.content > *:last-child { margin-bottom: 0; }
.content p { margin: 0 0 12px; }
.content h1, .content h2, .content h3, .content h4 {
  margin: 20px 0 10px; line-height: 1.3; font-weight: 600;
}
.content h1 { font-size: 22px; }
.content h2 { font-size: 19px; }
.content h3 { font-size: 17px; }
.content ul, .content ol { margin: 0 0 12px; padding-left: 24px; }
.content li { margin: 4px 0; }
.content blockquote {
  margin: 0 0 12px; padding: 4px 14px; border-left: 3px solid var(--border-strong);
  color: var(--text-soft);
}
.content a { word-break: break-word; }
.content table { border-collapse: collapse; margin: 0 0 12px; display: block;
                 overflow-x: auto; max-width: 100%; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.content th { background: var(--bg-code); font-weight: 600; }
.content hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }
.content code {
  font-family: var(--mono); font-size: 13px;
  background: var(--bg-code); border: 1px solid var(--border);
  border-radius: 5px; padding: 1px 5px;
}
.content pre {
  background: var(--bg-code); border: 1px solid var(--border);
  border-radius: var(--radius-sm); overflow: hidden; margin: 0 0 12px;
}
.content pre .code-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--text-faint);
}
.content pre .code-copy { color: var(--text-soft); font-size: 12px; }
.content pre .code-copy:hover { color: var(--text); }
.content pre code {
  display: block; padding: 12px; background: none; border: none;
  overflow-x: auto; font-size: 13px; line-height: 1.5;
}
.content pre.wrapped code { white-space: pre-wrap; word-break: break-word; }

/* sources */
.sources { margin-top: 12px; border-top: 1px solid var(--border); padding-top: 10px; }
.sources .src-title { font-size: 12px; font-weight: 600; color: var(--text-faint);
                      text-transform: uppercase; letter-spacing: .05em; margin-bottom: 6px; }
.source {
  display: block; padding: 7px 0; font-size: 13.5px;
  border-bottom: 1px solid var(--border);
}
.source:last-child { border-bottom: none; }
.source .s-url { color: var(--text-faint); font-size: 12px; }

/* tool confirmation card */
.tool-card {
  margin-top: 12px; border: 1px solid var(--accent);
  border-radius: var(--radius-sm); overflow: hidden;
  background: var(--accent-soft);
}
.tool-head { padding: 8px 12px; font-size: 13px; font-weight: 600;
             color: var(--accent); }
.tool-cmd {
  margin: 0; padding: 8px 12px; font-family: var(--mono); font-size: 12.5px;
  background: var(--bg-code); border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border); white-space: pre-wrap;
  word-break: break-word;
}
.tool-reason { padding: 6px 12px; font-size: 12px; color: var(--text-soft); }
.tool-actions { display: flex; gap: 8px; padding: 8px 12px; align-items: center; }

/* ============================ composer ============================ */
.composer-wrap { padding: 0 20px 16px; }
.attachments { max-width: 760px; margin: 0 auto 8px; display: flex;
               flex-wrap: wrap; gap: 6px; }
.att-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg-code); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 10px; font-size: 12.5px;
}
.att-chip .rm { color: var(--text-faint); }
.att-chip .rm:hover { color: var(--danger); }

.composer {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  border-radius: 22px; padding: 7px 8px;
  transition: border-color .12s;
}
.composer:focus-within { border-color: var(--text-faint); }
.plus-btn, .send-btn, .stop-btn, .websearch-on {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 50%; flex: 0 0 34px;
  color: var(--text-soft);
}
.plus-btn:hover { background: var(--bg-hover); color: var(--text); }
.send-btn { background: var(--accent); color: #fff; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { background: var(--border-strong); cursor: default; }
.stop-btn { background: var(--bg-hover); color: var(--text); }
#webBtn { color: var(--text-faint); }
#webBtn.websearch-on { color: var(--text-faint); }
#webBtn.active { background: var(--accent-soft); color: var(--accent); }
.input {
  flex: 1; border: none; background: none; outline: none; resize: none;
  padding: 7px 6px; max-height: 200px; line-height: 1.5;
}
.composer-meta {
  max-width: 760px; margin: 8px auto 0;
  display: flex; align-items: center; justify-content: space-between;
  font-size: 12px; color: var(--text-faint);
}
.composer-meta .meta-right { display: flex; align-items: center; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 999px; font-size: 12.5px;
  color: var(--text-soft); border: 1px solid transparent;
}
.chip:hover { background: var(--bg-hover); }
.chip .caret { color: var(--text-faint); }

/* popover */
.popover {
  position: absolute; bottom: 92px; right: 20px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 6px; min-width: 240px; z-index: 40;
}
.pop-title { font-size: 12px; font-weight: 600; color: var(--text-faint);
             text-transform: uppercase; letter-spacing: .05em; padding: 8px 10px 4px; }
.pop-list { display: flex; flex-direction: column; }
.pop-item {
  display: flex; flex-direction: column; gap: 2px;
  padding: 9px 10px; border-radius: var(--radius-sm); text-align: left;
}
.pop-item:hover { background: var(--bg-hover); }
.pop-item.active { background: var(--accent-soft); }
.pop-item .pi-title { font-size: 14px; font-weight: 500; }
.pop-item .pi-desc { font-size: 12px; color: var(--text-faint); }

/* ============================ modal ============================ */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 60; padding: 18px;
}
.modal-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 640px; max-height: 86vh;
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.modal-body { padding: 16px; overflow-y: auto; }

/* shared form bits */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-soft);
               margin-bottom: 5px; font-weight: 500; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--bg); outline: none;
}
.field textarea { resize: vertical; min-height: 90px; font-family: inherit; }
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent);
}
.btn-primary {
  background: var(--accent); color: #fff; padding: 9px 16px;
  border-radius: var(--radius-sm); font-weight: 500;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
  padding: 9px 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); color: var(--text);
}
.btn-ghost:hover { background: var(--bg-hover); }
.btn-danger { color: var(--danger); border: 1px solid var(--border-strong);
              padding: 9px 16px; border-radius: var(--radius-sm); }
.btn-danger:hover { background: var(--bg-hover); }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.list-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  margin-bottom: 8px;
}
.list-item .li-main { flex: 1; min-width: 0; }
.list-item .li-title { font-weight: 500; }
.list-item .li-sub { font-size: 12.5px; color: var(--text-faint);
                     white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px;
         background: var(--bg-code); border: 1px solid var(--border); color: var(--text-soft); }
.badge.ok { color: var(--ok); border-color: var(--ok); }
.badge.err { color: var(--danger); border-color: var(--danger); }
.muted { color: var(--text-faint); font-size: 13px; }
.hint { font-size: 12.5px; color: var(--text-faint); margin-top: 4px; }
.divider { border-top: 1px solid var(--border); margin: 16px 0; }
.switch { display: flex; align-items: center; gap: 8px; }
.switch input { width: auto; }
.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; }
.tab { padding: 7px 12px; border-radius: var(--radius-sm); font-size: 13.5px;
       color: var(--text-soft); }
.tab:hover { background: var(--bg-hover); }
.tab.active { background: var(--bg-hover); color: var(--text); font-weight: 500; }
.code-view {
  font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap;
  background: var(--bg-code); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px; max-height: 420px;
  overflow: auto;
}

/* toasts */
.toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
          display: flex; flex-direction: column; gap: 8px; z-index: 80;
          align-items: center; }
.toast {
  background: var(--bg-elev); border: 1px solid var(--border);
  box-shadow: var(--shadow); border-radius: var(--radius-sm);
  padding: 10px 16px; font-size: 13.5px; max-width: 90vw;
}
.toast.err { border-color: var(--danger); color: var(--danger); }

/* typing caret */
.caret-blink { display: inline-block; width: 8px; height: 15px;
               background: var(--text); margin-left: 2px; vertical-align: text-bottom;
               animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0 } }

/* ============================ responsive ============================ */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 50;
    transform: translateX(0); box-shadow: var(--shadow);
  }
  .app:not(.side-open) .sidebar { transform: translateX(-100%); }
  .app.side-collapsed .sidebar { margin-left: 0; }
  .scrim {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4);
    z-index: 45; opacity: 0; pointer-events: none; transition: opacity .18s;
  }
  .app.side-open .scrim { opacity: 1; pointer-events: auto; }
  .menu-btn { display: inline-flex; }
  .desktop-menu-btn { display: none; }
}

@media (max-width: 400px) {
  body { font-size: 14.5px; }
  .composer-wrap { padding: 0 10px 10px; }
  .thread { padding: 16px 14px 30px; }
  .greeting { font-size: 22px; }
  .side-nav { padding: 4px 6px; }
  .chat-item { padding: 9px 8px; }
  .popover { right: 10px; left: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* ============================ login ============================ */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; min-height: 100dvh; background: var(--bg-side); padding: 20px;
}
.login-card {
  width: 100%; max-width: 380px; background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 28px; box-shadow: var(--shadow);
}
.login-brand { text-align: center; margin-bottom: 24px; }
.login-logo { width: 52px; height: 52px; }
.login-title { font-size: 24px; font-weight: 700; margin: 10px 0 4px;
               letter-spacing: .04em; }
.login-sub { color: var(--text-soft); font-size: 14px; margin: 0; }
.login-field { display: block; margin-bottom: 16px; }
.login-field span { display: block; font-size: 13px; color: var(--text-soft);
                    margin-bottom: 6px; font-weight: 500; }
.login-field input {
  width: 100%; padding: 11px 13px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); background: var(--bg); outline: none;
}
.login-field input:focus { border-color: var(--accent); }
.login-submit {
  width: 100%; padding: 12px; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; font-weight: 600; font-size: 15px;
  transition: background .12s;
}
.login-submit:hover { background: var(--accent-hover); }
.login-submit:disabled { background: var(--border-strong); cursor: default; }
.login-err { color: var(--danger); font-size: 13px; margin-top: 12px;
             text-align: center; min-height: 18px; }
