:root {
    --bg: #f6f5f2; --panel: #ffffff; --ink: #1c1b19; --muted: #6f6c66; --line: #e4e1db;
    --me: #2f5e9e; --me-ink: #ffffff; --her: #ffffff; --accent: #2f5e9e; --danger: #b3261e;
    --code: #efede8; --radius: 18px;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #161616; --panel: #1f1f1f; --ink: #ecebe8; --muted: #9a978f; --line: #2c2c2c;
      --me: #3b6fb6; --me-ink: #ffffff; --her: #242424; --accent: #7fa7e0; --danger: #ff7b72; --code: #2a2a2a;
    }
  }
  * { box-sizing: border-box; }
  html, body { height: 100%; margin: 0; }
  body {
    background: var(--bg); color: var(--ink);
    font: 16px/1.45 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
    display: flex; flex-direction: column; height: 100dvh; overscroll-behavior: none;
  }
  button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
  button:disabled { opacity: .45; cursor: default; }
  .icon { width: 44px; height: 44px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center; font-size: 22px; }
  .icon:active { background: var(--line); }

  header {
    display: flex; align-items: center; gap: 4px; padding: 6px 8px; padding-top: calc(6px + env(safe-area-inset-top));
    border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: 0; z-index: 2;
  }
  header h1 { flex: 1; margin: 0; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }

  main { flex: 1; overflow-y: auto; padding: 12px 12px 8px; }
  .turn { display: flex; flex-direction: column; margin: 0 0 8px; }
  .turn.user { align-items: flex-end; }
  .turn.assistant { align-items: flex-start; }
  .msg { max-width: min(80ch, 92%); margin: 0; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; }
  /* the row under a bubble: time + actions, shown while the pointer is over the turn; always dim on touch */
  .tools { display: flex; align-items: center; gap: 2px; margin-top: 3px; min-height: 30px; color: var(--muted); font-size: 12px; opacity: 0; transition: opacity .15s; }
  .turn:hover .tools, .turn:focus-within .tools { opacity: 1; }
  @media (hover: none) { .tools { opacity: .75; } }
  .tools .when { margin: 0 8px; }
  .tools button { width: 32px; height: 30px; border-radius: 8px; font-size: 16px; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; }
  .tools button:hover, .tools button:active { background: var(--line); color: var(--ink); }
  .tools button.edit { color: var(--accent); font-size: 17px; }
  .tools button.retry { color: #d98a2b; }
  /* copy on an answer: a sticky pill at the bubble's top-right that stays in view while the answer scrolls */
  .msg.assistant .copyfloat { position: sticky; top: 6px; float: right; margin: -4px -8px 4px 8px; width: 32px; height: 32px; border-radius: 9px; font-size: 16px; color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, var(--her)); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); display: inline-flex; align-items: center; justify-content: center; opacity: .95; z-index: 1; }
  .msg.assistant .copyfloat:hover, .msg.assistant .copyfloat:active { color: #fff; background: var(--accent); }
  .msg.user { background: var(--me); color: var(--me-ink); margin-left: auto; border-bottom-right-radius: 6px; }
  .msg.assistant { background: var(--her); border: 1px solid var(--line); margin-right: auto; border-bottom-left-radius: 6px; }
  .msg.assistant.live::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--muted); }
  @keyframes blink { 50% { opacity: 0; } }
  .msg pre { background: var(--code); padding: 10px 12px; border-radius: 10px; overflow-x: auto; white-space: pre; margin: 8px 0; font-size: 14px; }
  .msg code { background: var(--code); padding: 1px 5px; border-radius: 6px; font-size: .92em; }
  .msg pre code { background: none; padding: 0; }
  .msg ul, .msg ol { margin: 4px 0; padding-left: 22px; white-space: normal; }
  .note { text-align: center; color: var(--muted); font-size: 13px; margin: 8px 0 14px; }
  .note.err { color: var(--danger); }
  .empty { color: var(--muted); text-align: center; margin-top: 30vh; padding: 0 24px; }

  footer {
    border-top: 1px solid var(--line); background: var(--bg); padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  .composer { display: flex; align-items: flex-end; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 24px; padding: 6px 6px 6px 8px; }
  textarea { flex: 1; resize: none; border: 0; outline: 0; background: none; color: inherit; font: inherit; max-height: 40vh; padding: 9px 0; min-height: 26px; }
  .send { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 20px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
  .send.stop { background: var(--danger); }
  .attach { width: 40px; height: 40px; border-radius: 50%; font-size: 20px; color: var(--muted); flex: none; display: inline-flex; align-items: center; justify-content: center; }
  .pending { display: flex; gap: 8px; overflow-x: auto; padding: 0 4px 8px; }
  .pending:empty { display: none; }
  .pending .chip { position: relative; flex: none; }
  .pending .doc { width: 64px; height: 64px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 10px; padding: 4px; overflow: hidden; text-align: center; }
  .pending .doc b { font-size: 22px; }
  .thumbs .docchip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 10px; background: rgba(0,0,0,.08); font-size: 13px; max-width: 100%; }
  .thumbs .docchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .pending img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); display: block; }
  .pending .rm { position: absolute; top: -6px; right: -6px; width: 24px; height: 24px; border-radius: 50%; background: var(--danger); color: #fff; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; }
  .thumbs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
  .thumbs img { max-width: 160px; max-height: 160px; border-radius: 10px; display: block; }
  .step { font-size: 13px; color: var(--muted); margin: 0 0 10px 6px; }
  /* the live status while a turn runs: a spinner, what is happening, elapsed seconds, approximate tokens */
  .thinking { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; margin: 2px 0 12px 6px; }
  .thinking .spin { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .9s linear infinite; flex: none; }
  @keyframes spin { to { transform: rotate(360deg); } }
  .thinking .secs { font-variant-numeric: tabular-nums; color: var(--ink); }

  /* the sessions drawer */
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 5; opacity: 0; pointer-events: none; transition: opacity .2s; }
  .drawer { position: fixed; top: 0; bottom: 0; left: 0; width: min(85vw, 340px); background: var(--panel); z-index: 6; transform: translateX(-100%); transition: transform .2s; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
  body.drawer-open .scrim { opacity: 1; pointer-events: auto; }
  body.drawer-open .drawer { transform: none; }
  .drawer .top { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
  .drawer .top strong { flex: 1; }
  .drawer ul { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
  .drawer li { display: flex; align-items: center; gap: 4px; border-radius: 12px; }
  .drawer li.active { background: var(--line); }
  .drawer li > button.t { flex: 1; text-align: left; padding: 12px 12px; min-height: 48px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .drawer li > button.x { width: 44px; height: 44px; color: var(--muted); }
  .drawer .who { padding: 10px 16px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }
  .newchat { margin: 8px; padding: 12px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; text-align: center; }

  /* key setup */
  dialog { border: 0; border-radius: 16px; background: var(--panel); color: var(--ink); padding: 20px; width: min(92vw, 420px); }
  dialog::backdrop { background: rgba(0,0,0,.4); }
  dialog input { width: 100%; font: inherit; padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: inherit; margin: 10px 0; }
  dialog .row { display: flex; gap: 8px; justify-content: flex-end; }
  dialog .row button { padding: 10px 16px; border-radius: 10px; }
  dialog .row .ok { background: var(--accent); color: #fff; }
  dialog p { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
  dialog .bad { color: var(--danger); font-size: 14px; min-height: 1.4em; }


  /* drop anywhere: the overlay only appears while something with files is being dragged over the page */
  .dropzone { position: fixed; inset: 10px; border: 3px dashed var(--accent); border-radius: 20px; background: color-mix(in srgb, var(--bg) 85%, transparent); z-index: 20; display: none; align-items: center; justify-content: center; font-size: 20px; color: var(--accent); pointer-events: none; }
  body.dragging .dropzone { display: flex; }


  /* jump buttons, bottom-right of the chat, each shown only when there is somewhere to go */
  .jumps { position: absolute; right: 14px; bottom: 12px; display: flex; flex-direction: column; gap: 8px; z-index: 3; }
  .jump { width: 42px; height: 42px; border-radius: 50%; color: #fff; font-size: 20px; display: none; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,.25); opacity: .9; }
  .jump.on { display: inline-flex; }
  .jump.top { background: #6b5cc4; }
  .jump.bottom { background: #2e8b57; }
  .logwrap { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; }

  /* selection menu: right-click on desktop, a floating bar over the selection on touch */
  .selmenu { position: fixed; z-index: 30; background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.25); padding: 4px; display: none; min-width: 190px; max-width: 92vw; }
  .selmenu.on { display: block; }
  .selmenu button { display: block; width: 100%; text-align: left; padding: 10px 12px; border-radius: 8px; font-size: 15px; }
  .selmenu button:hover, .selmenu button:active { background: var(--line); }
  .selmenu .sep { height: 1px; background: var(--line); margin: 4px 6px; }
  .selmenu .sub { color: var(--muted); font-size: 12px; padding: 6px 12px 2px; }




  /* the viewer: an image large, or a document's name with open/download */
  .viewer { position: fixed; inset: 0; background: rgba(0,0,0,.82); z-index: 40; display: none; flex-direction: column; align-items: center; justify-content: center; padding: 16px; padding-top: calc(16px + env(safe-area-inset-top)); }
  .viewer.on { display: flex; }
  /* sized against the viewport, not the wrapper (which has no height of its own), so the bar always fits */
  .viewer #viewerBody { flex: 0 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }
  .viewer img { max-width: calc(100vw - 32px); max-height: calc(100dvh - 130px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); object-fit: contain; border-radius: 8px; background: #000; }
  .viewer .bar { flex: none; }
  .viewer .docbox { background: var(--panel); color: var(--ink); border-radius: 16px; padding: 24px 28px; text-align: center; max-width: 92vw; }
  .viewer .docbox .big { font-size: 48px; margin-bottom: 8px; }
  .viewer .docbox .name { font-weight: 600; word-break: break-all; margin-bottom: 4px; }
  .viewer .docbox .meta { color: var(--muted); font-size: 13px; }
  .viewer .bar { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; justify-content: center; }
  .viewer .bar button, .viewer .bar a { padding: 11px 18px; border-radius: 12px; background: var(--panel); color: var(--ink); text-decoration: none; font: inherit; border: 1px solid var(--line); }
  .viewer .bar .primary { background: var(--accent); color: #fff; border-color: transparent; }
  .viewer .x { position: absolute; top: calc(8px + env(safe-area-inset-top)); right: 8px; width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,.15); color: #fff; font-size: 22px; display: inline-flex; align-items: center; justify-content: center; }
  .thumbs img, .thumbs .docchip { cursor: zoom-in; }
  .thumbs .docchip { cursor: pointer; }
  #chat { display: contents; }


  /* the menu: pinned = part of the layout, resizable by its right edge; unpinned = a drawer over the chat */
  .drawer { width: min(85vw, var(--drawer-w, 300px)); }
  .drawer .grip { display: none; position: absolute; top: 0; bottom: 0; right: -3px; width: 7px; cursor: col-resize; z-index: 7; }
  body.pinned { flex-direction: row; }
  body.pinned .drawer { position: relative; transform: none; width: var(--drawer-w, 300px); min-width: 180px; max-width: 60vw; border-right: 1px solid var(--line); flex: none; }
  body.pinned .drawer .grip { display: block; }
  body.pinned .scrim, body.pinned #menu, body.pinned #closeDrawer { display: none; }
  body.pinned #chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
  body.pinned main { padding: 20px max(12px, 4vw); }
  body.resizing { cursor: col-resize; user-select: none; }
  .drawer .top button.pin { width: 44px; height: 44px; border-radius: 12px; font-size: 18px; color: var(--muted); }
  body.pinned .drawer .top button.pin { color: var(--accent); }
  /* in-place edit of a sent message */
  /* the textarea IS the text: same font, no box, grows with its content; the bubble keeps its width */
  .msg.user textarea.inplace { display: block; width: 100%; font: inherit; line-height: inherit; color: inherit; background: none; border: 0; outline: 0; padding: 0; margin: 0; resize: none; overflow: hidden; white-space: pre-wrap; }
  .msg.user.editing { outline: 2px solid rgba(255,255,255,.45); outline-offset: -2px; }
  .msg.user .inplace-row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
  .msg.user .inplace-row button { padding: 7px 14px; border-radius: 10px; font-size: 14px; }
  .msg.user .inplace-row .save { background: #fff; color: var(--me); font-weight: 600; }
  .msg.user .inplace-row .cancel { background: rgba(255,255,255,.15); color: #fff; }
  .msg.user .inplace-hint { font-size: 12px; opacity: .8; margin-top: 6px; }

  /* login */
  .login { max-width: 360px; margin: 14vh auto 0; padding: 0 24px; }
  .login h2 { margin: 0 0 4px; font-size: 26px; }
  .login p { color: var(--muted); margin: 0 0 18px; }
  .login input { width: 100%; font: inherit; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: inherit; margin: 6px 0; }
  .login button { width: 100%; padding: 13px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; margin-top: 10px; }
  .login .remember { display: flex; align-items: center; gap: 10px; margin: 10px 2px 0; color: var(--muted); font-size: 15px; }
  .login .remember input { width: 20px; height: 20px; margin: 0; }
  .login .link { background: none; color: var(--accent); font-size: 14px; padding: 8px 0; width: auto; margin-top: 6px; }
  .login .bad { color: var(--danger); min-height: 1.4em; font-size: 14px; margin-top: 8px; }
  .sheet { padding: 4px 0; }
  .sheet > button { display: block; width: 100%; text-align: left; padding: 12px; border-radius: 10px; }
  .sheet .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
  .sheet .row button { padding: 10px 16px; border-radius: 10px; }
  .sheet .row .ok { background: var(--accent); color: #fff; }
  .sheet button:active { background: var(--line); }
  .sheet input { width: 100%; font: inherit; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: inherit; margin: 6px 0; }
  .sheet .hint { color: var(--muted); font-size: 13px; margin: 4px 0 8px; }
  .hidden { display: none !important; }
