  :root {
    --cream: #F7F3EC;
    --surface: #EDE8E0;
    --sand: #D3C9B8;
    --ink: #2C2C2A;
    --ink-muted: #888780;
    --ink-faint: #C4C1BA;
    --blush: #F5C4BC;     --blush-s: #D99A90;
    --mist: #A8CCF0;      --mist-s: #7AACD8;
    --lavender: #CDB8F0;  --lavender-s: #A88AD8;
    --sage: #A8DFC0;      --sage-s: #72BB96;
    --surf-s: #C8C2B8;
    --ink-s: #1A1A18;
    --logo-blue: #0008e8;
    --mono: 'Geist Mono', monospace;
    --sans: 'IBM Plex Sans', sans-serif;
    --mono-ls: 0.1em;
    /* result card gradient — periwinkle top → blush bottom */
    --grad-top: #C0C4DC;
    --grad-mid: #D4C8D8;
    --grad-bot: #E8D4CE;
  }

  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
  html { width: 100%; height: 100dvh; background: var(--cream); overflow: hidden; -webkit-font-smoothing: antialiased; }
  body { width: 100%; height: 100%; background: var(--cream); overflow: hidden; -webkit-font-smoothing: antialiased; }

  .app {
    width: 100%; height: 100%;
    display: flex; flex-direction: column;
    padding-top: env(safe-area-inset-top);
    background: var(--cream); overflow: hidden;
    transition: background 0.4s ease;
  }
  .app.res-active { background: transparent; }

  /* ── desktop breakpoint ── */
  /* above tablet width: freeze app at 430px, centre it, fill rest with cream */
  @media (min-width: 600px) {
    html, body {
      overflow: auto;
      display: flex;
      justify-content: center;
      background: var(--cream);
    }
    .app {
      width: 430px;
      min-width: 430px;
      max-width: 430px;
      height: 100vh;
      min-height: 100vh;
      flex-shrink: 0;
    }
  }

  /* ── nav ── */
  .nav { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px 0; flex-shrink: 0; flex-wrap: wrap; row-gap: 10px; }
  .wordmark { display: flex; align-items: center; gap: 10px; }
  .wordmark-icon { width: 40px; height: 40px; flex-shrink: 0; }
  .wordmark-text { display: flex; flex-direction: column; gap: 2px; }
  .wordmark-name { font-family: var(--mono); font-size: 15px; font-weight: 500; color: var(--ink); letter-spacing: var(--mono-ls); line-height: 1; }
  .wordmark-name span { color: var(--ink-muted); }
  .wordmark-sub { display: none; }
  .nav-pills { display: flex; border: 1.5px solid var(--ink); border-radius: 99px; overflow: hidden; }
  .pill { height: 28px; padding: 0 14px; border-radius: 0; border: none; background: transparent; font-family: var(--mono); font-size: 10px; font-weight: 500; color: var(--ink); cursor: pointer; transition: background 0.12s, color 0.12s; letter-spacing: var(--mono-ls); }
  .pill.on { background: var(--ink); color: var(--cream); }

  /* ── screens ── */
  .screen { display: none; flex-direction: column; flex: 1; min-height: 0; }
  .screen.active { display: flex; animation: fade-up 0.2s ease; }
  @keyframes fade-up { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: translateY(0); } }

  /* calc screen: display fills space above grid and centres its content */
  #sc-calc { justify-content: flex-start; }

  /* ── display panel ── */
  .display {
    flex: 1;
    padding: 0 16px;
    display: flex; flex-direction: column; justify-content: center;
  }
  .prompt { font-family: var(--mono); font-size: 10px; color: var(--ink-muted); letter-spacing: var(--mono-ls); margin-bottom: 6px; }
  .prompt.mod-active { color: var(--logo-blue); }
  .bc { font-family: var(--mono); font-size: 16px; font-weight: 500; color: var(--ink); line-height: 1.4; word-break: break-word; letter-spacing: var(--mono-ls); }
  .bc .crumb { color: var(--ink); }

  /* ── key grid ── */
  .grid {
    padding: 10px 14px calc(env(safe-area-inset-bottom) + 14px);
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 8px;
    flex-shrink: 0;
  }
  .key {
    border-radius: 13px; border: none;
    font-family: var(--mono); font-size: 10px; font-weight: 500; color: var(--ink);
    cursor: pointer; display: flex; align-items: center; justify-content: center;
    text-align: center; padding: 4px 6px; line-height: 1.3;
    white-space: normal; overflow-wrap: normal; word-break: normal;
    width: 100%; aspect-ratio: 1 / 1;
    overflow: hidden;
    letter-spacing: var(--mono-ls);
    box-shadow: 0 3px 0 0 var(--ks, var(--surf-s));
    transition: transform .07s ease, box-shadow .07s ease;
    -webkit-user-select: none; user-select: none;
  }
  .key:active:not(.kd) { transform: translateY(2px); box-shadow: 0 1px 0 0 var(--ks, var(--surf-s)); }

  /* one colour per row for relative keys */
  .kr1 { background: var(--blush);    --ks: var(--blush-s); }
  .kr2 { background: var(--sage);     --ks: var(--sage-s); }
  .kr3 { background: var(--lavender); --ks: var(--lavender-s); }
  .kr4 { background: var(--mist);     --ks: var(--mist-s); }

  /* op keys */
  .ki  { background: var(--ink); color: var(--cream); font-size: 20px; --ks: var(--ink-s); }
  .kg  { background: var(--mist); border: none; color: var(--ink); --ks: var(--mist-s); }
  .kg:active:not(.kd) { transform: translateY(2px); box-shadow: 0 1px 0 0 var(--mist-s); }
  .kmod { background: var(--mist); color: var(--ink); --ks: var(--mist-s); }

  .kd { opacity: .26; pointer-events: none; }
  .ksel { outline: 2px solid var(--ink); outline-offset: -2px; }
  @keyframes mod-suggest {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
  }
  .key.kmod-suggest { animation: mod-suggest 1.1s ease-in-out infinite; transform-origin: center; }

  @keyframes eq-pulse {
    0%, 100% { box-shadow: 0 3px 0 0 var(--ink-s); }
    50% { box-shadow: 0 3px 0 0 var(--ink-s), 0 0 0 6px rgba(44,44,42,0.13); }
  }
  .key.ki.ki-active { animation: eq-pulse 2.5s ease-in-out infinite; }

  /* ── result screen ── */
  #sc-res {
    position: relative;
    justify-content: flex-end;
    background: transparent;
  }

  /* grain overlay — applied to all gradient surfaces */
  .grain { position: relative; overflow: hidden; }
  .grain::after {
    content: '';
    position: absolute;
    inset: -50%;
    opacity: 0.50;
    mix-blend-mode: overlay;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-size: 180px 180px;
    pointer-events: none;
    border-radius: inherit;
    z-index: 1;
  }

  /* full-screen radial gradient background — covers everything incl. nav */
  .res-bg {
    position: fixed; inset: 0;
    transition: background 0.4s ease;
    z-index: 0;
    overflow: hidden;
  }
  /* nav sits above res-bg */
  .nav { position: relative; z-index: 2; }

  /* half-card anchored to bottom */
  .res-card {
    position: relative; z-index: 1;
    width: 100%;
    background: var(--cream);
    border-radius: 24px 24px 0 0;
    padding: 30px 26px calc(env(safe-area-inset-bottom) + 26px);
    display: flex; flex-direction: column; gap: 28px;
  }

  /* top meta row: app label + chain + colour dots */
  .res-meta { display: flex; flex-direction: column; gap: 4px; }
  .res-app-label { font-family: var(--mono); font-size: 10px; color: var(--ink-muted); letter-spacing: var(--mono-ls); }
  .res-chain-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
  .res-chain { font-family: var(--mono); font-size: 10px; color: var(--ink); letter-spacing: var(--mono-ls); line-height: 1.5; flex: 1; }
  .res-dots { display: flex; gap: 4px; flex-shrink: 0; }
  .res-dot { width: 13px; height: 13px; border-radius: 50%; }

  /* result text */
  .res-result { display: flex; flex-direction: column; gap: 8px; }
  .res-hanzi {
    font-family: var(--sans); font-size: 80px; font-weight: 500; color: var(--ink);
    line-height: 1; letter-spacing: 0.11em;
    display: flex; flex-wrap: wrap; gap: 0.15em; align-items: baseline;
  }
  .res-hanzi-part { white-space: nowrap; }
  .res-hanzi-sep { white-space: nowrap; opacity: 0.3; font-size: 0.5em; align-self: center; }
  .res-pinyin { font-family: var(--mono); font-size: 24px; color: var(--ink); letter-spacing: var(--mono-ls); }
  .res-english { font-family: var(--mono); font-size: 12px; color: var(--ink-muted); letter-spacing: var(--mono-ls); }

  /* play button — full width pill with gradient */
  .play-btn {
    width: 100%; height: 46px; border-radius: 10px; border: none; cursor: pointer;
    display: flex; align-items: center; justify-content: center; gap: 10px;
    font-family: var(--mono); font-size: 10px; font-weight: 500; color: var(--ink);
    letter-spacing: var(--mono-ls);
    transition: opacity .1s;
    position: relative; overflow: hidden;
  }
  .play-btn .play-icon,
  .play-btn > * { position: relative; z-index: 2; }
  .play-btn:active { opacity: 0.75; }
  .play-icon { width: 0; height: 0; border-top: 5px solid transparent; border-bottom: 5px solid transparent; border-left: 9px solid var(--ink); }

  /* 2x2 action grid */
  .res-actions {
    display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr;
    gap: 10px;
  }
  .res-btn {
    height: 46px; border-radius: 10px; cursor: pointer;
    font-family: var(--mono); font-size: 10px; font-weight: 500;
    letter-spacing: var(--mono-ls);
    display: flex; align-items: center; justify-content: center;
    transition: opacity .1s;
  }
  .res-btn:active { opacity: 0.7; }
  .res-btn-surf { background: var(--surface); border: none; color: var(--ink); box-shadow: 0 3px 0 0 var(--surf-s); }
  .res-btn-outline { background: transparent; border: 1.5px solid var(--sand); color: var(--ink); box-shadow: none; }
  .res-btn-ink { background: var(--ink); border: none; color: var(--cream); box-shadow: none; }

  /* ── toast ── */
  .toast { position: absolute; bottom: calc(env(safe-area-inset-bottom) + 28px); left: 50%; transform: translateX(-50%) translateY(6px); background: var(--ink); color: var(--cream); font-family: var(--mono); font-size: 11px; font-weight: 500; letter-spacing: var(--mono-ls); padding: 8px 18px; border-radius: 99px; opacity: 0; pointer-events: none; transition: opacity .15s, transform .15s; white-space: nowrap; z-index: 99; }
  .toast.on { opacity: 1; transform: translateX(-50%) translateY(0); }

  /* ── reverse screen ── */
  #sc-rev { position: relative; overflow: hidden; }
  .rev-wrap { flex: 1; display: flex; flex-direction: column; padding-top: 189px; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }

  /* search screen labels */
  .rev-field-label {
    font-family: var(--mono); font-size: 10px; font-weight: 500; color: var(--ink);
    letter-spacing: var(--mono-ls);
    padding: 0 16px; margin-bottom: 10px;
  }

  /* search row */
  .rev-row { display: flex; gap: 8px; padding: 0 16px; margin-bottom: 16px; flex-shrink: 0; }
  .rev-input-wrap { position: relative; flex: 1; display: flex; align-items: center; }
  .rev-clear {
    position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
    display: none; align-items: center; justify-content: center;
    width: 20px; height: 20px; border-radius: 50%; border: none;
    background: var(--ink-faint); color: var(--cream);
    font-size: 14px; line-height: 1; cursor: pointer; padding: 0;
  }
  .rev-clear:active { opacity: 0.7; }
  .rev-input {
    flex: 1; height: 46px; border-radius: 8px;
    border: 1.5px solid var(--ink); background: var(--cream);
    font-family: var(--mono); font-size: 13px; color: var(--ink);
    padding: 0 38px 0 14px; outline: none; transition: border-color .15s;
    letter-spacing: var(--mono-ls); width: 100%;
  }
  .rev-input:focus { border-color: var(--ink); }
  .rev-input::placeholder { color: var(--ink-faint); font-size: 11px; }
  .rev-go {
    width: 46px; height: 46px; border-radius: 8px; border: none;
    background: var(--ink); color: var(--cream); font-size: 18px;
    cursor: pointer; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .1s;
  }
  .rev-go:active { opacity: 0.7; }

  /* chips — 6-column grid fills full width, square keys */
  .chips { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; flex-shrink: 0; padding: 0 16px; }
  .chip {
    aspect-ratio: 1; padding: 0; border-radius: 10px; border: none;
    background: var(--surface); font-family: var(--sans); font-size: 14px;
    color: var(--ink); cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .1s;
  }
  .chip:active { opacity: 0.7; }

  /* results grid */
  .rev-grid {
    display: grid; grid-template-columns: 1fr 1fr;
    grid-auto-rows: auto;
    gap: 10px; padding: 12px 16px calc(env(safe-area-inset-bottom) + 20px); align-content: start;
  }

  /* card */
  .rev-card {
    border-radius: 14px; cursor: pointer;
    display: flex; flex-direction: column;
    background: var(--cream);
    box-shadow: 0 2px 0 0 var(--surf-s);
    transition: transform .1s ease, box-shadow .1s ease;
    overflow: hidden;
  }
  .rev-card:active { transform: translateY(1px); box-shadow: 0 1px 0 0 var(--surf-s); }

  /* gradient top — fixed height, never clips body */
  .rev-card-grad {
    width: 100%; height: 90px;
    flex: 0 0 90px;
    position: relative; overflow: hidden;
  }

  /* cream body — padding matches Figma scaled down */
  .rev-card-body {
    background: var(--cream);
    padding: 10px 11px 14px;
    display: flex; flex-direction: column;
    gap: 4px; flex: 1;
  }

  /* hanzi */
  .rev-card-hz {
    font-family: var(--sans); font-size: 26px; font-weight: 400;
    color: var(--ink); line-height: 1; letter-spacing: 0.11em;
    white-space: nowrap;
  }

  /* pinyin */
  .rev-card-py {
    font-family: var(--mono); font-size: 11px; color: var(--ink);
    letter-spacing: 0.11em; line-height: 1.3;
  }

  /* english description */
  .rev-card-en {
    font-family: var(--mono); font-size: 9px; color: #808080;
    letter-spacing: 0.11em; line-height: 1.5;
    overflow-wrap: break-word; word-break: break-word;
    padding-bottom: 2px;
  }

  /* empty state */
  .rev-empty { font-family: var(--mono); font-size: 11px; color: var(--logo-blue); text-align: center; margin-top: 20px; line-height: 1.9; letter-spacing: var(--mono-ls); grid-column: 1 / -1; }
