*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #f4f2ef;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: #1c1917;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100svh;
  max-height: 100dvh;
  max-height: 100svh;
  overflow: hidden;
  background: #f4f2ef;
}

.bar {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  padding: calc(0.65rem + env(safe-area-inset-top, 0px)) 1rem 0.65rem;
  background: #312e81;
  color: #eef2ff;
  flex-shrink: 0;
  z-index: 10;
  border-bottom: 3px solid #1e1b4b;
}

.bar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: 100%;
}

.week-track {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.3rem;
  width: 100%;
  font-size: 0.72rem;
}

.week-day {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 1.55rem;
  padding: 0.12rem 0.2rem;
  border-radius: 5px;
  background: rgb(255 255 255 / 0.08);
  opacity: 0.55;
  line-height: 1.1;
}

.week-day.today {
  outline: 1px solid rgb(255 255 255 / 0.45);
  opacity: 1;
}

.week-day.done {
  background: rgb(134 239 172 / 0.32);
  opacity: 1;
}

.week-lbl {
  font-weight: 700;
}

.week-n {
  font-size: 0.62rem;
  opacity: 0.92;
}

.week-streak {
  margin-left: auto;
  font-weight: 600;
  white-space: nowrap;
}

.bar h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.ver {
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  background: rgb(255 255 255 / 0.15);
  border-radius: 6px;
}

.account-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
  font-size: 0.8rem;
}

.account-email {
  max-width: 9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.9;
}

.btn-account {
  border: 1px solid rgb(255 255 255 / 0.35);
  background: rgb(255 255 255 / 0.12);
  color: inherit;
  border-radius: 6px;
  padding: 0.2rem 0.55rem;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.btn-account:hover {
  background: rgb(255 255 255 / 0.22);
}

.paywall {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgb(15 23 42 / 0.72);
  backdrop-filter: blur(4px);
}

.paywall-card {
  width: min(22rem, 100%);
  padding: 1.5rem;
  border-radius: 12px;
  background: #fff;
  color: #1e1b4b;
  box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
  text-align: center;
}

.paywall-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.35rem;
}

.paywall-msg {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  line-height: 1.45;
  color: #4338ca;
}

.paywall-price {
  margin: 0 0 1rem;
  font-size: 1.1rem;
  font-weight: 600;
}

.paywall-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.paywall-auth {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.paywall-input {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #c7d2fe;
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  font: inherit;
  font-size: 0.95rem;
}

.btn-paywall {
  border: 1px solid #c7d2fe;
  background: #eef2ff;
  color: #312e81;
  border-radius: 8px;
  padding: 0.65rem 1rem;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn-paywall.primary {
  background: #4f46e5;
  border-color: #4f46e5;
  color: #fff;
}

.btn-paywall:hover {
  filter: brightness(1.05);
}

#app.paywall-active .workspace,
#app.paywall-active .foot .dock {
  pointer-events: none;
  opacity: 0.35;
}

.problem {
  padding: 0.75rem 1rem;
  background: #eef2ff;
  border-bottom: 1px solid #c7d2fe;
  flex-shrink: 0;
  max-height: 22vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.problem-prompt {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 650;
  color: #1e1b4b;
  line-height: 1.5;
  overflow-x: auto;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
}

.problem-prompt .katex, .problem-answer .katex, .problem-note .katex {
  font-size: 1.05em;
}

.problem-prompt .katex-display {
  margin: 0.35rem 0;
  text-align: left;
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

.problem-meta {
  margin: 0.35rem 0 0;
  font-size: 0.8rem;
  color: #4338ca;
}

.problem-answer {
  margin: 0.45rem 0 0;
  padding: 0.45rem 0.55rem;
  font-size: 0.95rem;
  font-weight: 650;
  color: #166534;
  background: #ecfdf5;
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  line-height: 1.45;
}

.problem-answer[hidden] {
  display: none;
}

.problem-answer .katex {
  font-size: 1.02em;
}

.problem-report {
  margin: 0.35rem 0 0;
  padding: 0;
  border: none;
  background: none;
  color: #7c3aed;
  font: inherit;
  font-size: 0.78rem;
  text-decoration: underline;
  cursor: pointer;
}

.problem-report:hover {
  color: #5b21b6;
}

.solution-panel {
  padding: 0.65rem 1rem 0.75rem;
  background: #f0fdf4;
  border-bottom: 1px solid #bbf7d0;
  flex-shrink: 0;
  max-height: 28vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.solution-panel[hidden] {
  display: none;
}

.solution-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.45rem;
}

.solution-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  color: #166534;
}

.solution-close {
  font-size: 0.82rem;
  padding: 0.3rem 0.55rem;
  color: #166534;
  background: #dcfce7;
  border-radius: 8px;
}

.solution-steps {
  margin: 0;
  padding-left: 1.25rem;
  color: #14532d;
}

.solution-step {
  margin: 0.35rem 0;
  font-size: 0.92rem;
  line-height: 1.45;
}

.solution-step .katex {
  font-size: 1em;
}

.solution-step:last-child {
  font-weight: 700;
  color: #15803d;
}

.problem.celebrate {
  animation: celebrate-pop 0.9s ease-out;
}

@keyframes celebrate-pop {
  0% { background: #eef2ff; transform: scale(1); }
  18% { background: #bbf7d0; transform: scale(1.025); }
  55% { background: #dcfce7; }
  100% { background: #eef2ff; transform: scale(1); }
}

.confetti {
  position: absolute;
  top: 28%;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  pointer-events: none;
  z-index: 50;
  opacity: 0.95;
  animation: confetti-fall 1.1s ease-out forwards;
}

@keyframes confetti-fall {
  0% { transform: translateY(0) rotate(0deg) scale(1); opacity: 1; }
  100% { transform: translateY(140px) rotate(620deg) scale(0.6); opacity: 0; }
}

.status.correct-flash {
  color: #15803d;
  background: #dcfce7;
  font-weight: 700;
  transition: background 0.25s, color 0.25s;
}

.workspace {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  background: #fffdf8;
  touch-action: none;
}

.rules-bar {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
  padding: 0.45rem 0.65rem;
  background: #fffbeb;
  border-bottom: 1px solid #fde68a;
  flex-shrink: 0;
  min-height: 0;
}

.rules-bar.collapsed {
  padding: 0.3rem 0.65rem;
}

.rules-bar.collapsed .rules-scroll {
  display: none;
}

.rules-toggle {
  flex-shrink: 0;
  align-self: center;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #92400e;
  padding: 0.25rem 0.55rem;
  border: 1px solid #fbbf24;
  border-radius: 6px;
  background: #fef3c7;
  cursor: pointer;
}

.rules-label {
  flex-shrink: 0;
  align-self: center;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #92400e;
  padding: 0.2rem 0.35rem;
}

.rules-scroll {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.4rem;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: #fbbf24 #fef3c7;
  padding: 0.1rem 0.15rem 0.2rem;
  flex: 1;
  min-width: 0;
}

.rules-scroll::-webkit-scrollbar {
  height: 6px;
}

.rules-scroll::-webkit-scrollbar-thumb {
  background: #fbbf24;
  border-radius: 999px;
}

.type-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex: 1;
  min-width: 0;
}

.type-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1rem;
  padding: 0.45rem 0.6rem;
  border: 2px solid #312e81;
  border-radius: 8px;
  background: #fff;
}

.rule-chip {
  flex: 0 0 auto;
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  max-width: 11rem;
  padding: 0.35rem 0.55rem;
  border: 1.5px solid #fde68a;
  border-radius: 10px;
  background: #fff;
  color: #78350f;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.2;
  cursor: pointer;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}

.rule-chip:active {
  transform: scale(0.97);
}

.rule-chip.is-relevant {
  border-color: #4f46e5;
  background: #eef2ff;
  color: #312e81;
  box-shadow: 0 0 0 1px rgb(79 70 229 / 0.15);
}

.rule-chip-math {
  font-size: 0.68rem;
  font-weight: 500;
  color: #57534e;
  max-width: 10rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.rule-chip.is-relevant .rule-chip-math {
  color: #4338ca;
}

.rule-chip-math .katex {
  font-size: 0.95em;
}

.scratch-stamps {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 2;
}

.scratch-stamp {
  position: absolute;
  left: 0.65rem;
  right: 0.65rem;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.15rem 0.35rem;
  border-radius: 8px;
  background: rgb(255 251 235 / 0.88);
  border: 1px dashed #fbbf24;
  pointer-events: none;
  animation: stamp-pop 0.22s ease-out;
}

@keyframes stamp-pop {
  from {
    opacity: 0;
    transform: translateY(4px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.scratch-stamp-label {
  flex-shrink: 0;
  font-size: 0.62rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #b45309;
}

.scratch-stamp-math {
  min-width: 0;
  overflow: hidden;
  color: #44403c;
}

.scratch-stamp-math .katex {
  font-size: 0.92em;
}

#pad {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  -webkit-touch-callout: none;
  cursor: crosshair;
}

.foot {
  flex-shrink: 0;
  background: #fff;
  border-top: 1px solid #e7e5e4;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  z-index: 10;
}

.dock {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.65rem 0.35rem;
  max-height: 26vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.tools {
  display: inline-flex;
  padding: 3px;
  background: #f5f5f4;
  border: 1px solid #e7e5e4;
  border-radius: 10px;
}

.tool,
.btn {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 0.88rem;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  color: #44403c;
  touch-action: manipulation;
}

.tool.on,
.btn.on {
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.08);
  color: #312e81;
  font-weight: 600;
}

.dock:has(.label-group:not([hidden])) .size {
  margin-left: 0;
}

.size {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: #57534e;
  margin-left: auto;
}

.size input {
  width: 5rem;
}

.label-group {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: auto;
  padding: 2px 4px;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 10px;
}

.label-group[hidden] {
  display: none;
}

.label-hint {
  font-size: 0.82rem;
  font-weight: 600;
  color: #92400e;
  padding-left: 0.25rem;
}

.label-expected {
  font-size: 0.82rem;
  font-weight: 700;
  color: #b91c1c;
}

.label-expected .katex {
  font-size: 0.95em;
}

.label-expected[hidden] {
  display: none;
}

.label-good,
.label-bad,
.label-misread,
.label-wrong,
.label-correct {
  font-size: 0.82rem;
  font-weight: 700;
  min-width: 2.2rem;
  padding: 0.35rem 0.5rem;
}

.label-misread {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  color: #9a3412;
  background: #ffedd5;
  border-radius: 999px;
  min-width: 5.5rem;
  min-height: 2.75rem;
  padding: 0.35rem 0.85rem 0.35rem 3rem;
}

.misread-timer {
  position: absolute;
  left: 0.35rem;
  top: 50%;
  width: 2.25rem;
  height: 2.25rem;
  margin-top: -1.125rem;
  pointer-events: none;
}

.misread-timer-bg {
  fill: none;
  stroke: #fed7aa;
  stroke-width: 3;
}

.misread-timer-fg {
  fill: none;
  stroke: #ea580c;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 100.53;
  stroke-dashoffset: 0;
  transform: rotate(-90deg);
  transform-origin: 20px 20px;
}

.misread-timer-fg.run {
  animation: misread-countdown 5s linear forwards;
}

@keyframes misread-countdown {
  from { stroke-dashoffset: 0; }
  to { stroke-dashoffset: 100.53; }
}

.misread-label {
  position: relative;
  z-index: 1;
}

.label-wrong {
  color: #b45309;
  background: #fef3c7;
  border-radius: 8px;
}

.label-correct {
  color: #15803d;
  background: #dcfce7;
  border-radius: 8px;
}

.dock:has(.label-group:not([hidden])) .size {
  margin-left: 0;
}

.status {
  margin: 0;
  padding: 0.3rem 1rem 0.45rem;
  font-size: 0.82rem;
  color: #57534e;
  background: #fafaf9;
  border-top: 1px solid #f5f5f4;
  min-height: 1.4rem;
  flex-shrink: 0;
}

#boot-err {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 99;
  margin: 0;
  padding: 1rem;
  background: #fef2f2;
  color: #991b1b;
  font-size: 0.9rem;
  display: none;
}

#boot-err.show {
  display: block;
}
