:root{
  --bg0:#0b0f19;
  --bg1:#0f172a;
  --card:rgba(255,255,255,.06);
  --card2:rgba(255,255,255,.08);
  --stroke:rgba(255,255,255,.12);
  --text:#e6edf7;
  --muted:rgba(230,237,247,.70);
  --muted2:rgba(230,237,247,.55);
  --accent:#5eead4;
  --accent2:#38bdf8;
  --danger:#fb7185;
  --shadow: 0 20px 60px rgba(0,0,0,.40);
  --radius: 18px;
}

html[data-theme="light"]{
  --bg0:#f7fafc;
  --bg1:#eef2ff;
  --card:rgba(16,24,40,.04);
  --card2:rgba(16,24,40,.06);
  --stroke:rgba(16,24,40,.12);
  --text:#0b1220;
  --muted:rgba(11,18,32,.70);
  --muted2:rgba(11,18,32,.55);
  --shadow: 0 20px 60px rgba(15,23,42,.12);
}

*{box-sizing:border-box}
html, body{ height:100%; }
body{
  margin:0;
  position: fixed;
  inset: 0;
  overflow: hidden;
  font-family: ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,Helvetica,Arial,"Apple Color Emoji","Segoe UI Emoji";
  color:var(--text);
  background:linear-gradient(180deg,var(--bg0),var(--bg1));
  min-height:100vh;
  overflow-x:hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

.bg{
  position:fixed; inset:-40vh -30vw;
  background:
    radial-gradient(60vw 60vw at 20% 20%, rgba(56,189,248,.25), transparent 55%),
    radial-gradient(50vw 50vw at 80% 30%, rgba(94,234,212,.22), transparent 60%),
    radial-gradient(50vw 50vw at 50% 85%, rgba(99,102,241,.16), transparent 65%);
  filter: blur(30px);
  pointer-events:none;
}

.topbar{
  position:fixed; top:0; left:0; right:0; z-index:50;
  width:100vw;
  margin-left: calc(50% - 50vw);
  margin-right: calc(50% - 50vw);
  -webkit-backdrop-filter: blur(14px);
  display:flex; align-items:center; justify-content:space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 12px;
  backdrop-filter: blur(14px);
  /* Full-width blur (no cut-off on the right like a gradient) */
  background: rgba(0,0,0,.22);
  border-bottom: 1px solid rgba(255,255,255,.06);
}
html[data-theme="light"] .topbar{ background: rgba(255,255,255,.78); border-bottom: 1px solid rgba(16,24,40,.08); }

.brand{ display:flex; gap:12px; align-items:center; }
.logo{
  width:42px; height:42px; border-radius:14px;
  display:grid; place-items:center;
  background: linear-gradient(135deg, rgba(56,189,248,.35), rgba(94,234,212,.22));
  border:1px solid var(--stroke);
  box-shadow: var(--shadow);
  font-weight:800; letter-spacing:.6px;
}
.title{ font-weight:800; font-size:15px; line-height:1.1; }
.subtitle{ font-size:12px; color:var(--muted); margin-top:2px; }

.shell{ padding: 18px 14px 40px; max-width: 1100px; margin:0 auto;
  margin-top: var(--topbar-h, 64px);
  height: calc(var(--vh, 100dvh) - var(--topbar-h, 64px));
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(40px + var(--msr-kb-h, 0px));
}

.grid{ display:grid; grid-template-columns: 320px 1fr; gap:14px; align-items:start; }
@media (max-width: 860px){ .grid{ grid-template-columns: 1fr; } }

.card{
  background: var(--card);
  border:1px solid var(--stroke);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel{ padding:16px; }
.content{ padding:16px; min-height: 560px; }

h2{ margin:0 0 12px; font-size:16px; }

.muted{ color: var(--muted); }
.small{ font-size:12px; }

.chips{ display:flex; flex-wrap:wrap; gap:8px; }
.chip{
  display:flex; gap:8px; align-items:center;
  padding:10px 12px;
  border-radius: 999px;
  border:1px solid var(--stroke);
  background: var(--card2);
  cursor:pointer;
  user-select:none;
  transition: transform .08s ease, background .2s ease, border-color .2s ease;
}
.chip:hover{ transform: translateY(-1px); }
.chip.active{ border-color: rgba(94,234,212,.45); background: rgba(94,234,212,.10); }

.row{ display:flex; align-items:center; justify-content:space-between; gap:10px; margin-bottom:12px; }
.rowRight{ display:flex; align-items:center; gap:8px; }

.pill{
  padding:6px 10px;
  border-radius: 999px;
  border:1px solid var(--stroke);
  background: var(--card2);
  color: var(--muted);
  font-size:12px;
}

.list{ display:flex; flex-direction:column; gap:10px; }

.item{
  padding:12px 12px;
  border-radius: 14px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.04);
  display:flex; align-items:center; justify-content:space-between; gap:10px;
}
html[data-theme="light"] .item{ background: rgba(16,24,40,.03); }

.itemLeft{ min-width:0; }
.itemTitle{ font-weight:700; }
.itemMeta{ font-size:12px; color: var(--muted); margin-top:4px; }
.itemRight{ display:flex; gap:8px; align-items:center; }

.btn, .ghost{
  border-radius: 14px;
  border:1px solid var(--stroke);
  padding:10px 12px;
  font-weight:700;
  cursor:pointer;
  background: var(--card2);
  color: var(--text);
  transition: transform .08s ease, background .2s ease, border-color .2s ease;
}
.btn:hover, .ghost:hover{ transform: translateY(-1px); }
.btn:active, .ghost:active{ transform: translateY(0px) scale(.99); }
.btn.primary{
  border-color: rgba(56,189,248,.50);
  background: linear-gradient(135deg, rgba(56,189,248,.35), rgba(94,234,212,.18));
}
.btn.danger{ border-color: rgba(251,113,133,.55); background: rgba(251,113,133,.12); }
.ghost{ background: transparent; }

.hint{ margin-top:14px; padding:12px; border-radius: 14px; border:1px dashed var(--stroke); background: rgba(255,255,255,.03); }
.hintTitle{ font-weight:800; margin-bottom:6px; }
.spacer{ height:10px; }

.solveTop{ display:flex; justify-content:space-between; gap:12px; align-items:flex-start; margin: 6px 0 12px; flex-wrap:wrap; }
.solveName{ font-weight:900; font-size:16px; }
.solveMeta{ margin-top:4px; }
.solveActions{ display:flex; gap:8px; align-items:center; }

.progress{ height:10px; border-radius: 999px; border:1px solid var(--stroke); background: rgba(255,255,255,.04); overflow:hidden; margin-top:10px; }
.bar{ height:100%; width:0%; background: linear-gradient(90deg, rgba(56,189,248,.65), rgba(94,234,212,.65)); }

.solveGrid{ display:grid; grid-template-columns: 1fr 320px; gap:12px; margin-top:12px; }
@media (max-width: 860px){ .solveGrid{ grid-template-columns: 1fr; } }

.qnav{
  display:grid;
  grid-template-columns: repeat(8, 1fr);
  gap:8px;
  padding:12px;
  border-radius: 16px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.03);
  align-content:start;
}
@media (max-width: 480px){ .qnav{ grid-template-columns: repeat(6, 1fr);} }

.qbtn{
  padding:9px 0;
  border-radius: 12px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.04);
  color: var(--muted);
  font-weight:800;
  cursor:pointer;
}
.qbtn.active{ border-color: rgba(56,189,248,.55); color: var(--text); background: rgba(56,189,248,.12); }
.qbtn.done{ border-color: rgba(94,234,212,.50); color: var(--text); background: rgba(94,234,212,.10); }

.qcard{
  padding:12px;
  border-radius: 16px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.03);
  min-height: 220px;
  display:flex;
  flex-direction:column;
  gap:10px;
}
.qtitle{ font-weight:900; }
.choices{ display:flex; flex-wrap:wrap; gap:8px; }

.choice{
  min-width: 48px;
  padding:10px 12px;
  border-radius: 14px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.04);
  color: var(--text);
  font-weight:900;
  cursor:pointer;
}
.choice.active{ border-color: rgba(94,234,212,.55); background: rgba(94,234,212,.12); }
.choice.empty{ color: var(--muted2); }

.qbottom{ display:flex; justify-content:space-between; gap:8px; margin-top:auto; }

.result{ margin-top:16px; padding:14px; border-radius: 16px; border:1px solid var(--stroke); background: rgba(255,255,255,.03); }
.resultGrid{ display:grid; grid-template-columns: repeat(3, 1fr); gap:10px; margin-top:10px; }
@media (max-width: 640px){ .resultGrid{ grid-template-columns: 1fr; } }
.metric{ padding:12px; border-radius: 14px; border:1px solid var(--stroke); background: rgba(255,255,255,.04); }
.metricLabel{ color: var(--muted); font-size:12px; }
.metricValue{ font-size:18px; font-weight:900; margin-top:6px; }
.resultBtns{ display:flex; flex-wrap:wrap; gap:10px; margin-top:12px; }

.gateBtns{ display:flex; gap:10px; flex-wrap:wrap; margin: 12px 0; }

.footer{ padding: 18px 12px 26px; text-align:center; }

/* Manual answers UI */
.choicesRow{ display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.manualBox{
  width:100%;
  margin-top:10px;
  padding:10px;
  border-radius: 14px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.02);
}
.manualList{ display:flex; flex-wrap:wrap; gap:8px; margin-top:8px; }
.mchip{
  display:flex; align-items:center; gap:8px;
  padding:8px 10px;
  border-radius: 999px;
  border:1px solid var(--stroke);
  background: var(--card2);
  max-width: 100%;
}
.mtext{ max-width: 240px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.mx{
  border:0;
  background: transparent;
  color: var(--muted);
  cursor:pointer;
  font-weight:900;
}
.manualAdd{ display:flex; gap:8px; margin-top:10px; }
.manualAdd input{
  flex:1;
  padding:10px 12px;
  border-radius: 14px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.04);
  color: var(--text);
  outline:none;
}
.manualAdd input:focus{ border-color: rgba(56,189,248,.55); }
.dots{ min-width:54px; }

/* Manual answers (a,b,c,...) input rows */
.manualFields{ display:flex; flex-direction:column; gap:10px; margin-top:10px; }
.manualRow{ display:flex; align-items:center; gap:10px; }
.manualLabel{ width:32px; font-weight:900; color: var(--muted); }
.manualInputWrap{ position:relative; flex:1; }
.manualInput{
  width:100%;
  padding:10px 12px;
  border-radius: 14px;
  border:1px solid var(--stroke);
  background: rgba(255,255,255,.04);
  color: var(--text);
  outline:none;
}
.manualInput:focus{ border-color: rgba(56,189,248,.55); }
.manualInputWrap.focused::after{
  content:"";
  position:absolute;
  top:10px;
  bottom:10px;
  width:2px;
  left: calc(12px + var(--caret-x, 0px));
  border-radius: 2px;
  background: rgba(56,189,248,.95);
  animation: msr-caret 1s steps(2,end) infinite;
  pointer-events:none;
}
@keyframes msr-caret{ 0%,50%{opacity:1} 50.01%,100%{opacity:0} }
.manualMore{ min-width:54px; }

/* Smooth-ish, but cheap */
.card, .item, .qcard, .qnav, .manualBox{ animation: pop .18s ease-out; }
@keyframes pop{ from{ transform: translateY(2px); opacity:.85; } to{ transform: translateY(0); opacity:1; } }

/* Better tap targets on narrow screens */
@media (max-width: 420px){
  .choice{ min-width: 44px; }
  .btn, .ghost{ padding:10px 10px; }
}

/* Manual fraction strict validation */
.manualInput.invalid{
  border-color: rgba(239,68,68,.55) !important;
  background: rgba(239,68,68,.06) !important;
  animation: msr-shake 220ms ease-in-out 1;
}
@keyframes msr-shake{
  0%{ transform: translateX(0); }
  25%{ transform: translateX(-2px); }
  50%{ transform: translateX(2px); }
  75%{ transform: translateX(-1px); }
  100%{ transform: translateX(0); }
}



/* -----------------------------
   Photomath-like keyboard (MSR)
   ----------------------------- */
:root { --msr-kb-h: 0px; }

.msr-kb {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 9999;
  background: #f6f6f7;
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  box-shadow: 0 -8px 22px rgba(0,0,0,0.14);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.msr-kb.hidden { display: none; }

.msr-kb-wrap { padding-bottom: env(safe-area-inset-bottom); }

.msr-kb-top {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1.4fr 1fr;
  gap: 8px;
  padding: 10px 12px 8px;
  align-items: center;
}

.msr-kb-topbtn {
  border: 1px solid #e3e3e7;
  background: #ffffff;
  border-radius: 14px;
  padding: 10px 10px;
  font-weight: 700;
  font-size: 14px;
}

.msr-kb-topbtn:active { transform: translateY(1px); }
.msr-kb-topbtn.danger { border-color: #d7d7db; }

.msr-kb-pills { padding: 0 12px 6px; }
.msr-kb-pillrow { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr 0.9fr; gap: 10px; }

.msr-kb-key.pill {
  border-radius: 18px;
  padding: 10px 0;
  font-size: 14px;
  font-weight: 800;
  background: #ffffff;
  border: 1px solid #e2e2e6;
}

.msr-kb-key.pill.active {
  background: #111111;
  color: #ffffff;
  border-color: #111111;
}

.msr-kb-gridWrap {
  padding: 0 12px 14px;
  max-height: 46vh;
  overflow-y: auto;
}

.msr-kb-grid {
  display: grid;
  grid-template-columns: repeat(var(--msr-kb-cols, 6), 1fr);
  gap: 8px;
}

.msr-kb-key {
  border: 1px solid #e3e3e7;
  background: #ffffff;
  border-radius: 14px;
  padding: 14px 6px;
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  min-height: 44px;
}

.msr-kb-key.empty {
  visibility: hidden;
}

.msr-kb-key:active { transform: translateY(1px); }

@media (max-width: 420px) {
  .msr-kb-gridWrap { max-height: 48vh; }
  .msr-kb-key { font-size: 16px; padding: 12px 4px; }
  .msr-kb-topbtn { font-size: 13px; padding: 10px 8px; }
}


/* Hidden text measurer for caret positioning */
.msr-measure{
  position:absolute;
  visibility:hidden;
  white-space:pre;
  pointer-events:none;
  font: inherit;
  letter-spacing: inherit;
}
