:root {
  --bg: #f4f4f9;
  --surface: #ffffff;
  --grid-bg: #e0e0e0;
  --cell-bg: #f9f9f9;
  --cell-empty: #eeeeee;
  --student-bg: #ffffff;
  --border: #ccc;
  --primary: #4CAF50;
  --primary-dark: #388E3C;
  --danger: #f44336;
  --danger-bg: #ffebee;
  --text: #333333;
  --text-muted: #777777;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --radius: 10px;
  --cell-min: 52px;
}

body.dark {
  --bg: #121212;
  --surface: #1e1e1e;
  --grid-bg: #2a2a2a;
  --cell-bg: #2c2c2c;
  --cell-empty: #252525;
  --student-bg: #1e1e1e;
  --border: #444;
  --text: #e0e0e0;
  --text-muted: #999;
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
  --danger-bg: #3e2723;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--surface);
  padding: 10px 15px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  z-index: 20;
}

.header-left, .header-right { display: flex; align-items: center; gap: 8px; }
.header-center { display: flex; align-items: center; gap: 6px; flex: 1; justify-content: center; flex-wrap: wrap; }

#classSelect { min-width: 120px; max-width: 160px; }
.logo { font-weight: 800; font-size: 1.2rem; color: var(--primary); user-select: none; }

select, button, .btn {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 8px 12px;
  cursor: pointer;
  touch-action: manipulation;
}

.icon-btn { padding: 8px 10px; font-size: 1rem; line-height: 1; }
.icon-btn.danger { border-color: var(--danger); }

.mode-tabs { display: flex; gap: 4px; }
.mode-tab {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius);
  padding: 8px 14px;
  font-weight: 600;
}
.mode-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

#app { flex: 1; overflow: auto; padding: 15px; }

.view { display: none; }
.view.active { display: block; }

.toolbar {
  background: var(--surface);
  padding: 12px 15px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 15px;
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
}

.tool-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tool-group-title { font-weight: 700; color: var(--text-muted); }
.tool-group.hint { color: var(--text-muted); font-size: 0.9rem; }

input[type="range"] {
  width: 100%;
  max-width: 120px;
  accent-color: var(--primary);
  vertical-align: middle;
}
.grid-selects { display: flex; flex-direction: column; gap: 6px; }
.grid-select { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; }
.grid-select span { min-width: 60px; font-weight: 600; }
.grid-select select { min-width: 60px; padding: 4px 8px; }
.tool-radio input { display: none; }
.tool-label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 12px;
  background: var(--cell-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
}
.tool-radio input:checked + .tool-label {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.live-toolbar { justify-content: space-between; }
.live-hint { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 0.95rem; }
.live-actions { display: flex; align-items: center; gap: 8px; }
.session-info {
  background: var(--surface);
  padding: 10px 15px;
  border-radius: var(--radius);
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  font-weight: 600;
  color: var(--text-muted);
}
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }
.dot.ma { background: var(--primary); }
.dot.menu { background: #2196F3; }

.grid-container {
  display: grid;
  gap: 10px;
  background: var(--grid-bg);
  padding: 15px;
  border-radius: var(--radius);
  min-height: 50vh;
  touch-action: pan-y;
}

.cell {
  background: var(--cell-empty);
  border: 2px dashed #bbb;
  border-radius: var(--radius);
  min-height: var(--cell-min);
  aspect-ratio: 1 / 1;
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cell.empty::before {
  content: '+';
  font-size: 1.5rem;
  color: #bbb;
}

.cell.selected-move {
  border-color: #2196F3;
  background: #e3f2fd;
}

.cell.live-cell {
  cursor: pointer;
  border-style: solid;
  border-color: var(--border);
}

.student {
  width: 100%;
  height: 100%;
  background: var(--student-bg);
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 0 2px 5px rgba(0,0,0,0.08);
  transition: transform 0.08s;
  padding: 4px;
}
.student:active { transform: scale(0.96); }
.student.absent {
  background: var(--danger-bg);
  border-color: var(--danger);
  opacity: 0.75;
}

.initials { font-size: clamp(0.95rem, 4vw, 1.4rem); font-weight: 800; }
.badges {
  display: flex;
  gap: 6px;
  margin-top: 4px;
  font-size: clamp(0.65rem, 2.5vw, 0.85rem);
  font-weight: 700;
  flex-wrap: wrap;
  justify-content: center;
  line-height: 1;
}
.badge { display: inline-flex; align-items: center; gap: 2px; }
.badge.ma { color: var(--primary-dark); }
.badge.ha { color: #e65100; }
.badge.material { color: #f9a825; }
.badge.absent { color: var(--danger); }

.editor-student { position: relative; }
.editor-student::after {
  content: '⋯';
  position: absolute;
  bottom: 2px;
  right: 6px;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
}

/* Modals */
.modal, .overlay { display: none; }
.modal.open, .overlay.open { display: flex; }
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 40;
}
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}
.modal-content h3 { margin-top: 0; }
.modal-content label { display: block; margin: 12px 0 6px; font-weight: 600; }
.modal-content input[type="text"] {
  width: 100%;
  padding: 10px;
  font-size: 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-transform: uppercase;
}
.modal-actions, .settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}
.settings-actions { flex-direction: column; }
.settings-actions .btn, .settings-actions .file-label { width: 100%; text-align: center; }

.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.file-label { display: inline-block; }

/* Live menu */
.live-menu {
  display: none;
  position: absolute;
  z-index: 60;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px;
  min-width: 180px;
}
.live-menu.open { display: block; }
.live-menu-title { font-weight: 700; margin-bottom: 8px; text-align: center; }
.live-menu-actions { display: flex; flex-direction: column; gap: 6px; }
.live-menu-btn {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  background: var(--cell-bg);
  font-weight: 600;
  cursor: pointer;
}
.live-menu-btn.ma { color: var(--primary-dark); border-color: var(--primary); }
.live-menu-btn.ma-minus { color: #c62828; }
.live-menu-btn.ha { color: #e65100; }
.live-menu-btn.material { color: #f57f17; }
.live-menu-btn.absent { color: var(--danger); border-color: var(--danger); }

/* Stats table */
.stats-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.stats-table th, .stats-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.stats-table th { background: var(--grid-bg); font-weight: 700; }
.stats-table tr:last-child td { border-bottom: none; }
.stats-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: #222;
  color: #fff;
  padding: 10px 18px;
  border-radius: 24px;
  font-weight: 600;
  z-index: 70;
  transition: transform 0.25s ease;
}
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 700px) {
  .app-header { padding: 8px 10px; gap: 8px; }
  .header-center { order: 3; width: 100%; justify-content: flex-start; gap: 4px; }
  .header-right { margin-left: auto; }
  .mode-tab { padding: 6px 10px; font-size: 0.85rem; }
  #app { padding: 8px; }
  .toolbar { padding: 10px; gap: 10px; }
  .tool-group-title { font-size: 0.85rem; }
  .tool-label { padding: 6px 10px; font-size: 0.9rem; }
  .grid-container { gap: 6px; padding: 8px; min-height: unset; }
  .cell { min-height: var(--cell-min); }
  .initials { font-size: clamp(0.85rem, 5vw, 1.2rem); }
  .badges { font-size: clamp(0.6rem, 2.5vw, 0.75rem); gap: 3px; }
  .icon-btn { padding: 6px 8px; }
  select { font-size: 0.9rem; padding: 6px 8px; }
}

@media (max-width: 380px) {
  .grid-container { gap: 4px; padding: 6px; }
  .mode-tab { padding: 5px 8px; font-size: 0.8rem; }
}

@media (min-width: 1024px) and (max-height: 1400px) {
  .grid-container { min-height: 65vh; }
  .cell { --cell-min: 70px; }
}
