  * { box-sizing: border-box; }
  body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: #f4f4f4;
    color: #222;
  }

  header {
    position: sticky;
    top: 0;
    background: #fff;
    border-bottom: 1px solid #ddd;
    padding: 10px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
    z-index: 10;
  }
  header h1 { font-size: 17px; margin: 0; }
  header .sub { font-size: 12px; color: #888; margin-top: 2px; }
  .controls { display: flex; align-items: center; gap: 6px; font-size: 13px; width: 100%; }
  .controls input[type="number"] {
    flex: 1 1 auto; min-width: 0; padding: 9px 8px; border: 1px solid #ccc; border-radius: 8px; font-size: 16px;
  }
  .controls button, .reset-btn, .formula-btn {
    padding: 9px 10px; border: 1px solid #ccc; border-radius: 8px; background: #fff; cursor: pointer; font-size: 13px;
    flex: 0 0 auto;
  }
  .controls button:hover, .reset-btn:hover, .formula-btn:hover { background: #f0f0f0; }
  .controls button:active, .reset-btn:active, .formula-btn:active { background: #e6e6e6; }
  .formula-btn { margin-left: auto; }
  .formula-btn-label { display: none; }
  .solved-count { font-weight: 600; }

  main { padding: 10px; }
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 8px;
    max-width: 900px;
    margin: 0 auto;
  }
  .num-btn {
    aspect-ratio: 1 / 1;
    border: none;
    border-radius: 8px;
    background: #d9d9d9;
    color: #555;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    white-space: nowrap;
    padding: 2px;
    font-variant-numeric: tabular-nums;
    font-size: 13px;
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  .num-btn:hover { filter: brightness(0.94); }
  .num-btn.flash { outline: 3px solid #ffb300; }
  .grid-tooltip {
    position: fixed; display: none; z-index: 5;
    background: #222; color: #fff; font-size: 12px; font-weight: 600;
    padding: 5px 9px; border-radius: 6px; pointer-events: none;
    white-space: nowrap; box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  }

  /* problem-type colors — plain +/- stay light near 0, then shift toward
     more "unusual"/procedural colors as harder operation types unlock */
  .num-btn.type-plus     { background: #e3f2fd; color: #1565c0; }
  .num-btn.type-minus    { background: #fff3e0; color: #e65100; }
  .num-btn.type-times    { background: #f3e5f5; color: #6a1b9a; }
  .num-btn.type-div      { background: #fce4ec; color: #ad1457; }
  .num-btn.type-frac     { background: #fff9c4; color: #9e8b00; }
  .num-btn.type-mixed    { background: #ffe0b2; color: #bf5b00; }
  .num-btn.type-sum      { background: #e0f2f1; color: #00695c; }
  .num-btn.type-power    { background: #e8eaf6; color: #283593; }
  .num-btn.type-product  { background: #ede7f6; color: #4527a0; }
  .num-btn.type-tower    { background: #f1f8e9; color: #558b2f; }
  .num-btn.type-limit    { background: #fbe9e7; color: #bf360c; }
  .num-btn.type-integral { background: #eceff1; color: #37474f; }
  .num-btn.type-log      { background: #fce4ff; color: #8e24aa; }
  .num-btn.type-geometry { background: #e0f7fa; color: #00838f; }
  .num-btn.type-algebra  { background: #fff0f6; color: #ad1457; }
  .num-btn.type-trig     { background: #e8f5e9; color: #2e7d32; }
  .num-btn.type-lawsin   { background: #f3e5ab; color: #8d6e00; }
  .num-btn.type-lawcos   { background: #ffe0e0; color: #c62828; }
  .num-btn.type-calculus { background: #e1f5fe; color: #01579b; }

  /* higher specificity than the type-* rules above so "done" always wins */
  button.num-btn.done { background: #4caf50 !important; color: #fff !important; }
  button.num-btn.done:hover { background: #43a047 !important; }

  .legend {
    display: flex; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch;
    gap: 7px 12px;
    padding: 8px 12px; font-size: 11px; color: #666;
    background: #fff; border-bottom: 1px solid #eee;
    scrollbar-width: none;
  }
  .legend::-webkit-scrollbar { display: none; }
  .legend .item { display: flex; align-items: center; gap: 5px; white-space: nowrap; flex-shrink: 0; }
  .legend .swatch { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
  .legend .from { color: #aaa; }

  /* --- Desktop / wide-viewport refinements --- */
  @media (min-width: 640px) {
    header { padding: 14px 18px; gap: 10px; }
    header h1 { font-size: 18px; }
    .controls { width: auto; gap: 8px; }
    .controls input[type="number"] { flex: 0 0 auto; width: 90px; font-size: 13px; padding: 6px 8px; border-radius: 6px; }
    .controls button, .reset-btn, .formula-btn { padding: 6px 10px; border-radius: 6px; }
    .formula-btn { margin-left: 0; }
    .formula-btn-label { display: inline; }
    main { padding: 18px; }
    .grid { grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); }
    .num-btn { min-height: 0; }
    .legend { flex-wrap: wrap; overflow-x: visible; gap: 7px 14px; padding: 8px 18px; }
  }

  #sentinel, #top-sentinel { height: 1px; grid-column: 1 / -1; }

  /* modal — full screen on phones (roomy tap targets, no fiddly small
     dialog), a large centered card on bigger screens */
  .overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    padding: 0; z-index: 100;
  }
  .modal {
    background: #fff; border-radius: 0; padding: 28px 24px;
    width: 100%; height: 100%; max-width: none;
    display: flex; flex-direction: column;
    text-align: center; position: relative;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .modal { padding: 24px 18px; padding-top: 56px; }
  .modal math-field { font-size: 20px; padding: 12px 14px; max-height: 90px; }
  .modal-buttons { flex-shrink: 0; padding-top: 8px; }
  .modal input[type="text"] { font-size: 20px; padding: 14px; }
  .modal-buttons button { padding: 16px; font-size: 16px; }
  .modal-top-close {
    position: absolute; top: 14px; right: 14px;
    width: 34px; height: 34px; border-radius: 50%; border: none;
    background: #f0f0f0; color: #666; font-size: 18px; line-height: 1;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
  }
  .modal-top-close:hover { background: #e2e2e2; }
  .modal .label { font-size: 13px; color: #888; letter-spacing: .02em; margin-bottom: 4px; }
  .modal .type-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; margin-bottom: 18px; }
  .modal .problem {
    font-size: 24px;
    font-weight: 700;
    margin: 8px 0 22px;
    min-height: 1.4em;
    text-align: center;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }
  .modal .problem .katex-display { margin: 0; }
  .modal .problem.fallback { font-family: "Courier New", monospace; font-size: 20px; white-space: pre-wrap; }
  .modal input[type="text"] {
    width: 100%; padding: 16px; font-size: 22px; text-align: center;
    border: 2px solid #ddd; border-radius: 10px; margin-bottom: 16px;
    font-family: "Courier New", monospace;
  }
  .modal math-field {
    display: block; width: 100%; box-sizing: border-box;
    margin-bottom: 16px; border: 2px solid #ddd; border-radius: 10px;
    padding: 14px 16px;
    --caret-color: #222;
    --contains-highlight-background-color: transparent;
    font-size: 22px;
  }
  .modal math-field:focus-within { border-color: #999; }
  .answer-hint { font-size: 12px; color: #999; margin-bottom: 6px; }
  .modal input.shake, .modal math-field.shake { animation: shake .3s; border-color: #e53935; }
  @keyframes shake {
    25% { transform: translateX(-6px); } 75% { transform: translateX(6px); }
  }
  .modal-buttons { display: flex; gap: 10px; }
  .modal-buttons button {
    flex: 1; padding: 14px; border: none; border-radius: 10px; font-size: 16px; cursor: pointer;
  }
  .btn-check { background: #222; color: #fff; }
  .btn-close { background: #eee; color: #333; }
  .feedback { min-height: 22px; font-size: 15px; margin-bottom: 12px; font-weight: 600; }
  .feedback.ok { color: #2e7d32; }
  .feedback.bad { color: #c62828; }

  /* formula reference panel */
  .formula-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    display: flex; align-items: center; justify-content: center;
    padding: 0; z-index: 200;
  }
  .formula-modal {
    background: #fff; border-radius: 0; width: 100%; height: 100%;
    max-width: none; display: flex; flex-direction: column; position: relative;
  }
  .formula-header {
    display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
    padding: 14px 16px 10px; border-bottom: 1px solid #eee; flex-shrink: 0;
  }
  .formula-header h2 { margin: 0; font-size: 17px; flex-shrink: 0; }
  .formula-search {
    flex: 1 1 100%; padding: 10px 12px; border: 1px solid #ccc; border-radius: 8px; font-size: 16px;
    order: 3;
  }
  .formula-close { order: 2; margin-left: auto; }
  .formula-close {
    width: 32px; height: 32px; border-radius: 50%; border: none;
    background: #f0f0f0; color: #666; font-size: 16px; line-height: 1;
    cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  }
  .formula-close:hover { background: #e2e2e2; }
  .formula-list { overflow-y: auto; padding: 8px 16px 24px; flex: 1; -webkit-overflow-scrolling: touch; }

  /* --- Desktop / wide-viewport modal refinements --- */
  @media (min-width: 640px) {
    .overlay { padding: 20px; }
    .modal {
      border-radius: 16px; width: 100%; max-width: 640px;
      height: auto; max-height: 90vh; min-height: 0; padding: 48px 44px;
      box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    }
    .modal .problem { font-size: 42px; }
    .modal math-field { font-size: 28px; padding: 20px; }
    .modal-buttons { padding-top: 0; }
    .modal { padding-top: 48px; }

    .formula-overlay { padding: 24px; }
    .formula-modal {
      border-radius: 16px; width: 100%; max-width: 720px;
      height: 85vh; max-height: 800px;
      box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    }
    .formula-header { flex-wrap: nowrap; padding: 18px 20px 12px; }
    .formula-header h2 { font-size: 18px; }
    .formula-search { flex: 1 1 auto; order: 0; font-size: 14px; }
    .formula-close { order: 0; margin-left: 0; }
    .formula-list { padding: 8px 20px 24px; }
  }
  .formula-card { padding: 14px 0; border-bottom: 1px solid #f0f0f0; }
  .formula-card:last-child { border-bottom: none; }
  .formula-card .fc-name {
    font-size: 12px; font-weight: 700; text-transform: uppercase;
    letter-spacing: .05em; margin-bottom: 4px;
  }
  .formula-card .fc-note { font-size: 13px; color: #888; margin-top: 4px; }
  .formula-card .fc-latex { font-size: 18px; overflow-x: auto; padding: 2px 0; }
  .formula-empty { text-align: center; color: #999; padding: 40px 0; font-size: 14px; }
