:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #17202e;
  --muted: #5d6879;
  --line: #dde2ea;
  --primary: #1f3a68;
  --primary-ink: #ffffff;
  --primary-soft: #e3eaf6;
  --accent: #b7862b;
  --present: #1d7a4c;
  --present-soft: #dcf2e6;
  --absent: #c03535;
  --absent-soft: #fbe1e1;
  --excused: #b06d00;
  --excused-soft: #fdefd3;
  --warn1: #8a6a00;
  --warn1-soft: #fff3c4;
  --warn2: #a24d00;
  --warn2-soft: #ffe0c2;
  --fail: #b42323;
  --fail-soft: #ffd6d6;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 4px 14px rgba(20, 30, 50, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --surface: #18202b;
    --surface-2: #212b38;
    --text: #e8ecf2;
    --muted: #9aa5b5;
    --line: #2c3746;
    --primary: #6f9be0;
    --primary-ink: #0d1520;
    --primary-soft: #1e2c42;
    --accent: #e0b25a;
    --present: #5fd197;
    --present-soft: #163527;
    --absent: #ff8080;
    --absent-soft: #3d1c1f;
    --excused: #f2b84b;
    --excused-soft: #3a2c12;
    --warn1: #f0d264;
    --warn1-soft: #3a3313;
    --warn2: #ffad66;
    --warn2-soft: #3d2614;
    --fail: #ff7d7d;
    --fail-soft: #431b1e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Tahoma, "SF Arabic", "Geeza Pro", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- الرأس */
header#top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--primary); color: var(--primary-ink);
}
.brand { display: flex; align-items: center; gap: 10px; color: inherit; font-weight: 700; font-size: 1.05rem; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.who { font-size: .85rem; opacity: .9; max-width: 45vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; color: inherit; background: rgba(255,255,255,.14); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; }

main { max-width: 860px; margin: 0 auto; padding: 16px 16px 120px; }

.pending {
  max-width: 860px; margin: 10px auto 0; padding: 10px 14px;
  background: var(--excused-soft); color: var(--excused);
  border-radius: 10px; font-size: .9rem; display: flex; gap: 10px; align-items: center; justify-content: space-between;
}
.pending { margin-left: 16px; margin-right: 16px; }
@media (min-width: 892px) { .pending { margin-left: auto; margin-right: auto; } }

/* ---------- عناصر عامة */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.card.error { border-color: var(--absent); color: var(--absent); }
.page-head { display: flex; align-items: center; gap: 12px; margin: 4px 0 16px; }
.page-head .grow { flex: 1; min-width: 0; }
.back {
  display: grid; place-items: center; flex: none; width: 40px; height: 40px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--line); color: var(--text); font-size: 1.2rem;
}
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 22px 0 10px; }
.section-title h3 { font-size: 1rem; color: var(--muted); font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; font-size: .95rem; cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { color: var(--absent); border-color: var(--absent-soft); background: var(--absent-soft); }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: .85rem; border-radius: 9px; }
.btn.block { width: 100%; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.row-btns .btn { flex: 1; }

label.field { display: block; font-size: .85rem; color: var(--muted); font-weight: 600; }
.field input, .field select, input.input, select.input {
  display: block; width: 100%; margin-top: 5px; min-height: 46px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 16px;
}
.field input:focus, .field select:focus, .input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.field.check { display: flex; align-items: center; gap: 10px; color: var(--text); font-size: .95rem; }
.field.check input { width: 22px; height: 22px; min-height: 0; margin: 0; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }
.form-err { color: var(--absent); font-size: .9rem; margin: 6px 0; }
.hint { font-size: .85rem; color: var(--muted); margin: 6px 0 0; }

/* ---------- تسجيل الدخول */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .logo { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 12px; }
.login-card h1 { text-align: center; font-size: 1.35rem; }
.login-card .sub { text-align: center; color: var(--muted); font-size: .9rem; margin: 4px 0 20px; }
.login-card .field { margin-bottom: 12px; }

/* ---------- لوحة المقرر */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.tile {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 92px; padding: 10px; border-radius: var(--radius); text-align: center;
  background: var(--surface); border: 1px solid var(--line); color: var(--text); font-weight: 600; font-size: .9rem;
  box-shadow: var(--shadow);
}
.tile svg { width: 28px; height: 28px; fill: none; stroke: var(--primary); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.4rem; color: var(--primary); font-variant-numeric: tabular-nums; }
.stat span { font-size: .78rem; color: var(--muted); }
.notice { border-inline-start: 4px solid var(--accent); }

/* ---------- بطاقات المواد */
.subj h3 { margin-bottom: 2px; }
.progress { height: 7px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 12px 0 6px; }
.progress span { display: block; height: 100%; background: var(--primary); border-radius: 99px; }
.subj .meta { display: flex; justify-content: space-between; gap: 8px; font-size: .82rem; color: var(--muted); margin-bottom: 12px; }
.alert-link {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--fail-soft); color: var(--fail); border: 0; font-weight: 600;
}
.alert-link .count { background: var(--fail); color: #fff; border-radius: 99px; padding: 0 10px; font-size: .9rem; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .78rem; font-weight: 700; white-space: nowrap; }
.badge.ok { background: var(--present-soft); color: var(--present); }
.badge.warn1 { background: var(--warn1-soft); color: var(--warn1); }
.badge.warn2 { background: var(--warn2-soft); color: var(--warn2); }
.badge.fail { background: var(--fail-soft); color: var(--fail); }
.badge.soft { background: var(--surface-2); color: var(--muted); }

/* ---------- تسجيل الغياب */
.toolbar { display: flex; gap: 8px; align-items: center; margin: 4px 0 10px; }
.toolbar .input { margin: 0; flex: 1; }
.counts { display: flex; gap: 8px; margin-bottom: 10px; }
.counts span { flex: 1; text-align: center; padding: 8px 4px; border-radius: 10px; font-weight: 700; font-size: .9rem; }
.counts .c-p { background: var(--present-soft); color: var(--present); }
.counts .c-a { background: var(--absent-soft); color: var(--absent); }
.counts .c-e { background: var(--excused-soft); color: var(--excused); }
.slist { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.srow { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); transition: background .15s; }
.srow:last-child { border-bottom: 0; }
.srow.st-A { background: var(--absent-soft); }
.srow.st-E { background: var(--excused-soft); }
.srow .num { flex: none; width: 26px; text-align: center; color: var(--muted); font-size: .8rem; font-variant-numeric: tabular-nums; }
.srow .sname { flex: 1; min-width: 0; font-weight: 600; font-size: .95rem; cursor: pointer; user-select: none; }
.srow .sname small { color: var(--muted); font-weight: 400; margin-inline-start: 4px; }
.seg { display: flex; flex: none; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg button {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .82rem; font-weight: 700;
  min-width: 48px; min-height: 40px; padding: 0 6px; cursor: pointer;
}
.seg button + button { border-inline-start: 1px solid var(--line); }
.seg button.on[data-s="P"] { background: var(--present); color: #fff; }
.seg button.on[data-s="A"] { background: var(--absent); color: #fff; }
.seg button.on[data-s="E"] { background: var(--excused); color: #fff; }
.sticky-bar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 15;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.sticky-bar > div { max-width: 860px; margin: 0 auto; display: flex; gap: 8px; }
.sticky-bar .btn { flex: 1; min-height: 50px; font-size: 1.05rem; }
.warnbox { background: var(--warn1-soft); color: var(--warn1); border-radius: 10px; padding: 10px 12px; font-size: .9rem; margin-top: 10px; }

/* ---------- القوائم والجداول */
.list-item { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .t { font-weight: 600; }
.list-item .s { font-size: .82rem; color: var(--muted); }
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }
.pills { display: flex; gap: 6px; flex-wrap: wrap; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { padding: 10px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: var(--surface-2); color: var(--muted); font-weight: 700; font-size: .8rem; position: sticky; top: 0; }
td.name { white-space: normal; min-width: 150px; font-weight: 600; }
td.num, th.num { text-align: center; font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: 0; }
tr.row-warn1 td { background: color-mix(in srgb, var(--warn1-soft) 55%, transparent); }
tr.row-warn2 td { background: color-mix(in srgb, var(--warn2-soft) 55%, transparent); }
tr.row-fail td { background: color-mix(in srgb, var(--fail-soft) 60%, transparent); }

.chips { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 10px; padding-bottom: 2px; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 99px; padding: 6px 14px; font: inherit; font-size: .85rem; font-weight: 600; cursor: pointer;
}
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }

.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.url-box { direction: ltr; text-align: left; font-family: Consolas, monospace; background: var(--surface-2); padding: 10px 12px; border-radius: 10px; margin: 6px 0; font-size: .95rem; user-select: all; }

/* ---------- النافذة المنبثقة والتنبيه */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(8, 12, 20, .5); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
}
.sheet h3 { margin-bottom: 14px; }
.sheet .field { margin-bottom: 12px; }
@media (min-width: 600px) { .modal { align-items: center; } .sheet { border-radius: 20px; } }

#toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px);
  background: #1d2533; color: #fff; padding: 12px 18px; border-radius: 12px; font-size: .92rem;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--absent); }
.loading { text-align: center; color: var(--muted); padding: 48px 0; }

@media (max-width: 420px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .seg button { min-width: 42px; font-size: .78rem; }
  .form-grid { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 700px) {
  .subjects-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .subjects-grid .card { margin: 0; }
  .tiles { grid-template-columns: repeat(6, 1fr); }
}

.sheet .table-wrap table { font-size: .82rem; }
.sheet th, .sheet td { padding: 8px 6px; white-space: normal; }
.sheet td.name { min-width: 0; }
.sheet td[dir=ltr] { white-space: nowrap; }
