:root {
  --ink: #1d1d1f;
  --secondary: #6e6e73;
  --tertiary: #98989d;
  --canvas: #f5f5f7;
  --surface: rgba(255, 255, 255, 0.86);
  --paper: #ffffff;
  --separator: rgba(0, 0, 0, 0.09);
  --control: #ececf0;
  --control-hover: #e4e4e8;
  --blue: #0071e3;
  --blue-hover: #0077ed;
  --blue-soft: #e8f2ff;
  --green: #248a3d;
  --green-soft: #e8f7ec;
  --red: #d70015;
  --red-soft: #fff0f1;
  --stage-width: 1600px;
  --stage-height: 1131.43px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  color: var(--ink);
  background: var(--canvas);
  font-family: "SF Pro Display", "SF Pro Text", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
}

@supports (height: 100dvh) {
  body,
  .app-shell {
    height: 100dvh;
  }
}

button,
select,
input {
  font: inherit;
  letter-spacing: 0;
}

button,
select {
  color: inherit;
}

button:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 3px solid rgb(0 113 227 / 24%);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr);
  width: 100%;
  height: 100%;
}

.topbar {
  position: relative;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(230px, 1fr) auto auto 104px;
  align-items: center;
  gap: 22px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--separator);
  box-shadow: 0 1px 0 rgb(255 255 255 / 70%);
  backdrop-filter: saturate(180%) blur(18px);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
}

.brand-block {
  min-width: 0;
}

.brand-code,
.eyebrow {
  display: block;
  color: var(--blue);
  font-size: 10px;
  font-weight: 650;
  line-height: 1;
}

.brand-block h1 {
  margin: 5px 0 0;
  overflow: hidden;
  font-size: 17px;
  font-weight: 650;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.practice-controls,
.zoom-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.mode-switch {
  display: flex;
  height: 34px;
  padding: 2px;
  background: var(--control);
  border-radius: 8px;
}

.mode-button {
  padding: 0 14px;
  color: var(--secondary);
  background: transparent;
  border: 0;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: color 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.mode-button:hover {
  color: var(--ink);
}

.mode-button.is-active {
  color: var(--ink);
  background: #ffffff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 12%);
  font-weight: 600;
}

.section-picker {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--secondary);
  font-size: 11px;
}

.section-picker select {
  min-width: 116px;
  height: 32px;
  padding: 0 28px 0 10px;
  background: var(--control);
  border: 0;
  border-radius: 7px;
  font-size: 12px;
  cursor: pointer;
}

.icon-button,
.fit-button {
  height: 32px;
  background: var(--control);
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 140ms ease, transform 140ms ease;
}

.icon-button {
  width: 32px;
  padding: 0;
  font-size: 18px;
  line-height: 32px;
}

.fit-button {
  padding: 0 11px;
  font-size: 11px;
}

.icon-button:hover,
.fit-button:hover,
.secondary-action:hover {
  background: var(--control-hover);
}

.icon-button:active,
.fit-button:active,
.primary-action:active,
.secondary-action:active {
  transform: scale(0.97);
}

#zoomValue {
  width: 44px;
  color: var(--secondary);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  text-align: center;
}

.header-progress {
  display: grid;
  justify-items: end;
  min-width: 84px;
}

.header-progress strong {
  color: var(--ink);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  font-weight: 650;
}

.header-progress span {
  margin-top: 2px;
  color: var(--tertiary);
  font-size: 9px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 244px;
  min-height: 0;
}

.diagram-viewport {
  position: relative;
  min-width: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  background: var(--canvas);
}

.diagram-viewport.is-dragging {
  cursor: grabbing;
}

.next-blank-float {
  position: absolute;
  top: 14px;
  left: 14px;
  z-index: 12;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 32px;
  padding: 0 10px;
  color: #ffffff;
  background: rgb(0 113 227 / 94%);
  border: 0;
  border-radius: 999px;
  box-shadow: 0 5px 16px rgb(0 65 130 / 20%);
  font-size: 11px;
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 140ms ease, transform 140ms ease, opacity 140ms ease;
}

.next-blank-float span:first-child {
  font-size: 16px;
  line-height: 1;
}

.next-blank-float:hover { background: var(--blue-hover); }
.next-blank-float:active { transform: scale(0.96); }
.next-blank-float:disabled {
  color: var(--tertiary);
  background: var(--control);
  box-shadow: none;
  cursor: default;
}

.diagram-stage {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--stage-width);
  height: var(--stage-height);
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 8px 30px rgb(0 0 0 / 10%);
  transform-origin: 0 0;
  will-change: transform;
}

.diagram-stage.is-smooth {
  transition: transform 300ms cubic-bezier(.25, .1, .25, 1);
}

.diagram-image,
.annotation-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.diagram-image {
  display: block;
  user-select: none;
  pointer-events: none;
}

.annotation-layer {
  pointer-events: none;
}

.answer-field {
  position: absolute;
  z-index: 2;
  height: 18px;
  min-width: 0;
  margin: 0;
  padding: 0 0 8px;
  color: var(--ink);
  background: linear-gradient(
    to bottom,
    #ffffff 0,
    #ffffff 9px,
    #a8a8ad 9px,
    #a8a8ad 10px,
    transparent 10px
  );
  border: 0;
  border-radius: 0;
  font-family: "SFMono-Regular", Consolas, "Courier New", monospace;
  font-size: 8.5px;
  font-weight: 650;
  line-height: 9px;
  text-align: center;
  text-transform: uppercase;
  pointer-events: auto;
  transition: color 130ms ease, background-color 130ms ease, border-color 130ms ease, box-shadow 130ms ease;
}

.answer-field:hover {
  background: linear-gradient(
    to bottom,
    var(--blue-soft) 0,
    var(--blue-soft) 9px,
    var(--blue) 9px,
    var(--blue) 10px,
    transparent 10px
  );
}

.answer-field:focus {
  z-index: 5;
  color: var(--ink);
  background: linear-gradient(
    to bottom,
    var(--blue-soft) 0,
    var(--blue-soft) 8.5px,
    var(--blue) 8.5px,
    var(--blue) 10px,
    transparent 10px
  );
  outline: none;
}

.answer-field.is-correct {
  color: var(--green);
  background: linear-gradient(
    to bottom,
    var(--green-soft) 0,
    var(--green-soft) 8.5px,
    var(--green) 8.5px,
    var(--green) 10px,
    transparent 10px
  );
}

.answer-field.is-incorrect {
  color: var(--red);
  background: linear-gradient(
    to bottom,
    var(--red-soft) 0,
    var(--red-soft) 8.5px,
    var(--red) 8.5px,
    var(--red) 10px,
    transparent 10px
  );
  animation: field-error 180ms ease-out;
}

.answer-field.is-restored {
  animation: field-restored 420ms ease-out;
}

@keyframes field-error {
  0%, 100% { transform: translateX(0); }
  35% { transform: translateX(-2px); }
  70% { transform: translateX(2px); }
}

@keyframes field-restored {
  from { box-shadow: 0 0 0 3px rgb(36 138 61 / 20%); }
  to { box-shadow: none; }
}

.side-panel {
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 26px 20px 18px;
  background: rgb(255 255 255 / 78%);
  border-left: 1px solid var(--separator);
  backdrop-filter: saturate(160%) blur(18px);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
}

.progress-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  color: var(--secondary);
  font-size: 11px;
}

.progress-heading strong {
  color: var(--ink);
  font-size: 28px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.progress-track {
  height: 5px;
  margin-top: 12px;
  overflow: hidden;
  background: var(--control);
  border-radius: 3px;
}

.progress-track span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--blue);
  border-radius: inherit;
  transition: width 260ms ease;
}

.metric-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px 14px;
  margin: 28px 0 0;
}

.metric-list div {
  min-width: 0;
}

.metric-list dt {
  color: var(--tertiary);
  font-size: 10px;
}

.metric-list dd {
  margin: 4px 0 0;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.current-section {
  display: grid;
  gap: 6px;
  margin-top: 30px;
  padding: 22px 0;
  border-top: 1px solid var(--separator);
  border-bottom: 1px solid var(--separator);
}

.current-section strong {
  font-size: 17px;
  font-weight: 600;
}

.current-section > span:last-child {
  color: var(--secondary);
  font-size: 11px;
}

.panel-actions {
  display: grid;
  gap: 9px;
  margin-top: 20px;
}

.primary-action,
.secondary-action {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  transition: background-color 140ms ease, transform 140ms ease;
}

.primary-action {
  color: #ffffff;
  background: var(--blue);
  font-weight: 600;
}

.primary-action:hover {
  background: var(--blue-hover);
}

.primary-action:disabled {
  color: var(--tertiary);
  background: var(--control);
  cursor: default;
}

.secondary-action {
  color: var(--blue);
  background: var(--blue-soft);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 13px;
  margin-top: auto;
  padding-top: 18px;
  color: var(--secondary);
  font-size: 9px;
  border-top: 1px solid var(--separator);
}

.legend span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}

.legend-dot.pending { background: #a1a1a6; }
.legend-dot.correct { background: var(--green); }
.legend-dot.incorrect { background: var(--red); }

.canvas-status {
  position: absolute;
  right: 18px;
  bottom: 16px;
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 12px;
  color: var(--ink);
  background: rgb(255 255 255 / 88%);
  border: 1px solid var(--separator);
  border-radius: 8px;
  box-shadow: 0 4px 14px rgb(0 0 0 / 8%);
  font-size: 10px;
  pointer-events: none;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.status-dot {
  width: 7px;
  height: 7px;
  background: var(--blue);
  border-radius: 50%;
}

.canvas-status.is-success .status-dot { background: var(--green); }
.canvas-status.is-error .status-dot { background: var(--red); }

.minimap {
  position: absolute;
  bottom: 16px;
  left: 16px;
  z-index: 7;
  width: 170px;
  aspect-ratio: 1.414 / 1;
  overflow: hidden;
  background: #ffffff;
  border: 1px solid var(--separator);
  border-radius: 8px;
  box-shadow: 0 5px 18px rgb(0 0 0 / 10%);
  cursor: crosshair;
}

.minimap img {
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.minimap-window {
  position: absolute;
  min-width: 8px;
  min-height: 8px;
  background: rgb(0 113 227 / 10%);
  border: 1.5px solid var(--blue);
  pointer-events: none;
}

.empty-notice {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 9;
  display: grid;
  gap: 6px;
  min-width: 260px;
  padding: 20px 24px;
  text-align: center;
  background: rgb(255 255 255 / 92%);
  border: 1px solid var(--separator);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 10%);
  transform: translate(-50%, -50%);
  backdrop-filter: blur(14px);
}

.empty-notice strong {
  font-size: 15px;
  font-weight: 600;
}

.empty-notice span {
  color: var(--secondary);
  font-size: 11px;
}

.mobile-actionbar,
.mobile-panel-header,
.mobile-panel-backdrop {
  display: none;
}

@media (max-width: 1180px) {
  .topbar {
    grid-template-columns: minmax(190px, 1fr) auto auto;
    gap: 14px;
  }

  .header-progress {
    display: none;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr) 220px;
  }
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-rows: 108px minmax(0, 1fr);
  }

  .topbar {
    grid-template-columns: 1fr auto;
    grid-template-rows: 54px 44px;
    padding: 0 14px 9px;
  }

  .brand-block {
    grid-column: 1;
    grid-row: 1;
  }

  .zoom-controls {
    grid-column: 2;
    grid-row: 1;
  }

  .practice-controls {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-content: space-between;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .side-panel {
    position: absolute;
    top: 108px;
    right: 0;
    width: 190px;
    height: calc(100% - 108px);
    padding: 18px 14px;
    transform: translateX(calc(100% - 5px));
    transition: transform 180ms ease;
  }

  .side-panel:hover,
  .side-panel:focus-within {
    transform: translateX(0);
  }

  .metric-list {
    gap: 14px 10px;
  }

  .minimap {
    width: 138px;
  }
}

@media (max-width: 620px), (max-height: 500px) and (orientation: landscape) {
  .app-shell {
    grid-template-rows: calc(106px + env(safe-area-inset-top)) minmax(0, 1fr);
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 48px 48px;
    gap: 0 10px;
    padding: env(safe-area-inset-top) 12px 10px;
  }

  .brand-block {
    grid-column: 1;
    grid-row: 1;
  }

  .brand-code {
    display: none;
  }

  .brand-block h1 {
    margin: 0;
    font-size: 16px;
  }

  .header-progress {
    display: grid;
    grid-column: 2;
    grid-row: 1;
    min-width: 68px;
  }

  .practice-controls {
    grid-column: 1 / -1;
    grid-row: 2;
    gap: 8px;
    width: 100%;
  }

  .mode-switch {
    flex: 1 1 auto;
    min-width: 0;
  }

  .mode-button {
    flex: 1 1 50%;
    min-width: 0;
    padding: 0 7px;
    font-size: 11px;
  }

  .section-picker {
    flex: 0 0 auto;
  }

  .section-picker > span {
    display: none;
  }

  .section-picker select {
    width: 118px;
    min-width: 0;
    height: 34px;
  }

  .zoom-controls {
    position: fixed;
    top: calc(118px + env(safe-area-inset-top));
    right: 10px;
    z-index: 18;
    display: flex;
    flex-direction: column;
    gap: 3px;
    width: 44px;
    padding: 4px;
    background: rgb(255 255 255 / 88%);
    border: 1px solid var(--separator);
    border-radius: 8px;
    box-shadow: 0 6px 20px rgb(0 0 0 / 10%);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    transition: opacity 160ms ease, transform 160ms ease;
  }

  .zoom-controls .icon-button {
    width: 34px;
    height: 34px;
    background: transparent;
  }

  .zoom-controls #zoomValue {
    width: 34px;
    height: 20px;
    line-height: 20px;
  }

  .zoom-controls .fit-button {
    width: 34px;
    height: 31px;
    padding: 0;
    overflow: hidden;
    font-size: 0;
    background: transparent;
  }

  .zoom-controls .fit-button::before {
    content: "适应";
    font-size: 9px;
  }

  .minimap {
    display: none;
  }

  .workspace {
    position: relative;
    grid-template-columns: minmax(0, 1fr);
  }

  .answer-field {
    /* iOS 会放大字号小于 16px 的输入框；图纸本身会缩放，因此保持命中区域稳定。 */
    font-size: 16px;
    line-height: 16px;
  }

  .diagram-viewport {
    grid-column: 1;
    grid-row: 1;
  }

  .canvas-status {
    right: 10px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    max-width: calc(100% - 20px);
  }

  .empty-notice {
    min-width: 0;
    width: calc(100% - 48px);
    max-width: 320px;
  }

  .mobile-actionbar {
    position: absolute;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    left: 10px;
    z-index: 22;
    display: grid;
    grid-template-columns: 112px;
    justify-content: end;
    gap: 8px;
    min-height: 54px;
    padding: 5px;
    background: rgb(255 255 255 / 90%);
    border: 1px solid var(--separator);
    border-radius: 8px;
    box-shadow: 0 8px 28px rgb(0 0 0 / 13%);
    backdrop-filter: saturate(170%) blur(18px);
    -webkit-backdrop-filter: saturate(170%) blur(18px);
    transition: opacity 160ms ease, transform 160ms ease;
  }

  body.is-answering .mobile-actionbar {
    opacity: 0;
    pointer-events: none;
    transform: translateY(calc(100% + 24px));
  }

  body.is-answering .zoom-controls {
    opacity: 0;
    pointer-events: none;
    transform: translateX(18px);
  }

  .mobile-next-button,
  .mobile-stats-button {
    min-width: 0;
    border: 0;
    border-radius: 7px;
    cursor: pointer;
  }

  .mobile-next-button {
    display: none;
  }

  .mobile-next-button {
    color: #ffffff;
    background: var(--blue);
    font-size: 13px;
    font-weight: 600;
  }

  .mobile-next-button:disabled {
    color: var(--tertiary);
    background: var(--control);
  }

  .mobile-stats-button {
    display: grid;
    align-content: center;
    gap: 2px;
    color: var(--secondary);
    background: var(--control);
    font-size: 9px;
    text-align: left;
    padding: 0 11px;
  }

  .mobile-stats-button strong {
    color: var(--ink);
    font-size: 13px;
    font-variant-numeric: tabular-nums;
  }

  .mobile-panel-backdrop {
    position: absolute;
    inset: 0;
    z-index: 28;
    display: block;
    background: rgb(0 0 0 / 18%);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  .side-panel,
  .side-panel:hover,
  .side-panel:focus-within {
    position: absolute;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    width: 100%;
    height: auto;
    max-height: calc(100dvh - 82px - env(safe-area-inset-top));
    padding: 10px 20px calc(18px + env(safe-area-inset-bottom));
    overflow-y: auto;
    background: rgb(255 255 255 / 96%);
    border-top: 1px solid var(--separator);
    border-left: 0;
    border-radius: 8px 8px 0 0;
    box-shadow: 0 -10px 32px rgb(0 0 0 / 14%);
    transform: translateY(calc(100% + 12px));
    transition: transform 240ms cubic-bezier(.25, .1, .25, 1);
  }

  .side-panel.is-open,
  .side-panel.is-open:hover,
  .side-panel.is-open:focus-within {
    transform: translateY(0);
  }

  .mobile-panel-header {
    position: relative;
    display: grid;
    grid-template-columns: 36px 1fr 36px;
    align-items: center;
    min-height: 36px;
    margin-bottom: 16px;
    text-align: center;
  }

  .mobile-panel-header > strong {
    grid-column: 2;
    font-size: 14px;
    font-weight: 600;
  }

  .mobile-panel-grabber {
    position: absolute;
    top: -3px;
    left: 50%;
    width: 36px;
    height: 4px;
    background: #d1d1d6;
    border-radius: 2px;
    transform: translateX(-50%);
  }

  .mobile-panel-close {
    grid-column: 3;
    width: 30px;
    height: 30px;
    padding: 0;
    color: var(--secondary);
    background: var(--control);
    border: 0;
    border-radius: 50%;
    font-size: 20px;
    line-height: 28px;
    cursor: pointer;
  }

  .side-panel .metric-list {
    gap: 18px 14px;
    margin-top: 22px;
  }

  .side-panel .current-section {
    margin-top: 22px;
    padding: 18px 0;
  }

  .side-panel .legend {
    margin-top: 22px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .app-shell {
    grid-template-rows: calc(58px + env(safe-area-inset-top)) minmax(0, 1fr);
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 48px;
    padding-bottom: 6px;
  }

  .brand-block,
  .header-progress {
    display: none;
  }

  .practice-controls {
    grid-column: 1;
    grid-row: 1;
    max-width: 460px;
  }

  .zoom-controls {
    top: calc(70px + env(safe-area-inset-top));
  }

  .mobile-actionbar {
    left: max(10px, env(safe-area-inset-left));
    width: min(420px, calc(100% - 88px));
  }

  .side-panel,
  .side-panel:hover,
  .side-panel:focus-within {
    left: auto;
    width: min(390px, 54vw);
    max-height: calc(100dvh - env(safe-area-inset-top));
    border-left: 1px solid var(--separator);
    border-radius: 8px 0 0 0;
    transform: translateX(calc(100% + 12px));
  }

  .side-panel.is-open,
  .side-panel.is-open:hover,
  .side-panel.is-open:focus-within {
    transform: translateX(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
