/* ==========================================================================
   安全知识考试 —— 统一设计系统（移动优先 · 全设备响应式）
   顺序：设计令牌 → 基础 → 按钮 → 卡片/开始页 → 顶栏 → 答题区 →
         底部操作栏 → 弹窗 → 结果页 → Toast → 响应式增强 → 深色模式 → 动效偏好
   ========================================================================== */

/* ---------- 设计令牌 ---------- */
:root {
  /* 品牌与语义色 */
  --brand: #4f46e5;
  --brand-strong: #4338ca;
  --brand-ink: #4f46e5;
  --brand-soft: #eef2ff;
  --brand-soft-2: #e0e7ff;
  --brand-ring: rgba(79, 70, 229, 0.16);
  --ok: #16a34a;
  --danger: #dc2626;
  --danger-strong: #b91c1c;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --warn-ink: #b45309;
  --warn-soft: #fffbeb;
  --warn-border: #fde68a;

  /* 中性色 */
  --bg: #f1f5f9;
  --bg-tint: #e0e7ff;
  --card: #ffffff;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --overlay: rgba(15, 23, 42, 0.45);
  --focus: #93c5fd;

  /* 圆角 */
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 20px;
  --r-full: 999px;

  /* 阴影 */
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 16px 40px rgba(15, 23, 42, 0.14);
  --shadow-bar: 0 -4px 16px rgba(15, 23, 42, 0.06);

  /* 尺寸 */
  --topbar-h: 60px;
  --nav-h: 68px;
  --content-w: 800px;

  /* 字号 */
  --fs-sm: 13px;
  --fs-md: 15px;
  --fs-lg: 17px;

  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

/* ---------- 基础 ---------- */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", "Segoe UI", system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(1200px 480px at 85% -10%, var(--bg-tint), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--ink);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body.modal-open { overflow: hidden; }

.hidden { display: none !important; }

.screen { min-height: 100vh; min-height: 100dvh; }

h1 {
  margin: 0 0 4px;
  font-size: clamp(22px, 4.5vw, 28px);
  line-height: 1.3;
}

/* ---------- 按钮体系 ---------- */
button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 10px 18px;
  border: 1.5px solid transparent;
  border-radius: var(--r-sm);
  font-size: var(--fs-md);
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s, color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary { background: var(--brand); color: #fff; box-shadow: var(--shadow-sm); }
.btn-danger { background: var(--danger); color: #fff; box-shadow: var(--shadow-sm); }
.btn-ghost { background: var(--card); border-color: var(--border); color: var(--ink-2); }

.btn-lg { width: 100%; min-height: 52px; font-size: var(--fs-lg); }
.btn-sm { min-height: 40px; padding: 8px 16px; }

/* ---------- 卡片 / 开始页 / 结果页共用 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  padding: clamp(24px, 5vw, 40px);
  width: 100%;
  max-width: 640px;
}

#start-screen,
#result-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.hero { text-align: center; }
.hero-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 14px;
  border-radius: 18px;
  background: var(--brand-soft);
  color: var(--brand-ink);
  display: flex;
  align-items: center;
  justify-content: center;
}
.hero-icon svg { width: 34px; height: 34px; }
.hero-sub { margin: 0 0 22px; color: var(--muted); font-size: var(--fs-md); }

.rules {
  list-style: none;
  margin: 0 0 26px;
  padding: 0;
  display: grid;
  gap: 10px;
  text-align: left;
}
.rules li {
  position: relative;
  padding-left: 30px;
  color: var(--ink-2);
  font-size: var(--fs-md);
}
.rules li::before {
  content: "\2713";
  position: absolute;
  left: 0;
  top: 0.18em;
  width: 20px;
  height: 20px;
  border-radius: var(--r-full);
  background: var(--brand-soft);
  color: var(--brand-ink);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rules b { color: var(--ink); }

.hint { text-align: center; color: var(--muted); font-size: var(--fs-sm); margin: 12px 0 0; }

/* ---------- 答题页骨架 ---------- */
#quiz-screen { display: block; }

.topwrap {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.topbar {
  max-width: var(--content-w);
  margin: 0 auto;
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
}

.tb-title { display: none; font-weight: 700; font-size: 16px; white-space: nowrap; }

.timer {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: clamp(15px, 2vw, 19px);
  color: var(--brand-ink);
  background: var(--brand-soft);
  border: 1px solid var(--brand-soft-2);
  border-radius: var(--r-full);
  padding: 5px 14px;
  min-width: 86px;
  text-align: center;
  transition: color 0.2s, background 0.2s, border-color 0.2s;
}
.timer.warn { color: var(--warn-ink); background: var(--warn-soft); border-color: var(--warn-border); }
.timer.critical {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: var(--danger-border);
  animation: pulse 1s infinite;
}
@keyframes pulse { 50% { opacity: 0.55; } }

.progress { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; }

.progress-track { height: 3px; background: var(--border); }
.progress-bar {
  height: 100%;
  width: 0;
  background: var(--brand);
  transition: width 0.25s var(--ease);
}

.quiz-main {
  max-width: var(--content-w);
  margin: 0 auto;
  padding: clamp(16px, 3vw, 28px) 16px;
  padding-bottom: calc(var(--nav-h) + 28px);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 28px);
}

/* ---------- 题目卡片 ---------- */
.question-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-md);
  padding: clamp(20px, 4vw, 32px);
}

.q-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.q-no { font-weight: 700; font-size: clamp(15px, 2vw, 17px); color: var(--brand-ink); }
.q-type {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 12px;
  border-radius: var(--r-full);
  background: var(--brand-soft);
  color: var(--brand-ink);
  border: 1px solid var(--brand-soft-2);
}
.q-type.multi { background: var(--warn-soft); color: var(--warn-ink); border-color: var(--warn-border); }

.q-text {
  font-size: clamp(16px, 2.2vw, 19px);
  font-weight: 500;
  margin: 0 0 20px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

/* ---------- 选项 ---------- */
.options { display: flex; flex-direction: column; gap: 12px; }

.option {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 13px 16px;
  min-height: 52px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  background: var(--card);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.12s, background 0.12s, box-shadow 0.12s;
}
.option:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.option.selected {
  border-color: var(--brand);
  background: var(--brand-soft);
  box-shadow: 0 0 0 3px var(--brand-ring);
}

.opt-badge {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border-radius: var(--r-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: var(--brand-ink);
  background: var(--brand-soft);
  border: 1px solid var(--brand-soft-2);
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.option.selected .opt-badge {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}
.opt-text { flex: 1; font-size: 16px; overflow-wrap: break-word; }

/* ---------- 底部操作栏（手机通栏 / 桌面悬浮胶囊） ---------- */
.nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  gap: 10px;
  padding: 10px 16px;
  padding-bottom: 10px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--card);
  border-top: 1px solid var(--border);
  box-shadow: var(--shadow-bar);
}
.nav .btn { flex: 1; }

/* ---------- 弹窗（手机底部抽屉 / 桌面居中对话框） ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--overlay);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal-content {
  background: var(--card);
  width: 100%;
  max-height: 85vh;
  max-height: 85dvh;
  overflow: auto;
  overscroll-behavior: contain;
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: 6px 20px 20px;
  padding-bottom: 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  box-shadow: var(--shadow-lg);
  animation: sheet-up 0.25s var(--ease);
}
@keyframes sheet-up { from { transform: translateY(24px); opacity: 0.6; } }

.sheet-handle {
  width: 44px;
  height: 5px;
  border-radius: var(--r-full);
  background: var(--border-strong);
  margin: 6px auto 14px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.modal-head h2 { margin: 0; font-size: 19px; }
.modal-content > h2 { margin: 0 0 8px; font-size: 19px; }

.close {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: var(--r-full);
  background: var(--bg);
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.close:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin-bottom: 16px;
  font-size: 14px;
  color: var(--muted);
}
.legend span { display: inline-flex; align-items: center; gap: 7px; }
.dot { width: 12px; height: 12px; border-radius: 4px; display: inline-block; }
.dot.answered { background: var(--brand); }
.dot.current { background: var(--card); border: 2px solid var(--brand); }
.dot.unanswered { background: var(--border); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(46px, 1fr));
  gap: 8px;
}
.grid button {
  aspect-ratio: 1;
  min-height: 44px;
  border-radius: var(--r-sm);
  background: var(--bg);
  border: 1.5px solid var(--border);
  font-weight: 600;
  font-size: 14px;
  color: var(--ink-2);
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.grid button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.grid button.answered { background: var(--brand); color: #fff; border-color: var(--brand); }
.grid button.current { outline: 3px solid var(--focus); outline-offset: 1px; }

.confirm-actions { display: flex; gap: 12px; margin-top: 20px; }
.confirm-actions .btn { flex: 1; }
#confirm-text { margin: 0; color: var(--ink-2); }

/* ---------- 结果页 ---------- */
.result-card {
  text-align: center;
  max-width: 720px;
  max-height: calc(100vh - 48px);
  max-height: calc(100dvh - 48px);
  overflow: auto;
  overscroll-behavior: contain;
}
.score {
  font-size: clamp(48px, 10vw, 68px);
  font-weight: 800;
  line-height: 1.1;
  margin: 8px 0;
  font-variant-numeric: tabular-nums;
}
.score.pass { color: var(--ok); }
.score.fail { color: var(--danger); }
.result-msg { font-size: var(--fs-lg); margin: 0 0 20px; }
.result-msg.pass { color: var(--ok); font-weight: 700; }
.result-msg.fail { color: var(--danger); font-weight: 700; }

.review {
  text-align: left;
  margin: 0 0 24px;
  max-height: 40vh;
  max-height: 40dvh;
  overflow: auto;
  overscroll-behavior: contain;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.review-item { padding: 12px 0; border-bottom: 1px dashed var(--border); font-size: 14px; }
.review-item .r-q { font-weight: 600; margin-bottom: 4px; }
.review-item .r-bad { color: var(--danger); }
.review-item .r-good { color: var(--ok); }
.review-note { color: var(--muted); font-size: 14px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + 16px);
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 16px);
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--ink);
  color: var(--card);
  padding: 11px 20px;
  border-radius: var(--r-sm);
  font-size: 14px;
  z-index: 100;
  box-shadow: var(--shadow-lg);
}

/* ==========================================================================
   响应式增强：平板（≥768px）与桌面（≥1024px）
   ========================================================================== */
@media (min-width: 768px) {
  .tb-title { display: block; }
  .topbar { gap: 14px; padding: 8px 20px; }

  /* 底部操作栏变为居中的悬浮胶囊 */
  .nav {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    bottom: 20px;
    width: min(var(--content-w), calc(100% - 32px));
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-lg);
    padding: 10px 12px;
  }

  /* 弹窗变为居中对话框 */
  .modal { align-items: center; padding: 24px; }
  .modal-content {
    max-width: 640px;
    border-radius: var(--r-lg);
    padding: 24px;
    max-height: 86vh;
    max-height: 86dvh;
    animation: pop-in 0.18s var(--ease);
  }
  .modal-content.small { max-width: 420px; }
  .sheet-handle { display: none; }
  @keyframes pop-in { from { transform: scale(0.96); opacity: 0; } }

  .toast { bottom: 108px; }
}

@media (min-width: 1024px) {
  .quiz-main { padding-top: 32px; }
}

/* ==========================================================================
   悬停效果仅用于精确指针设备，避免触屏"粘住"高亮
   ========================================================================== */
@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover:not(:disabled) { background: var(--brand-strong); }
  .btn-danger:hover:not(:disabled) { background: var(--danger-strong); }
  .btn-ghost:hover:not(:disabled) { border-color: var(--brand); color: var(--brand-ink); background: var(--brand-soft); }
  .option:hover { border-color: var(--focus); }
  .option.selected:hover { border-color: var(--brand); }
  .grid button:hover { border-color: var(--brand); }
  .close:hover { background: var(--border); color: var(--ink); }
}

/* ==========================================================================
   深色模式：仅覆盖令牌，组件自动适配
   ========================================================================== */
@media (prefers-color-scheme: dark) {
  :root {
    --brand: #6366f1;
    --brand-strong: #4f46e5;
    --brand-ink: #a5b4fc;
    --brand-soft: rgba(99, 102, 241, 0.16);
    --brand-soft-2: rgba(99, 102, 241, 0.32);
    --brand-ring: rgba(99, 102, 241, 0.28);
    --ok: #4ade80;
    --danger: #f87171;
    --danger-strong: #ef4444;
    --danger-soft: rgba(248, 113, 113, 0.14);
    --danger-border: rgba(248, 113, 113, 0.35);
    --warn-ink: #fbbf24;
    --warn-soft: rgba(251, 191, 36, 0.12);
    --warn-border: rgba(251, 191, 36, 0.3);

    --bg: #0f172a;
    --bg-tint: #1e2547;
    --card: #1e293b;
    --ink: #f1f5f9;
    --ink-2: #cbd5e1;
    --muted: #94a3b8;
    --border: #334155;
    --border-strong: #475569;
    --overlay: rgba(2, 6, 23, 0.62);
    --focus: #6366f1;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 1px 3px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.5);
    --shadow-bar: 0 -4px 16px rgba(0, 0, 0, 0.3);
  }
}

/* ==========================================================================
   减少动效偏好
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
