:root {
  color-scheme: light;
  --ink: #1e293b;
  --muted: #64748b;
  --brand: #0d9488;
  --brand-dark: #0f766e;
  --brand-soft: #f0fdfa;
  --accent: #d97706;
  --surface: #ffffff;
  --canvas: #f8fafc;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --warning: #d97706;
  --warning-soft: #fffbeb;
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.07), 0 2px 4px -2px rgb(0 0 0 / 0.05);
  --shadow-md: 0 10px 15px -3px rgb(0 0 0 / 0.08), 0 4px 6px -4px rgb(0 0 0 / 0.04);
  --radius: 10px;
}

* { box-sizing: border-box; }

html { min-width: 320px; overflow-x: clip; direction: ltr; }

body {
  margin: 0;
  direction: ltr;
  background: var(--canvas);
  color: var(--ink);
  font-family: Vazirmatn, Tahoma, sans-serif;
  line-height: 1.75;
}

.app-frame { direction: rtl; }

a { color: var(--brand); }
a:hover { color: var(--brand-dark); }

:where(a, button, input, select, textarea):focus-visible {
  outline: 3px solid rgb(23 107 98 / 35%);
  outline-offset: 2px;
}

.topbar {
  min-height: 66px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 8px clamp(16px, 4vw, 56px);
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
  font-size: 1.08rem;
  font-weight: 800;
  white-space: nowrap;
}

.brand::before {
  content: "م";
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: var(--brand);
  color: #fff;
  font-size: 1rem;
}

.school-name {
  margin-inline-start: auto;
  color: var(--muted);
  font-size: .92rem;
}

.topbar form { margin: 0; }

.link-button {
  padding: 7px 12px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
}

.link-button:hover { color: var(--brand); border-color: var(--brand); }

.shell {
  width: min(100%, 1500px);
  min-height: calc(100vh - 116px);
  margin-inline: auto;
  padding: 28px clamp(14px, 3vw, 40px);
  display: grid;
  grid-template-columns: 228px minmax(0, 1fr);
  align-items: start;
  gap: 24px;
}

body:not(:has(.sidebar)) .shell { display: block; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 84px;
  align-self: start;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.sidebar a {
  min-height: 42px;
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  color: #405262;
  text-decoration: none;
  font-size: .92rem;
}

.sidebar a:hover { background: #f2f7f5; color: var(--brand-dark); }
.sidebar a.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 700; }
.sidebar a.active::before { content: ""; width: 3px; height: 20px; margin-inline-end: 8px; border-radius: 3px; background: var(--brand); }

.content { min-width: 0; }

.page-head,
.section-head {
  min-height: 48px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 18px;
}

.page-head h1 { margin: 2px 0; font-size: clamp(1.35rem, 2vw, 1.7rem); line-height: 1.45; }
.page-head p, .section-head h2 { margin: 0; }
.section-head h2 { font-size: 1.08rem; }
.eyebrow { color: var(--brand); font-size: .8rem; font-weight: 700; }
.muted { color: var(--muted); }

.panel,
.stat,
.report-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel { margin-bottom: 18px; padding: 21px; }
.panel h2 { margin: 0 0 12px; font-size: 1.08rem; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.stat { min-height: 94px; padding: 14px 18px; display: flex; flex-direction: column; justify-content: center; color: var(--muted); }
.stat strong { color: var(--ink); font-size: 1.55rem; font-variant-numeric: tabular-nums; }
.grid-two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; }

.quick-links { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.quick-links a,
.section-head a { color: var(--brand); text-decoration: none; }
.quick-links a { padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: .9rem; }
.quick-links a:hover { border-color: var(--brand); background: var(--brand-soft); }

.button {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  line-height: 1.4;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

.button:hover { border-color: var(--brand); color: var(--brand-dark); }
.button.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 700; }
.button.primary:hover { background: var(--brand-dark); }
.button.small { min-height: 34px; padding: 5px 10px; font-size: .86rem; }
.button.full { width: 100%; margin-top: 18px; }
.button:disabled { opacity: .55; cursor: not-allowed; }

.input {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

.input:focus { border-color: var(--brand); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
.field label, .login-card label { display: block; margin-bottom: 5px; font-weight: 650; }
.form-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 9px; margin-top: 8px; }
.field-error { display: block; margin-top: 4px; color: var(--danger); font-size: .84rem; }

.form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  margin-bottom: 1.25rem;
}

.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-inline: contain;
  border-radius: var(--radius);
}
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  text-align: right;
}
th, td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  vertical-align: middle;
}
th {
  position: sticky;
  top: 0;
  background: #f8fafc;
  color: #475569;
  font-size: 0.86rem;
  font-weight: 700;
  white-space: nowrap;
}
td {
  color: #1e293b;
  font-size: 0.92rem;
}
td .button { margin: 2px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #f8fafc; }
.row-actions { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.row-actions form { display: inline-flex; margin: 0; }
.grade-table { min-width: 760px; }
.grade-table th { z-index: 1; }
.grade-table .student-name { min-width: 170px; font-weight: 650; }
.grade-input { width: 112px; min-height: 42px; padding: 6px 10px; text-align: center; font-size: 1rem; font-variant-numeric: tabular-nums; }
.grade-form-footer { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-top: 14px; }
.grade-form-footer p { margin: 0; }
.empty-cell { padding: 28px; color: var(--muted); text-align: center; }

.badge { display: inline-flex; align-items: center; border-radius: 99px; padding: 3px 10px; background: #edf1f2; color: #526371; font-size: .78rem; white-space: nowrap; }
.badge.success { background: var(--success-soft); color: var(--success); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 13px; }
.assignment { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.assignment h2 { margin: 9px 0 2px; }
.assignment p { margin: 0; color: var(--muted); }
.assignment .status-line { margin-top: 6px; color: var(--brand-dark); font-size: .86rem; }
.period-select { display: flex; align-items: end; gap: 20px; color: var(--muted); }
.period-select form { width: min(100%, 340px); display: grid; gap: 5px; }

.alert {
  margin-bottom: 16px;
  padding: 12px 16px;
  border: 1px solid #99f6e4;
  border-radius: var(--radius);
  background: var(--brand-soft);
  color: #115e59;
  font-size: 0.92rem;
  line-height: 1.6;
}
.alert.error { border-color: #fecaca; background: var(--danger-soft); color: var(--danger); }
.alert.success { border-color: #bbf7d0; background: var(--success-soft); color: var(--success); }
.alert.warning { border-color: #fde68a; background: var(--warning-soft); color: #92400e; }
.messages { margin-bottom: 12px; }
.empty, .empty-state { padding: 30px 16px; color: var(--muted); text-align: center; }
.empty-state h2 { color: var(--ink); }
.pagination { display: flex; justify-content: center; align-items: center; gap: 18px; padding: 18px; }
.pagination a { color: var(--brand); }

.login-wrap { width: min(100% - 24px, 460px); margin: clamp(28px, 8vh, 80px) auto; }
.login-card { padding: 32px; border: 1px solid var(--line); border-top: 3px solid var(--brand); border-radius: 14px; background: #fff; box-shadow: var(--shadow); }
.login-card h1 { margin: 6px 0; font-size: 1.45rem; }
.login-card label:not(:first-child) { margin-top: 14px; }

.report-card { width: min(100%, 900px); margin: 0 auto; padding: clamp(20px, 4vw, 42px); }
.report-brand { padding-bottom: 18px; border-bottom: 2px solid var(--brand); text-align: center; }
.report-brand h1 { margin: 5px; font-size: 1.7rem; }
.report-brand p { margin: 3px 0; color: var(--muted); }
.report-meta { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; padding: 19px 0; }
.report-meta div, .report-summary div { min-width: 0; display: flex; flex-direction: column; }
.report-meta span, .report-summary span, .report-note span { color: var(--muted); font-size: .84rem; }
.report-meta strong { overflow-wrap: anywhere; }
.report-summary { display: flex; justify-content: flex-start; gap: 48px; padding: 16px 0; border-top: 1px solid var(--line); text-align: center; }
.report-summary strong { color: var(--brand-dark); font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.report-note { padding: 12px 15px; border: 1px solid var(--line); border-radius: 8px; background: #f7f9f8; }
.report-note p { margin: 5px 0 0; }
.print-button { margin-top: 16px; }
.own-row { background: #edf6f1; font-weight: 700; }

.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;
}

footer { padding: 14px; color: var(--muted); font-size: .8rem; text-align: center; }

@media (max-width: 960px) {
  .shell { grid-template-columns: 1fr; gap: 16px; padding: 20px 16px; }
  .sidebar {
    position: static;
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    padding: 8px;
  }
  .sidebar a {
    flex: 0 0 auto;
    white-space: nowrap;
    min-height: 40px;
  }
  .grid-two { grid-template-columns: 1fr; }
  .class-detail-grid { grid-template-columns: 1fr !important; }
}

@media (max-width: 768px) {
  .topbar {
    padding: 8px 14px;
    gap: 10px;
    flex-wrap: wrap;
  }
  .school-name {
    order: 3;
    width: 100%;
    margin: 2px 0 0 0;
    font-size: 0.85rem;
  }
  .page-head {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
  }
  .page-head > div:last-child {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    width: 100%;
  }
  .page-head .button {
    flex: 1 1 auto;
    text-align: center;
  }
  .form-row-2 {
    grid-template-columns: 1fr;
    gap: 1rem;
    margin-bottom: 1rem;
  }
  .report-meta {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
}

@media (max-width: 620px) {
  .topbar { min-height: 54px; gap: 8px; padding-inline: 10px; }
  .brand { gap: 6px; font-size: 0.95rem; }
  .brand::before { width: 28px; height: 28px; font-size: 0.88rem; }
  .school-name { font-size: 0.8rem; }
  .link-button { padding: 4px 7px; font-size: 0.82rem; }
  .shell { padding: 12px 8px; }
  .sidebar {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    padding: 6px;
    gap: 5px;
    border-radius: 8px;
  }
  .sidebar a {
    min-height: 36px;
    padding: 5px 8px;
    font-size: 0.82rem;
  }
  .page-head { align-items: stretch; flex-direction: column; }
  .page-head h1 { font-size: 1.35rem; }
  .form-grid { grid-template-columns: 1fr; }
  .form-actions { grid-column: auto; }
  .panel { padding: 14px 10px; border-radius: 8px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 8px; }
  .ranking-stats { grid-template-columns: 1fr !important; }
  .stat { min-height: 80px; padding: 10px 12px; }
  .stat strong { font-size: 1.3rem; }
  .period-select { align-items: stretch; flex-direction: column; gap: 8px; }
  .period-select form { width: 100%; }
  .assignment { align-items: stretch; flex-direction: column; gap: 1rem; }
  .assignment .button { width: 100%; text-align: center; }
  .grade-form-footer { align-items: stretch; flex-direction: column; }
  .grade-form-footer .button { width: 100%; }
  .login-wrap { width: calc(100% - 16px); margin: 3vh auto; }
  .login-card { padding: 20px 16px; border-radius: 10px; }
  .button { min-height: 38px; padding: 6px 12px; font-size: 0.88rem; }
  table th, table td { padding: 8px 10px; font-size: 0.86rem; }
}

@media (max-width: 380px) {
  .stats { grid-template-columns: 1fr; }
  .report-meta { grid-template-columns: 1fr; }
  .topbar { padding: 6px 8px; }
  .brand { font-size: 0.9rem; }
  .brand::before { width: 24px; height: 24px; font-size: 0.75rem; }
  .button { width: 100%; justify-content: center; }
}

@page { size: A4 portrait; margin: 14mm; }

@media print {
  html, body { background: #fff !important; color: #111 !important; font-size: 10pt; }
  body { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .topbar, .sidebar, footer, .print-button, .messages { display: none !important; }
  .shell { display: block !important; width: auto; min-height: 0; margin: 0; padding: 0; }
  .content { width: 100%; }
  .report-card { width: auto; max-width: none; margin: 0; padding: 0; border: 0; border-radius: 0; box-shadow: none; }
  .report-brand { padding-bottom: 7mm; border-color: #555; }
  .report-brand h1 { font-size: 18pt; }
  .report-meta { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4mm; padding: 7mm 0; }
  .report-meta div, .report-summary div { break-inside: avoid; }
  .table-wrap { overflow: visible; }
  table { min-width: 0; white-space: normal; }
  thead { display: table-header-group; }
  tr { break-inside: avoid; }
  th, td { padding: 3mm; border-color: #999; }
  .report-summary { padding: 6mm 0; break-inside: avoid; }
  .report-note { background: transparent; border-color: #999; break-inside: avoid; }
  .report-card a { color: #111; }
}
