:root {
  --bg: #f2f5f9;
  --card: #ffffff;
  --line: #e3e8ef;
  --line-strong: #cfd6e0;
  --text: #16202e;
  --muted: #6b7789;
  --primary: #2563eb;
  --primary-d: #1d4ed8;
  --ok: #16a34a;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 1px 2px rgba(16, 24, 40, .04);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ------------------------------- الأزرار ------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font: inherit;
  white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #f5f8fc; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-d); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 11px; font-size: 13px; }
.btn.icon {
  padding: 6px 9px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  border-radius: 8px;
}
.btn.icon:hover { color: var(--danger); background: #fee2e2; }

/* ------------------------------ تسجيل الدخول ---------------------------- */
.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, #eef2f8, #e4ebf6);
  padding: 20px;
}
.login-box {
  background: var(--card);
  padding: 32px;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(16, 24, 40, .1);
  width: min(400px, 100%);
  display: grid;
  gap: 14px;
}
.login-box h1 { margin: 0; font-size: 22px; text-align: center; }
.login-box p { margin: 0; text-align: center; font-size: 14px; }
.role-picker { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.role-opt {
  border: 1.5px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  font-size: 14px;
}
.role-opt input { display: none; }
.role-opt:has(input:checked) { border-color: var(--primary); background: #eff5ff; color: var(--primary-d); font-weight: 600; }
#password {
  padding: 11px 14px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font: inherit;
  width: 100%;
}
.error { color: var(--danger); font-size: 14px; text-align: center; min-height: 18px; }

/* -------------------------------- الشريط -------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
  position: sticky;
  top: 0;
  z-index: 30;
}
.brand { font-weight: 700; font-size: 17px; }
.toolbar { display: flex; align-items: center; gap: 10px; flex: 1; flex-wrap: wrap; }
#search {
  padding: 8px 13px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  font: inherit;
  min-width: 190px;
}
.filters { display: flex; gap: 6px; }
.chip {
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 999px;
  padding: 6px 15px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
}
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.who { display: flex; align-items: center; gap: 10px; }
.badge {
  background: #eff5ff;
  color: var(--primary-d);
  border-radius: 999px;
  padding: 5px 13px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.sync { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* ------------------------------ إضافة مهمة ------------------------------ */
.composer {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  background: var(--card);
  margin: 18px 20px 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.composer textarea {
  flex: 1;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 11px 13px;
  font: inherit;
  line-height: 1.7;
  resize: vertical;
  min-height: 56px;
}
.composer textarea:focus { outline: 2px solid #bfdbfe; outline-offset: -1px; border-color: var(--primary); }
.composer-side { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* -------------------------------- الجدول -------------------------------- */
.table-wrap {
  margin: 18px 20px 26px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1000px;
  table-layout: fixed;
}

thead th {
  text-align: right;
  font-size: 13px;
  color: var(--muted);
  font-weight: 700;
  padding: 13px 14px;
  border-bottom: 2px solid var(--line-strong);
  background: #f7f9fc;
  white-space: nowrap;
}
thead th + th { border-inline-start: 1px solid var(--line); }
thead th small { display: block; font-weight: 400; font-size: 11px; opacity: .8; margin-top: 2px; }

tbody td {
  padding: 14px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
tbody td + td { border-inline-start: 1px solid #eef1f6; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #fbfcfe; }
tbody tr.is-done td { background: #f5fbf7; }
tbody tr.is-done:hover td { background: #f0f9f3; }
tbody tr.active-row td:first-child { box-shadow: inset 3px 0 0 var(--primary); }

.col-id { width: 58px; }
.col-title { width: 30%; }
.col-img { width: 190px; }
.col-note { width: 24%; }
.col-done { width: 72px; text-align: center; }
.col-status { width: 165px; }
.col-actions { width: 52px; }
th.col-done, th.col-actions { text-align: center; }

.rownum {
  display: inline-block;
  min-width: 26px;
  text-align: center;
  background: #eef2f7;
  color: var(--muted);
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 12px;
  font-weight: 600;
}

/* الحقول داخل الخلايا */
.cell-input {
  display: block;
  width: 100%;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
  line-height: 1.8;
  background: transparent;
  resize: none;
  overflow: hidden;
  min-height: 44px;
  color: inherit;
  transition: background .15s, border-color .15s;
}
.cell-input:hover:not(:disabled) { border-color: var(--line-strong); background: #fff; }
.cell-input:focus { border-color: var(--primary); background: #fff; outline: 2px solid #bfdbfe; outline-offset: -1px; }
.cell-input.saving { border-color: #f59e0b; }
/* عمود الطرف الآخر: للقراءة فقط بخلفية خفيفة تميّزه */
.cell-input:disabled {
  color: var(--text);
  opacity: 1;
  cursor: default;
  background: #f8fafc;
  border-color: #eef1f6;
}
.cell-input:disabled:placeholder-shown { background: transparent; border-color: transparent; color: var(--muted); }

.chk { width: 22px; height: 22px; cursor: pointer; accent-color: var(--ok); margin-top: 11px; }
.chk:disabled { cursor: not-allowed; opacity: .5; }

select.status {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
select.status:focus { outline: 2px solid #bfdbfe; }
.status-pill {
  display: inline-block;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}

/* -------------------------------- الصور --------------------------------- */
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.thumb { position: relative; width: 64px; height: 64px; }
.thumb img {
  width: 100%; height: 100%;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  cursor: zoom-in;
  display: block;
  background: #f1f5f9;
}
.thumb .del {
  position: absolute;
  top: -7px;
  inset-inline-end: -7px;
  background: var(--danger);
  color: #fff;
  border: 2px solid #fff;
  border-radius: 50%;
  width: 22px; height: 22px;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  display: none;
  padding: 0;
}
.thumb:hover .del { display: block; }
.add-img {
  width: 64px; height: 64px;
  border: 1.5px dashed var(--line-strong);
  border-radius: 8px;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  font-size: 20px;
}
.add-img:hover { border-color: var(--primary); color: var(--primary); background: #f5f9ff; }

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .82);
  display: grid;
  place-items: center;
  z-index: 100;
  cursor: zoom-out;
  padding: 24px;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* -------------------------------- متفرقات ------------------------------- */
.empty { padding: 44px; text-align: center; color: var(--muted); }
.hint { text-align: center; color: var(--muted); font-size: 13px; padding-bottom: 26px; }
kbd {
  background: #fff;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 12px;
}
.toast {
  position: fixed;
  bottom: 22px;
  inset-inline-start: 22px;
  background: #16202e;
  color: #fff;
  padding: 11px 18px;
  border-radius: var(--radius);
  z-index: 200;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  font-size: 14px;
}
.toast.err { background: var(--danger); }

/* ================= الموبايل: كل صف يصير كرت مستقل ================= */
@media (max-width: 760px) {
  body { font-size: 16px; }

  /* على الموبايل الشريط بيلتف لعدة أسطر — نلغي التثبيت حتى ما ياكل الشاشة */
  .topbar { padding: 10px 12px; position: static; gap: 10px; }
  .brand { width: 100%; }
  .toolbar { order: 3; }
  #search { flex: 1; min-width: 0; }
  .filters { width: 100%; }
  .chip { flex: 1; text-align: center; padding: 8px 6px; }
  .who { flex: 1; justify-content: flex-end; }
  .sync { display: none; }

  .composer { flex-direction: column; margin-inline: 10px; gap: 10px; }
  .composer-side { width: 100%; }
  .composer-side .btn { flex: 1; justify-content: center; }

  /* الجدول ينفك لكروت — بلا سحب أفقي */
  .table-wrap {
    margin: 14px 10px 20px;
    border: 0;
    background: transparent;
    box-shadow: none;
    overflow: visible;
  }
  table { display: block; min-width: 0; table-layout: auto; }
  thead { display: none; }
  tbody { display: block; }

  tbody tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
    margin-bottom: 12px;
    padding: 10px 14px 14px;
  }
  tbody tr.is-done { border-color: #b9e7c9; background: #f6fbf8; }
  tbody tr:hover td,
  tbody tr:hover,
  tbody tr.is-done td { background: transparent; }
  tbody tr.active-row { border-color: var(--primary); }
  tbody tr.active-row td:first-child { box-shadow: none; }

  tbody td {
    display: block;
    border: 0 !important;
    padding: 7px 0;
    text-align: right;
  }
  /* لازم `td.` عشان تغلب عروض الأعمدة تبعت الديسكتوب */
  td.col-title, td.col-img, td.col-note { width: 100%; }
  tbody td::before {
    content: attr(data-label);
    display: block;
    font-size: 12px;
    font-weight: 700;
    color: var(--muted);
    margin-bottom: 4px;
  }

  /* رأس الكرت: الرقم يمين وزر الحذف يسار */
  td.col-id { order: -2; width: auto; margin-inline-end: auto; padding-top: 0; }
  td.col-actions { order: -1; width: auto; padding-top: 0; }
  td.col-actions:empty { display: none; }
  .rownum { font-size: 14px; padding: 4px 10px; }

  /* "تم" و"الحالة" جنب بعض بآخر الكرت */
  td.col-done {
    width: auto;
    display: flex;
    align-items: center;
    gap: 9px;
    padding-inline-end: 18px;
  }
  td.col-done::before { margin-bottom: 0; font-size: 14px; }
  td.col-status { flex: 1; min-width: 150px; }
  .chk { margin-top: 0; width: 26px; height: 26px; }

  .cell-input { padding: 10px 12px; border-color: var(--line); background: #fff; }
  .cell-input:disabled { background: #f8fafc; }
  select.status { padding: 11px 10px; }
  .status-pill { padding: 9px 16px; }

  /* الصور أكبر شوي عشان اللمس */
  .thumb, .add-img { width: 72px; height: 72px; }
  .thumb .del { display: block; width: 24px; height: 24px; }

  .hint { padding: 0 16px 24px; }
}
