:root {
  /* الألوان من design/README.md: زمردي للتمييز، ذهبي للتنبيه والإجراء النهائي، الأحمر للخطر فقط */
  --bg: #eef1ef;
  --surface: #ffffff;
  --ink: #10201d;
  --muted: #5b6b68;
  --line: #d5dcd9;
  --field-line: #84948f;
  --accent: #12806f;
  --accent-soft: #e0f1ee;
  --accent-ink: #ffffff;
  --gold: #d4a853;
  --gold-soft: #f8efd9;
  --gold-ink: #5e4412;
  --danger: #b42318;
  --danger-soft: #fbe9e7;
  --shadow: 0 1px 2px rgba(20,25,35,.06), 0 4px 14px rgba(20,25,35,.06);
  --font-display: "Readex Pro", "IBM Plex Sans Arabic", Tahoma, sans-serif;
  --font-body: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1211; --surface: #151f1d; --ink: #edf2f0; --muted: #8fa19d; --line: #2a3634; --field-line: #66807a;
    --accent: #2fb5a0; --accent-soft: #10332e; --accent-ink: #04231e;
    --gold-soft: #3a2f16; --gold-ink: #f0d9a3;
    --danger: #f07167; --danger-soft: #3a1f1d;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--font-body); font-size: 14px; line-height: 1.55; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2 { font-family: var(--font-display); font-weight: 600; text-wrap: balance; margin: 0; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.err { color: var(--danger); font-weight: 500; }
.sr { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.spacer { flex: 1; }

header.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
}
header.top h1 { font-size: 19px; }
header.top .sub { color: var(--muted); font-size: 13px; }
.session { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; }

main { padding: 18px 20px 40px; max-width: 1500px; margin: 0 auto; }

.btn {
  min-height: 40px; padding: 7px 14px; border-radius: 10px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--field-line); font-weight: 500;
}
.btn:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.mini { min-height: 32px; padding: 3px 10px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: .55; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.field input, .field select, .field textarea {
  min-height: 40px; padding: 7px 10px; border-radius: 10px;
  border: 1px solid var(--field-line); background: var(--surface); color: var(--ink); font-size: 14px;
}
.field textarea { resize: vertical; }
.check { display: flex; align-items: center; gap: 6px; min-height: 40px; align-self: flex-end; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.connect {
  max-width: 560px; margin: 40px auto; padding: 26px; display: flex; flex-direction: column; gap: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.connect h2 { font-size: 21px; }
.connect form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; }
.connect .field { flex: 1 1 240px; }

.toolbar {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px;
  padding: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.toolbar .grow { flex: 1 1 260px; }
.toolbar .spacer { flex: 0 1 auto; }
.status { margin: 10px 2px; color: var(--muted); font-size: 13px; min-height: 20px; }

.panel {
  margin-bottom: 10px; padding: 12px 14px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--line); border-inline-start: 4px solid var(--gold);
}
.panel.danger { border-inline-start-color: var(--danger); }
.panel.ok { border-inline-start-color: var(--accent); }
.panel h3 { margin: 0 0 4px; font-family: var(--font-display); font-size: 14.5px; font-weight: 600; }
.panel ul { margin: 8px 0 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; max-height: 240px; overflow: auto; }
.panel li { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.panel li .path { flex: 1 1 280px; overflow-wrap: anywhere; direction: ltr; text-align: right; unicode-bidi: plaintext; }
.panel .actions { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 8px; }

.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; text-align: start; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-family: var(--font-display); font-weight: 500; font-size: 12.5px; color: var(--muted); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.voided td { color: var(--muted); text-decoration: line-through; }
tbody tr.voided td.acts, tbody tr.voided td.file { text-decoration: none; }
td.subject { min-width: 220px; overflow-wrap: anywhere; }
td.nowrap { white-space: nowrap; }
td.date { direction: ltr; text-align: right; }
td.file { font-size: 12.5px; color: var(--muted); max-width: 260px; overflow-wrap: anywhere; }
td.acts { white-space: nowrap; }
td.acts .btn { margin-inline-end: 4px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--gold-soft); color: var(--gold-ink); }
.badge.bad { background: var(--danger-soft); color: var(--danger); }
.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.more { text-align: center; margin-top: 12px; }

dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--ink); padding: 0; box-shadow: 0 30px 60px -20px rgba(0,0,0,.5); }
dialog::backdrop { background: rgba(6,10,9,.55); }
.review { width: min(1180px, 96vw); height: min(860px, 94vh); }
.review form { display: flex; flex-direction: column; height: 100%; }
.review-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.review-head h2 { font-size: 17px; overflow-wrap: anywhere; }
.review-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 360px; }
.preview { background: var(--bg); overflow: auto; padding: 12px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.preview canvas, .preview img { max-width: 100%; height: auto; background: #fff; box-shadow: var(--shadow); }
.preview .msg { margin: auto; text-align: center; color: var(--muted); padding: 20px; overflow-wrap: anywhere; }
.form { padding: 14px 18px; overflow: auto; display: flex; flex-direction: column; gap: 10px; border-inline-start: 1px solid var(--line); }
.note { padding: 8px 10px; border-radius: 10px; background: var(--gold-soft); color: var(--gold-ink); font-size: 13px; }
.read-panel { display: flex; flex-direction: column; gap: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.summary-choice { display: flex; flex-direction: column; gap: 8px; }
.summary-option { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; font-size: 13px; overflow-wrap: anywhere; }
.summary-option.chosen { border-color: var(--accent); background: var(--accent-soft); }
.summary-option strong { font-family: var(--font-display); font-weight: 500; }
.ai-fields { display: flex; flex-direction: column; gap: 10px; }
.confirm .check { align-items: flex-start; min-height: 0; align-self: stretch; }
.confirm .check input { flex: none; margin-top: 2px; }
#btnMode.ai-on { background: var(--gold-soft); border-color: var(--gold); color: var(--gold-ink); }
.profile { width: min(920px, 96vw); max-height: 94vh; overflow: hidden; }
.profile form { display: flex; flex-direction: column; max-height: 94vh; }
.profile-body { padding: 14px 18px; overflow: auto; display: flex; flex-direction: column; gap: 10px; }
.profile-body h3 { margin: 6px 0 0; font-family: var(--font-display); font-size: 15px; font-weight: 600; }
.profile-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; }
.rules { display: flex; flex-direction: column; gap: 6px; }
.rule { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px; border: 1px solid var(--line); border-radius: 10px; font-size: 13px; }
.rule select, .rule input { min-height: 34px; padding: 3px 8px; border-radius: 8px; border: 1px solid var(--field-line); background: var(--surface); color: var(--ink); font-size: 13px; max-width: 170px; }
.rule .grow { flex: 1; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.page-bar { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 2px; font-size: 12.5px; color: var(--muted); }
.page-bar.attachment { color: var(--gold-ink); font-weight: 500; }
.preview canvas.attachment { opacity: .45; }
td.subject .summary { margin-top: 3px; font-size: 12.5px; color: var(--muted); }
td .badge + .badge { margin-inline-start: 4px; }
.pick-row { display: flex; align-items: flex-end; gap: 6px; }
.pick-row .field { flex: 1; min-width: 0; }
.pick-row .pick { min-height: 40px; }
.pick[aria-pressed="true"] { background: var(--gold); border-color: var(--gold); color: #2a1d05; }
.preview { position: relative; }
.preview.picking { touch-action: none; user-select: none; }
.preview.picking canvas, .preview.picking img { cursor: crosshair; }
.pick-box { position: absolute; border: 2px solid var(--accent); background: rgba(18,128,111,.15); pointer-events: none; z-index: 2; }
.ocr-status { padding: 8px 10px; border-radius: 10px; background: var(--accent-soft); font-size: 13px; }
.ocr-status.err { background: var(--gold-soft); color: var(--gold-ink); font-weight: 400; }
#ocrText { margin: 8px 0 0; padding: 8px; max-height: 220px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; unicode-bidi: plaintext; font-family: inherit; font-size: 12.5px; background: var(--bg); border-radius: 8px; }
.review-foot { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
details summary { cursor: pointer; font-weight: 500; }
details ul { margin: 8px 0 0; padding-inline-start: 18px; font-size: 12.5px; color: var(--muted); }
details li { overflow-wrap: anywhere; margin-bottom: 4px; }

.confirm { width: min(480px, 94vw); }
.confirm form { display: flex; flex-direction: column; gap: 10px; padding: 18px 18px 4px; }
.confirm h2 { font-size: 17px; }
.confirm .review-foot { border-top: 0; padding: 8px 0 14px; }
.confirm p { overflow-wrap: anywhere; }

.toasts { position: fixed; bottom: 18px; inset-inline-start: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(420px, 90vw); }
.toast { padding: 10px 14px; border-radius: 10px; background: var(--ink); color: var(--bg); box-shadow: var(--shadow); overflow-wrap: anywhere; }
.toast.err { background: var(--danger); color: #fff; }
/* يظهر فوق نوافذ dialog أيضًا لأن العنصر يُنقل إلى داخل النافذة المفتوحة */
dialog .toasts { position: fixed; }

@media (max-width: 860px) {
  .review-body { grid-template-columns: 1fr; grid-template-rows: 45% 1fr; }
  .form { border-inline-start: 0; border-top: 1px solid var(--line); }
}
