:root {
  --ink-950: #070708;
  --ink-900: #0b0c0f;
  --ink-800: #101218;
  --ink-700: #181b22;
  --ink-600: #222631;
  --text: #e8ecf2;
  --muted: #9aa4b2;
  --brand: #10b981;
  --orange: #f68a1e;
  --gold: #fbbf24;
  --red: #ef4444;
  --radius: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { height: 100dvh; }
body {
  background: var(--ink-950);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
  display: flex;
  flex-direction: column;
}
button, .button, select, input, textarea {
  font: inherit;
  color: var(--text);
  background: var(--ink-700);
  border: 1px solid var(--ink-600);
  border-radius: 8px;
  padding: 7px 12px;
}
button, .button { cursor: pointer; text-decoration: none; }
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: default; }
.primary { background: var(--orange); border-color: var(--orange); color: #111; font-weight: 600; }
.danger { border-color: var(--red); color: var(--red); background: transparent; }
.ok { border-color: var(--brand); color: var(--brand); background: transparent; }

header {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--ink-700);
  background: var(--ink-900);
}
.brand { font-size: 16px; display: flex; align-items: center; gap: 8px; }
.brand b { color: var(--orange); }
.logo {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--brand), var(--orange)); color: #111; font-weight: 800;
}
nav { display: flex; gap: 4px; flex: 1; }
nav button { background: transparent; border-color: transparent; color: var(--muted); }
nav button.active { color: var(--text); background: var(--ink-700); }
.badge { color: var(--muted); font-size: 12px; }

.tab { display: none; flex: 1; min-height: 0; overflow: auto; padding: 16px 20px; }
.tab.active { display: block; }
#tab-chat.active { display: flex; padding: 0; overflow: hidden; }

.sidebar { width: 260px; border-right: 1px solid var(--ink-700); padding: 12px; overflow: auto; background: var(--ink-900); }
.sidebar ul { list-style: none; padding: 0; margin: 12px 0 0; }
.sidebar li { padding: 7px 8px; border-radius: 8px; cursor: pointer; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.sidebar li:hover, .sidebar li.active { background: var(--ink-700); color: var(--text); }
.sidebar li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar li .del { visibility: hidden; color: var(--muted); }
.sidebar li:hover .del { visibility: visible; }
#new-chat { width: 100%; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#messages { flex: 1; overflow: auto; padding: 20px 10% 10px; }
.msg { margin: 0 0 16px; max-width: 100%; }
.msg.user { background: var(--ink-700); padding: 10px 14px; border-radius: var(--radius); margin-left: 20%; }
.msg.assistant { padding: 2px 0; }
.msg.tool { font-size: 12px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; padding-left: 10px; border-left: 2px solid var(--ink-600); white-space: pre-wrap; word-break: break-all; }
.msg.tool.err { border-left-color: var(--red); }
.msg.thinking { font-size: 12px; color: var(--muted); font-style: italic; border-left: 2px solid var(--brand); padding-left: 10px; white-space: pre-wrap; max-height: 140px; overflow: auto; }
.msg.error { color: var(--red); }
.msg.meta { font-size: 11px; color: var(--muted); }
.msg table { border-collapse: collapse; margin: 8px 0; }
.msg th, .msg td { border: 1px solid var(--ink-600); padding: 4px 8px; text-align: left; }
.msg code { background: var(--ink-700); padding: 1px 5px; border-radius: 4px; }
.msg pre { background: var(--ink-800); padding: 10px; border-radius: 8px; overflow: auto; }
.empty { text-align: center; margin-top: 10vh; color: var(--muted); }
.empty h2 { color: var(--text); font-weight: 600; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; max-width: 760px; margin: 20px auto; }
.suggestions button { text-align: left; color: var(--muted); }
#composer { display: flex; gap: 8px; padding: 12px 10%; border-top: 1px solid var(--ink-700); }
#composer textarea { flex: 1; resize: none; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar input[type="text"], #course-filter { min-width: 240px; }
.status { color: var(--muted); margin-bottom: 10px; min-height: 20px; }
.hint { color: var(--muted); }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { border-bottom: 1px solid var(--ink-700); padding: 7px 8px; text-align: left; }
table.grid th { color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--ink-950); }
.bar { height: 6px; background: var(--ink-700); border-radius: 6px; overflow: hidden; width: 140px; display: inline-block; vertical-align: middle; }
.bar > i { display: block; height: 100%; background: var(--brand); }

.card { background: var(--ink-900); border: 1px solid var(--ink-700); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.card h3 { margin: 0 0 6px; font-size: 14px; }
.card .meta { color: var(--muted); font-size: 12px; }
.ar { direction: rtl; text-align: right; font-family: "Geeza Pro", "Scheherazade New", "Noto Naskh Arabic", serif; font-size: 22px; line-height: 1.9; margin: 8px 0; }
.diff { display: grid; grid-template-columns: 110px 1fr; gap: 4px 10px; margin: 8px 0; }
.diff .k { color: var(--muted); }
.diff .old { color: #fca5a5; text-decoration: line-through; text-decoration-color: rgba(252,165,165,.5); }
.diff mark { color: inherit; border-radius: 3px; padding: 0 2px; }
.diff mark.del { background: rgba(239, 68, 68, .28); color: #fca5a5; text-decoration: line-through; text-decoration-color: rgba(252,165,165,.7); }
.diff mark.add { background: rgba(16, 185, 129, .28); color: #86efac; font-weight: 600; }
.diff .ar-line { direction: rtl; text-align: right; font-family: "Geeza Pro", "Scheherazade New", "Noto Naskh Arabic", serif; font-size: 20px; line-height: 1.9; }
.diff .new { color: #86efac; }
.diff textarea { width: 100%; min-height: 40px; }
.sev { font-size: 11px; padding: 2px 8px; border-radius: 20px; border: 1px solid; margin-right: 6px; }
.sev.erreur { color: var(--red); }
.sev.doute { color: var(--gold); }
.sev.style { color: var(--muted); }
.actions { display: flex; gap: 8px; margin-top: 8px; }

/* Connexion */
.user { margin-left: auto; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.login { position: fixed; inset: 0; z-index: 100; background: var(--ink-950); display: grid; place-items: center; padding: 16px; }
.login[hidden], .user[hidden] { display: none; }
.login-box { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; background: var(--ink-900); border: 1px solid var(--ink-700); border-radius: var(--radius); padding: 24px; }
.login-box .brand { justify-content: center; margin-bottom: 4px; }
.login-box .hint { text-align: center; margin: 0; }
.login-error { color: var(--red); min-height: 1em; margin: 0; font-size: 13px; }

/* Transcription */
.tr-layout { display: grid; grid-template-columns: 1fr 260px; gap: 20px; align-items: start; }
.tr-main textarea { width: 100%; font-family: "Geeza Pro", "Scheherazade New", "Noto Naskh Arabic", serif; font-size: 22px; line-height: 1.8; margin: 8px 0; }
.tr-line .tr-text { font-size: 16px; color: #86efac; margin: 4px 0 6px; }
.tr-doubt { color: #fcd34d; font-size: 13px; margin-top: 6px; }
.tr-history h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
.tr-history ul { list-style: none; margin: 0; padding: 0; }
.tr-history li[data-id] { padding: 8px; border-radius: 6px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.tr-history li[data-id]:hover { background: var(--ink-800); }
.ar-mini { direction: rtl; text-align: right; font-family: "Geeza Pro", "Noto Naskh Arabic", serif; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 800px) { .tr-layout { grid-template-columns: 1fr; } }

/* Profil : bouton rond + fenêtre (ordinateur ; sur mobile, en-tête compact) */
.avatar {
  width: 34px; height: 34px; padding: 0; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--brand), var(--orange)); border: 0; color: #111; font-weight: 700;
}
.avatar.big { width: 48px; height: 48px; font-size: 20px; flex: 0 0 auto; }
#sync-badge, #user-email, #logout { display: none; }
.modal { position: fixed; inset: 0; z-index: 90; background: rgba(0, 0, 0, .55); display: flex; justify-content: flex-end; align-items: flex-start; padding: 60px 20px 20px; }
.modal[hidden] { display: none; }
.modal-box { position: relative; width: 340px; max-width: 100%; background: var(--ink-900); border: 1px solid var(--ink-700); border-radius: var(--radius); padding: 20px; box-shadow: 0 20px 50px rgba(0, 0, 0, .5); }
.modal-close { position: absolute; top: 10px; right: 10px; background: transparent; border-color: transparent; color: var(--muted); padding: 4px 8px; }
.profile-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; padding-right: 24px; }
.profile-email { font-weight: 600; word-break: break-all; }
.profile-info { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 18px; font-size: 13px; }
.profile-info dt { color: var(--muted); }
.profile-info dd { margin: 0; }
#profile-logout { width: 100%; }

/* --- Mobile ---------------------------------------------------------------- */
.chat-bar, .backdrop, .ico { display: none; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (hover: none) {
  .sidebar li .del { visibility: visible; }
}

@media (max-width: 760px) {
  /* iOS zoome sur les champs < 16 px */
  input, textarea, select { font-size: 16px; }
  button, .button { min-height: 40px; }

  header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px 10px;
    padding: 8px 12px 0;
    padding-top: max(8px, env(safe-area-inset-top));
  }
  .brand { grid-column: 1; grid-row: 1; }
  .user { grid-column: 2; grid-row: 1; margin-left: 0; }
  #user-email, .user .lbl, #profile-btn { display: none; }
  #sync-badge, #logout { display: block; }
  #logout { display: inline-flex; align-items: center; }
  .user .ico { display: inline; font-size: 16px; }
  .badge { grid-column: 1 / -1; grid-row: 2; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  nav {
    grid-column: 1 / -1; grid-row: 3;
    min-width: 0; overflow-x: auto; scrollbar-width: none; margin: 0 -12px; padding: 0 12px 6px;
  }
  nav::-webkit-scrollbar { display: none; }
  nav button { flex: 0 0 auto; padding: 6px 12px; }

  body { overflow-x: hidden; }
  .tab { padding: 12px; min-width: 0; }

  /* Assistant : liste des conversations en tiroir */
  #tab-chat { position: relative; }
  .chat-bar { display: flex; padding: 8px 12px; border-bottom: 1px solid var(--ink-700); }
  .chat-bar button { min-height: 34px; padding: 4px 12px; }
  .sidebar {
    position: absolute; z-index: 20; top: 0; bottom: 0; left: 0;
    width: min(300px, 85vw); transform: translateX(-100%); transition: transform .2s ease;
  }
  #tab-chat.conv-open .sidebar { transform: none; }
  #tab-chat.conv-open .backdrop { display: block; position: absolute; inset: 0; z-index: 10; background: rgba(0, 0, 0, .55); }
  #messages { padding: 12px; }
  .msg.user { margin-left: 8%; }
  .msg table { display: block; overflow-x: auto; }
  .empty { margin-top: 4vh; }
  .empty h2 { font-size: 18px; }
  .suggestions { grid-template-columns: 1fr; }
  #composer { padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  #composer textarea { min-height: 44px; }

  /* Vérification */
  .toolbar input[type="text"], #course-filter { min-width: 0; width: 100%; }
  .toolbar > button { flex: 1 1 auto; }
  table.grid { white-space: nowrap; font-size: 13px; }
  table.grid td:nth-child(2) { white-space: normal; min-width: 160px; }
  .bar { width: 80px; }

  /* Corrections : étiquettes au-dessus des valeurs */
  .card { padding: 12px; }
  .card h3 { font-size: 13px; line-height: 1.6; }
  .diff { grid-template-columns: 1fr; gap: 2px; }
  .diff .k { font-size: 12px; margin-top: 6px; }
  .ar { font-size: 20px; }
  .actions { flex-wrap: wrap; }
  .actions button { flex: 1 1 auto; }

  /* Transcription */
  .tr-main textarea { font-size: 20px; height: 150px; }
}
