/* =========================================================
   Assistant Juridique – Préfecture de l'Allier
   Styles complémentaires au DSFR pour l'interface de chat
   ========================================================= */

/* --- Variables chat -------------------------------------- */
:root {
  --chat-sidebar-w: 300px;
  --chat-blue: #000091;
  --chat-blue-hover: #1212ff;
  --chat-blue-pale: #e3e3fd;
  --chat-grey-bg: #f6f6f6;
  --chat-text: #161616;
  --chat-text-muted: #666666;
  --chat-radius: 4px;
  --chat-font-mono: 'Fira Code', 'Cascadia Code', 'Courier New', monospace;
}

/* --- Override body for chat layout ----------------------- */
html {
  height: 100%;
}

body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}

/* --- Compact DSFR overrides for chat app context --------- */
/* Header */
.fr-header {
  flex-shrink: 0;
}

.fr-header__body {
  padding: .25rem 0 !important;
}

.fr-header__brand-top {
  padding: .25rem 0 !important;
}

.fr-logo {
  font-size: .7rem !important;
}

.fr-header__service {
  padding: .125rem 0 0 !important;
  margin-top: 0 !important;
  box-shadow: none !important;
}

.fr-header__service-title {
  font-size: .875rem !important;
}

.fr-header__service-tagline {
  font-size: .6875rem !important;
}

/* Notice */
.fr-notice {
  flex-shrink: 0;
  min-height: auto !important;
}

.fr-notice .fr-notice__body {
  padding: .1875rem 0 !important;
  min-height: auto !important;
}

.fr-notice__title {
  font-size: .6875rem !important;
}

/* Footer */
.fr-footer__body {
  padding: .375rem 0 !important;
}

.fr-footer__brand {
  padding: 0 !important;
}

.fr-footer .fr-logo {
  font-size: .6rem !important;
}

.fr-footer__bottom {
  padding: .25rem 0 !important;
}

.fr-footer__content-desc {
  font-size: .6875rem !important;
  margin: 0 !important;
}

.fr-footer__content-link {
  font-size: .6875rem !important;
}

.fr-footer__bottom-link {
  font-size: .625rem !important;
}

.fr-footer__bottom-copy p {
  font-size: .625rem !important;
}

/* --- Header operator custom ------------------------------ */
.chat-operator-name {
  font-size: .8125rem;
  font-weight: 700;
  color: var(--chat-blue);
  line-height: 1.2;
  margin: 0;
}

.chat-operator-ministry {
  font-size: .6875rem;
  color: var(--chat-text-muted);
  line-height: 1.2;
  margin: .0625rem 0 0;
}

/* =========================================================
   CHAT LAYOUT
   ========================================================= */

.chat-layout {
  display: flex;
  flex: none;
  height: var(--chat-layout-h, 100vh);
  min-height: 0;
  overflow: hidden;
}

/* =========================================================
   SIDEBAR
   ========================================================= */

.chat-sidebar {
  width: var(--chat-sidebar-w);
  min-width: var(--chat-sidebar-w);
  background: #f6f6f6;
  color: var(--chat-text);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 1.25rem 1rem;
  gap: .25rem;
  border-right: 1px solid #e5e5e5;
}

.chat-sidebar__section {
  padding: .75rem 0;
  border-bottom: 1px solid #e5e5e5;
}

.chat-sidebar__section:last-of-type {
  border-bottom: none;
}

.chat-sidebar__title {
  font-size: .6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--chat-text-muted);
  margin-bottom: .625rem;
}

/* Example buttons */
.chat-sidebar__examples {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: .375rem;
}

.example-btn {
  background: #fff;
  border: 1px solid #ddd;
  border-radius: var(--chat-radius);
  color: var(--chat-text);
  font-size: .8125rem;
  font-family: inherit;
  padding: .5rem .625rem;
  text-align: left;
  cursor: pointer;
  line-height: 1.45;
  width: 100%;
  transition: background .15s, border-color .15s, box-shadow .15s;
}

.example-btn:hover {
  background: #fff;
  border-color: var(--chat-blue);
  box-shadow: 0 0 0 1px var(--chat-blue);
}

.example-btn:focus-visible {
  outline: 2px solid var(--chat-blue);
  outline-offset: 2px;
}

/* New conversation button */
.chat-sidebar__new-btn {
  width: 100%;
  justify-content: center;
  flex-shrink: 0;
}

/* Conversation history */
.chat-sidebar__history {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.chat-sidebar__history li {
  display: flex;
  align-items: stretch;
  gap: 0;
}

.history-btn {
  flex: 1;
  min-width: 0;
  background: #fff;
  border: 1px solid #ddd;
  border-right: none;
  border-radius: var(--chat-radius) 0 0 var(--chat-radius);
  color: var(--chat-text);
  font-size: .8125rem;
  font-family: inherit;
  padding: .5rem .625rem;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: .125rem;
  overflow: hidden;
  transition: background .15s, border-color .15s;
}

.history-btn:hover {
  border-color: var(--chat-blue);
  background: #fff;
}

.history-btn--active {
  background: var(--chat-blue-pale);
  border-color: var(--chat-blue);
}

.history-btn:focus-visible {
  outline: 2px solid var(--chat-blue);
  outline-offset: -2px;
}

.history-btn__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.3;
}

.history-btn__date {
  font-size: .6875rem;
  color: var(--chat-text-muted);
}

.history-del {
  width: 28px;
  flex-shrink: 0;
  background: #fff;
  border: 1px solid #ddd;
  border-left: none;
  border-radius: 0 var(--chat-radius) var(--chat-radius) 0;
  color: #929292;
  font-size: .875rem;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color .15s, background .15s;
}

.history-del:hover {
  background: #fef2f2;
  color: #c62828;
}

/* =========================================================
   CHAT MAIN AREA
   ========================================================= */

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
}

/* --- Messages -------------------------------------------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.message {
  display: flex;
  gap: .875rem;
  padding: .75rem 1.5rem;
  max-width: 100%;
}

.message.assistant {
  background: var(--chat-grey-bg);
  border-left: 3px solid var(--chat-blue-pale);
}

.message.user {
  background: #fff;
  border-left: 3px solid transparent;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
}

.message.assistant .message-avatar {
  background: var(--chat-blue);
  color: #fff;
}

.message.user .message-avatar {
  background: #e5e5e5;
  color: #3a3a3a;
}

.message-avatar svg {
  width: 16px;
  height: 16px;
}

.message-body {
  flex: 1;
  min-width: 0;
}

/* --- Markdown content ------------------------------------ */
.message-content {
  font-size: .9375rem;
  line-height: 1.75;
  color: var(--chat-text);
}

.message-content p { margin-bottom: .5em; }
.message-content p:last-child { margin-bottom: 0; }

.message-content h1,
.message-content h2,
.message-content h3 {
  color: var(--chat-blue);
  margin: 1em 0 .35em;
  font-weight: 700;
}
.message-content h2 { font-size: 1rem; }
.message-content h3 { font-size: .9375rem; }

.message-content ul,
.message-content ol {
  padding-left: 1.4em;
  margin-bottom: .5em;
}

.message-content li { margin-bottom: .25em; }

.message-content code {
  font-family: var(--chat-font-mono);
  font-size: .84em;
  background: #eee;
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--chat-blue);
}

.message-content pre {
  background: #eee;
  border: 1px solid #ddd;
  border-radius: var(--chat-radius);
  padding: .75rem .875rem;
  overflow-x: auto;
  margin: .7em 0;
}

.message-content pre code {
  background: none;
  padding: 0;
  font-size: .82em;
}

.message-content blockquote {
  border-left: 3px solid #ccc;
  padding-left: .75rem;
  color: var(--chat-text-muted);
  font-style: italic;
  margin: .5em 0;
}

.message-content strong { color: var(--chat-blue); }

.message-content a {
  color: var(--chat-blue);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message-content a:hover { color: var(--chat-blue-hover); }

.message-content table {
  width: 100%;
  border-collapse: collapse;
  margin: .7em 0;
  font-size: .875em;
}

.message-content th,
.message-content td {
  padding: .375rem .625rem;
  border: 1px solid #ddd;
  text-align: left;
}

.message-content th {
  background: #eee;
  font-weight: 600;
  color: var(--chat-blue);
}

.message.user .message-content {
  white-space: pre-wrap;
  word-break: break-word;
}

/* --- Tool call indicator --------------------------------- */
.tool-indicator {
  display: inline-flex;
  align-items: center;
  gap: .4375rem;
  font-size: .8rem;
  color: var(--chat-blue);
  background: var(--chat-blue-pale);
  padding: .1875rem .625rem;
  border-radius: 20px;
  margin-bottom: .5rem;
  border: 1px solid rgba(0,0,145,.12);
}

.tool-indicator .spinner {
  width: 11px;
  height: 11px;
  border: 2px solid rgba(0,0,145,.2);
  border-top-color: var(--chat-blue);
  border-radius: 50%;
  animation: chat-spin .6s linear infinite;
  flex-shrink: 0;
}

@keyframes chat-spin {
  to { transform: rotate(360deg); }
}

/* --- Sources panel --------------------------------------- */
.sources-panel {
  margin-top: .75rem;
  padding: .625rem .875rem;
  background: var(--chat-blue-pale);
  border: 1px solid rgba(0,0,145,.15);
  border-radius: var(--chat-radius);
  font-size: .8125rem;
}

.sources-panel h4 {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--chat-blue);
  margin-bottom: .4375rem;
}

.sources-panel ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.sources-panel li::before {
  content: "\2192\00a0";
  color: var(--chat-blue);
}

.sources-panel a {
  color: var(--chat-blue);
  text-decoration: underline;
  word-break: break-all;
}

.sources-panel a:hover { color: var(--chat-blue-hover); }

/* --- Typing cursor --------------------------------------- */
.cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--chat-blue);
  vertical-align: text-bottom;
  animation: chat-blink .8s step-end infinite;
  margin-left: 1px;
}

@keyframes chat-blink {
  50% { opacity: 0; }
}

/* --- Error message --------------------------------------- */
.message.error {
  background: #fff4f4;
  border-left-color: #e53935;
}

.message.error .message-content {
  color: #c62828;
}

/* =========================================================
   INPUT AREA
   ========================================================= */

.input-area {
  border-top: 1px solid #e5e5e5;
  padding: .75rem 1.5rem .625rem;
  background: #fff;
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: .625rem;
}

.input-wrapper .fr-input {
  flex: 1;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  line-height: 1.5;
  border-radius: var(--chat-radius);
  padding: .5625rem .75rem;
  font-size: .9375rem;
}

.input-wrapper .fr-btn {
  width: 40px;
  height: 40px;
  min-height: 40px;
  flex-shrink: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.input-wrapper .fr-btn:disabled {
  opacity: .4;
  cursor: not-allowed;
}

.input-hint {
  font-size: .6875rem;
  color: #929292;
  margin-top: .3125rem;
  text-align: center;
}

.input-hint kbd {
  font-family: var(--chat-font-mono);
  background: #eee;
  border: 1px solid #ddd;
  border-radius: 3px;
  padding: 1px 4px;
  font-size: .65rem;
}

/* =========================================================
   SCROLLBARS
   ========================================================= */

.messages::-webkit-scrollbar,
.chat-sidebar::-webkit-scrollbar {
  width: 5px;
}

.messages::-webkit-scrollbar-thumb {
  background: #ddd;
  border-radius: 10px;
}

.chat-sidebar::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 10px;
}

/* =========================================================
   FOOTER – below the fold, visible on scroll
   ========================================================= */

.fr-footer {
  flex-shrink: 0;
}

/* =========================================================
   RESPONSIVE
   ========================================================= */

@media (max-width: 768px) {
  .chat-sidebar { display: none; }
  .message { padding: .625rem 1rem; }
  .messages { padding: .875rem 0; }
  .input-area { padding: .625rem .75rem .5rem; }
}

@media (max-width: 480px) {
  .fr-header__service-tagline { display: none; }
  .chat-operator-ministry { display: none; }
}
