:root {
  --blue: #0b5ed7;
  --blue-dark: #073b79;
  --blue-soft: #eaf3ff;
  --green: #15915b;
  --green-soft: #e9f8f0;
  --orange: #f28c28;
  --orange-soft: #fff3e5;
  --red: #d94343;
  --red-soft: #ffecec;
  --purple: #7c5cff;
  --purple-soft: #f3f0ff;
  --text: #14213d;
  --muted: #6b7280;
  --line: #dce3ee;
  --bg: #f6f9fd;
  --card: #fff;
  --shadow: 0 22px 55px rgba(20, 32, 51, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Arial, "Noto Sans Thai", "Sarabun", sans-serif;
  background: url("assets/point.png") center center / cover no-repeat fixed;
  color: var(--text);
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.lang-toggle {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 50;
  min-width: 48px;
  padding: 8px 16px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  color: var(--text);
  font-weight: 800;
  font-size: 13px;
  box-shadow: 0 8px 20px rgba(20, 32, 51, 0.18);
}

.lang-toggle:hover {
  background: #fff;
}

/* ---------- LOGIN / STATE SCREENS ---------- */

.login-shell,
.state-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: transparent;
}

.login-panel,
.state-panel {
  width: min(100%, 470px);
  display: grid;
  gap: 18px;
  padding: 40px 36px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: 0 30px 70px rgba(40, 30, 90, 0.35);
}

.login-panel {
  justify-items: center;
  text-align: center;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: inline-block;
  object-fit: contain;
  background: var(--blue-soft);
  border-radius: 10px;
  padding: 4px;
}

.login-logo-big {
  width: 160px;
  max-width: 70%;
  height: auto;
  object-fit: contain;
  margin: 0 auto 6px;
}

.login-copy {
  margin: 12px 0 8px;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--blue);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

.login-copy h1,
.state-panel h1 {
  margin-bottom: 12px;
  font-size: 2.1rem;
  line-height: 1.15;
  color: #102a56;
}

.login-copy p:last-child {
  color: var(--muted);
  font-size: 1.02rem;
  line-height: 1.6;
}

.auth-box {
  min-height: 100px;
  display: grid;
  gap: 12px;
  justify-items: center;
  width: 100%;
}

.auth-box #googleSignIn {
  display: flex;
  justify-content: center;
}

.admin-link {
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: none;
}

.admin-link:hover {
  color: var(--blue);
  text-decoration: underline;
}

.ghost-button {
  min-width: 200px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--text);
  background: #fff;
  font-weight: 750;
}

.setup-note,
.auth-error {
  max-width: 320px;
  margin: 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.55;
}

.auth-error {
  color: var(--red);
  font-weight: 700;
}

.state-panel p {
  color: var(--muted);
  line-height: 1.65;
}

.state-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.loader {
  width: 48px;
  height: 48px;
  border: 5px solid #dbe7f7;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.error-panel {
  border-color: #ffd2d2;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- DASHBOARD ---------- */

.app-grid {
  display: grid;
  grid-template-columns: 230px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: linear-gradient(180deg, #063f86 0%, #052e66 100%);
  color: white;
  padding: 24px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar .brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  margin-bottom: 28px;
  font-size: 15px;
  text-align: center;
}

.sidebar .brand-icon {
  width: 96px;
  height: 96px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.14);
  object-fit: contain;
  padding: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 12px;
  margin: 6px 0;
  opacity: 0.92;
  font-size: 15px;
  color: #fff;
  background: transparent;
  border: 0;
  width: 100%;
  text-align: left;
}

.nav-item.active {
  background: #2a7be8;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
}

.nav-item.logout {
  margin-top: 24px;
  opacity: 0.9;
}

.main {
  padding: 28px;
  min-width: 0;
}

.admin-preview-banner {
  background: #fff3cd;
  border: 1px solid #ffe69c;
  color: #664d03;
  padding: 10px 16px;
  border-radius: 10px;
  margin-bottom: 16px;
  font-weight: 600;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin-bottom: 20px;
}

.topbar h1 {
  margin: 0;
  font-size: 28px;
  color: #102a56;
  line-height: 1.1;
}

.subtitle {
  color: var(--blue);
  font-weight: 700;
  margin-top: 4px;
  font-size: 16px;
}

.student-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 300px;
  box-shadow: 0 8px 24px rgba(16, 42, 86, 0.06);
}

.avatar,
.avatar-img {
  width: 54px;
  height: 54px;
  border-radius: 50%;
}

.avatar {
  background: var(--blue-soft);
  color: var(--blue);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
}

.avatar-img {
  object-fit: cover;
}

.student-name {
  font-weight: 800;
}

.student-meta {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}

.kpis {
  display: grid;
  grid-template-columns: repeat(5, minmax(150px, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}

.kpi {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  min-height: 150px;
  box-shadow: 0 8px 24px rgba(16, 42, 86, 0.05);
  position: relative;
  overflow: hidden;
}

.kpi.blue {
  border-left: 4px solid var(--blue);
}

.kpi.green {
  border-left: 4px solid var(--green);
}

.kpi.purple {
  border-left: 4px solid var(--purple);
}

.kpi.orange {
  border-left: 4px solid var(--orange);
}

.kpi-title {
  font-size: 14px;
  font-weight: 800;
  color: #31517d;
}

.kpi-value {
  margin-top: 22px;
  font-size: 32px;
  font-weight: 900;
  color: #0d1b38;
  white-space: nowrap;
}

.kpi-value-danger {
  color: var(--red);
}

.kpi-value-warning {
  color: var(--orange);
}

.kpi-centered {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.kpi-sub {
  margin-top: 8px;
  color: var(--muted);
  font-size: 14px;
}

.badge-icon {
  position: absolute;
  right: 16px;
  top: 16px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--blue-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.green .badge-icon {
  background: var(--green-soft);
  color: var(--green);
}

.purple .badge-icon {
  background: var(--purple-soft);
  color: var(--purple);
}

.orange .badge-icon {
  background: var(--orange-soft);
  color: var(--orange);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(600px, 1fr) 360px;
  gap: 18px;
  align-items: start;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 8px 24px rgba(16, 42, 86, 0.05);
  overflow: hidden;
}

.panel-head {
  padding: 18px 20px 8px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-title {
  margin: 0;
  font-size: 18px;
  font-weight: 900;
  color: #102a56;
}

.chips {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  padding: 0 20px 14px 20px;
}

.chip {
  border: 1px solid var(--line);
  background: white;
  color: #24364f;
  padding: 8px 14px;
  border-radius: 12px;
  font-weight: 700;
  font-size: 13px;
}

.chip.active {
  background: var(--blue);
  color: white;
  border-color: var(--blue);
}

.table-wrap {
  overflow: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

th,
td {
  padding: 13px 14px;
  border-top: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 14px;
}

th {
  background: #f8fbff;
  font-weight: 900;
  color: #253b63;
  white-space: nowrap;
}

td {
  max-width: 260px;
}

.truncate-cell {
  display: block;
}

.truncate-cell .truncate-text {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.truncate-cell.expanded .truncate-text {
  white-space: normal;
  word-break: break-word;
}

.truncate-toggle {
  border: 0;
  background: transparent;
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
  padding: 2px 0 0;
  cursor: pointer;
}

.status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  padding: 6px 10px;
  font-weight: 900;
  font-size: 12px;
  white-space: nowrap;
}

.status.graded {
  background: var(--green-soft);
  color: var(--green);
}

.status.pending {
  background: var(--orange-soft);
  color: #c76500;
}

.status.missing {
  background: var(--red-soft);
  color: var(--red);
}

.status.overdue {
  background: #fff0f0;
  color: #a40000;
}

.status.submitted {
  background: #e9f2ff;
  color: #0b5ed7;
}

.status.muted {
  background: #eef2f7;
  color: #667085;
}

.status.redo {
  background: #f3e8ff;
  color: #7c3aed;
}

.row-icon {
  font-size: 20px;
  margin-right: 9px;
}

/* Days-left badge */
.days-left {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  white-space: nowrap;
}
.days-left.urgent {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fca5a5;
  animation: pulse-badge 1.5s ease-in-out infinite;
}
.days-left.warn {
  background: #fff7ed;
  color: #c2410c;
  border: 1px solid #fdba74;
}
.days-left.ok {
  background: #f0fdf4;
  color: #15803d;
  border: 1px solid #86efac;
}
@keyframes pulse-badge {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* Urgent banner */
.urgent-banner {
  margin: 0 0 18px 0;
  background: linear-gradient(135deg, #fff1f2 0%, #fff7ed 100%);
  border: 2px solid #f87171;
  border-radius: 14px;
  padding: 16px 20px;
}
.urgent-banner-title {
  font-size: 16px;
  font-weight: 800;
  color: #dc2626;
  margin-bottom: 12px;
  letter-spacing: 0.01em;
}
.urgent-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #fff;
  border-radius: 8px;
  margin-bottom: 6px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.urgent-item:last-child { margin-bottom: 0; }
.urgent-item-name {
  font-weight: 600;
  font-size: 14px;
  color: #1e293b;
}
.urgent-item-days {
  font-size: 13px;
  font-weight: 800;
  padding: 3px 12px;
  border-radius: 20px;
  white-space: nowrap;
}
.urgent-item-days.red {
  background: #fee2e2;
  color: #b91c1c;
}
.urgent-item-days.orange {
  background: #ffedd5;
  color: #c2410c;
}

.side-stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.mini-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 8px 24px rgba(16, 42, 86, 0.05);
}

.mini-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  color: var(--blue);
  font-weight: 900;
}

.pill {
  background: var(--blue-soft);
  color: var(--blue);
  border-radius: 9px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 900;
}

.pill.orange {
  background: var(--orange-soft);
  color: #c76500;
}

.task {
  padding: 9px 0;
  border-top: 1px dashed var(--line);
}

.task:first-of-type {
  border-top: 0;
}

.task-name {
  font-weight: 800;
}

.task-meta {
  font-size: 13px;
  color: var(--red);
  margin-top: 3px;
}

.task-meta.neutral {
  color: var(--muted);
}

.rules {
  background: linear-gradient(180deg, #f7fbff 0%, #fff 100%);
  border-color: #b7d4ff;
}

.rules ul {
  margin: 8px 0 0 18px;
  padding: 0;
}

.rules li {
  margin: 6px 0;
  font-size: 13px;
  line-height: 1.5;
}

.donut {
  width: 98px;
  height: 98px;
  border-radius: 50%;
  margin: 12px auto 8px auto;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.donut::before {
  content: "";
  position: absolute;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: white;
}

.donut span {
  position: relative;
  z-index: 1;
  font-weight: 900;
  font-size: 20px;
}

@media (max-width: 1100px) {
  .app-grid {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .kpis {
    grid-template-columns: repeat(2, 1fr);
  }

  .content-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    flex-direction: column;
  }

  .student-card {
    width: 100%;
    min-width: 0;
  }
}

/* ---------- ADMIN ---------- */

.admin-shell {
  min-height: 100vh;
  padding: 24px;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.admin-header h1 {
  font-size: 22px;
  color: #102a56;
  margin: 0;
}

.admin-email {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.admin-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.admin-tab {
  padding: 9px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-weight: 800;
  font-size: 13px;
}

.admin-tab.active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

.admin-panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 8px 24px rgba(16, 42, 86, 0.05);
  overflow: auto;
}

.admin-filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.admin-filter-bar select,
.admin-filter-bar input {
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  background: #fff;
}

.admin-filter-bar input {
  flex: 1;
  min-width: 200px;
}

.admin-summary-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  font-weight: 700;
}

.admin-summary-bar select {
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  background: #fff;
}

.admin-filter-count {
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
}

.admin-table th,
.admin-table td {
  border: 1px solid var(--line);
  padding: 0;
  font-size: 13px;
}

.admin-table th {
  padding: 8px;
  background: #f8fbff;
  white-space: nowrap;
}

.admin-table input {
  width: 100%;
  min-width: 100px;
  padding: 7px 8px;
  border: 0;
  background: transparent;
  font-size: 13px;
}

.admin-table input:focus {
  outline: 2px solid var(--blue);
}

.admin-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.admin-button {
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-weight: 800;
  font-size: 13px;
}

.admin-button.primary {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

.admin-button.danger {
  color: var(--red);
}

.admin-button:disabled,
.admin-table input:disabled,
.admin-row-delete:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.admin-lock-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 12px 16px;
  margin-bottom: 16px;
  border-radius: 12px;
  font-weight: 750;
  font-size: 13px;
}

.admin-lock-bar.locked {
  background: var(--orange-soft);
  color: #92400e;
}

.admin-lock-bar.unlocked {
  background: var(--green-soft);
  color: var(--green);
}

.admin-row-delete {
  color: var(--red);
  font-weight: 900;
  border: 0;
  background: transparent;
  padding: 6px 10px;
}

.admin-row-view {
  color: var(--blue);
  font-weight: 600;
  border: 0;
  background: transparent;
  padding: 6px 10px;
  white-space: nowrap;
}

.admin-message {
  margin-bottom: 14px;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 750;
}

.admin-message.success {
  color: #14633a;
  background: #dff7e9;
}

.admin-message.error {
  color: #a21d1d;
  background: #ffe3e3;
}

.admin-login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: transparent;
}

.admin-login-card {
  width: min(100%, 380px);
  display: grid;
  gap: 14px;
  padding: 30px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.admin-login-card input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.sync-btn { background: #0ea5e9; color: #fff; border-color: #0284c7; }
.sync-btn:hover:not(:disabled) { background: #0284c7; }

.admin-row-qr { margin-left:4px; padding:3px 8px; background:#7c3aed; color:#fff; border:none; border-radius:6px; cursor:pointer; font-size:11px; font-weight:700; }
.admin-row-qr:hover { background:#6d28d9; }
.qr-overlay { position:fixed; inset:0; background:rgba(0,0,0,0.85); z-index:9999; display:flex; align-items:center; justify-content:center; }
.qr-modal { background:#fff; border-radius:20px; padding:36px 40px; text-align:center; position:relative; max-width:480px; width:90%; box-shadow:0 20px 60px rgba(0,0,0,0.5); }
.qr-close { position:absolute; top:12px; right:16px; background:none; border:none; font-size:22px; cursor:pointer; color:#666; line-height:1; }
.qr-close:hover { color:#111; }
.qr-student-name { font-size:26px; font-weight:800; color:#111; margin-bottom:6px; }
.qr-student-id { font-size:18px; font-weight:600; color:#6d28d9; letter-spacing:2px; margin-bottom:20px; font-family:ui-monospace,monospace; }
.qr-image { width:280px; height:280px; border-radius:12px; border:3px solid #ede9fe; display:block; margin:0 auto; }

.qr-countdown { position:absolute; top:12px; right:48px; font-size:13px; font-weight:700; color:#7c3aed; background:#ede9fe; border-radius:20px; padding:2px 10px; min-width:32px; text-align:center; }
.qr-btn { display:inline-flex; align-items:center; gap:5px; margin-left:12px; padding:6px 12px; background:#7c3aed; color:#fff; border:none; border-radius:20px; cursor:pointer; font-size:13px; font-weight:700; vertical-align:middle; }
.qr-btn:hover { background:#6d28d9; }

.score-toast { display:flex; align-items:center; gap:12px; margin-top:16px; padding:14px 18px; background:linear-gradient(135deg,#10b981,#059669); color:#fff; border-radius:16px; box-shadow:0 4px 20px rgba(16,185,129,0.4); animation:score-toast-in 0.4s cubic-bezier(0.34,1.56,0.64,1) both; }
.score-toast.score-toast-out { animation:score-toast-out 0.4s ease-in both; }
.score-toast-emoji { font-size:2rem; line-height:1; flex-shrink:0; }
.score-toast-body { display:flex; flex-direction:column; gap:2px; }
.score-toast-score { font-size:1.1rem; font-weight:700; }
.score-toast-score strong { font-size:1.4rem; }
.score-toast-name { font-size:0.85rem; opacity:0.9; }
@keyframes score-toast-in { from { opacity:0; transform:translateY(20px) scale(0.9); } to { opacity:1; transform:translateY(0) scale(1); } }
@keyframes score-toast-out { from { opacity:1; transform:scale(1); } to { opacity:0; transform:scale(0.9); } }
