:root {
  --bg: #0a0a0b;
  --surface: #131315;
  --surface2: #1b1b1e;
  --border: #26262a;
  --text: #e8e8ea;
  --dim: #8b8b93;
  --faint: #55555e;
  --accent: #a3e635;
  --accent-dim: #3a4d17;
  --danger: #f87171;
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: var(--mono);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

/* ── header ─────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.logo {
  width: 26px; height: 26px; border-radius: 8px;
  background: var(--accent); color: #0a0a0b;
  display: grid; place-items: center;
  font-weight: 700; font-size: 15px;
}
.name { font-weight: 600; font-size: 14px; letter-spacing: -0.02em; color: var(--dim); }

.spaces {
  display: flex; gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px; padding: 3px;
}
.space {
  font-family: var(--mono); font-size: 12px; font-weight: 600;
  background: none; border: 0; color: var(--dim);
  padding: 5px 12px; border-radius: 8px;
  display: flex; align-items: center; gap: 6px;
  cursor: pointer;
}
.space.active { background: var(--surface2); color: var(--text); }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); transition: background .3s; }
.dot.ok { background: var(--accent); box-shadow: 0 0 6px color-mix(in srgb, var(--accent) 60%, transparent); }
.dot.down { background: var(--danger); }

.iconbtn {
  background: none; border: 0; color: var(--dim);
  width: 36px; height: 36px; border-radius: 10px;
  display: grid; place-items: center; cursor: pointer; flex-shrink: 0;
}
.iconbtn:active { background: var(--surface2); }
.iconbtn:disabled { opacity: .3; }

/* ── banner ─────────────────────────────────────────── */
.banner {
  padding: 8px 14px; font-size: 12px;
  background: color-mix(in srgb, var(--danger) 14%, var(--bg));
  color: var(--danger); border-bottom: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
}
.hidden { display: none !important; }

/* ── chat ───────────────────────────────────────────── */
main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
#messages { max-width: 760px; margin: 0 auto; padding: 14px 14px 8px; display: flex; flex-direction: column; gap: 4px; }

.empty { display: flex; flex-direction: column; align-items: center; padding-top: 12vh; gap: 10px; }
.empty-mark { font-size: 44px; font-weight: 800; color: var(--faint); letter-spacing: -0.06em; }
.empty-sub { font-size: 12px; color: var(--faint); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; padding: 12px 20px; max-width: 480px; }
.chip {
  font-family: var(--mono); font-size: 12px;
  background: var(--surface); border: 1px solid var(--border); color: var(--dim);
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
}
.chip:active { border-color: var(--accent-dim); color: var(--text); }

.msg { padding: 8px 0; }
.msg.user { text-align: right; }
.msg.user .bubble {
  display: inline-block; text-align: left;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius); border-bottom-right-radius: 4px;
  padding: 10px 14px; max-width: 88%;
  white-space: pre-wrap; word-break: break-word;
}
.msg.user .imgs { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; margin-bottom: 6px; }
.msg.user .imgs img { width: 84px; height: 84px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }

.msg.agent { color: var(--text); }
.msg.agent .md { word-break: break-word; }
.msg.agent .md p { margin: 0 0 8px; }
.msg.agent .md p:last-child { margin-bottom: 0; }
.msg.agent .md pre {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; overflow-x: auto;
  font-size: 12.5px; margin: 8px 0; line-height: 1.5;
}
.msg.agent .md code { font-family: var(--mono); font-size: 0.92em; }
.msg.agent .md :not(pre) > code { background: var(--surface2); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; }
.msg.agent .md h1, .msg.agent .md h2, .msg.agent .md h3 { font-size: 14px; margin: 12px 0 6px; color: var(--accent); }
.msg.agent .md ul, .msg.agent .md ol { margin: 4px 0 8px 20px; }
.msg.agent .md li { margin: 2px 0; }
.msg.agent .md a { color: var(--accent); text-decoration: none; }
.msg.agent .md blockquote { border-left: 2px solid var(--accent-dim); padding-left: 10px; color: var(--dim); margin: 6px 0; }

.tools { display: flex; flex-direction: column; gap: 2px; margin: 2px 0; }
.tool {
  font-size: 11.5px; color: var(--faint);
  display: flex; align-items: center; gap: 6px;
  padding: 2px 0; cursor: pointer;
}
.tool .ticon { color: var(--accent); flex-shrink: 0; }
.tool .tdetail {
  display: none; font-size: 11px; color: var(--dim);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; margin: 4px 0; white-space: pre-wrap; word-break: break-all; max-height: 180px; overflow-y: auto;
}
.tool.open .tdetail { display: block; }
.tool.run .ticon { animation: spin 1.2s linear infinite; display: inline-block; }

@keyframes spin { to { transform: rotate(360deg); } }

.typing { display: flex; gap: 4px; padding: 8px 0; max-width: 760px; margin: 0 auto; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--faint); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

/* ── footer / input ─────────────────────────────────── */
footer {
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
}
.attachments { display: flex; gap: 8px; padding: 8px 14px 0; max-width: 760px; margin: 0 auto; }
.attachments:empty { display: none; }
.attach { position: relative; }
.attach img { width: 56px; height: 56px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); display: block; }
.attach button {
  position: absolute; top: -6px; right: -6px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: #fff; border: 0;
  font-size: 11px; line-height: 1; cursor: pointer;
}
.inputbar {
  display: flex; align-items: flex-end; gap: 4px;
  max-width: 760px; margin: 0 auto; padding: 8px 10px;
}
#input {
  flex: 1; resize: none; border: 0; outline: 0;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text); font-family: var(--mono); font-size: 15px;
  border-radius: 14px; padding: 10px 14px;
  max-height: 132px; min-height: 42px;
}
#input:focus { border-color: var(--accent-dim); }
.send {
  width: 40px; height: 40px; border-radius: 12px; border: 0;
  background: var(--accent); color: #0a0a0b;
  display: grid; place-items: center; cursor: pointer; flex-shrink: 0;
}
.send:disabled { background: var(--surface2); color: var(--faint); }
.mic.rec { color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .4; } }
.safe { height: env(safe-area-inset-bottom); }

/* ── dictée ─────────────────────────────────────────── */
.rec {
  position: fixed; top: calc(env(safe-area-inset-top) + 58px); left: 50%;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 999px; padding: 8px 16px; font-size: 12px; z-index: 20;
}
.rec i { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.rec em { font-style: normal; color: var(--faint); }

/* ── sessions sheet ─────────────────────────────────── */
.sheet {
  position: fixed; inset: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg) 96%, transparent);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.new-session {
  margin: 12px 14px; padding: 12px; border-radius: 12px;
  background: var(--accent); color: #0a0a0b; border: 0;
  font-family: var(--mono); font-weight: 700; font-size: 13px; cursor: pointer;
}
.session-list { flex: 1; overflow-y: auto; padding: 0 14px 20px; }
.sess {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; border: 1px solid var(--border); border-radius: 12px;
  margin-bottom: 8px; cursor: pointer; background: var(--surface);
}
.sess.active { border-color: var(--accent-dim); }
.sess .st { flex: 1; min-width: 0; }
.sess .st strong { display: block; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess .st small { font-size: 11px; color: var(--faint); }
.sess .del { background: none; border: 0; color: var(--faint); width: 30px; height: 30px; flex-shrink: 0; cursor: pointer; }
.sess .del:active { color: var(--danger); }

/* ── toast ──────────────────────────────────────────── */
.toast {
  position: fixed; bottom: calc(env(safe-area-inset-bottom) + 90px); left: 50%;
  transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); font-size: 12.5px;
  padding: 10px 16px; border-radius: 999px; z-index: 40;
  max-width: 88vw; text-align: center;
}
