:root {
  --ink: #14282f;
  --ink-soft: #4a636b;
  --paper: #f3efe6;
  --card: #fffcf7;
  --line: #d8d2c6;
  --teal: #0e6b6b;
  --teal-dark: #0a4f51;
  --teal-soft: #d7efee;
  --amber: #c45a1a;
  --amber-soft: #fde8d2;
  --gold: #e2b14a;
  --blue: #1d5aa8;
  --blue-soft: #dceafb;
  --violet: #6b3ca8;
  --violet-soft: #eee4fb;
  --rose: #a61e5a;
  --rose-soft: #fadcea;
  --green: #1c7a45;
  --green-soft: #d8f3e3;
  --danger: #9b1c2c;
  --danger-soft: #fde2e4;
  --shadow: 0 12px 30px rgba(20, 40, 47, 0.08);
  --shadow-sm: 0 4px 14px rgba(20, 40, 47, 0.07);
  --radius: 20px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --acute: #0e7a6b;
  --right: #1d5aa8;
  --obtuse: #c45a1a;
  --straight: #6b3ca8;
  --reflex: #a61e5a;
}

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

/* class { display:flex } 的選擇器比 [hidden] 更強，必須蓋過，否則完成畫面會漏出 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(900px 420px at 0% -10%, #e7f3ef 0%, transparent 60%),
    radial-gradient(800px 400px at 100% 0%, #f8e8d2 0%, transparent 55%),
    var(--paper);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang HK", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 18px;
  line-height: 1.55;
}

body { padding-top: var(--safe-t); }

button, input {
  font-family: inherit;
}

button { cursor: pointer; border: 0; }

h1, h2, h3 { margin: 0; line-height: 1.25; }

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  min-height: 100dvh;
  padding: 12px 16px calc(20px + var(--safe-b));
  display: flex;
  flex-direction: column;
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  margin-bottom: 8px;
}

.top-titles { flex: 1; min-width: 0; }
.kicker {
  margin: 0 0 2px;
  color: var(--teal-dark);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}
.top h1 { font-size: 1.25rem; font-weight: 800; }

.top-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--teal-soft);
  color: var(--teal-dark);
  font-size: 0.82rem;
  font-weight: 700;
}
.pill.practice { background: var(--amber-soft); color: #8a3d0d; }

.progress {
  font-weight: 700;
  color: var(--ink-soft);
  font-size: 0.92rem;
  white-space: nowrap;
}

.btn-ghost, .btn-tiny, .btn-primary, .btn-secondary, .btn-text {
  border-radius: 14px;
  font-weight: 700;
}

.btn-ghost, .btn-tiny {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}
.btn-ghost { padding: 10px 14px; min-height: 44px; }
.btn-tiny { padding: 6px 10px; font-size: 0.82rem; min-height: 36px; }
.btn-primary {
  background: var(--teal);
  color: #fff;
  padding: 12px 18px;
  min-height: 48px;
  font-size: 1rem;
  box-shadow: 0 4px 0 var(--teal-dark);
}
.btn-primary:active { transform: translateY(1px); box-shadow: 0 3px 0 var(--teal-dark); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-secondary {
  background: #fff;
  color: var(--teal-dark);
  border: 2px solid var(--teal);
  padding: 10px 16px;
  min-height: 48px;
}
.btn-text {
  background: transparent;
  color: var(--teal-dark);
  padding: 8px 10px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.screen { animation: fade-in 0.35s ease; }
@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* ---- Home ---- */
.home-hero { text-align: center; padding: 8px 8px 6px; }
.hero-svg { width: min(280px, 70vw); height: auto; }
.badge {
  display: inline-block;
  margin: 4px 0 10px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--teal-dark);
  color: #f4faf9;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.home-hero h2 {
  font-size: clamp(1.35rem, 3vw, 1.85rem);
  max-width: 22em;
  margin: 0 auto 10px;
}
.lede {
  margin: 0 auto;
  max-width: 42em;
  color: var(--ink-soft);
}
.lede strong { color: var(--ink); }

.part-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  margin: 22px 0 18px;
}
.part-card {
  display: flex;
  gap: 12px;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 16px 18px;
  box-shadow: var(--shadow);
  min-height: 140px;
  color: inherit;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.part-card:hover, .part-card:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 16px 32px rgba(20, 40, 47, 0.12);
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}
.part-num {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background: var(--teal);
  color: #fff;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.part-title { display: block; font-size: 1.2rem; font-weight: 800; margin-bottom: 4px; }
.part-desc { display: block; color: var(--ink-soft); font-size: 0.92rem; }

.note {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
}
.note h3 { font-size: 1rem; margin-bottom: 6px; }
.note ul { margin: 0; padding-left: 1.2em; color: var(--ink-soft); }
.note li { margin: 0.3em 0; }

/* ---- Work layout ---- */
.screen.work {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.work-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 14px;
  align-items: stretch;
  flex: 1;
  min-height: 0;
}
.canvas-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px 10px 12px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.canvas-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 6px;
}
.canvas-hint {
  margin: 0;
  flex: 1;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--teal-dark);
}
.stage-wrap {
  flex: 1;
  min-height: 280px;
  background:
    radial-gradient(circle at 1px 1px, rgba(20, 40, 47, 0.06) 1px, transparent 0) 0 0 / 18px 18px,
    #fff;
  border: 1px solid #ece7dc;
  border-radius: 14px;
  overflow: hidden;
  touch-action: none;
}
#stage {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100dvh - 200px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 16px 14px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel-scroll { flex: 1; overflow: auto; }
.step-label {
  margin: 0 0 4px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--teal-dark);
}
.panel h2 { font-size: 1.28rem; margin-bottom: 8px; }
.panel-text { color: var(--ink); }
.panel-text p { margin: 0 0 0.7em; }
.panel-text p:last-child { margin-bottom: 0; }
.panel-text ol, .panel-text ul { margin: 0.3em 0 0.7em; padding-left: 1.25em; }
.calc-box {
  margin: 10px 0;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--amber-soft);
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.calc-box .slot {
  display: inline-block;
  min-width: 2.2em;
  padding: 0 6px;
  border-bottom: 2px solid var(--ink);
  text-align: center;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.panel-actions .btn-primary,
.panel-actions .btn-secondary { flex: 1 1 auto; }

/* classify choices */
.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 12px;
}
.choice {
  min-height: 52px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: #fff;
  font-weight: 800;
  font-size: 1.05rem;
  color: var(--ink);
}
.choice:active { transform: scale(0.98); }
.choice[data-type="acute"] { border-color: var(--acute); color: var(--acute); }
.choice[data-type="right"] { border-color: var(--right); color: var(--right); }
.choice[data-type="obtuse"] { border-color: var(--obtuse); color: var(--obtuse); }
.choice[data-type="straight"] { border-color: var(--straight); color: var(--straight); }
.choice[data-type="reflex"] { border-color: var(--reflex); color: var(--reflex); grid-column: 1 / -1; }
.choice.selected { background: var(--teal-soft); }
.choice.correct { background: var(--green-soft); border-color: var(--green); color: var(--green); }
.choice.wrong { background: var(--danger-soft); border-color: var(--danger); }

/* keypad */
.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}
.key {
  min-height: 48px;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 1.25rem;
  font-weight: 800;
  box-shadow: var(--shadow-sm);
}
.key:active { background: var(--teal-soft); }
.key.wide { grid-column: span 2; }
.degree-display {
  margin-top: 8px;
  background: #fff;
  border: 2px solid var(--teal);
  border-radius: 14px;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: 0.04em;
}
.degree-display.empty { color: #b3bdc1; font-weight: 600; font-size: 1.1rem; }

.legend {
  display: grid;
  gap: 6px;
  margin: 8px 0;
  font-size: 0.9rem;
}
.legend div {
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.swatch {
  width: 10px;
  height: 10px;
  border-radius: 99px;
  flex-shrink: 0;
  transform: translateY(-1px);
}
.swatch.acute { background: var(--acute); }
.swatch.right { background: var(--right); }
.swatch.obtuse { background: var(--obtuse); }
.swatch.straight { background: var(--straight); }
.swatch.reflex { background: var(--reflex); }

.feedback {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 14px;
  font-weight: 600;
}
.feedback.ok { background: var(--green-soft); color: var(--green); }
.feedback.no { background: var(--danger-soft); color: var(--danger); }
.feedback.hint { background: var(--blue-soft); color: var(--blue); }
.feedback.near { background: var(--amber-soft); color: #8a3d0d; }

/* done */
.done { display: flex; justify-content: center; padding-top: 40px; }
.done-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  text-align: center;
  width: min(520px, 100%);
}
.done-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 18px; }

/* snap badge drawn in svg, plus CSS cursor */
.protractor-grab { cursor: grab; }
.protractor-grab:active, .is-dragging .protractor-grab { cursor: grabbing; }
.rotate-handle { cursor: grab; }

@media (max-width: 900px) {
  .part-grid { grid-template-columns: 1fr; }
  .work-layout { grid-template-columns: 1fr; }
  #stage { max-height: 52dvh; }
  .panel { min-height: auto; }
  .choice[data-type="reflex"] { grid-column: auto; }
  .choice-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
  body { font-size: 16px; }
  .app { padding: 8px 10px calc(14px + var(--safe-b)); }
  .top h1 { font-size: 1.05rem; }
  .key { min-height: 46px; }
}

@media (prefers-reduced-motion: reduce) {
  .screen { animation: none; }
  .part-card { transition: none; }
}
