/* =====================================================================
   Islami — feuille de style globale (mobile-first)
   Palette : vert islamique + or + sable
   Light + Dark mode auto
   ===================================================================== */

/* ---- Tokens ---- */
:root {
  --c-primary: #0f5132;
  --c-primary-2: #146c43;
  --c-primary-dark: #0a3622;
  --c-accent: #c89b3c;
  --c-accent-2: #b8881e;

  --c-bg: #f6f1e7;
  --c-bg-2: #fdfaf2;
  --c-card: #ffffff;
  --c-line: #e6e1d5;
  --c-ink: #1a1a1a;
  --c-ink-2: #3b3f42;
  --c-muted: #6b6f72;
  --c-muted-2: #9ba0a4;

  --c-err: #b42318;
  --c-err-bg: #fef3f2;
  --c-err-line: #fecdca;
  --c-ok:  #027a48;
  --c-ok-bg: #ecfdf3;
  --c-ok-line: #a6f4c5;
  --c-warn: #b54708;
  --c-warn-bg: #fffaeb;
  --c-warn-line: #fedf89;
  --c-info: #175cd3;
  --c-info-bg: #eff8ff;
  --c-info-line: #b2ddff;

  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --r-xl: 18px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow:    0 1px 2px rgba(0,0,0,.05), 0 4px 12px rgba(0,0,0,.06);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.10), 0 24px 48px rgba(0,0,0,.10);

  --topbar-h: 56px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-bg: #14181a;
    --c-bg-2: #1a1f22;
    --c-card: #1f262a;
    --c-line: #2c3438;
    --c-ink: #ecedee;
    --c-ink-2: #c9cdcf;
    --c-muted: #8a9094;
    --c-muted-2: #6a6f73;
    --c-err-bg:  #2c0f0d;  --c-err-line: #5c1f1a;
    --c-ok-bg:   #07261c;  --c-ok-line:  #145239;
    --c-warn-bg: #2a1f06;  --c-warn-line:#5b3d05;
    --c-info-bg: #0c1f3a;  --c-info-line:#1d3e74;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
    --shadow:    0 1px 2px rgba(0,0,0,.5), 0 4px 12px rgba(0,0,0,.4);
    --shadow-lg: 0 8px 24px rgba(0,0,0,.55), 0 24px 48px rgba(0,0,0,.5);
  }
}

/* ---- Reset minimal ---- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--c-bg);
  color: var(--c-ink);
  line-height: 1.5;
  font-size: 16px;
  min-height: 100vh;
}
img { max-width: 100%; height: auto; display: block; }
a { color: var(--c-primary-2); text-decoration: none; }
a:hover { text-decoration: underline; }
hr { border: 0; border-top: 1px solid var(--c-line); margin: 1rem 0; }
code, pre {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: .9em;
  background: var(--c-bg-2);
  border: 1px solid var(--c-line);
  border-radius: var(--r-sm);
  padding: .1em .4em;
}
pre { padding: .8rem 1rem; overflow-x: auto; }
pre code { background: none; border: 0; padding: 0; }

/* ============== TOPBAR ============== */
.topbar {
  display: flex; align-items: center; gap: .6rem;
  height: var(--topbar-h);
  padding: 0 .8rem;
  background: var(--c-primary);
  color: #fff;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 100;
  padding-top: env(safe-area-inset-top);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
}
.topbar a, .topbar button { color: #fff; }
.topbar .brand {
  font-weight: 700; font-size: 1.05rem;
  display: inline-flex; align-items: center; gap: .35rem;
  white-space: nowrap;
}
.topbar .brand:hover { text-decoration: none; }
.topbar .brand span { display: none; }
@media (min-width: 480px) { .topbar .brand span { display: inline; } }

.hamburger {
  width: var(--tap); height: var(--tap);
  background: transparent; border: 0; padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  cursor: pointer;
}
.hamburger span { display: block; width: 22px; height: 2px; background: #fff; border-radius: 2px; }

.topnav {
  display: none;
  flex: 1;
  gap: .15rem;
  margin-left: 1rem;
}
@media (min-width: 1024px) { .topnav { display: flex; } }
.topnav .navlink {
  padding: .5rem .75rem; border-radius: 6px; font-size: .92rem;
  display: inline-flex; align-items: center; gap: .35rem;
}
.topnav .navlink:hover { background: rgba(255,255,255,.12); text-decoration: none; }
.topnav .navlink.active { background: rgba(255,255,255,.18); }
.navlink .emo { font-size: 1em; }

.topright {
  margin-left: auto;
  display: flex; align-items: center; gap: .5rem;
}

/* ---- Balance widget ---- */
.balance {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .35rem .65rem;
  border: 1px solid rgba(255,255,255,.35);
  border-radius: 999px;
  font-size: .85rem; font-weight: 600;
  white-space: nowrap;
  min-height: 32px;
}
.balance:hover { background: rgba(255,255,255,.12); text-decoration: none; }
.balance.warn { background: rgba(255,178,30,.25); border-color: rgba(255,178,30,.6); }
.balance.danger { background: rgba(255,90,76,.3); border-color: rgba(255,90,76,.7); animation: pulse 2s infinite; }
.balance .balance-value { font-variant-numeric: tabular-nums; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,90,76,.5); }
  50%      { box-shadow: 0 0 0 6px rgba(255,90,76,0); }
}

/* ---- User menu ---- */
.usermenu { position: relative; }
.user-btn {
  display: inline-flex; align-items: center; gap: .5rem;
  background: transparent; border: 1px solid rgba(255,255,255,.35);
  border-radius: 999px; padding: .25rem .65rem .25rem .25rem;
  cursor: pointer; min-height: 36px;
  font: inherit;
}
.user-btn:hover { background: rgba(255,255,255,.12); }
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--c-accent); color: var(--c-primary-dark);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .85rem;
}
.user-btn .email { font-size: .85rem; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 720px) { .user-btn .email { display: none; } }

.user-dropdown {
  position: absolute; right: 0; top: calc(100% + .35rem);
  background: var(--c-card); color: var(--c-ink);
  border: 1px solid var(--c-line); border-radius: var(--r);
  box-shadow: var(--shadow-lg);
  min-width: 180px; padding: .35rem;
  z-index: 200;
}
.user-dropdown a {
  display: block; padding: .55rem .75rem; border-radius: 6px;
  color: var(--c-ink);
}
.user-dropdown a:hover { background: var(--c-bg-2); text-decoration: none; }

/* ============== DRAWER (mobile) ============== */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 80%; max-width: 320px;
  background: var(--c-card);
  z-index: 300;
  transform: translateX(-100%);
  transition: transform .25s ease;
  box-shadow: var(--shadow-lg);
  padding-top: env(safe-area-inset-top);
}
.drawer.open { transform: translateX(0); }
.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem; border-bottom: 1px solid var(--c-line);
}
.drawer-header .brand { font-weight: 700; font-size: 1.1rem; color: var(--c-primary); }
.drawer-close {
  background: transparent; border: 0; font-size: 1.6rem; line-height: 1;
  width: var(--tap); height: var(--tap); cursor: pointer; color: var(--c-ink);
}
.drawer-nav { padding: .5rem; }
.drawer-nav a.navlink {
  display: flex; align-items: center; gap: .6rem;
  padding: .8rem .9rem; border-radius: var(--r-sm);
  color: var(--c-ink); font-size: 1rem;
  min-height: var(--tap);
}
.drawer-nav a.navlink:hover { background: var(--c-bg-2); text-decoration: none; }
.drawer-nav a.navlink.active { background: rgba(15,81,50,.1); color: var(--c-primary); font-weight: 600; }
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  z-index: 250; opacity: 0; transition: opacity .2s;
}
.drawer-backdrop.open { opacity: 1; }

/* ============== CONTAINER ============== */
.container {
  max-width: 1200px; margin: 0 auto;
  padding: 1rem;
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
}
@media (min-width: 720px) { .container { padding: 1.5rem; } }

h1 { font-size: 1.5rem; margin: 0 0 1rem; color: var(--c-ink); }
h2 { font-size: 1.2rem; margin: 1.5rem 0 .75rem; }
h3 { font-size: 1.05rem; margin: 1.2rem 0 .5rem; }
.muted { color: var(--c-muted); }
.text-sm { font-size: .9rem; }

/* ============== CARDS ============== */
.card {
  background: var(--c-card); border: 1px solid var(--c-line);
  border-radius: var(--r); padding: 1rem;
  box-shadow: var(--shadow-sm);
  margin-bottom: 1rem;
}
@media (min-width: 720px) { .card { padding: 1.25rem; } }
.card-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1rem; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; margin-bottom: .8rem;
}
.card-head h2, .card-head h3 { margin: 0; }

/* ============== FORMS ============== */
label { display: block; margin: .8rem 0 .3rem; font-weight: 600; font-size: .92rem; }
.form-help { font-size: .85rem; color: var(--c-muted); margin-top: .25rem; }
input[type=text], input[type=email], input[type=password], input[type=url],
input[type=number], input[type=search], textarea, select {
  width: 100%;
  padding: .65rem .75rem;
  font: inherit; font-size: 1rem;   /* 16px : empêche zoom iOS */
  background: var(--c-card);
  color: var(--c-ink);
  border: 1px solid var(--c-line);
  border-radius: 8px;
  min-height: var(--tap);
}
textarea { min-height: 140px; resize: vertical; line-height: 1.5; }
textarea.code, .mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--c-primary-2); outline-offset: 1px;
}
input[type=file] { padding: .5rem; }
input[type=checkbox], input[type=radio] {
  width: 18px; height: 18px; vertical-align: -3px; margin-right: .35rem;
  accent-color: var(--c-primary);
}
.form-row {
  display: grid; gap: .75rem;
  grid-template-columns: 1fr;
}
@media (min-width: 720px) { .form-row.cols-2 { grid-template-columns: 1fr 1fr; } }
@media (min-width: 720px) { .form-row.cols-3 { grid-template-columns: 1fr 1fr 1fr; } }

/* ============== BUTTONS ============== */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  background: var(--c-primary); color: #fff; border: 0;
  padding: .65rem 1.1rem; border-radius: 8px;
  cursor: pointer;
  font: inherit; font-weight: 600; font-size: .95rem;
  min-height: var(--tap);
  text-decoration: none;
  transition: background .15s, transform .05s;
}
button:hover, .btn:hover { background: var(--c-primary-2); text-decoration: none; }
button:active, .btn:active { transform: translateY(1px); }
button:disabled, .btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-secondary {
  background: var(--c-card); color: var(--c-primary);
  border: 1px solid var(--c-primary);
}
.btn-secondary:hover { background: var(--c-primary); color: #fff; }
.btn-danger { background: var(--c-err); }
.btn-danger:hover { background: #921a13; }
.btn-ghost {
  background: transparent; color: var(--c-ink);
  border: 1px solid var(--c-line);
}
.btn-ghost:hover { background: var(--c-bg-2); }
.btn-sm { padding: .35rem .7rem; min-height: 32px; font-size: .85rem; }
.btn-icon {
  width: var(--tap); height: var(--tap); padding: 0;
  border-radius: 8px;
}

/* ============== ALERTS ============== */
.alert {
  padding: .75rem 1rem; border-radius: 8px; margin: .6rem 0;
  font-size: .92rem; border: 1px solid;
}
.alert.err  { background: var(--c-err-bg);  color: var(--c-err);  border-color: var(--c-err-line); }
.alert.ok   { background: var(--c-ok-bg);   color: var(--c-ok);   border-color: var(--c-ok-line); }
.alert.warn { background: var(--c-warn-bg); color: var(--c-warn); border-color: var(--c-warn-line); }
.alert.info { background: var(--c-info-bg); color: var(--c-info); border-color: var(--c-info-line); }

/* ============== TOASTS / FLASHES ============== */
.flashes {
  position: fixed; top: calc(var(--topbar-h) + 1rem + env(safe-area-inset-top));
  right: 1rem; left: 1rem;
  z-index: 90;
  display: flex; flex-direction: column; gap: .5rem;
  pointer-events: none;
}
@media (min-width: 720px) { .flashes { left: auto; max-width: 380px; } }
.toast {
  pointer-events: auto;
  background: var(--c-card); color: var(--c-ink);
  border: 1px solid var(--c-line); border-left: 4px solid var(--c-primary);
  border-radius: var(--r); padding: .8rem 1rem;
  box-shadow: var(--shadow-lg);
  font-size: .92rem;
  animation: toast-in .25s ease both;
}
.toast.ok   { border-left-color: var(--c-ok); }
.toast.err  { border-left-color: var(--c-err); }
.toast.warn { border-left-color: var(--c-warn); }
.toast.info { border-left-color: var(--c-info); }
.toast.fade-out { animation: toast-out .25s ease both; }
@keyframes toast-in  { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to   { opacity: 0; transform: translateY(-8px); } }

/* ============== TABLES + LISTES ============== */
.table-wrap { overflow-x: auto; border-radius: var(--r); box-shadow: var(--shadow-sm); background: var(--c-card); }
.table {
  width: 100%; border-collapse: collapse;
  background: var(--c-card);
  font-size: .94rem;
}
.table th, .table td { padding: .7rem 1rem; text-align: left; border-bottom: 1px solid var(--c-line); }
.table th { background: var(--c-bg-2); font-weight: 600; font-size: .85rem; color: var(--c-muted); text-transform: uppercase; letter-spacing: .03em; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: var(--c-bg-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* Mobile cards : transforme la table en cartes empilées */
@media (max-width: 640px) {
  .table.cards { display: block; }
  .table.cards thead { display: none; }
  .table.cards tbody { display: block; }
  .table.cards tr { display: block; padding: .8rem; border: 1px solid var(--c-line); border-radius: var(--r); margin-bottom: .5rem; background: var(--c-card); }
  .table.cards tr:hover td { background: transparent; }
  .table.cards td { display: flex; justify-content: space-between; gap: 1rem; padding: .35rem 0; border-bottom: none; align-items: baseline; }
  .table.cards td::before {
    content: attr(data-label);
    font-size: .78rem; text-transform: uppercase; letter-spacing: .04em;
    color: var(--c-muted); font-weight: 600; flex-shrink: 0;
  }
  .table.cards td.full { flex-direction: column; align-items: flex-start; }
}

/* ---- Badges ---- */
.badge {
  display: inline-block; padding: .2rem .6rem; border-radius: 999px;
  font-size: .76rem; font-weight: 600; line-height: 1.4;
}
.badge.queued       { background: #f1f5f9; color: #475569; }
.badge.downloading,
.badge.transcribing,
.badge.analyzing,
.badge.generating   { background: #fff7ed; color: #c2410c; }
.badge.done         { background: #ecfdf3; color: var(--c-ok); }
.badge.error        { background: #fef3f2; color: var(--c-err); }
.badge.needs_resources { background: #fef9c3; color: #854d0e; }
.badge.pending      { background: #f1f5f9; color: #475569; }
.badge.indexed      { background: #ecfdf3; color: var(--c-ok); }

/* ---- Tag ---- */
.tag {
  display: inline-block; padding: .15rem .5rem; border-radius: 6px;
  background: var(--c-bg-2); border: 1px solid var(--c-line);
  font-size: .78rem; color: var(--c-muted);
  margin-right: .25rem; margin-bottom: .25rem;
}

/* ============== AUTH ============== */
.auth-wrap {
  min-height: 100vh; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--c-primary) 0%, var(--c-primary-dark) 100%);
  padding: 1rem;
}
.auth-card {
  background: var(--c-card); border-radius: var(--r-lg);
  padding: 1.5rem; max-width: 420px; width: 100%;
  box-shadow: var(--shadow-lg);
}
@media (min-width: 480px) { .auth-card { padding: 2rem; } }
.auth-card h1 { margin: 0 0 .5rem; color: var(--c-primary); font-size: 1.7rem; }
.auth-card h2 { margin: .2rem 0 1rem; font-size: 1.1rem; color: var(--c-muted); font-weight: 500; }
.auth-card .qr { display: block; margin: 1rem auto; max-width: 220px; border-radius: 8px; }
.auth-card details { margin: .8rem 0; font-size: .85rem; color: var(--c-muted); }
.auth-card details code {
  display: block; word-break: break-all; padding: .5rem;
  margin-top: .4rem; font-size: .8rem;
}
.auth-card button { width: 100%; margin-top: 1rem; padding: .8rem; }

/* ============== EDITOR (Markdown split) ============== */
.editor-tabs { display: flex; gap: .25rem; margin-bottom: .5rem; border-bottom: 1px solid var(--c-line); }
.editor-tab {
  background: transparent; color: var(--c-muted); border: 0;
  padding: .55rem 1rem; cursor: pointer; font-weight: 600;
  border-bottom: 2px solid transparent;
}
.editor-tab.active { color: var(--c-primary); border-bottom-color: var(--c-primary); }

.editor-pane { display: none; }
.editor-pane.active { display: block; }

.md-textarea {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: .92rem; min-height: 60vh;
  tab-size: 2;
}
.md-preview {
  min-height: 60vh;
  padding: 1rem 1.25rem;
  background: var(--c-bg-2);
  border: 1px solid var(--c-line);
  border-radius: 8px;
  line-height: 1.6;
}
.md-preview h1, .md-preview h2, .md-preview h3 { margin-top: 1.2rem; }
.md-preview blockquote {
  margin: .8rem 0; padding: .4rem .9rem;
  border-left: 4px solid var(--c-accent); color: var(--c-muted);
}
.md-preview ul, .md-preview ol { padding-left: 1.5rem; }
.md-preview table { border-collapse: collapse; }
.md-preview th, .md-preview td { border: 1px solid var(--c-line); padding: .35rem .6rem; }

/* Split desktop, tabs mobile */
@media (min-width: 1024px) {
  .editor-split { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
  .editor-split .editor-pane { display: block !important; }
  .editor-tabs { display: none; }
}

/* ============== UPLOAD ZONE ============== */
.dropzone {
  border: 2px dashed var(--c-line);
  border-radius: var(--r);
  padding: 2rem 1rem;
  text-align: center;
  background: var(--c-bg-2);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.dragover {
  border-color: var(--c-primary);
  background: rgba(15,81,50,.06);
}
.dropzone input[type=file] { display: none; }
.dropzone .hint { color: var(--c-muted); font-size: .9rem; margin-top: .35rem; }

/* ============== UTILITIES ============== */
.row { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }
.gap-1 { gap: .5rem; } .gap-2 { gap: 1rem; }
.mt-0 { margin-top: 0; } .mb-0 { margin-bottom: 0; }
.text-center { text-align: center; }
.hide { display: none !important; }
@media (max-width: 639px)  { .hide-mobile  { display: none !important; } }
@media (min-width: 640px)  { .show-mobile  { display: none !important; } }
@media (max-width: 1023px) { .hide-tablet  { display: none !important; } }

/* ============== SEARCH BAR ============== */
.searchbar {
  display: flex; gap: .5rem;
  margin-bottom: 1rem;
}
.searchbar input { flex: 1; }

/* ============== SAVE STATE ============== */
.save-state {
  display: inline-flex; align-items: center; gap: .35rem;
  font-size: .85rem; color: var(--c-muted);
}
.save-state.saving { color: var(--c-warn); }
.save-state.saved  { color: var(--c-ok); }
.save-state.dirty  { color: var(--c-err); }
