:root {
  --bg: #f3f4f1;
  --panel: #ffffff;
  --ink: #1d2521;
  --muted: #5d6b63;
  --line: #d9ded8;
  --accent: #2f6b3f;
  --accent-ink: #ffffff;
  --green-cell: #a8d08d;
  --danger: #b3261e;
  --jawi: "Arabic Typesetting", "Noto Naskh Arabic", "Amiri", serif;
  --jawi-size: 18pt;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-title { font-weight: 700; font-size: 17px; }
.brand-sub { font-family: var(--jawi); font-size: 18px; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger, button.danger { color: var(--danger); }
.menu { position: relative; }
.menu summary { list-style: none; }
.menu summary::-webkit-details-marker { display: none; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 230px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12); padding: 6px; display: flex; flex-direction: column;
}
.menu-pop button, .menu-pop .file-btn {
  text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 6px;
  font: inherit; cursor: pointer; color: inherit;
}
.menu-pop button:hover, .menu-pop .file-btn:hover { background: var(--bg); }

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(340px, 460px) 1fr; gap: 16px;
  padding: 16px; align-items: start;
}
.editor {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  position: sticky; top: 72px; max-height: calc(100vh - 88px); display: flex; flex-direction: column;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; padding: 11px; background: none; border: 0; font: inherit; font-weight: 600;
  color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-panel { padding: 14px 16px; overflow-y: auto; flex: 1; }
.rph-list-head { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.rph-list-head label { font-size: 12px; color: var(--muted); font-weight: 600; }
.row-btns { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }

/* ---------- forms ---------- */
form { display: flex; flex-direction: column; gap: 12px; }
fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px 12px; margin: 0; }
legend { font-weight: 700; font-size: 13px; padding: 0 4px; color: var(--accent); }
.f { display: flex; flex-direction: column; gap: 3px; }
.f + .f { margin-top: 8px; }
.f > label { font-size: 12px; font-weight: 600; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.f > label .jl { font-family: var(--jawi); font-size: 17px; font-weight: 400; line-height: 1; }
.hint { font-size: 11.5px; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid2 .f + .f { margin-top: 0; }
input[type=text], input[type=number], input[type=date], select, textarea {
  width: 100%; font: inherit; padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px;
  background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); border-color: var(--accent); }
.jawi-in { font-family: var(--jawi); font-size: 20px; line-height: 1.5; direction: rtl; padding: 4px 9px; }
textarea.jawi-in { min-height: 70px; resize: vertical; }
.chk { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; }
.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; direction: rtl; }
.cat-grid label { font-family: var(--jawi); font-size: 20px; display: flex; align-items: center; gap: 6px; }
.logo-row { display: flex; gap: 10px; align-items: center; }
.logo-row img { width: 44px; height: 44px; object-fit: contain; border: 1px solid var(--line); border-radius: 6px; background: #fff; }

.jawi-pad {
  border-top: 1px solid var(--line); padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center;
}
.pad-label { font-size: 12px; color: var(--muted); margin-right: 4px; }
.jawi-pad button {
  font-family: "Noto Naskh Arabic", var(--jawi); font-size: 18px; min-width: 34px; height: 34px;
  border: 1px solid var(--line); background: #fff; border-radius: 6px; cursor: pointer;
}
.jawi-pad button:hover { border-color: var(--accent); }

/* ---------- preview / paper ---------- */
.preview-wrap { min-width: 0; }
.preview-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; color: var(--muted); font-weight: 600; }
.preview { display: flex; flex-direction: column; align-items: center; gap: 18px; overflow-x: auto; }
.paper {
  width: 8.5in; min-height: 11in; background: #fff; color: #000;
  box-shadow: 0 2px 10px rgba(0,0,0,.12);
  padding: 0.3in 1in 0.4in;
  flex-shrink: 0;
}
.rph-head { display: grid; grid-template-columns: 60px 1fr 60px; align-items: center; gap: 10px; }
.rph-head img { width: 56px; height: 52px; object-fit: contain; }
.rph-head .ht { text-align: center; }
.rph-head .school { font: 9pt Arial, Helvetica, sans-serif; text-transform: uppercase; }
.rph-head .title { font-family: var(--jawi); font-size: calc(var(--jawi-size) * 0.72); line-height: 1.2; }
.rph-head-line { border-top: 3px double #7b1f1f; margin: 4px 0 18px; }

table.rph {
  width: 100%; border-collapse: collapse; table-layout: fixed; direction: rtl;
  font-family: var(--jawi); font-size: var(--jawi-size); line-height: 1.15;
}
table.rph td { border: 1px solid #000; padding: 2px 6px; vertical-align: middle; text-align: right; }
table.rph td.lbl { background: var(--green-cell); font-weight: 700; }
table.rph td.c { text-align: center; }
table.rph td p { margin: 0; }
table.rph .cat { width: 100%; border-collapse: collapse; }
table.rph .cat td { border: 0; padding: 0 2px; }
table.rph .cat .box {
  display: inline-block; width: 0.95em; height: 0.95em; border: 1px solid #000; vertical-align: middle;
  text-align: center; line-height: .9em; font-family: Arial, sans-serif; font-size: .8em;
}
.sign { font-family: var(--jawi); font-size: var(--jawi-size); direction: rtl; margin-top: 18px; line-height: 1.5; }
.sign u { text-underline-offset: 3px; }
.empty { color: var(--muted); padding: 40px; text-align: center; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: #1d2521; color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0;
  transition: .2s; pointer-events: none; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- responsive ---------- */
@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
  .editor { position: static; max-height: none; }
  .preview { align-items: flex-start; }
  .paper { zoom: .5; }
}

/* ---------- print ---------- */
@page { size: letter; margin: 0; }
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  .layout { display: block; padding: 0; }
  .preview { display: block; gap: 0; overflow: visible; }
  .paper { box-shadow: none; margin: 0; min-height: 0; zoom: 1; break-after: page; }
  .paper:last-child { break-after: auto; }
  table.rph tr { break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- templat refleksi ---------- */
.rf-list { display: flex; flex-direction: column; gap: 8px; }
.rf-line { border: 1px solid var(--line); border-radius: 7px; background: #fff; padding: 6px 10px; line-height: 1.9; }
.rf-line input.rf-in {
  width: 3.2em; margin: 0 4px; padding: 0 4px; text-align: center; direction: ltr;
  font: 600 15px system-ui, sans-serif; border: 0; border-bottom: 2px solid var(--accent);
  background: #eef6e8; border-radius: 4px 4px 0 0; vertical-align: middle;
}
.rf-line input.rf-in:focus { outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); }

/* ---------- kotak tarikh ---------- */
.date-box { background: #eef6e8; border: 1px solid #cfe3c0; border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.date-box input[type=date] { font-size: 16px; font-weight: 600; padding: 9px 10px; }
.date-box .f + .f, .date-box .quick-dates + .f { margin-top: 8px; }
.quick-dates { display: flex; gap: 6px; margin-top: 6px; }
