* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: #203039; background: #edf2f3; overflow: hidden;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
.muted { color: #51646e; font-size: 12px; }

button, input, textarea {
  font: inherit; color: inherit; border: 1px solid #d1dde0; border-radius: 10px;
  background: #fff; min-height: 44px; padding: 8px 12px; touch-action: manipulation;
}
button { cursor: pointer; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid #267478; outline-offset: 2px; }
button:disabled { cursor: default; }
button.small { min-height: 36px; padding: 4px 8px; font-size: 13px; white-space: nowrap; }
button.primary { background: #174b50; border-color: #174b50; color: #fff; font-weight: 700; }
button.primary:disabled { background: #dce5e7; border-color: #dce5e7; color: #52636b; }
button.warn { background: #fff8e9; border-color: #e6d4ac; color: #714b08; font-size: 14px; }
button.ghost { color: #51646e; font-size: 14px; }

.login {
  height: 100%; height: 100dvh; overflow: auto; display: flex; flex-direction: column; justify-content: center;
  max-width: 380px; margin: 0 auto; padding: 24px calc(20px + env(safe-area-inset-right)) 24px calc(20px + env(safe-area-inset-left));
}
.login form { display: grid; gap: 10px; }
.login input { font-size: 18px; }
.msg { color: #9b2c31; font-size: 13px; margin: 0; overflow-wrap: anywhere; }
.msg:empty { display: none; }
.login .msg { margin-top: 8px; }

#app { height: 100%; height: var(--viewport-height, 100dvh); display: flex; flex-direction: column; overflow: hidden; }
header {
  flex: none; background: #fff; border-bottom: 1px solid #dce5e7;
  padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px calc(12px + env(safe-area-inset-left));
}
.headerInner { max-width: 640px; margin: 0 auto; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sampleInfo { min-width: 0; display: grid; }
#meta { font-size: 14px; overflow-wrap: anywhere; }
#who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tools { flex: none; display: flex; align-items: center; gap: 4px; }
header .tools button { background: #f2f6f7; border-color: transparent; }
header .tools #previous:not(:disabled) { background: #e1eeee; color: #174b50; }
header .tools button:disabled { opacity: .5; }
.progressRow { display: flex; align-items: center; gap: 10px; margin-top: 5px; }
#progress, #overallProgress { font-variant-numeric: tabular-nums; white-space: nowrap; }
#overallBar { flex: 1; min-width: 0; height: 6px; accent-color: #267478; }

main { flex: 1; min-height: 0; width: 100%; max-width: 664px; margin: 0 auto; }
#done, #work { height: 100%; }
#done { overflow: auto; text-align: center; padding: 20px 12px; }
#work { display: grid; grid-template-rows: minmax(0, 1fr) auto; }
.workContent {
  min-height: 0; overflow-y: auto; overscroll-behavior-y: contain;
  display: flex; flex-direction: column; gap: 8px;
  padding: 8px calc(12px + env(safe-area-inset-right)) 8px calc(12px + env(safe-area-inset-left));
}
.cropWrap {
  flex: 1 0 clamp(140px, 26vh, 220px); min-height: 140px; max-height: 480px;
  overflow: hidden; border-radius: 12px; background: #1a2b34;
}
#board {
  display: block; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: crosshair;
}
.roi { fill: none; stroke: #24d16b; stroke-width: 3; vector-effect: non-scaling-stroke; }
.axis { stroke: #f12ed4; stroke-width: 3.6; stroke-linecap: round; vector-effect: non-scaling-stroke; visibility: hidden; }
.handle { fill: #f12ed4; r: 4; vector-effect: non-scaling-stroke; visibility: hidden; }
#photo { pointer-events: none; }

.controls {
  flex: none; display: grid; gap: 8px; padding: 8px;
  border: 1px solid #dce5e7; border-radius: 12px; background: #fff;
}
.angleLine { display: flex; justify-content: space-between; align-items: center; gap: 4px; flex-wrap: wrap; }
#angle { font-size: 14px; font-weight: 650; color: #783478; }
#pending.active { background: #e9f1f2; border-color: #7da3a6; color: #174b50; }
.quality { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.quality button {
  position: relative; min-height: 48px; padding-inline: 34px; font-size: 18px; font-weight: 800;
  border-color: #d1dde0; background: #f5f8f8;
}
button.ok { color: #17603d; }
button.ng { color: #9d2933; }
button.ok[aria-pressed="true"] { background: #17603d; border-color: #17603d; color: #fff; }
button.ng[aria-pressed="true"] { background: #9d2933; border-color: #9d2933; color: #fff; }
.quality button[aria-pressed="true"]::after {
  content: "✓"; position: absolute; inset-inline-end: 10px; top: 50%; transform: translateY(-50%);
  width: 20px; height: 20px; border-radius: 50%; background: #fff; line-height: 20px; font-size: 14px;
}
button.ok::after { color: #17603d; }
button.ng::after { color: #9d2933; }

#ngBox { display: grid; gap: 6px; padding-top: 7px; border-top: 1px solid #e4ecee; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
#reasons[aria-invalid="true"], #note[aria-invalid="true"] {
  outline: 2px solid #b5313d; outline-offset: 1px; border-radius: 8px; background: #fff2f3;
}
button.chip { padding: 4px 8px; min-height: 38px; font-size: 13px; }
button.chip.active { background: #fcebed; border-color: #a63540; color: #8a242e; font-weight: 650; }
#note { width: 100%; resize: none; font-size: 16px; }

.actions {
  display: grid; gap: 6px; background: #fff; border-top: 1px solid #dce5e7;
  padding: 8px calc(12px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
}
.actions .msg { max-height: 2.9em; overflow: auto; }
#retryWork { justify-self: start; grid-column: 1 / -1; }
.secondaryActions { display: grid; grid-template-columns: minmax(0, 1fr) minmax(64px, 26%); gap: 8px; }
#save { width: 100%; min-height: 48px; font-size: 17px; }

.dialog { width: min(92vw, 560px); max-height: 82dvh; padding: 12px; border: 0; border-radius: 12px; }
.dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialogHead { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
#logoutDialog { max-width: 340px; padding: 16px; }
.logoutActions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.tutorial { padding: 0; }
.tutorial form {
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; max-height: 82dvh; padding: 14px;
}
.tutorial h2 { margin: 0; font-size: 19px; }
.tutorialContent { overflow-y: auto; overscroll-behavior-y: contain; }
.tutorialStep h3 { margin: 0 0 8px; font-size: 16px; color: #174b50; }
.tutorialStep p { margin: 8px 0; }
.tutorialFigure { margin: 0 auto; width: min(100%, 220px, 40dvh); }
.tutorialFigure img { display: block; width: 100%; height: auto; border-radius: 10px; }
.tutorialFigure figcaption {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  margin-top: 4px; padding: 4px; border-radius: 6px;
  background: #f6edf6; color: #783478; font-size: 13px; font-weight: 700;
}
.tutorialFigure .exampleOk { background: #17603d; color: #fff; }
.tutorialFigure .exampleNg { background: #9d2933; color: #fff; }
.exampleNg span { background: #fff; color: #9d2933; border-radius: 4px; padding: 0 6px; }
.angleExample { position: relative; }
.exampleTrack {
  position: absolute; left: 31%; top: 40%; width: 40%;
  transform: rotate(21.8deg); transform-origin: left center;
}
.exampleLine {
  display: block; height: 4px; border: 1px solid #53244c;
  background: #ff52e5; border-radius: 9px;
}
.exampleTouch {
  position: absolute; top: 50%; left: 100%; opacity: 0;
  width: clamp(34px, 12dvh, 54px); height: auto; pointer-events: none;
  transform: translate(-29%, -14%) rotate(-21.8deg); transform-origin: 29% 14%;
  filter: drop-shadow(0 1px 2px #1a2b34);
}

@media (prefers-reduced-motion: no-preference) {
  #tutorialDialog[open] .exampleLine { animation: drawExample 3.8s linear infinite; }
  #tutorialDialog[open] .exampleTouch { animation: dragExample 3.8s linear infinite; }
  #tutorialDialog[open] #angleDemo figcaption { animation: revealAngle 3.8s linear infinite; }
}
@keyframes drawExample {
  0%, 15% { clip-path: inset(0 100% 0 0); }
  52%, 100% { clip-path: inset(0); }
}
@keyframes dragExample {
  0% { left: 0; opacity: 0; }
  7%, 15% { left: 0; opacity: 1; }
  52% { left: 100%; opacity: 1; }
  60%, 100% { left: 100%; opacity: 0; }
}
@keyframes revealAngle {
  0%, 56% { opacity: 0; }
  64%, 100% { opacity: 1; }
}
.tutorialActions {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding-top: 10px; border-top: 1px solid #dce5e7;
}
.tutorialActions label { display: flex; align-items: center; gap: 6px; min-height: 44px; cursor: pointer; }
#hideTutorial { margin: 0; min-height: 0; width: 18px; height: 18px; accent-color: #174b50; }
#thumb { width: 100%; border-radius: 6px; display: block; }
.recent { display: grid; gap: 6px; margin-top: 6px; }
.recentItem { text-align: left; width: 100%; font-size: 14px; background: #f8fafb; }

@media (max-width: 360px) {
  header .tools button { padding-inline: 5px; }
}

@media (max-height: 480px) {
  .actions { grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); }
  .actions .msg { grid-column: 1 / -1; }
}

@media (orientation: landscape) and (max-height: 480px) {
  .workContent {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); overflow: hidden;
  }
  .cropWrap { min-height: 0; }
  .controls { align-content: start; overflow-y: auto; }
}
