/* Klavika Basic Web Fonts */
@font-face{
  font-family: "Klavika Basic";
  src: url("fonts/KlavikaWebBasicRegular.eot");
  src: url("fonts/KlavikaWebBasicRegular.eot?#iefix") format("embedded-opentype"),
       url("fonts/KlavikaWebBasicRegular.woff") format("woff");
  font-weight: 400 499;
  font-style: normal;
  font-display: swap;
}
@font-face{
  font-family: "Klavika Basic";
  src: url("fonts/KlavikaWebBasicMedium.eot");
  src: url("fonts/KlavikaWebBasicMedium.eot?#iefix") format("embedded-opentype"),
       url("fonts/KlavikaWebBasicMedium.woff") format("woff");
  font-weight: 500 699;
  font-style: normal;
  font-display: swap;
}
@font-face{
  font-family: "Klavika Basic";
  src: url("fonts/KlavikaWebBasicBold.eot");
  src: url("fonts/KlavikaWebBasicBold.eot?#iefix") format("embedded-opentype"),
       url("fonts/KlavikaWebBasicBold.woff") format("woff");
  font-weight: 700 900;
  font-style: normal;
  font-display: swap;
}

:root{
  --bg: #f6f7fb;
  --panel: #ffffff;
  --text: #101828;
  --muted: #667085;
  --border: #e4e7ec;
  --primary: #2563eb;
  --primary-2: #1d4ed8;
  --shadow: 0 10px 30px rgba(16,24,40,0.08);
  --radius: 14px;
  --sidebar-w: 320px;
  --topbar-h: 56px;
}

*{ box-sizing: border-box; }

html, body {
  height: 100%;
}

body{
  margin:0;
  font-family: "Klavika Basic", Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 18px;
}

a.link{ color: var(--primary); text-decoration: none; }
a.link:hover{ text-decoration: underline; }

.btn{
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover{ filter: brightness(0.98); }
.btn:active{ transform: translateY(1px); }

.btn-primary{
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}
.btn-primary:hover{ background: var(--primary-2); border-color: var(--primary-2); }
.btn-stop{
  background: #fff1f2;
  border-color: #fecaca;
  color: #7f1d1d;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}
.btn-stop:hover{ filter: brightness(0.98); }

.iconbtn{
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 18px;
}

#btnToggleSidebar{ display:none; }

/* DB button: blue icon */
#btnDb{ color: var(--primary); }

.iconbtn:hover{ filter: brightness(0.98); }

.pill{
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
}

/* Pill with an embedded select (Recherche dropdown) */
.pill-select{
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.pill-select .pill-label{
  font-weight: 600;
  color: var(--muted);
}
.pill-select select{
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  outline: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
}
.pill-select select:focus{ outline: none; }

.app{
  display: flex;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

.sidebar{
  width: var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-top{
  padding: 16px;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.brand-title{ font-weight: 800; }
.brand-sub{ font-size: 14px; color: var(--muted); margin-top: 2px; }

/* Brand logo */
.brand{ display:flex; align-items:center; gap:10px; }
.brand-logo{
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background: var(--primary);
  color: #ffffff;
  display: grid;
  place-items: center;
  font-weight: 900;
  letter-spacing: .02em;
  user-select: none;
}
.brand-text{ display:flex; flex-direction: column; }

.login-brandline{ display:flex; align-items:center; gap: 12px; margin-bottom: 14px; }
.login-brandline .login-sub{ margin-bottom: 0; }

.sidebar-section{
  padding: 12px;
  overflow: auto;
  flex: 1;
}
.sidebar-heading{
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 8px;
}

.chat-list{ display: flex; flex-direction: column; gap: 6px; }
.chat-search{
  margin: 8px 0 10px 0;
}
.chat-search input{
  width: 100%;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  font-size: 14px;
  outline: none;
}
.chat-search input:focus{
  border-color: rgba(37,99,235,0.45);
  box-shadow: 0 0 0 4px rgba(37,99,235,0.10);
}
.chat-item{
  position: relative;
  padding: 10px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  background: #fbfcff;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  justify-content: space-between;
}

/* Ensure the 3-dots chat menu overlays other chat cards (hover filter creates a stacking context) */
.chat-item.menu-open{
  z-index: 1500;
}
.chat-item-left{ flex: 1; min-width: 0; }
.chat-item:hover{ filter: brightness(0.99); }
.chat-item.active{
  border-color: rgba(37,99,235,0.35);
  box-shadow: 0 0 0 4px rgba(37,99,235,0.08);
  background: #ffffff;
}
.chat-title{ font-weight: 700; font-size: 15px; }
.chat-pin{
  margin-right: 6px;
  font-size: 14px;
}
.chat-meta{ font-size: 14px; color: var(--muted); margin-top: 4px; }

.chat-menu-btn{
  width: 28px;
  height: 28px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  padding: 0;
  flex: 0 0 auto;
}
.chat-menu-btn:hover{ background: #f5f7ff; }

.chat-menu{
  position: absolute;
  top: 40px;
  right: 10px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 14px 36px rgba(15,23,42,0.12);
  padding: 6px;
  min-width: 160px;
  display: none;
  flex-direction: column;
  gap: 2px;
  z-index: 1600;
}
.chat-menu.open{ display: flex; }
.chat-menu-item{
  text-align: left;
  width: 100%;
  border: none;
  background: transparent;
  padding: 10px 10px;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
}
.chat-menu-item:hover{ background: #f5f7ff; }
.chat-menu-item.danger{ color: #b91c1c; }

.sidebar-bottom{
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
}
.sidebar-links{ display:flex; gap:12px; align-items:center; }
.user-line{ display:flex; align-items:center; gap:8px; }
.user-dot{
  width:10px; height:10px; border-radius: 50%;
  background: #22c55e;
}

.main{
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar{
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  z-index: 50;
  height: var(--topbar-h);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 12px;
  background: rgba(246,247,251,0.8);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-title{
  font-weight: 800;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar-right{
  margin-left: auto;
  display:flex;
  align-items:center;
  gap:10px;
  flex-shrink: 0;
}

.messages{
  flex: 1;
  overflow: auto;
  /* Keep messages below the fixed topbar */
  padding: calc(var(--topbar-h) + 18px) 16px 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg{
  max-width: 920px;
  width: 100%;
  margin: 0 auto;
  display: flex;
}
.msg.user{ justify-content: flex-end; }
.msg.assistant{ justify-content: flex-start; }

.bubble{
  max-width: 92%;
  min-width: 0;
  border-radius: var(--radius);
  padding: 12px 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  box-shadow: 0 4px 14px rgba(16,24,40,0.05);
  line-height: 1.45;
}

.msg-actions{
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 10px;
  flex-wrap: wrap;
}

.followups{
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.msg-attachments{
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0 4px 0;
}

.msg-attachment{
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  font-size: 12px;
  text-decoration: none;
}

.msg-attachment:hover{
  background: #f8fafc;
}

.msg-image-previews{
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 120px));
  gap: 8px;
}

.msg-image-previews a{
  display: block;
}

.msg-image{
  width: 100%;
  max-width: 120px;
  height: auto;
  display: block;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
}

.chart-preview-wrap{
  margin: 8px 0 12px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
}

.chart-preview{
  display: block;
  width: 100%;
  height: auto;
}

.followup{
  font-size: 12px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  background: #ffffff;
  color: var(--muted);
  border-radius: 999px;
  cursor: pointer;
  line-height: 1.2;
}

.followup:hover{
  background: #f8fafc;
  color: var(--text);
}

.followup:active{
  transform: translateY(1px);
}

.actionbtn{
  /* Smaller frame for message action icons (copy/retry) */
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--text);
  border-radius: 10px;
  padding: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  user-select: none;
}

.actionbtn svg{
  width: 14px;
  height: 14px;
  display: block;
}

.actionbtn svg *{
  vector-effect: non-scaling-stroke;
}

/* Slightly smaller glyphs for copy/retry icons under assistant messages */
.actionbtn.small{
  font-size: 12px;
}

.actionbtn:hover{ filter: brightness(0.98); }
.actionbtn:active{ transform: translateY(1px); }
.msg.user .bubble{
  background: #eef2ff;
  border-color: #dbeafe;
}
.msg.assistant .bubble{
  background: #ffffff;
}

.bubble .meta{
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.bubble .content{
  font-size: 16px;
  /* Prevent long words/URLs (e.g. file names, links) from overflowing bubbles on mobile */
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Ensure streamed images never exceed the message bubble width */
.bubble .content img{
  max-width: 100% !important;
  height: auto !important;
  display: block;
}

/* Prevent long streamed links (RAG/Web) from overflowing the message container */
.bubble .content a{
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Document management tables: long filenames should wrap instead of forcing horizontal overflow */
.table-wrap th,
.table-wrap td{
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Markdown styling */
.bubble .content h1,
.bubble .content h2,
.bubble .content h3{
  font-weight: 800;
  margin: 14px 0 8px;
}
.bubble .content h1{ font-size: 20px; }
.bubble .content h2{ font-size: 18px; }
.bubble .content h3{ font-size: 16px; }
.bubble .content p{ margin: 8px 0; }
.bubble .content ul, .bubble .content ol{ margin: 8px 0 8px 22px; }
.bubble .content pre{
  overflow: auto;
  padding: 12px;
  border-radius: 12px;
  background: #0b1020;
  color: #e5e7eb;
}
.bubble .content code{
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 14px;
}
.bubble .content blockquote{
  border-left: 3px solid #c7d2fe;
  margin: 10px 0;
  padding: 8px 12px;
  color: var(--muted);
  background: #f8fafc;
  border-radius: 10px;
}

.composer{
  position: fixed;
  left: 320px;
  right: 0;
  bottom: 0;
  padding: 12px 12px 14px 12px;
  background: rgba(246,247,251,0.92);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}

.statusline{
  font-size: 14px;
  color: var(--muted);
  min-height: 18px;
  padding: 0 2px 8px 2px;
}

.attachments{
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 2px 10px 2px;
}

/*
  File inputs (chat attachments):
  Keep them *not* display:none so mobile browsers (notably iOS Safari/Chrome)
  reliably open the file chooser when triggered via a <label for="...">.
*/
.fileinput-hidden{
  position: fixed;
  left: -9999px;
  top: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.chip{
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 999px;
  font-size: 14px;
  color: var(--text);
}
.chip .x{
  cursor: pointer;
  color: var(--muted);
  font-weight: 700;
}

.composer-row{
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.actioncol{
  display: flex;
  flex-direction: row;
  gap: 8px;
  align-items: center;
}

.promptcol{
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.mobile-pills{
  display: none;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.toolrow{
  display: flex;
  flex-direction: row;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.attach-menu{ position: relative; }

.attach-dropdown{
  position: absolute;
  left: 0;
  bottom: 46px;
  min-width: 180px;
  padding: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: none;
  flex-direction: column;
  gap: 4px;
  z-index: 60;
}

.attach-dropdown.open{ display: flex; }

.attach-item{
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  padding: 10px 10px;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  font-family: inherit;
  font-size: inherit;
  line-height: 1.2;
}

button.attach-item{
  appearance: none;
  -webkit-appearance: none;
}

.attach-item .icon{
  width: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 20px;
}

.attach-item:hover{ background: #f2f4f7; }
.attach-item:active{ transform: translateY(1px); }

.toolcol{
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
}

.iconbtn.active{
  border-color: rgba(37,99,235,0.45);
  box-shadow: 0 0 0 4px rgba(37,99,235,0.10);
}

.prompt{
  flex: 1;
  min-height: 40px;
  max-height: 140px;
  resize: none;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  font-size: 16px;
  line-height: 1.35;
  outline: none;
}
.prompt:focus{
  border-color: rgba(37,99,235,0.45);
  box-shadow: 0 0 0 4px rgba(37,99,235,0.10);
}

/* Login */
.login-body{
  display: grid;
  place-items: center;
  height: 100vh;
}

.login-card{
  width: min(420px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 22px;
}
.login-title{
  font-weight: 900;
  font-size: 20px;
}
.login-sub{
  margin-top: 4px;
  color: var(--muted);
  margin-bottom: 14px;
}
.login-form{ display:flex; flex-direction: column; gap: 12px; }
.login-form label span{ font-size: 14px; color: var(--muted); display:block; margin-bottom:6px; }
.login-form input{
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  font-size: 16px;
  outline: none;
}
.login-form input:focus{
  border-color: rgba(37,99,235,0.45);
  box-shadow: 0 0 0 4px rgba(37,99,235,0.10);
}
.alert{
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #7f1d1d;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 15px;
  margin-bottom: 12px;
}

/* Responsive: sidebar overlay on small screens */
@media (max-width: 860px) {
  #btnToggleSidebar{ display:grid; }

  /* Mobile: keep Web/RAG status pills in the header */
  .topbar-right{ display: flex; }
  .mobile-pills{ display: none; }

  .sidebar{
    position: fixed;
    left: 0;
    /* Keep drawer below the fixed header in mobile/PWA */
    top: var(--topbar-h);
    bottom: 0;
    transform: translateX(-110%);
    transition: transform .22s ease;
    z-index: 30;
    box-shadow: var(--shadow);
  }
  .topbar{
    left: 0;
  }
  .sidebar.open{
    transform: translateX(0);
  }
  .composer{
    left: 0;
  }

  /* Mobile: make the prompt wider by stacking Web/RAG above +/Mic */
  .toolrow{
    display: grid;
    grid-template-columns: 40px 40px;
    grid-template-areas:
      "db rag"
      "attach mic";
    gap: 8px;
    align-items: center;
    justify-content: start;
  }

  #btnDb{ grid-area: db; }
  #btnGroupRag{ grid-area: rag; }
  .attach-menu{ grid-area: attach; }
  #btnMic{ grid-area: mic; }

  /* Mobile: make the prompt as tall as the 2x2 tool button grid */
  .prompt{
    min-height: 88px;
  }

  /* Mobile: make prompt roughly as tall as the 2x2 tool buttons */
  .prompt{
    min-height: 88px;
  }

  /* Mobile: smaller send button with centered arrow icon */
  #btnStop{
    width: 46px;
    min-width: 46px;
    height: 40px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
  }
  #btnSend{
    width: 46px;
    min-width: 46px;
    height: 40px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0; /* hide label text */
    line-height: 0;
    border-radius: 12px;
  }
  #btnSend::before{
    content: "";
    width: 18px;
    height: 18px;
    display: block;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23fff' d='M4 12h13.2l-5.6-5.6L13 5l9 7-9 7-1.4-1.4L17.2 13H4z'/%3E%3C/svg%3E");
  }

  /* Mobile: make prompt input as tall as the 4 tool icons block */
  .prompt{
    min-height: 88px;
    max-height: 220px;
  }

  .actioncol{
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }
}
