:root {
  --bg: #f5f2ee;
  --card: #ffffff;
  --text: #22201d;
  --muted: #7a746c;
  --accent: #8a4fff;
  --accent-soft: #efe7ff;
  --danger: #c0392b;
  --danger-soft: #fbe4e1;
  --warn: #b9770e;
  --warn-soft: #fdf1dc;
  --input-bg: #fdfcfa;
  --border: #d8d2c9;
  --topbar-border: #e8e2da;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(30, 25, 20, .08), 0 4px 14px rgba(30, 25, 20, .05);
}

[data-theme="dark"] {
  --bg: #17161a;
  --card: #232227;
  --text: #efece7;
  --muted: #a39d94;
  --accent: #a77fff;
  --accent-soft: #2b2145;
  --danger: #e86a5c;
  --danger-soft: #3d211e;
  --warn: #e0a53e;
  --warn-soft: #3a2f18;
  --input-bg: #1c1b20;
  --border: #3a3841;
  --topbar-border: #302e36;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  transition: background .25s ease, color .25s ease;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--card);
  border-bottom: 1px solid var(--topbar-border);
  padding: 14px 20px;
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { margin: 0; font-size: 1.4rem; }
.topbar .sub { font-size: .85rem; color: var(--muted); font-weight: normal; margin-inline-start: 8px; }
.toolbar { display: flex; gap: 8px; }
.toolbar .btn { min-width: 44px; min-height: 44px; }

.container { max-width: 760px; margin: 20px auto; padding: 0 16px calc(40px + env(safe-area-inset-bottom)); }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 10px; font-size: 1.05rem; }

.overall { display: flex; gap: 12px; margin-bottom: 16px; }
.overall .stat {
  flex: 1; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px; text-align: center;
}
.overall .num { display: block; font-size: 1.3rem; font-weight: 700; color: var(--accent); }
.overall .lbl { font-size: .8rem; color: var(--muted); }

.row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.row label { flex: 1; min-width: 140px; font-size: .85rem; color: var(--muted); display: block; }
input, select {
  width: 100%; padding: 11px 10px; margin-top: 4px;
  border: 1px solid var(--border); border-radius: 9px; font-size: 1rem; font-family: inherit;
  background: var(--input-bg); color: var(--text);
}
input:focus-visible, select:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input:focus { border-color: var(--accent); }

.error {
  color: var(--danger); background: var(--danger-soft);
  padding: 8px 12px; border-radius: 9px; font-size: .88rem; margin: 0 0 10px;
}

.btn {
  border: 0; border-radius: 9px; padding: 11px 18px; font-size: .95rem; font-family: inherit;
  cursor: pointer; background: var(--topbar-border); color: var(--text);
  min-height: 44px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn:hover { filter: brightness(.96); }
.btn:active { transform: translateY(1px); }

.subject .head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.subject .name { font-weight: 700; font-size: 1.05rem; }
.badge { font-size: .75rem; padding: 2px 10px; border-radius: 999px; font-weight: 600; }
.badge.safe { background: var(--accent-soft); color: var(--accent); }
.badge.warning { background: var(--warn-soft); color: var(--warn); }
.badge.critical { background: var(--warn-soft); color: var(--danger); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.unknown { background: var(--topbar-border); color: var(--muted); }

.progress { height: 8px; background: var(--topbar-border); border-radius: 99px; overflow: hidden; margin: 12px 0 6px; }
.progress > div { height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s ease; }
.progress.warn > div { background: var(--warn); }
.progress.danger > div { background: var(--danger); }

.meta { font-size: .85rem; color: var(--muted); margin: 0 0 12px; }
.meta strong { color: var(--text); }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .btn { padding: 9px 14px; font-size: .88rem; }
.btn.attend { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.btn.absent { background: var(--danger-soft); color: var(--danger); font-weight: 600; }
.btn.ghost { background: transparent; color: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 40px 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

@media (max-width: 560px) {
  .overall { flex-direction: column; }
  .row { flex-direction: column; }
  .topbar h1 { font-size: 1.15rem; }
  .topbar .sub { display: block; margin-inline-start: 0; }
  .actions .btn { flex: 1 1 40%; }
}

/* قائمة أدوات الترتيب والنسخ */
.list-tools { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.sort-wrap { font-size: .85rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.sort-wrap select { width: auto; margin-top: 0; }
.io { display: flex; gap: 6px; }

/* فورم التعديل داخل الكارت */
.edit-form { border-top: 1px dashed var(--border); margin-top: 12px; padding-top: 12px; }

/* التنبيهات */
.toast {
  position: fixed; inset-inline: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  max-width: 480px; margin: 0 auto; z-index: 20;
  background: var(--text); color: var(--bg);
  padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  animation: toast-in .25s ease;
}
@keyframes toast-in { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }
.toast button {
  background: transparent; color: var(--accent); border: 0; font: inherit; font-weight: 700;
  cursor: pointer; padding: 6px 8px; min-height: 44px;
}

@media print {
  .topbar .toolbar, .add-form, .actions, .list-tools, .toast { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  .topbar { position: static; }
}

/* شريط تقدم الترم */
.term-progress { height: 4px; background: var(--topbar-border); border-radius: 99px; overflow: hidden; margin: -2px 0 8px; }
.term-progress > div { height: 100%; background: var(--muted); opacity: .55; border-radius: 99px; transition: width .3s ease; }

.projection { color: var(--warn); font-weight: 600; }

/* دخول الكروت */
.card.subject { animation: card-in .3s ease backwards; }
@keyframes card-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* نبضة للخطر */
.badge.danger { animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .65; } }

.container.wide { max-width: 1080px; }

.board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 16px; }
.column { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px; min-height: 120px; }
.column h3 { margin: 2px 4px 10px; font-size: .95rem; display: flex; justify-content: space-between; align-items: center; }
.column h3 .count { background: var(--topbar-border); border-radius: 99px; padding: 0 10px; font-size: .78rem; color: var(--muted); }

.task { background: var(--input-bg); border: 1px solid var(--topbar-border); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.task .t-title { font-weight: 600; font-size: .92rem; margin: 0 0 4px; }
.task .t-meta { font-size: .78rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.task .t-actions { display: flex; gap: 6px; margin-top: 8px; }
.task .t-actions .btn { padding: 4px 10px; min-height: 36px; font-size: .8rem; }

.due-badge { font-size: .72rem; padding: 1px 8px; border-radius: 999px; font-weight: 700; }
.due-badge.overdue { background: var(--danger-soft); color: var(--danger); }
.due-badge.today { background: var(--warn-soft); color: var(--warn); }
.due-badge.soon { background: var(--warn-soft); color: var(--warn); }
.due-badge.future { background: var(--topbar-border); color: var(--muted); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 16px; }

.chips, .notes { list-style: none; padding: 0; margin: 10px 0 0; }
.chips li { display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 4px 12px; margin: 0 0 6px 6px; font-size: .85rem; font-weight: 600; }
.chips li button { background: none; border: 0; color: inherit; cursor: pointer; font: inherit; padding: 2px 4px; min-height: 32px; }
.notes li { border-top: 1px solid var(--topbar-border); padding: 8px 4px; display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.notes li .n-date { color: var(--muted); font-size: .78rem; white-space: nowrap; }
.notes li .n-del { min-height: 32px; }
.notes li .ms-days { font-weight: 700; color: var(--accent); font-size: .78rem; white-space: nowrap; }
.notes li .ms-days.late { color: var(--danger); }

@media (max-width: 860px) {
  .board { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
}

@media print {
  .board { grid-template-columns: repeat(3, 1fr); }
}

.who-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-inline-end: 5px; vertical-align: middle; }
