:root {
  --bg: #07111f;
  --bg-grad-a: #1c3f68;
  --bg-grad-b: #07111f;
  --card: #10223ae8;
  --card-hover: #16304f;
  --line: #294561;
  --text: #edf6ff;
  --muted: #9db3cc;
  --blue: #3fb0ff;
  --blue-dark: #1b4a7a;
  --green: #35d583;
  --orange: #ff9e31;
  --red: #ff4f5e;
  --input-bg: #0b1a30;
  --header-bg: #0a1930cc;
  --row-hover: #ffffff08;
  --divider: #ffffff14;
}

:root[data-theme="light"] {
  --bg: #f3f6fb;
  --bg-grad-a: #dbe9fb;
  --bg-grad-b: #f3f6fb;
  --card: #ffffffef;
  --card-hover: #e9f2fe;
  --line: #d7e2ef;
  --text: #14263c;
  --muted: #5c7188;
  --blue: #1c7fe0;
  --blue-dark: #cfe6ff;
  --green: #12925a;
  --orange: #b9640c;
  --red: #cf2d3a;
  --input-bg: #ffffff;
  --header-bg: #ffffffdd;
  --row-hover: #00000007;
  --divider: #00000014;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg) radial-gradient(circle at 50% 0, var(--bg-grad-a) 0, var(--bg-grad-b) 55%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
}

a { color: var(--blue); }

header.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--header-bg);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
  gap: 10px;
}

.search-form { flex: 1 1 220px; max-width: 360px; }
.search-form input { width: 100%; }

header.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
}
header.topbar .brand-logo { height: 34px; width: auto; display: block; }
header.topbar .brand-text { display: flex; flex-direction: column; line-height: 1.2; }
header.topbar .brand-name { font-weight: 800; letter-spacing: 0.02em; }
header.topbar .brand-name span { color: var(--blue); }
header.topbar .brand-dept { font-size: 0.72rem; color: var(--muted); letter-spacing: 0.03em; text-transform: uppercase; }

header.topbar nav a {
  color: var(--muted);
  text-decoration: none;
  margin-left: 18px;
  font-size: 0.92rem;
}
header.topbar nav a:hover, header.topbar nav a.active { color: var(--text); }

main { max-width: 1180px; margin: 0 auto; padding: 28px 20px 60px; }

h1 { font-size: 1.6rem; margin: 0 0 6px; }
h2 { font-size: 1.15rem; margin: 0 0 12px; color: var(--text); }
p.sub { color: var(--muted); margin: 0 0 22px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
}

a.card-link { text-decoration: none; color: inherit; display: block; }
a.card-link .card { transition: border-color .15s, background .15s; overflow: hidden; }
a.card-link:hover .card { border-color: var(--blue); background: var(--card-hover); }

.card h3 { margin: 0 0 8px; font-size: 1.05rem; color: var(--text); }
.card .meta { color: var(--muted); font-size: 0.85rem; }

.project-cover {
  display: block;
  width: calc(100% + 36px);
  height: 140px;
  object-fit: cover;
  margin: -18px -18px 12px;
}
.project-cover-placeholder {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--blue-dark), var(--card-hover));
  color: var(--blue); font-weight: 800; letter-spacing: 0.06em; font-size: 1.4rem;
}
.project-desc { color: var(--muted); font-size: 0.85rem; margin: 8px 0 0; line-height: 1.4; }
.project-desc .read-more { color: var(--blue); font-weight: 600; white-space: nowrap; }

.view-toggle { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.view-toggle button { background: transparent; border: none; color: var(--muted); padding: 7px 12px; cursor: pointer; font-size: 1rem; line-height: 1; }
.view-toggle button.active { background: var(--blue); color: #051325; }

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.badge.servante { background: #1b4a7a; color: #a7d3ff; }
.badge.mcmob { background: #1b4a2f; color: #a0ffcb; }
/* Catégorie libre non reconnue (nouvelle catégorie créée depuis l'admin) */
.badge:not(.servante):not(.mcmob) { background: #3a2f56; color: #d6c4ff; }

table { width: 100%; border-collapse: collapse; }
table th, table td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--divider); font-size: 0.92rem; }
table th { color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: .03em; }
table tr:hover td { background: var(--row-hover); }

.breadcrumbs { margin-bottom: 16px; color: var(--muted); font-size: 0.9rem; }
.breadcrumbs a { text-decoration: none; }

.file-row { display: flex; align-items: center; gap: 10px; }
.file-icon { width: 20px; text-align: center; opacity: 0.8; }

.btn {
  display: inline-block;
  background: var(--blue);
  color: #051325;
  font-weight: 700;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 0.88rem;
  text-decoration: none;
}
.btn:hover { filter: brightness(1.1); }
.btn.secondary { background: transparent; color: var(--blue); border: 1px solid var(--blue); }
.btn.danger { background: var(--red); color: #2a0006; }
.btn.small { padding: 4px 10px; font-size: 0.78rem; }

form.inline { display: inline; }

input[type=text], input[type=password], input[type=search], input[type=file],
input[type=datetime-local], select, textarea {
  background: var(--input-bg);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 0.92rem;
  width: 100%;
}

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 340px; }
.login-card h1 { text-align: center; }
.login-card .field { margin-bottom: 14px; }
.login-card label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 0.85rem; }
.error-msg { color: var(--red); background: color-mix(in srgb, var(--red) 14%, transparent); border: 1px solid color-mix(in srgb, var(--red) 45%, transparent); border-radius: 8px; padding: 8px 12px; margin-bottom: 14px; font-size: 0.88rem; }

.ok { color: var(--green); font-weight: 700; }
.bad { color: var(--red); font-weight: 700; }
.warn { color: var(--orange); font-weight: 700; }

.preview-modal-backdrop {
  position: fixed; inset: 0; background: #000a; display: none;
  align-items: center; justify-content: center; z-index: 100; padding: 30px;
}
.preview-modal-backdrop.open { display: flex; }
.preview-modal {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  width: min(1000px, 100%); max-height: 90vh; display: flex; flex-direction: column;
}
.preview-modal header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.preview-modal .body { padding: 12px; overflow: auto; flex: 1; display: flex; align-items: center; justify-content: center; }
.preview-modal img { max-width: 100%; max-height: 75vh; border-radius: 6px; }
.preview-modal iframe { width: 100%; height: 75vh; border: 0; border-radius: 6px; background: #fff; }
#viewer3d { width: 100%; height: 75vh; border-radius: 6px; }
.close-x { cursor: pointer; color: var(--muted); font-size: 1.2rem; background: none; border: none; }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tabs button { background: transparent; border: 1px solid var(--line); color: var(--muted); padding: 7px 14px; border-radius: 8px; cursor: pointer; }
.tabs button.active { background: var(--blue); color: #051325; border-color: var(--blue); font-weight: 700; }

.stat-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--divider); font-size: 0.92rem; }
.stat-row .label { color: var(--muted); }

#historyChart { width: 100%; max-height: 340px; }

.monitor-panel canvas { width: 100%; max-height: 360px; margin-top: 10px; }
.field-picker { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 10px 0; }
.field-picker label { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--text); cursor: pointer; width: auto; }
.field-picker input[type=checkbox] { width: auto; accent-color: var(--blue); }
#liveFrame { color-scheme: dark; }

/* Outils de calcul */
.tool-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-bottom: 18px; }
.tool-mode { display: flex; gap: 8px; margin-bottom: 18px; flex-wrap: wrap; }
.result-box {
  background: color-mix(in srgb, var(--blue) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--blue) 35%, transparent);
  border-radius: 12px; padding: 16px 18px; margin-top: 18px;
}
.result-box .result-value { font-size: 1.7rem; font-weight: 800; color: var(--text); }
.result-box .result-unit { font-size: 1rem; color: var(--muted); font-weight: 600; margin-left: 4px; }
.result-box .result-detail { color: var(--muted); font-size: 0.85rem; margin-top: 6px; }
.verdict-badge {
  display: inline-block; padding: 4px 12px; border-radius: 999px; font-weight: 700; font-size: 0.82rem; margin-top: 8px;
}
.verdict-ok { background: color-mix(in srgb, var(--green) 18%, transparent); color: var(--green); }
.verdict-bad { background: color-mix(in srgb, var(--red) 18%, transparent); color: var(--red); }
.formula-note { color: var(--muted); font-size: 0.8rem; margin-top: 14px; border-top: 1px solid var(--divider); padding-top: 10px; line-height: 1.5; }
.tool-form .field { margin-bottom: 12px; }
.tool-form label { display: block; margin-bottom: 4px; color: var(--muted); font-size: 0.85rem; }

/* Chatbot */
.chat-widget { position: fixed; right: 22px; bottom: 22px; z-index: 90; }
.chat-bubble {
  width: 54px; height: 54px; border-radius: 50%; border: none; cursor: pointer;
  background: var(--blue); color: #051325; font-size: 1.5rem;
  box-shadow: 0 6px 18px #0006; display: flex; align-items: center; justify-content: center;
}
.chat-bubble:hover { filter: brightness(1.08); }
.chat-panel {
  position: absolute; right: 0; bottom: 66px; width: min(340px, 90vw); height: 440px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  flex-direction: column; box-shadow: 0 10px 30px #0008; overflow: hidden;
}
/* display géré en JS (style.display) : le panneau doit être un flex column
   uniquement quand il est affiché, jamais "display:flex" fixe ici — sinon
   ça écrase l'attribut "hidden"/la fermeture du panneau. */
.chat-panel header { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.chat-log { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.chat-msg { max-width: 85%; padding: 8px 12px; border-radius: 12px; font-size: 0.87rem; line-height: 1.4; white-space: pre-wrap; }
.chat-msg-user { align-self: flex-end; background: var(--blue); color: #051325; }
.chat-msg-assistant { align-self: flex-start; background: var(--input-bg); border: 1px solid var(--line); color: var(--text); }
.chat-msg-pending { opacity: 0.6; }
.chat-msg strong { font-weight: 700; }
.chat-msg code { background: #00000022; border-radius: 4px; padding: 1px 5px; font-size: 0.85em; font-family: ui-monospace, Consolas, monospace; }
.chat-msg-user code { background: #ffffff33; }
.chat-msg pre { background: #00000022; border-radius: 8px; padding: 8px 10px; overflow-x: auto; white-space: pre; margin: 4px 0; }
.chat-msg pre code { background: none; padding: 0; }
.chat-msg p { margin: 0 0 8px; }
.chat-msg p:last-child { margin-bottom: 0; }
.chat-msg ul, .chat-msg ol { margin: 0 0 8px; padding-left: 20px; }
.chat-msg li { margin-bottom: 2px; }
.chat-msg .chat-h { font-weight: 800; margin: 4px 0 6px; line-height: 1.3; }
.chat-msg .chat-h1 { font-size: 1.15em; }
.chat-msg .chat-h2 { font-size: 1.08em; }
.chat-msg .chat-h3, .chat-msg .chat-h4 { font-size: 1em; text-decoration: underline; }
.chat-msg .chat-math-block { overflow-x: auto; margin: 8px 0; text-align: center; }
.chat-msg .katex { font-size: 1.02em; }
.chat-input-row { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.chat-input-row input { flex: 1; }

/* Firmware / OTA */
.job-log {
  background: #05101f; color: #b9e4ff; border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-family: ui-monospace, Consolas, monospace; font-size: 0.78rem;
  max-height: 300px; overflow: auto; white-space: pre-wrap; margin-top: 10px;
}
.code-viewer {
  background: #05101f; border: 1px solid var(--line); border-radius: 8px;
  max-height: 480px; overflow: auto; font-family: ui-monospace, Consolas, monospace; font-size: 0.8rem;
}
.code-line { display: flex; padding: 0 8px; }
.code-line:hover { background: #ffffff08; }
.code-line .line-no { flex: 0 0 44px; text-align: right; margin-right: 12px; color: #45607f; user-select: none; }
.code-line .line-content { white-space: pre; color: #cfe6ff; }
.code-line.review-info { background: color-mix(in srgb, var(--blue) 12%, transparent); }
.code-line.review-attention { background: color-mix(in srgb, var(--orange) 16%, transparent); }
.code-line.review-important { background: color-mix(in srgb, var(--red) 18%, transparent); }

/* --- Notifications (cloche + panneau + popup) --- */
.notif-wrap { position: relative; margin-left: 14px; display: inline-block; }
.notif-bell { position: relative; }
.notif-bell.has-unread { animation: notif-ring 2.4s ease-in-out 1; }
@keyframes notif-ring {
  0%, 100% { transform: rotate(0); }
  10%, 30% { transform: rotate(-12deg); }
  20%, 40% { transform: rotate(12deg); }
  50% { transform: rotate(0); }
}
.notif-badge {
  position: absolute; top: -6px; right: -6px;
  background: var(--red, #e5484d); color: #fff;
  font-size: .62rem; font-weight: 700; line-height: 1;
  padding: 3px 5px; border-radius: 10px; min-width: 10px; text-align: center;
}
.notif-panel {
  position: absolute; right: 0; top: calc(100% + 10px);
  width: 360px; max-width: 92vw; z-index: 300;
  background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: 0 18px 50px #00000055; overflow: hidden;
}
.notif-panel-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
}
.notif-list { max-height: 420px; overflow-y: auto; }
.notif-item {
  display: flex; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--line); text-decoration: none; color: inherit;
}
.notif-item:hover { background: var(--card-hover); }
.notif-item.unread { border-left: 3px solid var(--blue); background: color-mix(in srgb, var(--blue) 7%, transparent); }
.notif-item-icon { font-size: 1.15rem; flex-shrink: 0; }
.notif-item-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.notif-item-title { font-weight: 600; font-size: .88rem; line-height: 1.35; }
.notif-item-text { font-size: .8rem; color: var(--muted); line-height: 1.4; }
.notif-item-date { font-size: .72rem; color: var(--muted); margin-top: 2px; }
.notif-popup-overlay {
  position: fixed; inset: 0; background: #000000a0; z-index: 900;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.notif-popup {
  background: var(--card); color: var(--text); border: 1px solid var(--line);
  border-radius: 14px; width: 100%; max-width: 460px;
  box-shadow: 0 24px 70px #000000aa; overflow: hidden;
  animation: notif-popup-in .22s ease-out;
}
@keyframes notif-popup-in { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }
.notif-popup-head {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 18px; border-bottom: 1px solid var(--line); font-size: 1rem;
}
.notif-popup-head .close-x { margin-left: auto; }
.notif-popup-list { max-height: 50vh; overflow-y: auto; }
.notif-popup-foot { padding: 12px 18px; border-top: 1px solid var(--line); text-align: right; }

/* --- Modale de partage de document en lecture --- */
.docshare-overlay {
  position: fixed; inset: 0; background: #000000a0; z-index: 900;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.docshare-modal {
  background: var(--card); color: var(--text); border: 1px solid var(--line);
  border-radius: 14px; width: 100%; max-width: 440px; padding: 20px 22px;
  box-shadow: 0 24px 70px #000000aa;
}
