:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e3e6ec;
  --text: #1a1f2c;
  --text-soft: #5b6473;
  --primary: #1f4e78;
  --primary-hover: #163a5a;
  --primary-soft: #e8eef5;
  --accent: #c47a3d;
  --user-bubble: #1f4e78;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #f0f2f6;
  --error: #c0392b;
  --success: #27ae60;
  --warn: #d68910;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 14px; }
[hidden] { display: none !important; }

/* App layout: header / left-rail / sidebar / main */
#app {
  display: grid;
  grid-template-columns: 56px 280px 1fr;
  grid-template-rows: 52px 1fr;
  grid-template-areas:
    "header header header"
    "rail   sidebar main";
  height: 100vh;
}

header.topbar {
  grid-area: header;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}
header.topbar h1 { margin: 0; font-size: 15px; font-weight: 600; }
header.topbar .subtitle { font-size: 11.5px; opacity: 0.85; margin-left: 10px; }
header.topbar .icon-btn {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.3);
  color: white;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 12.5px;
}
header.topbar .icon-btn:hover { background: rgba(255,255,255,0.1); }
header.topbar .right-cluster { display: flex; gap: 8px; align-items: center; }

/* Left rail (vertical icons for view switching) */
.rail {
  grid-area: rail;
  background: var(--primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0;
  gap: 4px;
}
.rail button {
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.75);
  font-size: 18px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.rail button:hover { background: rgba(255,255,255,0.1); color: white; }
.rail button.active { background: rgba(255,255,255,0.2); color: white; }
.rail button[title]::after {
  content: attr(title);
  position: absolute;
  left: 100%;
  margin-left: 8px;
  background: #1a1f2c;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11.5px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  z-index: 200;
}
.rail button:hover[title]::after { opacity: 1; }

/* Sidebar */
aside.sidebar {
  grid-area: sidebar;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.section-title {
  font-size: 10.5px;
  text-transform: uppercase;
  color: var(--text-soft);
  font-weight: 600;
  letter-spacing: 0.6px;
  margin: 4px 0 6px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.section-title button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 11px;
  color: var(--text-soft);
}
.section-title button:hover { background: var(--primary-soft); color: var(--primary); border-color: var(--primary); }

.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.quick-action {
  padding: 10px 8px;
  background: var(--primary-soft);
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--primary);
  font-weight: 500;
  font-size: 11.5px;
  text-align: left;
  line-height: 1.25;
}
.quick-action:hover { background: #d8e2ee; border-color: var(--primary); }
.quick-action .qa-emoji { display: block; font-size: 16px; margin-bottom: 3px; }

.conversation-list { display: flex; flex-direction: column; gap: 2px; }
.conv-item {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border: 1px solid transparent;
}
.conv-item:hover { background: #f0f2f6; }
.conv-item.active { background: var(--primary-soft); border-color: var(--primary); }
.conv-item .title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .meta { font-size: 11px; color: var(--text-soft); display: flex; gap: 6px; align-items: center; }
.conv-item .kind-badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 8px;
  background: white;
  color: var(--text-soft);
  font-size: 10px;
  border: 1px solid var(--border);
}
.conv-item .kind-badge.programmation { background: #fff4e6; border-color: #ffd9a8; color: #b15e00; }
.conv-item .kind-badge.fiche { background: #e8f3ff; border-color: #b8d8ff; color: #0a4d99; }
.conv-item .kind-badge.email { background: #f0e8ff; border-color: #c8b8ff; color: #5e2db5; }
.conv-item .kind-badge.referentiel { background: #e8f5ee; border-color: #a8dbb8; color: #2a7d3f; }

.file-drop {
  border: 2px dashed var(--border);
  border-radius: 6px;
  padding: 12px;
  text-align: center;
  color: var(--text-soft);
  cursor: pointer;
  font-size: 12px;
}
.file-drop:hover, .file-drop.drag-over {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}
.file-drop input { display: none; }
.file-list { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; }
.file-chip {
  display: flex;
  align-items: center;
  background: #f9fafc;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 11.5px;
}
.file-chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.file-chip .size { color: var(--text-soft); margin: 0 6px; font-size: 10.5px; }
.file-chip .remove { background: transparent; border: none; color: var(--error); font-size: 14px; padding: 0 4px; line-height: 1; }

.btn-block {
  width: 100%;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 8px;
  font-weight: 500;
  font-size: 13px;
}
.btn-block:hover:not(:disabled) { background: var(--primary-hover); }
.btn-block.outline { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn-block.outline:hover:not(:disabled) { background: #f0f2f6; }
.btn-block.danger { background: transparent; color: var(--error); border: 1px solid #f5c6c0; }
.btn-block.danger:hover:not(:disabled) { background: #fdf0ee; }

/* Main area — different views toggle visibility */
main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.view { display: none; flex: 1; flex-direction: column; overflow: hidden; }
.view.active { display: flex; }
.view-header {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.view-header h2 { margin: 0; font-size: 16px; font-weight: 600; }
.view-body { flex: 1; overflow-y: auto; padding: 16px 24px; }

/* Chat */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-soft);
  max-width: 480px;
}
.empty-state h2 { color: var(--text); margin-bottom: 6px; font-weight: 600; }

.msg { display: flex; gap: 10px; }
.msg .avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600;
  flex-shrink: 0;
}
.msg.user .avatar { background: var(--accent); }
.msg.user { flex-direction: row-reverse; }
.msg .bubble {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--assistant-bubble);
  overflow-wrap: anywhere;
  max-width: calc(100% - 50px);
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-bubble-text); white-space: pre-wrap; }

.bubble.markdown p { margin: 0 0 0.5em 0; }
.bubble.markdown p:last-child { margin-bottom: 0; }
.bubble.markdown h1, .bubble.markdown h2, .bubble.markdown h3, .bubble.markdown h4 { margin: 0.7em 0 0.3em 0; font-weight: 600; }
.bubble.markdown h1 { font-size: 17px; }
.bubble.markdown h2 { font-size: 15.5px; }
.bubble.markdown h3 { font-size: 14px; }
.bubble.markdown ul, .bubble.markdown ol { margin: 0.3em 0 0.5em 1.4em; padding: 0; }
.bubble.markdown li { margin: 0.15em 0; }
.bubble.markdown table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 12px;
  width: 100%;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.bubble.markdown th, .bubble.markdown td {
  border: 1px solid var(--border);
  padding: 5px 8px;
  text-align: left;
  vertical-align: top;
}
.bubble.markdown th { background: var(--primary-soft); font-weight: 600; }
.bubble.markdown code {
  background: rgba(0,0,0,0.05);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}
.bubble.markdown pre {
  background: #1e2530;
  color: #e6e8eb;
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
}
.bubble.markdown pre code { background: transparent; color: inherit; padding: 0; }
.bubble.markdown blockquote {
  margin: 0.5em 0;
  padding: 5px 12px;
  border-left: 3px solid var(--primary);
  color: var(--text-soft);
  background: rgba(31, 78, 120, 0.04);
}
.bubble.markdown a { color: var(--primary); }

.typing { display: inline-flex; gap: 3px; align-items: center; height: 18px; }
.typing span { width: 5px; height: 5px; border-radius: 50%; background: var(--text-soft); animation: bounce 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

.composer {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 12px 28px;
}
.composer-inner {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
}
.composer-inner:focus-within { border-color: var(--primary); }
.composer textarea {
  flex: 1;
  border: none;
  resize: none;
  outline: none;
  padding: 7px 10px;
  max-height: 200px;
  background: transparent;
  line-height: 1.5;
}
.send-btn {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-weight: 500;
  height: 32px;
}
.send-btn:hover:not(:disabled) { background: var(--primary-hover); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.composer-meta {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
  font-size: 11px;
  color: var(--text-soft);
}
.status-pill {
  padding: 1px 8px;
  border-radius: 8px;
  background: #f0f2f6;
}
.status-pill.ok { background: #e8f5ee; color: var(--success); }
.status-pill.warn { background: #fdf3dc; color: var(--warn); }
.status-pill.err { background: #fdf0ee; color: var(--error); }

/* Calendar */
.calendar-toolbar { display: flex; align-items: center; gap: 12px; }
.calendar-toolbar button { padding: 6px 12px; border: 1px solid var(--border); border-radius: 6px; background: white; }
.calendar-toolbar button:hover { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.calendar-toolbar .month-label { font-size: 16px; font-weight: 600; min-width: 200px; text-align: center; }
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.calendar-grid .head {
  background: var(--primary-soft);
  padding: 8px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  color: var(--primary);
}
.calendar-cell {
  background: white;
  min-height: 96px;
  padding: 6px;
  font-size: 12px;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.calendar-cell.outside { background: #fafbfd; color: var(--text-soft); }
.calendar-cell.weekend { background: #fbfcfe; }
.calendar-cell.today { box-shadow: inset 0 0 0 2px var(--primary); }
.calendar-cell .day-num { font-weight: 600; font-size: 12.5px; }
.calendar-event {
  background: var(--primary-soft);
  color: var(--primary);
  padding: 2px 5px;
  border-radius: 3px;
  font-size: 11px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-left: 3px solid var(--primary);
}
.calendar-event.theatre_verdure { background: #fff4e6; color: #b15e00; border-left-color: #f59e0b; }
.calendar-event.salle_cinema { background: #e8f3ff; color: #0a4d99; border-left-color: #2563eb; }
.calendar-event.petit_theatre { background: #f0e8ff; color: #5e2db5; border-left-color: #8b5cf6; }
.calendar-event.parvis { background: #e8f5ee; color: #2a7d3f; border-left-color: #10b981; }
.calendar-event.externe { background: #f5f5f5; color: var(--text-soft); border-left-color: #94a3b8; }

/* Reference data tables */
.ref-tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.ref-tabs button {
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: white;
  border-radius: 6px;
  font-size: 13px;
}
.ref-tabs button.active { background: var(--primary); color: white; border-color: var(--primary); }
.ref-table { width: 100%; border-collapse: collapse; font-size: 12.5px; background: white; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.ref-table th { background: var(--primary-soft); color: var(--primary); padding: 8px 10px; text-align: left; font-weight: 600; font-size: 11.5px; }
.ref-table td { padding: 6px 10px; border-top: 1px solid var(--border); }
.ref-table tr:hover { background: #fbfbfd; }
.ref-table input, .ref-table select { width: 100%; border: 1px solid transparent; padding: 4px 6px; background: transparent; }
.ref-table input:focus, .ref-table select:focus { background: white; border-color: var(--primary); border-radius: 4px; outline: none; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 20, 30, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal {
  background: white;
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 480px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}
.modal h2 { margin: 0 0 8px 0; font-size: 17px; }
.modal label { display: block; font-size: 12.5px; font-weight: 600; margin: 12px 0 4px; }
.modal input, .modal select, .modal textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
}
.modal input:focus, .modal select:focus, .modal textarea:focus { border-color: var(--primary); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.btn-primary { background: var(--primary); color: white; border: none; padding: 8px 16px; border-radius: 6px; font-weight: 500; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: transparent; color: var(--text); border: 1px solid var(--border); padding: 8px 16px; border-radius: 6px; }
.btn-secondary:hover { background: #f0f2f6; }

.alert-error {
  margin-top: 10px;
  padding: 10px 12px;
  background: #fdf0ee;
  color: var(--error);
  border: 1px solid #f5c6c0;
  border-radius: 6px;
  font-size: 13px;
}

/* Mobile sidebar toggle */
.menu-toggle { display: none; }
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 52px 0 0 0;
  background: rgba(15, 20, 30, 0.4);
  z-index: 90;
}

/* Responsive */
@media (max-width: 900px) {
  #app { grid-template-columns: 56px 1fr; grid-template-areas: "header header" "rail main"; }
  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid rgba(255,255,255,0.3);
    color: white;
    width: 36px;
    height: 32px;
    border-radius: 6px;
    font-size: 18px;
    margin-right: 8px;
  }
  .menu-toggle:hover { background: rgba(255,255,255,0.1); }
  aside.sidebar {
    position: fixed;
    top: 52px;
    bottom: 0;
    left: 56px;
    width: 280px;
    max-width: 75vw;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.22s ease-out;
    box-shadow: 2px 0 12px rgba(0,0,0,0.12);
  }
  aside.sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.active { display: block; }
  .messages, .composer { padding-left: 16px; padding-right: 16px; }
  header.topbar { padding: 0 12px; }
  header.topbar h1 { font-size: 14px; }
  .hide-mobile { display: none !important; }
  .calendar-cell { min-height: 64px; padding: 4px; font-size: 11px; }
  .calendar-cell .day-num { font-size: 11.5px; }
  .calendar-event { font-size: 10px; padding: 1px 3px; }
}

@media (max-width: 480px) {
  /* On phones, completely hide non-active rail buttons would be too much,
     keep the rail compact */
  .rail button { width: 36px; height: 36px; font-size: 16px; }
}
