    :root {
      --bg: #f9f8f5;
      --surface: #ffffff;
      --surface-2: #f4f3ee;
      --line: #f4f3ee;
      --text: #2c2c34;
      --muted: #8c8c96;
      --accent: #5b6abf;
      --accent-text: #2c2c34;
      --ai-bubble: #ffffff;
      --ai-text: #2c2c34;
      --user-bubble: #f4f3ee;
      --user-text: #2c2c34;
      --warn: #f4b53f;
      --error: #ff7f7f;
      --shadow: 0 12px 30px rgba(0, 0, 0, 0.06);
      --motion: cubic-bezier(0.2, 0.8, 0.2, 1);
      /* Gamification colors */
      --duo-green: #58cc02;
      --duo-gold: #ffc800;
      --duo-blue: #1cb0f6;
      --elo-purple: #ce82ff;
    }

    * { box-sizing: border-box; }

    html, body {
      margin: 0;
      min-height: 100%;
      background:
        radial-gradient(1200px 800px at 20% -200px, #e5e2d9 0%, #f9f8f5 42%, #f9f8f5 100%);
      color: var(--text);
      font-family: "Inter", "Segoe UI", sans-serif;
    }

    .shell {
      width: 100vw;
      margin: 0 auto;
      min-height: 100vh;
      display: grid;
      grid-template-rows: auto 1fr auto auto;
      background: #ffffff;
      border-left: none;
      border-right: none;
      position: relative;
      overflow: hidden;
    }

    /* ── WhatsApp-style chat topbar ── */
    .topbar {
      position: sticky;
      top: 0;
      z-index: 12;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px 10px;
      background: #f4f3ee;
      border-bottom: 1px solid #f4f3ee;
      backdrop-filter: blur(8px);
    }

    .topbar-left {
      display: flex;
      align-items: center;
      gap: 10px;
    }

    .topbar-avatar {
      width: 38px;
      height: 38px;
      border-radius: 50%;
      background: linear-gradient(135deg, #5b6abf, #4a58a8);
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: "Instrument Serif", sans-serif;
      
      font-size: 12px;
      color: #ffffff;
      flex-shrink: 0;
      letter-spacing: 0.02em;
    }

    .topbar-info {
      display: flex;
      flex-direction: column;
      gap: 1px;
    }

    .topbar-name {
      font-family: "Instrument Serif", "Inter", sans-serif;
      font-size: 15px;
      
      color: #2c2c34;
      line-height: 1.2;
    }

    .topbar-status {
      font-size: 11.5px;
      color: var(--accent);
      
      display: flex;
      align-items: center;
      gap: 4px;
      line-height: 1;
    }

    .topbar-dot {
      width: 6px;
      height: 6px;
      border-radius: 50%;
      background: var(--accent);
      flex-shrink: 0;
      animation: status-pulse 2.4s ease-in-out infinite;
    }

    .topbar-right {
      display: flex;
      align-items: center;
      gap: 4px;
    }

    .icon-btn {
      border: 1px solid transparent;
      border-radius: 10px;
      background: transparent;
      color: #555560;
      
      min-width: 40px;
      min-height: 40px;
      padding: 8px 10px;
      cursor: pointer;
      font-size: 15px;
      transition: background 0.12s, color 0.12s;
    }

    .icon-btn:hover {
      background: rgba(0,0,0,0.03);
      color: #2c2c34;
    }

    /* legacy .brand hidden, topbar-name replaces it */
    .brand { display: none; }

    @keyframes status-pulse {
      0%, 100% { opacity: 1; }
      50% { opacity: 0.35; }
    }

    .stream {
      overflow: auto;
      padding: 10px 8px 8px;
      display: grid;
      gap: 10px;
      align-content: start;
      background-image:
        radial-gradient(rgba(91, 106, 191, 0.03) 1px, transparent 1px);
      background-size: 20px 20px;
    }

    /* Keep scrolled-to messages clear of the sticky topbar. */
    .msg { scroll-margin-top: 68px; }

    .empty {
      max-width: 560px;
      margin: 24px auto 0;
      border: 1px solid #f4f3ee;
      border-radius: 18px;
      background: #ffffff;
      padding: 20px 18px;
      display: grid;
      gap: 14px;
      box-shadow: 0 8px 30px rgba(0,0,0,0.06);
      animation: rise 0.3s var(--motion) both;
      text-align: center;
    }

    .empty-icon {
      font-size: 36px;
      line-height: 1;
    }

    .empty h2 {
      margin: 0;
      font-family: "Instrument Serif", "Inter", sans-serif;
      font-size: 18px;
      
      letter-spacing: -0.01em;
      color: #2c2c34;
    }

    .empty p {
      margin: 0;
      color: #555560;
      font-size: 13px;
      line-height: 1.5;
    }

    .examples {
      display: flex;
      flex-wrap: wrap;
      gap: 7px;
      justify-content: center;
    }

    .chip {
      border: 1px solid #e5e2d9;
      background: #f4f3ee;
      color: #2c2c34;
      border-radius: 999px;
      padding: 8px 14px;
      font-size: 12.5px;
      
      cursor: pointer;
      text-align: left;
      transition: border-color 0.12s, background 0.12s, color 0.12s;
    }

    .chip:hover {
      border-color: var(--accent);
      background: rgba(91,106,191,.08);
      color: #2c2c34;
    }

    .msg {
      max-width: 92%;
      border-radius: 14px;
      padding: 10px 12px;
      display: grid;
      gap: 8px;
      box-shadow: 0 8px 20px rgba(5, 12, 16, 0.2);
      border: 1px solid rgba(22, 42, 52, 0.22);
      animation: rise 0.2s var(--motion) both;
    }

    .msg.user {
      margin-left: auto;
      background: var(--user-bubble);
      color: var(--user-text);
      border-color: #2c2c34;
      position: relative;
    }

    /* WhatsApp user bubble tail */
    .msg.user::after {
      content: '';
      position: absolute;
      bottom: 0;
      right: -8px;
      border: 8px solid transparent;
      border-bottom-color: var(--user-bubble);
      border-right: 0;
      border-top: 0;
    }

    .msg.assistant {
      margin-right: auto;
      background: var(--ai-bubble);
      color: var(--ai-text);
      border-color: #8c8c96;
      position: relative;
    }

    /* WhatsApp AI bubble tail */
    .msg.assistant::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: -8px;
      border: 8px solid transparent;
      border-bottom-color: var(--ai-bubble);
      border-left: 0;
      border-top: 0;
    }

    .msg.system {
      margin-right: auto;
      background: #f4f3ee;
      color: #6a4700;
      border-color: #f2d89d;
    }

    .msg-head {
      font-size: 11px;
      
      opacity: 0.72;
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 8px;
    }

    .msg-body {
      font-size: 14px;
      line-height: 1.45;
      white-space: pre-wrap;
      word-break: break-word;
      display: grid;
      gap: 8px;
    }

    .image-thumb {
      width: min(320px, 100%);
      border-radius: 10px;
      overflow: hidden;
      border: 1px solid rgba(30, 49, 59, 0.22);
      background: #fff;
    }

    .image-thumb img {
      display: block;
      width: 100%;
      height: auto;
    }

    .expr {
      border: 1px solid #e5e2d9;
      border-radius: 9px;
      background: #f8fbfd;
      padding: 8px;
      overflow-x: auto;
      white-space: nowrap;
      font-size: 13px;
    }

    .steps {
      display: grid;
      gap: 8px;
    }

    .step {
      border: 1px solid #e5e2d9;
      border-radius: 10px;
      background: #f5fafc;
      padding: 8px;
      display: grid;
      gap: 5px;
    }

    .msg-actions {
      display: flex;
      flex-wrap: wrap;
      gap: 6px;
    }

    .mini-btn {
      border: 1px solid #8c8c96;
      border-radius: 10px;
      background: #ffffff;
      color: #8c8c96;
      text-decoration: none;
      font-size: 12px;
      
      padding: 6px 10px;
      cursor: pointer;
    }

    .mini-btn:hover {
      border-color: #555560;
      background: #f7fcff;
    }

    .typing {
      width: fit-content;
      display: inline-flex;
      align-items: center;
      gap: 4px;
      border-radius: 999px;
      border: 1px solid #2c2c34;
      background: #f2f7fa;
      color: #c0bfba;
      padding: 7px 10px;
      font-size: 12px;
      
    }

    .typing span {
      width: 6px;
      height: 6px;
      border-radius: 999px;
      background: #2c2c34;
      animation: blink 1s infinite;
    }

    .typing span:nth-child(2) { animation-delay: 0.14s; }
    .typing span:nth-child(3) { animation-delay: 0.28s; }

    /* WhatsApp input area */
    .composer {
      border-top: 1px solid #f4f3ee;
      background: #f4f3ee;
      backdrop-filter: blur(8px);
      padding: 8px;
      padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
      display: grid;
      gap: 8px;
    }

    .preview {
      border: 1px dashed #c0bfba;
      border-radius: 12px;
      background: #f4f3ee;
      padding: 8px;
      display: none;
      grid-template-columns: auto 1fr auto;
      gap: 8px;
      align-items: center;
    }

    .preview.active { display: grid; }

    .preview img {
      width: 52px;
      height: 52px;
      border-radius: 9px;
      object-fit: cover;
      border: 1px solid #c0bfba;
      background: #ffffff;
    }

    .preview-meta {
      color: #2c2c34;
      font-size: 12px;
      line-height: 1.3;
    }

    .row {
      display: grid;
      grid-template-columns: auto auto 1fr auto;
      gap: 8px;
      align-items: end;
    }

    .input {
      width: 100%;
      min-height: 44px;
      max-height: 150px;
      resize: vertical;
      border: 1px solid #c0bfba;
      border-radius: 20px;
      background: #ffffff;
      color: #2c2c34;
      padding: 10px 12px;
      font-family: "STIX Two Math", "Cambria Math", "Instrument Serif", serif;
      font-size: 15.5px;
      line-height: 1.4;
      outline: none;
    }

    .input:focus {
      border-color: #5b6abf;
      box-shadow: 0 0 0 3px rgba(91, 106, 191, 0.15);
    }

    /* WhatsApp circular send button */
    .send {
      border: none;
      background: var(--accent);
      color: var(--accent-text);
      border-radius: 50%;
      width: 44px;
      height: 44px;
      min-width: 44px;
      min-height: 44px;
      padding: 0;
      font-size: 16px;
      
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0 2px 10px rgba(91, 106, 191, 0.3);
      transition: background 0.12s, transform 0.1s, box-shadow 0.12s;
    }

    .send:hover {
      background: #4a58a8;
      transform: scale(1.06);
      box-shadow: 0 4px 16px rgba(91, 106, 191, 0.4);
    }

    /* ── Math keyboard, "typeset drawer" ─────────────────────────
       Keys read like typeset math (Instrument Serif glyphs on paper
       tiles), not calculator plastic. Slides up under the composer. */
    .mathkb {
      display: none;
      border: 1px solid #e5e2d9;
      border-radius: 14px;
      background: #fbfaf7;
      padding: 8px 8px 10px;
      animation: mathkb-rise 0.18s ease-out;
    }

    .mathkb.open { display: block; }

    @keyframes mathkb-rise {
      from { opacity: 0; transform: translateY(8px); }
      to { opacity: 1; transform: translateY(0); }
    }

    @media (prefers-reduced-motion: reduce) {
      .mathkb { animation: none; }
    }

    .mathkb-tabs {
      display: flex;
      gap: 4px;
      margin-bottom: 8px;
      border-bottom: 1px solid #eeece4;
    }

    .mathkb-tab {
      flex: 1;
      min-height: 44px;
      border: none;
      background: transparent;
      color: #8c8c96;
      font-family: "STIX Two Math", "Cambria Math", "Instrument Serif", serif;
      font-size: 18px;
      cursor: pointer;
      border-bottom: 2px solid transparent;
      transition: color 0.12s, border-color 0.12s;
    }

    .mathkb-tab:hover { color: #2c2c34; }

    .mathkb-tab.active {
      color: #2c2c34;
      border-bottom-color: #d4a24e;
    }

    .mathkb-keys {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 6px;
    }

    .mathkb-key {
      min-height: 44px;
      border: 1px solid #e5e2d9;
      border-radius: 10px;
      background: #ffffff;
      color: #2c2c34;
      font-family: "STIX Two Math", "Cambria Math", "Instrument Serif", serif;
      font-size: 19px;
      line-height: 1;
      cursor: pointer;
      transition: background 0.1s, transform 0.08s;
    }

    .mathkb-key:hover { background: #f4f3ee; }

    .mathkb-key:active {
      background: #efece3;
      transform: scale(0.94);
    }

    .mathkb-key.fn {
      font-family: "Inter", "Segoe UI", sans-serif;
      font-size: 13.5px;
      color: #555560;
    }

    .mathkb-key.act {
      background: #f4f3ee;
      color: #5b6abf;
    }

    @media (min-width: 761px) {
      .mathkb-keys { grid-template-columns: repeat(10, 1fr); }
    }

    .status {
      min-height: 18px;
      font-size: 12px;
      color: #8c8c96;
      padding: 0 4px;
    }

    .status.warn { color: var(--warn); }
    .status.error { color: var(--error); }

    /* WhatsApp-style bottom nav */
    .bottom-nav {
      border-top: 1px solid #f4f3ee;
      background: #f4f3ee;
      display: grid;
      grid-template-columns: repeat(5, minmax(0, 1fr));
      gap: 0;
      padding: 0;
      padding-bottom: env(safe-area-inset-bottom, 0px);
      position: sticky;
      bottom: 0;
      z-index: 10;
    }

    .bottom-nav a {
      text-decoration: none;
      color: #2c2c34;
      border: none;
      border-top: 2px solid transparent;
      border-right: 1px solid #f4f3ee;
      border-radius: 0;
      text-align: center;
      padding: 10px 8px;
      font-size: 12px;
      
      background: transparent;
      transition: color 0.12s, background 0.12s;
    }

    .bottom-nav a:last-child { border-right: none; }

    .bottom-nav a:hover { color: #2c2c34; background: rgba(0,0,0,.03); }

    .bottom-nav a.active {
      color: var(--accent);
      border-top-color: var(--accent);
      background: rgba(91,106,191,.05);
    }

    .overlay {
      position: fixed;
      inset: 0;
      background: rgba(0,0,0,0.06);
      z-index: 30;
      display: none;
    }

    body.settings-open .overlay {
      display: block;
    }

    .settings {
      position: fixed;
      right: 12px;
      top: 12px;
      width: min(440px, calc(100vw - 24px));
      border: 1px solid #e5e2d9;
      border-radius: 16px;
      background: #ffffff;
      box-shadow: var(--shadow);
      z-index: 40;
      padding: 14px;
      display: none;
      gap: 12px;
      animation: rise 0.2s var(--motion) both;
    }

    body.settings-open .settings {
      display: grid;
    }

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

    .setting {
      display: grid;
      gap: 6px;
    }

    .setting label {
      font-size: 12px;
      color: #8c8c96;
      
    }

    .setting select,
    .setting input[type="checkbox"] {
      font: inherit;
    }

    .setting select {
      border: 1px solid #c0bfba;
      border-radius: 10px;
      background: #f4f3ee;
      color: #2c2c34;
      padding: 10px;
      font-size: 13px;
      
    }

    .check-row {
      display: flex;
      align-items: center;
      gap: 8px;
      color: #2c2c34;
      font-size: 13px;
      
      border: 1px solid #c0bfba;
      border-radius: 10px;
      background: #f4f3ee;
      padding: 10px;
    }

    .check-row input {
      width: 16px;
      height: 16px;
      accent-color: var(--accent);
    }

    .settings-actions {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
    }

    .btn {
      border: 1px solid #c0bfba;
      border-radius: 10px;
      background: #f4f3ee;
      color: #2c2c34;
      padding: 9px 12px;
      font-size: 13px;
      
      text-decoration: none;
      cursor: pointer;
    }

    .btn:hover {
      border-color: #2c2c34;
      background: #e5e2d9;
    }

    .btn.primary {
      border-color: var(--accent);
      background: var(--accent);
      color: var(--accent-text);
      
    }

    .plan-note {
      margin: 0;
      padding: 10px;
      border-radius: 10px;
      border: 1px solid #c0bfba;
      background: #f4f3ee;
      color: #2c2c34;
      font-size: 12px;
      line-height: 1.4;
    }

    .hidden-file {
      display: none !important;
    }

    @keyframes rise {
      from {
        opacity: 0;
        transform: translateY(4px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    @keyframes blink {
      0%, 80%, 100% { opacity: 0.3; }
      40% { opacity: 1; }
    }

    @media (prefers-reduced-motion: reduce) {
      .msg,
      .empty,
      .settings {
        animation: none !important;
      }
    }

    /* Gamification & Daily Puzzle */
    .gamified-status { display: flex; gap: 8px; font-weight: 500; font-family: "Inter", sans-serif; font-size: 13px; margin-top: 2px; }
    .streak { color: #a85f00; background: #fff0d4; padding: 2px 6px; border-radius: 6px; border: 1px solid #ffd18a; }
    .elo { color: #7a3fd1; background: #f6e5ff; padding: 2px 6px; border-radius: 6px; border: 1px solid #eabfff; }
    .pro-badge { background: var(--duo-gold); color: #5c4a00; font-size: 10px; font-weight: 800; padding: 2px 4px; border-radius: 4px; vertical-align: top; margin-left: 4px; letter-spacing: 0.05em; text-transform: uppercase; }

    .daily-puzzle-header { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-bottom: 12px; }
    .daily-icon { font-size: 42px; line-height: 1; animation: pulse 2s infinite; }
    .daily-puzzle-card { background: linear-gradient(145deg, #ffffff, #f9f8f5); border: 2px solid var(--duo-green); border-radius: 16px; padding: 20px; box-shadow: 0 8px 0 rgba(88, 204, 2, 0.2); transition: transform 0.15s, box-shadow 0.15s; margin-bottom: 24px; }
    .daily-puzzle-card:hover { transform: translateY(-2px); box-shadow: 0 10px 0 rgba(88, 204, 2, 0.25); }
    .daily-puzzle-question { font-family: "STIX Two Math", serif; font-size: 24px; color: #2c2c34; margin: 16px 0; font-weight: bold; }
    .daily-stats { font-size: 12px; color: var(--muted); margin-bottom: 16px; font-weight: 500; }
    .btn-pulse { background: var(--duo-green) !important; border-color: var(--duo-green) !important; color: white !important; font-weight: bold; font-size: 16px; padding: 12px 24px; border-radius: 12px; box-shadow: 0 4px 0 #58a700 !important; transition: all 0.1s; cursor: pointer; }
    .btn-pulse:active { transform: translateY(4px); box-shadow: 0 0 0 #58a700 !important; }
    .divider { border: 0; height: 1px; background: #e5e2d9; margin: 24px 0; }
    .collab-examples .chip { font-weight: bold; font-size: 13.5px; border-width: 2px; }
    .coop-chip { border-color: var(--duo-blue); color: var(--duo-blue); background: #e5f7ff; }
    .coop-chip:hover { background: var(--duo-blue); color: #fff; }
    .battle-chip { border-color: #ff4b4b; color: #ff4b4b; background: #ffe5e5; }
    .battle-chip:hover { background: #ff4b4b; color: #fff; }
    .helper-text { font-size: 12px; color: var(--muted); margin-top: 16px; font-style: italic; }

    @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }

    /* Desktop (761px+) */
    @media (min-width: 761px) {
      .shell {
        width: min(860px, 100vw);
        border-left: 1px solid #e5e2d9;
        border-right: 1px solid #e5e2d9;
      }

      .msg {
        max-width: min(84%, 740px);
      }

      .topbar {
        padding: 10px 14px;
      }

      .stream {
        padding: 14px 12px 10px;
      }

      .composer {
        padding: 10px 10px 8px;
      }
    }
  
