/* ============================================================
   암산 게임(사칙연산 스피드) 전용 스타일
   - 콘셉트: 수학 익힘책. 도입부와 문제판에 가는 모눈종이 격자,
     채점은 빨간펜(맞으면 동그라미, 틀리면 빗금)으로 표시한다.
     원고지 격자(34px)와 구분되는 잔 모눈(17px)이 이 도구의 모티프.
   - 모바일 우선: 문제 활자를 화면 폭에 맞춰 크게(clamp + vw).
   ============================================================ */

:root {
  --ms-pen: #c9392b;              /* 빨간펜: 채점 표시 전용 */
  --ms-pen-weak: rgba(201, 57, 43, .1);
  --ms-ok: #2e7d3c;
  --ms-bad: #c0392b;
  --ms-slot-line: #b9ad94;
  --ms-life: #c9392b;
  --ms-life-off: #ddd3bf;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ms-pen: #ff8574;
    --ms-pen-weak: rgba(255, 133, 116, .12);
    --ms-ok: #7fd694;
    --ms-bad: #ff8f7d;
    --ms-slot-line: #5d5443;
    --ms-life: #ff8574;
    --ms-life-off: #453b2e;
  }
}
:root[data-theme="dark"] {
  --ms-pen: #ff8574;
  --ms-pen-weak: rgba(255, 133, 116, .12);
  --ms-ok: #7fd694;
  --ms-bad: #ff8f7d;
  --ms-slot-line: #5d5443;
  --ms-life: #ff8574;
  --ms-life-off: #453b2e;
}

/* ---- 도입부: 수학 노트 모눈 배경 ---- */
.tool-intro { position: relative; overflow: hidden; padding: clamp(30px, 5vw, 56px) 0 clamp(18px, 3vw, 30px); }
.tool-intro > * { position: relative; }
.tool-intro::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--color-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--color-grid) 1px, transparent 1px);
  background-size: 17px 17px;
  opacity: .34;
  -webkit-mask-image: linear-gradient(115deg, rgba(0,0,0,.55), transparent 58%);
          mask-image: linear-gradient(115deg, rgba(0,0,0,.55), transparent 58%);
  pointer-events: none;
}
.tool-intro h1 { margin: 0 0 var(--space-4); }
.tool-intro .lead {
  font-size: 1.08rem;
  line-height: 1.75;
  color: var(--color-text-sub);
  max-width: 52ch;
  margin: 0;
}
.tool-intro .lead strong { color: var(--color-text); }
.tool-intro .lead-note {
  margin: var(--space-3) 0 0;
  font-size: .88rem;
  color: var(--color-text-sub);
}

/* ---- 설정 줄 ---- */
.ms-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3) var(--space-5);
  padding: var(--space-4) var(--space-5);
  margin-bottom: var(--space-4);
}
.ms-field { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.ms-lab { font-size: .95rem; font-weight: 700; color: var(--color-text-sub); }
.ms-field-note {
  flex-basis: 100%;
  font-size: .85rem;
  color: var(--color-text-sub);
}
.ms-chips { display: flex; gap: var(--space-2); flex-wrap: wrap; }
.ms-chip {
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1.5px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
}
.ms-chip small {
  font-size: .78em;
  font-weight: 400;
  opacity: .75;
  margin-left: 2px;
}
.ms-chip:hover { border-color: var(--color-primary); }
.ms-chip[aria-pressed="true"] {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
  font-weight: 700;
}
.ms-btn-restart {
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1.5px solid var(--color-border);
  background: transparent;
  color: var(--color-text-sub);
}
.ms-btn-restart:hover { border-color: var(--color-primary); color: var(--color-text); }

/* ---- 게임판 ---- */
.ms-stage { max-width: 560px; margin: 0 auto; }

.ms-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}
.ms-stat {
  font-size: 1.05rem;
  color: var(--color-text-sub);
  font-variant-numeric: tabular-nums;
  min-width: 76px;
}
.ms-stat:last-child { text-align: right; }
.ms-stat b { font-size: 1.25rem; color: var(--color-text); }

/* 가운데 표시: 스프린트는 남은 시간, 서바이벌은 남은 목숨 */
.ms-mid {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--color-surface);
  border: 1.5px solid var(--color-border);
  color: var(--color-text-sub);
}
.ms-mid.is-live { color: var(--color-text); }
.ms-mid.is-hurry { color: var(--ms-bad); border-color: var(--ms-bad); }
.ms-life {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--ms-life);
  display: inline-block;
}
.ms-life.is-lost { background: var(--ms-life-off); }

/* ---- 문제판: 익힘책 종이 ---- */
.ms-panel-wrap { display: grid; }
.ms-panel-wrap > * { grid-area: 1 / 1; }
.ms-panel {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: clamp(220px, 52vw, 280px);
  padding: clamp(20px, 5vw, 28px);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 24px;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
/* 문제판에도 잔 모눈: 익힘책 종이 위에서 푸는 느낌 */
.ms-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(var(--color-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--color-grid) 1px, transparent 1px);
  background-size: 17px 17px;
  opacity: .16;
  pointer-events: none;
}
.ms-panel > * { position: relative; }

/* 남은 시간 막대: 판 위쪽에 가늘게 */
.ms-timer {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 7px;
  background: transparent;
}
.ms-timer-fill {
  height: 100%;
  background: var(--color-primary);
  opacity: .85;
  transform-origin: left;
  transform: scaleX(0);
  will-change: transform;
}
.ms-timer-fill.is-low { background: var(--ms-bad); opacity: 1; }

/* ---- 수식: 큰 활자 한 줄 ---- */
.ms-eq-wrap { position: relative; }
.ms-eq {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: .22em;
  font-size: clamp(2.3rem, 10.5vw, 4.3rem);
  font-weight: 800;
  line-height: 1.15;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
  letter-spacing: .01em;
  white-space: nowrap;
}
.ms-eq.is-in { animation: ms-slide .18s ease-out; }
@keyframes ms-slide {
  from { transform: translateY(14px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}
.ms-eq-sign { color: var(--color-text-sub); }
.ms-slots { display: inline-flex; gap: .1em; }
.ms-slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: .78em;
  min-height: 1.15em;
  border-bottom: .055em solid var(--ms-slot-line);
}
.ms-slot.is-next { border-bottom-color: var(--color-primary); }
.ms-slot.is-fix { color: var(--ms-bad); border-bottom-color: var(--ms-bad); }

/* ---- 채점 표시(빨간펜): 동그라미 / 빗금 ---- */
.ms-mark {
  position: absolute;
  inset: -18% -10%;
  width: 120%;
  height: 136%;
  pointer-events: none;
  transform: rotate(-3deg);
}
.ms-mark ellipse,
.ms-mark path {
  fill: none;
  stroke: var(--ms-pen);
  stroke-width: 6.5;
  stroke-linecap: round;
  opacity: 0;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
}
.ms-mark[data-kind="ok"] .ms-mark-circle,
.ms-mark[data-kind="bad"] .ms-mark-slash {
  opacity: .9;
  transition: stroke-dashoffset .3s ease-out;
  stroke-dashoffset: 0;
}

/* ---- 연속 정답 배지 ---- */
.ms-combo {
  position: absolute;
  top: 16px;
  right: 16px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1.5px solid var(--ms-pen);
  color: var(--ms-pen);
  background: var(--ms-pen-weak);
  font-size: .92rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.ms-combo.is-pop { animation: ms-pop .22s ease-out; }
@keyframes ms-pop {
  from { transform: scale(.7); }
  60%  { transform: scale(1.12); }
  to   { transform: scale(1); }
}

/* ---- 키패드 ---- */
.ms-keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  max-width: 340px;
  margin: var(--space-4) auto 0;
}
.ms-key {
  font: inherit;
  font-size: 1.55rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  min-height: clamp(54px, 12vw, 64px);
  border-radius: 14px;
  border: 1.5px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  transition: transform .06s ease, border-color .1s ease;
}
.ms-key:hover { border-color: var(--color-primary); }
.ms-key:active { transform: scale(.94); }
.ms-key:focus-visible { outline: 3px solid var(--color-text); outline-offset: 2px; }
.ms-key-del { font-size: 1.05rem; color: var(--color-text-sub); }
.ms-keypad[data-locked="true"] .ms-key {
  opacity: .4;
  cursor: default;
  transform: none;
}
.ms-keypad[data-locked="true"] .ms-key:hover { border-color: var(--color-border); }

/* ---- 오버레이(시작/종료) ---- */
.ms-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 24px;
  background: color-mix(in srgb, var(--color-bg) 80%, transparent);
  -webkit-backdrop-filter: blur(3px);
          backdrop-filter: blur(3px);
}
@supports not (background: color-mix(in srgb, red 50%, transparent)) {
  .ms-overlay { background: rgba(243, 240, 232, .88); }
  :root[data-theme="dark"] .ms-overlay { background: rgba(23, 19, 14, .88); }
}
.ms-overlay-panel {
  text-align: center;
  padding: var(--space-5);
  max-width: 100%;
}
.ms-overlay-note {
  margin: 0 0 var(--space-4);
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.6;
  color: var(--color-text);
}
.ms-btn-big {
  font: inherit;
  font-size: 1.35rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  padding: 18px 44px;
  border-radius: 999px;
  border: 0;
  background: var(--color-primary);
  color: #fff;
  box-shadow: var(--shadow);
}
.ms-btn-big:hover { filter: brightness(1.06); }
.ms-btn-big:active { transform: scale(.97); }

/* ---- 종료 화면: 시험지 채점 ---- */
.ms-over-eyebrow {
  margin: 0 0 var(--space-2);
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .2em;
  color: var(--color-text-sub);
}
.ms-score-wrap {
  position: relative;
  display: inline-block;
  padding: 8px 26px;
}
.ms-over-score {
  font-family: var(--font-serif);
  font-size: 2.1rem;
  font-weight: 700;
  color: var(--color-text);
}
.ms-over-score b {
  font-size: 3.4rem;
  color: var(--ms-pen);
  font-variant-numeric: tabular-nums;
}
.ms-over-score small {
  font-size: 1.2rem;
  margin-left: 2px;
}
/* 점수에도 빨간펜 동그라미 */
.ms-score-ring {
  position: absolute;
  inset: -6px -4px;
  width: calc(100% + 8px);
  height: calc(100% + 12px);
  pointer-events: none;
  transform: rotate(-2deg);
}
.ms-score-ring ellipse {
  fill: none;
  stroke: var(--ms-pen);
  stroke-width: 3.5;
  stroke-linecap: round;
  opacity: .85;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
}
.ms-score-ring.is-draw ellipse {
  transition: stroke-dashoffset .5s ease-out .15s;
  stroke-dashoffset: 0;
}
.ms-over-grade {
  margin: var(--space-2) 0 0;
  font-size: 1.12rem;
  font-weight: 700;
  color: var(--color-text);
}
.ms-over-stats {
  margin: var(--space-1) 0 0;
  font-size: .98rem;
  color: var(--color-text-sub);
  font-variant-numeric: tabular-nums;
}
.ms-over-best {
  margin: var(--space-1) 0 var(--space-3);
  font-size: 1rem;
  color: var(--color-text-sub);
}
.ms-over-best .ms-new-record { color: var(--ms-pen); font-weight: 700; }

/* 오답 다시보기 */
.ms-wrongs {
  margin: 0 auto var(--space-4);
  max-width: 340px;
  text-align: left;
}
.ms-wrongs-title {
  margin: 0 0 var(--space-2);
  font-size: .88rem;
  font-weight: 700;
  color: var(--color-text-sub);
}
.ms-wrongs-list {
  max-height: 168px;
  overflow-y: auto;
  display: grid;
  gap: 6px;
  padding-right: 4px;
}
.ms-wrong-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  font-variant-numeric: tabular-nums;
}
.ms-wr-eq { font-weight: 700; color: var(--color-text); white-space: nowrap; }
.ms-wr-eq b { color: var(--ms-pen); }
.ms-wr-my { font-size: .88rem; color: var(--ms-bad); white-space: nowrap; }

/* ---- 안내 문구 ---- */
.ms-msg {
  min-height: 1.8em;
  margin: var(--space-3) 0 0;
  text-align: center;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--color-text);
}
.ms-msg b { color: var(--ms-pen); }

/* ---- 본문 ---- */
.prose { max-width: 720px; margin: clamp(40px, 6vw, 64px) auto 0; }
.prose h2 { margin: 0 0 var(--space-3); }
.prose h3 { margin: var(--space-4) 0 var(--space-2); font-size: 1.08rem; }
.prose p { margin: 0 0 var(--space-3); color: var(--color-text); }
.prose ul { margin: 0; padding-left: 1.2em; }
.prose li { margin-bottom: var(--space-2); }
.prose li strong { color: var(--color-text); }

@media (prefers-reduced-motion: reduce) {
  .ms-eq.is-in { animation: none; }
  .ms-combo.is-pop { animation: none; }
  .ms-mark ellipse, .ms-mark path, .ms-score-ring ellipse { transition: none; }
  .ms-key, .ms-btn-big { transition: none; }
}

@media (max-width: 420px) {
  .ms-stat { min-width: 60px; font-size: .95rem; }
  .ms-mid { font-size: 1rem; padding: 9px 10px; }
}
