:root {
  --accent: #2f7cf6;
  --bg: #000;
  --sheet-bg: #fff;
  --text: #1a1a1a;
  --text-dim: #8a8f98;
  --line: #ebedf0;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* iOS 的原生控件样式会盖掉 width/height 并自带内边距，圆形按钮会被拉成横椭圆、
   字形也顶到一边去；而且 Safari 16 之前 <button> 根本不能当 flex/grid 容器，
   place-items 会被整个忽略。所以按钮一律关掉原生外观，圆形按钮的内容
   用「绝对定位 + transform 居中」或 line-height 摆正，不依赖 flex/grid。 */
button { -webkit-appearance: none; appearance: none; font-family: inherit; }

/* .loading / .modal / .camera-fallback 都设了 display，会盖过 UA 样式表里的
   [hidden]{display:none}，必须显式提权，否则遮罩永远关不掉。 */
[hidden] { display: none !important; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
}

.view { position: fixed; inset: 0; display: none; }
.view.active { display: block; }

/* ---------- 访问口令 ---------- */
/* 首屏由 app.js 在拿到 /api/access/status 后才点亮，
   免得没配口令的服务先闪一下口令页。 */
#gate-view { background: #0e0f11; }
#gate-view.active { display: flex; align-items: center; justify-content: center; padding: 0 32px 12vh; }

.gate-card { width: 100%; max-width: 320px; text-align: center; color: #fff; }
.gate-logo {
  width: 60px; height: 60px; margin: 0 auto 18px;
  display: grid; place-items: center;
  border-radius: 19px;
  background: rgba(47,124,246,.15); color: var(--accent);
}
.gate-card h1 { margin: 0 0 6px; font-size: 21px; font-weight: 600; letter-spacing: 2px; }
.gate-tip { margin: 0 0 24px; font-size: 14px; color: var(--text-dim); }

#gate-form { display: flex; flex-direction: column; gap: 12px; }
/* 字号低于 16px 时 iOS 会在聚焦瞬间放大整页，回不去 */
#gate-input {
  width: 100%; padding: 13px 15px;
  border: 1px solid rgba(255,255,255,.13); border-radius: 11px;
  background: rgba(255,255,255,.06); color: #fff;
  font-family: inherit; font-size: 16px; text-align: center; letter-spacing: 1px;
  outline: none; -webkit-appearance: none; appearance: none;
}
#gate-input::placeholder { color: #61666e; letter-spacing: 0; }
#gate-input:focus { border-color: var(--accent); background: rgba(47,124,246,.1); }
#gate-submit {
  padding: 13px 0; border: 0; border-radius: 11px;
  background: var(--accent); color: #fff;
  font-size: 16px; font-weight: 600; letter-spacing: 2px; cursor: pointer;
}
#gate-submit:disabled { opacity: .55; }

.gate-error { margin: 14px 0 0; font-size: 13px; line-height: 1.6; color: #ff6b6b; }
.gate-note { display: block; margin-top: 20px; font-size: 12px; color: #565b63; }

/* ---------- 拍照页 ---------- */
#preview {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover; background: #111;
}

.camera-fallback {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 8px; color: #fff; background: #1b1b1d;
}
.camera-fallback .fallback-icon { font-size: 44px; opacity: .5; }
.camera-fallback p { margin: 0; font-size: 16px; }
.camera-fallback small { color: var(--text-dim); }

.grid-lines { position: absolute; inset: 0; pointer-events: none; }
.grid-lines span { position: absolute; background: rgba(255,255,255,.38); }
.grid-lines .v { top: 0; bottom: 0; width: 1px; }
.grid-lines .h { left: 0; right: 0; height: 1px; }

.camera-top {
  position: absolute; left: 0; right: 0;
  top: calc(var(--safe-top) + 12px);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; z-index: 3;
}

.icon-btn {
  position: relative; flex: 0 0 auto;
  width: 38px; height: 38px;
  min-width: 38px; min-height: 38px;
  padding: 0; border: 0; border-radius: 50%;
  background: rgba(0,0,0,.42); color: #fff;
  cursor: pointer;
}
.icon-btn > svg { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
.icon-btn.small {
  width: 30px; height: 30px; min-width: 30px; min-height: 30px;
  background: transparent; color: var(--text-dim);
}
.icon-btn.floating { position: absolute; top: calc(var(--safe-top) + 8px); left: 12px; z-index: 6; }

.subject-chip {
  border: 0; border-radius: 999px;
  padding: 8px 15px;
  background: rgba(0,0,0,.42); color: #fff;
  font-size: 14px; cursor: pointer;
}
.subject-chip .caret { opacity: .65; margin-left: 2px; }

.camera-hint {
  position: absolute; left: 0; right: 0; top: 46%;
  transform: translateY(-50%);
  margin: 0; padding: 0 32px;
  text-align: center; line-height: 1.75;
  font-size: 17px; color: #fff;
  text-shadow: 0 1px 6px rgba(0,0,0,.55);
  pointer-events: none;
}

.camera-bottom {
  position: absolute; left: 0; right: 0;
  bottom: calc(var(--safe-bottom) + 30px);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 34px; z-index: 3;
}
.album-btn, .bottom-spacer { width: 52px; height: 52px; }
.album-btn {
  display: grid; place-items: center;
  border-radius: 13px; background: rgba(255,255,255,.16);
  color: #fff; cursor: pointer;
}
.shutter {
  position: relative;
  width: 74px; height: 74px; padding: 0;
  border: 4px solid #fff; border-radius: 50%;
  background: transparent; cursor: pointer;
}
.shutter span {
  position: absolute; top: 50%; left: 50%;
  width: 58px; height: 58px; border-radius: 50%;
  background: #fff; transition: transform .12s;
  transform: translate(-50%, -50%);
}
.shutter:active span { transform: translate(-50%, -50%) scale(.9); }

/* ---------- 结果页 ---------- */
/* 照片区 / 题号条 / 详情卡片是一列 flex：
   照片区高度由图片自身比例决定（封顶 48vh），横构图不会再吊在一大片黑底中间，
   下面也不会漏出照片和卡片之间那条灰缝。 */
#result-view { background: #f2f3f5; }
#result-view.active { display: flex; flex-direction: column; }

.result-photo {
  position: relative; flex: 0 0 auto;
  max-height: 48vh;
  overflow-x: hidden; overflow-y: auto;
  background: #17181a;
  transition: max-height .26s cubic-bezier(.32,.72,0,1);
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.result-photo::-webkit-scrollbar { display: none; }
/* 展开态照片整个收掉，题号条顶上去和返回箭头凑成一排 */
#result-view.expanded .result-photo { max-height: 0; }

.photo-stage { position: relative; }
/* 图片撑满宽度、高度自适应：stage 的百分比坐标系就等于图片坐标系，
   overlay 直接 inset:0 覆盖，不用再拿 offsetLeft/clientWidth 对齐 */
#result-image { display: block; width: 100%; height: auto; }

.region-overlay { position: absolute; inset: 0; }
.region-box {
  position: absolute;
  border: 2px solid rgba(47,124,246,.9);
  border-radius: 4px;
  background: rgba(47,124,246,.14);
  transition: opacity .15s;
  cursor: pointer;
}
.region-box.dim { opacity: .32; }
.region-box .badge {
  position: absolute; right: -9px; bottom: -9px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 12px; line-height: 20px; text-align: center;
}

.region-picker {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 11px 16px;
  overflow-x: auto; background: #17181a;
  scrollbar-width: none;
  transition: padding .26s cubic-bezier(.32,.72,0,1);
}
.region-picker::-webkit-scrollbar { display: none; }
/* 左边空出返回箭头的位置，上下 padding 让圆点和箭头对齐到同一条中线 */
#result-view.expanded .region-picker { padding: calc(var(--safe-top) + 10px) 16px 10px 58px; }
.region-dot {
  flex: 0 0 auto;
  width: 34px; height: 34px;
  min-width: 34px; min-height: 34px;
  padding: 0; border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,.35);
  background: transparent; color: rgba(255,255,255,.8);
  /* 34 减掉上下各 1.5px 边框，正好把数字压在正中 */
  font-size: 14px; line-height: 31px; text-align: center;
  cursor: pointer;
}
.region-dot.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* 详情卡片：flex 剩余空间，展开/收起靠上面照片区的高度变化带动 */
.sheet {
  position: relative; flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column;
  background: var(--sheet-bg);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -3px 18px rgba(0,0,0,.13);
  z-index: 4;
}

.sheet-handle { padding: 9px 0 5px; display: grid; place-items: center; cursor: grab; touch-action: none; }
.sheet-handle span { width: 36px; height: 4px; border-radius: 2px; background: #d6d9de; }

.result-tabs {
  display: flex; gap: 20px;
  padding: 4px 18px 0;
  overflow-x: auto; border-bottom: 1px solid var(--line);
  scrollbar-width: none; flex: 0 0 auto;
}
.result-tabs::-webkit-scrollbar { display: none; }
.result-tab {
  flex: 0 0 auto; position: relative;
  padding: 9px 0 11px; border: 0; background: none;
  font-size: 15px; color: var(--text-dim); cursor: pointer;
}
.result-tab.active { color: var(--text); font-weight: 600; }
.result-tab.active::after {
  content: ''; position: absolute; left: 50%; bottom: 0;
  transform: translateX(-50%);
  width: 22px; height: 3px; border-radius: 2px; background: var(--accent);
}

.sheet-body {
  flex: 1; overflow-y: auto;
  padding: 16px 18px calc(var(--safe-bottom) + 20px);
  -webkit-overflow-scrolling: touch;
}

/* 竖条用 inline-block 而不是 flex：<summary> 上挂 flex 在旧 Safari 上不稳 */
.block-title { margin: 0 0 10px; font-size: 15px; font-weight: 600; }
.block-title::before {
  content: ''; display: inline-block; vertical-align: -1px;
  width: 3px; height: 14px; margin-right: 7px;
  border-radius: 2px; background: var(--accent);
}
.block + .block { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--line); }

/* 识别题目：默认折起来，标题整行都是开关 */
summary.block-title {
  position: relative; padding-right: 34px;
  cursor: pointer; list-style: none;
}
summary.block-title::-webkit-details-marker { display: none; }
details:not([open]) > summary.block-title { margin-bottom: 0; }
.fold-hint {
  position: absolute; right: 0; top: 0;
  font-size: 12px; font-weight: 400; color: var(--text-dim);
}
.fold-hint::after { content: '展开'; }
details[open] .fold-hint::after { content: '收起'; }

.meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tag {
  padding: 3px 9px; border-radius: 4px;
  background: #f0f3f8; color: #5a6270; font-size: 12px;
}
.tag.accent { background: rgba(47,124,246,.1); color: var(--accent); }

.rich { font-size: 15px; line-height: 1.85; color: var(--text); word-break: break-word; }
.rich p { margin: 0 0 8px; }
.rich img { max-width: 100%; height: auto; }
.rich table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 14px; }
.rich td, .rich th { border: 1px solid #cfd4db; padding: 5px 7px; text-align: center; }
.rich .katex { font-size: 1em; }
/* 材料是几道小问共用的情境，跟题干区分开 */
.rich.material {
  margin-bottom: 12px; padding: 10px 12px;
  background: #f6f7f9; border-radius: 8px; font-size: 14px;
}

.ocr-text {
  font-size: 14px; line-height: 1.8; color: #4a5058;
  background: #f6f7f9; border-radius: 8px; padding: 12px 13px;
  white-space: pre-wrap; word-break: break-word;
}

.empty { padding: 40px 0; text-align: center; color: var(--text-dim); font-size: 14px; }

/* ---------- 弹层 ---------- */
.modal { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; }
.modal-mask { position: absolute; inset: 0; background: rgba(0,0,0,.42); }
.modal-panel {
  position: relative; width: 100%;
  max-height: 78vh; overflow-y: auto;
  padding: 4px 18px calc(var(--safe-bottom) + 22px);
  background: #fff; border-radius: 16px 16px 0 0;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 0 6px; }
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.seg {
  display: flex; gap: 4px; padding: 4px;
  margin: 10px 0 16px;
  background: #f2f3f5; border-radius: 9px;
}
.seg button {
  flex: 1; padding: 8px 0; border: 0; border-radius: 7px;
  background: transparent; color: var(--text-dim);
  font-size: 14px; cursor: pointer;
}
.seg button.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.subject-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.subject-grid button {
  padding: 11px 0; border: 1px solid var(--line); border-radius: 9px;
  background: #fff; font-size: 14px; color: var(--text); cursor: pointer;
}
.subject-grid button.active { border-color: var(--accent); background: rgba(47,124,246,.07); color: var(--accent); font-weight: 600; }

.captcha-manual .tip, .tip { font-size: 13px; line-height: 1.7; color: var(--text-dim); }
.captcha-manual code { background: #f2f3f5; padding: 1px 4px; border-radius: 3px; font-size: 12px; }
.captcha-manual textarea {
  width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 8px;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; line-height: 1.6;
  resize: vertical;
}

/* ---------- 反馈 ---------- */
.toast {
  position: fixed; left: 50%; bottom: 88px; transform: translateX(-50%);
  max-width: 82vw; padding: 10px 16px;
  background: rgba(0,0,0,.82); color: #fff;
  border-radius: 8px; font-size: 14px; line-height: 1.5;
  text-align: center; z-index: 40;
}

.loading {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px;
  background: rgba(0,0,0,.45); color: #fff; font-size: 14px;
}
.loading p { margin: 0; }
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.28);
  border-top-color: #fff;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
