:root { color-scheme: light; }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font-family: Manrope, sans-serif; -webkit-text-size-adjust: 100%; }
button { cursor: pointer; font: inherit; color: inherit; }
a { color: inherit; }
input[type=range] { accent-color: var(--ink); }
[hidden] { display: none !important; }
.serif { font-family: 'Instrument Serif', serif; font-weight: 400; }
.ar, [lang=ar] { font-family: 'IBM Plex Sans Arabic', sans-serif; }
.eyebrow { font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; color: var(--muted); }

/* Boutons */
.btn { height: 44px; padding: 0 16px; border-radius: 22px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap; text-decoration: none; }
.btn.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); font-size: 14px; padding: 0 20px; }
.btn.round { width: 44px; padding: 0; font-size: 16px; font-weight: 700; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.sm { height: 36px; padding: 0 14px; border-radius: 18px; }
.btn:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ---------- Application visiteur ---------- */
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; padding: 18px 24px 24px; gap: 12px; min-height: 0; }
.top { display: flex; align-items: center; gap: 12px 20px; flex-wrap: wrap; }
.title { display: flex; align-items: baseline; gap: 6px 14px; flex: 1 1 320px; flex-wrap: wrap; min-width: 0; }
.title h1 { margin: 0; font-family: 'Instrument Serif', serif; font-size: 40px; font-weight: 400; letter-spacing: -0.5px; line-height: 1; }
.title .ar-name { font-size: 20px; color: var(--muted); }
.title .meta { font-size: 13px; color: var(--muted); }
.search { position: relative; width: 300px; max-width: 100%; }
.search label { display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 16px; border-radius: 22px; background: var(--surface); border: 1px solid var(--border); }
.search input { border: 0; outline: 0; background: transparent; font: inherit; font-size: 14px; width: 100%; color: var(--ink); }
.search svg { flex-shrink: 0; stroke: var(--muted); }
.results { position: absolute; top: 50px; inset-inline: 0; z-index: 30; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: 0 16px 40px rgba(var(--rgb), 0.14); padding: 6px; display: flex; flex-direction: column; }
.results button { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 6px 10px; border: 0; border-radius: 10px; background: transparent; text-align: start; }
.results button:hover, .results button:focus-visible { background: var(--chip); }
.results .dot { width: 10px; height: 10px; border-radius: 5px; flex-shrink: 0; }
.results .rn { font-size: 14px; font-weight: 600; flex-grow: 1; }
.results .rd { font-size: 12px; color: var(--muted); }

.bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 44px; }
.bar .sep { width: 1px; height: 28px; background: var(--border); margin: 0 8px; }
.chip { height: 40px; padding: 0 14px 0 10px; display: inline-flex; align-items: center; gap: 8px; border-radius: 20px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; font-weight: 600; }
.chip .num { width: 18px; height: 18px; border-radius: 9px; color: var(--onC); font-size: 11px; display: flex; align-items: center; justify-content: center; }
.timebox { display: flex; align-items: center; gap: 12px; flex: 1 1 260px; min-width: 200px; }
.timebox input { flex-grow: 1; height: 40px; }
.timebox .yr { font-family: 'Instrument Serif', serif; font-size: 30px; width: 64px; }
.bar .hint { font-size: 13px; color: var(--muted); }

main { flex: 1; min-height: 0; display: flex; gap: 14px; }
#view { position: relative; flex: 1; min-width: 0; overflow: hidden; border-radius: 22px; background-color: var(--onC); background-image: radial-gradient(var(--dot) 1px, transparent 1px); background-size: 22px 22px; border: 1px solid var(--border2); cursor: grab; touch-action: none; direction: ltr; user-select: none; -webkit-user-select: none; }
#view.dragging { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; direction: ltr; }
#world > * { position: absolute; }
.band { left: 0; }
.roman { font-family: 'Instrument Serif', serif; font-size: 34px; color: var(--roman); }
.glabel { font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; color: var(--label); }
.line { border-radius: 2px; transition: opacity 0.2s; }
.ring { width: 22px; height: 22px; transform: rotate(45deg); border-radius: 5px; box-shadow: 0 0 0 3px var(--surface); display: flex; align-items: center; justify-content: center; }
.ring span { transform: rotate(-45deg); color: var(--onC); font-size: 11px; font-weight: 700; }
.card { padding: 10px 12px; display: flex; gap: 10px; align-items: center; border-radius: 16px; transition: opacity 0.2s, box-shadow 0.2s; cursor: pointer; }
.card .av { flex-shrink: 0; width: 40px; height: 40px; border-radius: 20px; background: var(--soft); box-shadow: inset 0 0 0 2px var(--c); color: var(--dark); display: flex; align-items: center; justify-content: center; font-family: 'Instrument Serif', serif; font-size: 20px; overflow: hidden; }
.av img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .tx { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.card .n1 { font-family: 'Instrument Serif', serif; font-size: 19px; line-height: 1.05; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .n1.ar { font-family: 'IBM Plex Sans Arabic', sans-serif; font-size: 16px; line-height: 1.25; }
.card .n2 { font-size: 12px; color: var(--muted); line-height: 1.4; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .n2.ar { font-family: 'IBM Plex Sans Arabic', sans-serif; }
.card .meta { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .badge { position: absolute; top: -10px; right: 12px; padding: 2px 9px; border-radius: 10px; background: var(--soft); color: var(--dark); font-size: 11px; font-weight: 700; border: 1px solid var(--c); white-space: nowrap; }
.card .ref { position: absolute; bottom: -11px; left: 12px; padding: 1px 8px; border-radius: 10px; background: var(--ink); color: var(--bg); font-size: 10px; font-weight: 600; white-space: nowrap; }
.card .fold { position: absolute; left: 50%; bottom: -13px; transform: translateX(-50%); min-width: 26px; height: 26px; padding: 0 7px; border-radius: 13px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 12px; font-weight: 700; box-shadow: 0 2px 6px rgba(var(--rgb), 0.1); }

.helpbox { position: absolute; inset-inline-start: 16px; top: 16px; width: min(330px, calc(100% - 32px)); padding: 16px 18px; border-radius: 18px; background: var(--overlay); color: var(--bg); display: flex; flex-direction: column; gap: 8px; font-size: 13px; line-height: 1.45; cursor: default; z-index: 5; direction: ltr; }
[dir=rtl] .helpbox { direction: rtl; }
.helpbox .ht { font-family: 'Instrument Serif', serif; font-size: 22px; }
.helpbox button { align-self: flex-start; height: 36px; padding: 0 14px; border-radius: 18px; border: 0; background: var(--bg); color: var(--ink); font-size: 13px; font-weight: 700; }
.zoombar { position: absolute; left: 16px; bottom: 16px; display: flex; align-items: center; gap: 6px; padding: 5px; border-radius: 24px; background: var(--surface); border: 1px solid var(--border); box-shadow: 0 6px 18px rgba(var(--rgb), 0.08); cursor: default; z-index: 4; }
.zoombar button { height: 38px; padding: 0 14px; border-radius: 19px; border: 0; background: var(--chip); color: var(--ink); font-size: 13px; font-weight: 600; }
.zoombar button.sq { width: 38px; padding: 0; font-size: 18px; }
.zoombar button.dark { background: var(--ink); color: var(--bg); }
.zoombar .pct { width: 48px; text-align: center; font-size: 13px; font-weight: 600; }
.mini { position: absolute; right: 16px; bottom: 16px; border-radius: 12px; background: var(--glass); border: 1px solid var(--border); box-shadow: 0 6px 18px rgba(var(--rgb), 0.08); overflow: hidden; cursor: pointer; z-index: 4; }
.mini > div { position: absolute; border-radius: 1px; }
.mini .vr { border: 2px solid var(--ink); border-radius: 4px; background: rgba(var(--rgb), 0.05); }

aside#panel { width: 340px; flex-shrink: 0; padding: 22px; background: var(--surface); border: 1px solid var(--border2); border-radius: 22px; display: flex; flex-direction: column; gap: 16px; overflow: auto; }
.phead { display: flex; gap: 14px; align-items: center; }
.phead .av { width: 64px; height: 64px; border-radius: 32px; background: var(--soft); box-shadow: inset 0 0 0 3px var(--c); color: var(--dark); display: flex; align-items: center; justify-content: center; font-family: 'Instrument Serif', serif; font-size: 32px; flex-shrink: 0; overflow: hidden; }
.phead .names { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.phead .pn { font-family: 'Instrument Serif', serif; font-size: 27px; line-height: 1.05; overflow-wrap: anywhere; }
.phead .pn.ar { font-family: 'IBM Plex Sans Arabic', sans-serif; font-size: 22px; }
.phead .pa { font-size: 16px; color: var(--muted); }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { padding: 4px 10px; border-radius: 12px; background: var(--chip); font-size: 12px; font-weight: 600; }
.tag.br { background: var(--soft); color: var(--dark); font-weight: 700; }
.sec { display: flex; flex-direction: column; gap: 8px; }
.pair { display: flex; gap: 8px; }
.pbtn { flex: 1; min-height: 52px; padding: 8px 10px; border-radius: 12px; border: 1px solid var(--border2); background: var(--bg); text-align: start; display: flex; flex-direction: column; }
.pbtn small { font-size: 11px; color: var(--muted); }
.pbtn b { font-size: 13px; }
.sibs { font-size: 12px; color: var(--muted); line-height: 1.6; }
.sibs button { border: 0; background: var(--chip); border-radius: 12px; padding: 2px 9px; margin: 1px 2px; font-size: 12px; }
.ucard { padding: 12px; border-radius: 14px; border: 1px solid var(--border2); display: flex; flex-direction: column; gap: 8px; }
.urow { display: flex; align-items: center; gap: 10px; }
.diam { width: 22px; height: 22px; border-radius: 5px; transform: rotate(45deg); display: flex; align-items: center; justify-content: center; flex-shrink: 0; margin: 0 3px; }
.diam span { transform: rotate(-45deg); color: var(--onC); font-size: 11px; font-weight: 700; }
.ulink { border: 0; background: transparent; padding: 0; text-align: start; display: flex; flex-direction: column; min-height: 36px; justify-content: center; }
.ulink b { font-size: 14px; }
.ulink small { font-size: 12px; color: var(--muted); }
.kids { display: flex; flex-wrap: wrap; gap: 6px; }
.kid { height: 30px; padding: 0 11px; border-radius: 15px; border: 0; font-size: 12px; font-weight: 700; }
.muted { font-size: 12px; color: var(--muted); }
.bio { margin: 0; font-size: 13px; line-height: 1.55; color: var(--body); white-space: pre-wrap; }
.facts { font-size: 13px; color: var(--body); line-height: 1.6; }
.rel select { width: 100%; height: 44px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); font: inherit; font-size: 13px; padding: 0 10px; }
.rel .out { font-size: 14px; font-weight: 600; line-height: 1.5; padding: 10px 12px; border-radius: 12px; background: var(--chip); }
.pfoot { display: flex; gap: 8px; margin-top: auto; }
.pfoot .btn { flex: 1; font-weight: 700; }
.drawer-close { display: none; }

/* Mobile : fiche en tiroir */
@media (max-width: 860px) {
  #app { padding: 12px 12px 12px; gap: 8px; }
  .title h1 { font-size: 30px; }
  .title { flex-basis: 100%; }
  .search { width: 100%; flex: 1 1 100%; order: 3; }
  .bar { gap: 8px; }
  .bar .hint { display: none; }
  main { position: relative; }
  aside#panel { position: fixed; inset-inline: 0; bottom: 0; width: auto; max-height: 62vh; z-index: 40; border-radius: 22px 22px 0 0; box-shadow: 0 -12px 40px rgba(var(--rgb), 0.25); transform: translateY(110%); transition: transform 0.25s; padding-top: 14px; }
  aside#panel.open { transform: none; }
  .drawer-close { display: inline-flex; align-self: flex-end; }
  .mini { right: 8px; bottom: 76px; }
  .zoombar { left: 8px; bottom: 8px; }
  .zoombar .pct { display: none; }
}

/* ---------- Connexion ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border2); border-radius: 22px; padding: 32px 28px; display: flex; flex-direction: column; gap: 16px; box-shadow: 0 16px 40px rgba(var(--rgb), 0.08); }
.login-card h1 { margin: 0; font-family: 'Instrument Serif', serif; font-weight: 400; font-size: 36px; line-height: 1.05; }
.login-card p { margin: 0; color: var(--muted); font-size: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 700; color: var(--muted); }
.field input, .field select, .field textarea { height: 44px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); font: inherit; font-size: 14px; font-weight: 500; padding: 0 12px; width: 100%; }
.field textarea { height: 96px; padding: 10px 12px; resize: vertical; }
.err { color: var(--danger); font-size: 13px; min-height: 18px; }
.ok { color: #2F7D62; font-size: 13px; }

/* ---------- Admin ---------- */
.adm { max-width: 1200px; margin: 0 auto; padding: 18px 24px 40px; display: flex; flex-direction: column; gap: 16px; }
.adm header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.adm header h1 { margin: 0; font-family: 'Instrument Serif', serif; font-weight: 400; font-size: 36px; flex: 1; }
.tabs { display: flex; gap: 8px; }
.tabs .btn[aria-pressed=true] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.cols { display: grid; grid-template-columns: 320px 1fr; gap: 16px; align-items: start; }
.box { background: var(--surface); border: 1px solid var(--border2); border-radius: 22px; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.box h2 { margin: 0; font-family: 'Instrument Serif', serif; font-weight: 400; font-size: 26px; }
.plist { display: flex; flex-direction: column; gap: 4px; max-height: 70vh; overflow: auto; }
.plist button { display: flex; align-items: center; gap: 10px; text-align: start; border: 1px solid transparent; border-radius: 12px; background: transparent; padding: 8px 10px; min-height: 44px; }
.plist button:hover { background: var(--chip); }
.plist button[aria-current=true] { border-color: var(--ink); background: var(--chip); }
.plist .sm { font-size: 12px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.grid2 .full { grid-column: 1 / -1; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tbl { display: flex; flex-direction: column; gap: 8px; }
.sub { border: 1px solid var(--border2); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.toast { position: fixed; bottom: 20px; inset-inline: 0; margin: 0 auto; width: fit-content; max-width: 90vw; padding: 10px 18px; border-radius: 20px; background: var(--ink); color: var(--bg); font-size: 14px; z-index: 100; }
.toast.bad { background: var(--danger); color: var(--bg); }
@media (max-width: 860px) {
  .adm { padding: 12px; }
  .cols { grid-template-columns: 1fr; }
  .grid2 { grid-template-columns: 1fr; }
  .plist { max-height: 40vh; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
