*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f5f6f8;
  color: #1a1a1a;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

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

/* ── Shell ── */
.shell {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

/* ── 4×4 Grid ── */
.grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(6px, 1.2vw, 12px);
  min-height: 0;
}

/* ── Card button ── */
.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(4px, 1vw, 10px);
  padding: clamp(6px, 1.5vw, 14px) 6px;
  border: none;
  border-radius: clamp(12px, 2vw, 18px);
  cursor: pointer;
  font-family: inherit;
  min-height: 0;
  min-width: 0;
  transition: transform 0.1s;
}

.card:active {
  transform: scale(0.95);
}

.card:focus-visible {
  outline: 4px solid #333;
  outline-offset: 2px;
}

.card-icon {
  font-size: clamp(24px, 5vw, 42px);
  line-height: 1;
}

.card-label {
  font-size: clamp(11px, 2.2vw, 17px);
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Teacher note card description ── */
.card-teacher {
  gap: clamp(2px, 0.6vw, 6px);
}

.card-desc {
  font-size: clamp(7px, 1.3vw, 11px);
  font-weight: 400;
  line-height: 1.25;
  text-align: center;
  opacity: 0.65;
  padding: 0 4px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

/* ── Placeholder card ── */
.card.placeholder {
  background: transparent !important;
  border: 3px dashed #ccc;
  cursor: default;
  opacity: 0.4;
}

.card.placeholder:active {
  transform: none;
}

/* ── Fullscreen overlay ── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  animation: fadeIn 0.15s ease-out;
}

.overlay.hidden {
  display: none;
}

.overlay-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 3vw, 24px);
  padding: 20px;
  width: 100%;
  height: 100%;
  position: relative;
}

.overlay-icon {
  font-size: clamp(60px, 16vw, 130px);
  line-height: 1;
}

.overlay-text {
  font-size: clamp(40px, 10vw, 90px);
  font-weight: 800;
  line-height: 1.15;
  text-align: center;
  padding: 0 16px;
  max-width: 100%;
  word-wrap: break-word;
}

.overlay-hint {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  font-size: 13px;
  font-weight: 400;
  opacity: 0.35;
  white-space: nowrap;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ── Teacher message panel ── */
.teacher-panel {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.teacher-panel.hidden {
  display: none;
}

.teacher-panel-inner {
  background: #fff;
  border-radius: 20px;
  width: min(560px, 95vw);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.teacher-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
}

.teacher-title {
  font-size: 18px;
  font-weight: 700;
  color: #333;
}

.teacher-close {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: #f0f0f0;
  color: #666;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.teacher-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  min-height: 120px;
}

.teacher-msg {
  padding: 10px 14px;
  margin-bottom: 10px;
  background: #f0f7ff;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.45;
  color: #333;
}

.teacher-msg-time {
  display: block;
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.teacher-msg-empty {
  text-align: center;
  color: #bbb;
  font-size: 15px;
  padding: 20px 0;
}

.teacher-input-row {
  display: flex;
  gap: 10px;
  padding: 12px 20px 16px;
  border-top: 1px solid #eee;
}

.teacher-input {
  flex: 1;
  border: 2px solid #ddd;
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}

.teacher-input:focus {
  border-color: #378ADD;
}

.teacher-send {
  padding: 10px 20px;
  border: none;
  border-radius: 12px;
  background: #378ADD;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.teacher-send:active {
  background: #2a6db8;
}

/* ── Cache toast ── */
.cache-toast {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  z-index: 50;
  animation: toastIn 0.3s ease-out;
}

.cache-toast.hidden {
  display: none;
}

.cache-toast.done {
  background: rgba(29, 158, 117, 0.85);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ── Responsive: larger tablets ── */
@media (min-width: 768px) {
  .grid {
    gap: clamp(8px, 1.5vw, 14px);
  }

  .card {
    border-radius: 18px;
    gap: clamp(6px, 1.2vw, 12px);
  }

  .card-icon {
    font-size: clamp(30px, 5.5vw, 48px);
  }

  .card-label {
    font-size: clamp(14px, 2.5vw, 19px);
  }

  .card-desc {
    font-size: clamp(9px, 1.4vw, 12px);
  }
}
