:root {
  --bg: #f2efe9;
  --surface: #ffffff;
  --sidebar: #ebe7dd;
  --text: #2a2825;
  --muted: #73706a;
  --user-bubble: #e6e0d4;
  --accent: #c96442;
  --accent-hover: #b5573a;
  --border: #ddd6c8;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15.5px; line-height: 1.65; -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
button { cursor: pointer; border: none; font-family: inherit; }

/* ---------- Login ---------- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { background: var(--surface); width: 100%; max-width: 380px; margin: 1rem; padding: 2.5rem 2rem; border-radius: 18px; border: 1px solid var(--border); box-shadow: 0 8px 30px rgba(0,0,0,.06); text-align: center; }
.login-card .brand { font-size: 2rem; color: var(--accent); }
.login-card h1 { font-size: 1.4rem; margin: .75rem 0 .25rem; }
.subtitle { color: var(--muted); margin: 0 0 1.5rem; }
.login-card form { display: flex; flex-direction: column; text-align: left; }
.login-card label { font-size: .85rem; color: var(--muted); margin: .6rem 0 .25rem; }
.login-card input { padding: .7rem .85rem; border: 1px solid var(--border); border-radius: 10px; font-size: 1rem; background: #fbfaf7; }
.login-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login-card button { margin-top: 1.25rem; background: var(--accent); color: #fff; padding: .75rem; border-radius: 10px; font-size: 1rem; font-weight: 600; }
.login-card button:hover { background: var(--accent-hover); }
.error { color: #b3261e; background: #fbe9e7; padding: .6rem .8rem; border-radius: 8px; font-size: .9rem; }

/* ---------- App ---------- */
.app { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
.sidebar { width: 270px; background: var(--sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-head { padding: 1rem .8rem .4rem; }
.sidebar .brand { font-weight: 700; color: var(--accent); font-size: 1.15rem; margin-bottom: .9rem; padding-left: .2rem; }
.btn-new { width: 100%; background: var(--surface); border: 1px solid var(--border); color: var(--text); padding: .6rem; border-radius: 10px; font-weight: 600; font-size: .9rem; text-align: left; }
.btn-new:hover { background: #fffdf8; }
.conv-label { padding: .8rem .9rem .2rem; font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.conv-list { list-style: none; margin: 0; padding: 0 .5rem .5rem; overflow-y: auto; flex: 1; }
.conv-item { display: flex; align-items: center; gap: .25rem; padding: .5rem .55rem; border-radius: 8px; cursor: pointer; font-size: .88rem; }
.conv-item:hover { background: rgba(0,0,0,.05); }
.conv-item.active { background: rgba(201,100,66,.13); }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del { opacity: 0; color: var(--muted); background: transparent; font-size: .85rem; padding: 0 .2rem; }
.conv-item:hover .del { opacity: .7; }
.conv-item .del:hover { color: #b3261e; opacity: 1; }
.sidebar-foot { padding: .7rem .9rem; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; font-size: .82rem; }
.user-tag { color: var(--muted); }
.logout { background: transparent; color: var(--muted); font-weight: 600; }
.logout:hover { color: var(--text); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; position: relative; min-width: 0; }
.main-top { display: flex; align-items: center; gap: .6rem; padding: .65rem 1rem; border-bottom: 1px solid var(--border); background: rgba(242,239,233,.85); backdrop-filter: blur(6px); }
.main-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: .5rem; }
.icon-btn { background: transparent; color: var(--muted); font-size: 1.1rem; border-radius: 8px; padding: .25rem .5rem; }
.icon-btn:hover { background: rgba(0,0,0,.06); color: var(--text); }
.menu-btn { display: none; }
.mode-select { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .4rem .5rem; font-size: .82rem; color: var(--text); cursor: pointer; }
.files-btn { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .4rem .7rem; font-size: .82rem; font-weight: 600; color: var(--text); }
.files-btn:hover { background: #fffdf8; }

.messages { flex: 1; overflow-y: auto; padding: 1.5rem 1rem 9rem; }
.messages > * { max-width: 740px; margin-left: auto; margin-right: auto; }
.empty-state { text-align: center; color: var(--muted); margin-top: 15vh; }
.es-logo { font-size: 2.4rem; color: var(--accent); margin-bottom: .3rem; }
.empty-state h2 { font-weight: 500; color: var(--text); }

.msg { display: flex; margin-bottom: 1.3rem; }
.msg.user { justify-content: flex-end; }
.bubble { word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); border-radius: 16px 16px 4px 16px; padding: .7rem 1rem; max-width: 80%; white-space: pre-wrap; }
.msg.assistant .bubble { background: transparent; max-width: 100%; width: 100%; }
.msg.assistant .bubble.thinking::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Markdown */
.bubble p { margin: 0 0 .8rem; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: #2b2925; color: #f3efe7; border-radius: 10px; padding: .9rem 1rem; overflow-x: auto; margin: .6rem 0; }
.bubble code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .88em; }
.bubble :not(pre) > code { background: #e7e1d4; padding: .12em .4em; border-radius: 5px; }
.bubble pre code { background: transparent; padding: 0; color: inherit; }
.bubble ul, .bubble ol { margin: .3rem 0 .8rem; padding-left: 1.45rem; }
.bubble li { margin: .2rem 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 1.1rem 0 .5rem; line-height: 1.3; }
.bubble table { border-collapse: collapse; margin: .6rem 0; font-size: .92em; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: .4rem .65rem; }
.bubble blockquote { border-left: 3px solid var(--border); margin: .5rem 0; padding-left: .85rem; color: var(--muted); }
.bubble a { color: var(--accent); }

/* Permission card */
.perm-card { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 12px; padding: .8rem 1rem; width: 100%; box-shadow: 0 2px 10px rgba(0,0,0,.04); }
.perm-head { font-size: .92rem; margin-bottom: .4rem; }
.perm-detail { background: #2b2925; color: #f3efe7; border-radius: 8px; padding: .55rem .7rem; margin: .35rem 0 .6rem; overflow-x: auto; font-size: .82rem; }
.perm-detail code { font-family: ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; }
.perm-actions { display: flex; gap: .5rem; justify-content: flex-end; }
.perm-actions button { padding: .4rem 1rem; border-radius: 8px; font-weight: 600; font-size: .85rem; }
.perm-allow { background: var(--accent); color: #fff; }
.perm-allow:hover { background: var(--accent-hover); }
.perm-deny { background: #efe9dd; color: var(--text); }
.perm-deny:hover { background: #e6dfcf; }
.perm-result { font-size: .85rem; color: var(--muted); font-weight: 600; }

/* Tool pill */
.tool-pill { display: inline-block; background: #e7e1d4; color: var(--muted); border-radius: 20px; padding: .3rem .8rem; font-size: .8rem; }

/* Composer */
.composer { position: absolute; bottom: 0; left: 0; right: 0; padding: .5rem 1rem 1rem; background: linear-gradient(transparent, var(--bg) 22%); }
.composer > * { max-width: 740px; margin: 0 auto; }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: .4rem; }
.chip { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: .25rem .6rem; font-size: .8rem; display: flex; align-items: center; gap: .35rem; }
.chip .x { cursor: pointer; color: var(--muted); }
.composer-inner { display: flex; align-items: flex-end; gap: .4rem; background: var(--surface); border: 1px solid var(--border); border-radius: 26px; padding: .4rem .45rem .4rem .55rem; box-shadow: 0 4px 24px rgba(0,0,0,.06); }
.attach { font-size: 1.35rem; line-height: 1; color: var(--muted); }
#input { flex: 1; border: none; resize: none; font: inherit; background: transparent; max-height: 200px; outline: none; padding: .5rem .2rem; }
#send { background: var(--accent); color: #fff; width: 34px; height: 34px; border-radius: 50%; font-size: 1.05rem; flex-shrink: 0; }
#send:hover { background: var(--accent-hover); }
#send:disabled { background: #d9cfc4; cursor: default; }
.disclaimer { text-align: center; color: var(--muted); font-size: .72rem; margin: .5rem auto 0; }

/* Files panel */
.files-panel { position: absolute; top: 54px; right: 1rem; width: 320px; max-height: 60vh; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.14); z-index: 20; }
.files-panel-head { display: flex; justify-content: space-between; align-items: center; padding: .7rem .9rem; border-bottom: 1px solid var(--border); font-weight: 600; }
.files-list { list-style: none; margin: 0; padding: .4rem; }
.files-list li { padding: .45rem .5rem; border-radius: 8px; display: flex; justify-content: space-between; gap: .5rem; font-size: .85rem; }
.files-list li:hover { background: rgba(0,0,0,.04); }
.files-list a { color: var(--accent); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files-list .sz { color: var(--muted); flex-shrink: 0; }
.files-empty { padding: 1rem; color: var(--muted); text-align: center; font-size: .85rem; }

/* Mic button */
.mic { font-size: 1.1rem; }
.mic.listening { color: #e0392b; animation: pulse 1.1s infinite; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.18); } }

/* Send -> Stop */
#send.stop { background: #2b2925; }
#send.stop:hover { background: #000; }

/* Code copy button */
.bubble pre { position: relative; }
.copy-code { position: absolute; top: .45rem; right: .45rem; background: rgba(255,255,255,.12); color: #f3efe7; border: 1px solid rgba(255,255,255,.2); border-radius: 6px; font-size: .72rem; padding: .15rem .45rem; opacity: 0; transition: opacity .15s; }
.bubble pre:hover .copy-code { opacity: 1; }
.copy-code:hover { background: rgba(255,255,255,.25); }

/* Message copy action */
.msg-actions { width: 100%; margin-top: .15rem; }
.msg.assistant .msg-actions { display: flex; }
.msg-copy { background: transparent; color: var(--muted); font-size: .76rem; padding: .2rem .4rem; border-radius: 6px; opacity: 0; transition: opacity .15s; }
.msg.assistant:hover .msg-copy { opacity: 1; }
.msg-copy:hover { background: rgba(0,0,0,.06); color: var(--text); }

/* Created-files strip (inline) */
.files-strip { width: 100%; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .7rem .9rem; display: flex; flex-wrap: wrap; gap: .55rem; align-items: center; }
.fs-label { width: 100%; font-size: .82rem; color: var(--muted); margin-bottom: .15rem; }
.fs-thumb { width: 96px; height: 96px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); cursor: pointer; }
.fs-thumb:hover { box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.fs-chip { background: #efe9dd; color: var(--text); border-radius: 10px; padding: .4rem .7rem; font-size: .82rem; text-decoration: none; }
.fs-chip:hover { background: #e6dfcf; }

/* Files panel thumbnails */
.files-list .thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; flex-shrink: 0; }

/* Lightbox */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: flex; align-items: center; justify-content: center; z-index: 100; cursor: zoom-out; }
.lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }

/* Mobile */
@media (max-width: 760px) {
  .sidebar { position: absolute; z-index: 30; height: 100%; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-block; }
  .files-panel { width: calc(100% - 2rem); }
  .mode-select { max-width: 130px; }
}
