:root {
  --bg: #f7f5f0; --panel: #ffffff; --side: #efece5; --text: #1f1e1c; --muted: #6b6760;
  --line: #e2ded5; --accent: #c2622f; --accent2: #3d6b8f; --user: #e9e4d8; --code: #f0ede6; --danger: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #1b1a18; --panel: #23221f; --side: #151412; --text: #ece9e2; --muted: #9c978d;
    --line: #34322d; --accent: #d9774a; --accent2: #6f9cc0; --user: #2f2d29; --code: #141311; --danger: #e5645a; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans Arabic", sans-serif; }
.hidden { display: none !important; }
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; border-radius: 10px; padding: 10px 14px; background: var(--code); }
.primary { background: var(--accent); color: #fff; }
.primary.alt { background: var(--accent2); }
.danger { background: var(--danger); color: #fff; }
.link { background: none; color: var(--muted); padding: 6px 4px; text-align: start; }
.icon { background: none; font-size: 20px; padding: 6px 10px; }
a.btn { text-decoration: none; border-radius: 10px; padding: 8px 12px; font-size: 14px; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { width: 100%; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 28px 22px; display: flex; flex-direction: column; gap: 12px; align-items: stretch; text-align: center; }
.card img { align-self: center; }
.card h1 { margin: 0 0 6px; font-size: 22px; }
.card input { padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 17px; }
.err { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* layout */
.app { height: 100%; display: flex; }
.side { width: 280px; background: var(--side); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; }
.newrow { display: flex; gap: 8px; padding: 12px; }
.newrow button { flex: 1; padding: 10px 6px; font-size: 14px; }
.list { flex: 1; overflow-y: auto; padding: 0 8px; }
.item { display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 9px; cursor: pointer; font-size: 14px; }
.item:hover, .item.on { background: var(--line); }
.item .tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--accent2); color: #fff; flex: none; }
.item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .del { visibility: hidden; background: none; padding: 0 4px; color: var(--muted); }
.item:hover .del, .item.on .del { visibility: visible; }
.item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.sidefoot { display: flex; flex-direction: column; padding: 8px 12px 14px; border-top: 1px solid var(--line); }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }
.top { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
.top .title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; }
select { background: var(--code); border: 0; border-radius: 8px; padding: 6px 8px; }

.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: auto; max-width: 440px; color: var(--muted); text-align: center; }
.msg { max-width: 820px; width: 100%; align-self: center; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); border-radius: 16px; padding: 10px 14px; margin-inline-start: auto; width: fit-content; max-width: 88%; white-space: pre-wrap; }
.msg.assistant .bubble { padding: 2px 2px; }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow-x: auto; font-size: 13.5px; position: relative; direction: ltr; text-align: left; }
.bubble code { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: .92em; }
.bubble :not(pre) > code { background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble .copy { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 3px 8px; background: var(--line); }
.bubble h1, .bubble h2, .bubble h3 { font-size: 1.1em; margin: 14px 0 6px; }
.bubble ul, .bubble ol { padding-inline-start: 22px; margin: 0 0 10px; }
.bubble a { color: var(--accent2); }
.bubble table { border-collapse: collapse; margin: 0 0 10px; display: block; overflow-x: auto; }
.bubble td, .bubble th { border: 1px solid var(--line); padding: 4px 8px; }
.tool { font-size: 13px; color: var(--muted); margin: 4px 0; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.composer { display: flex; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 10px 12px; font-size: 16px; }

/* downloads */
.dl { border: 1px solid var(--line); border-radius: 14px; padding: 10px; background: var(--panel); display: flex; flex-direction: column; gap: 6px; }
.dlhead { font-weight: 600; font-size: 14px; color: var(--muted); padding: 0 4px; }
.dlline { display: flex; gap: 6px; align-items: stretch; }
.dlline .dlrow { flex: 1; min-width: 0; }
.pub { background: var(--accent2); color: #fff; font-size: 13px; padding: 8px 10px; }
.pub:disabled { opacity: .8; }
.dlrow { display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 10px; background: var(--code); color: var(--text); text-decoration: none; }
.dlrow.match { background: var(--accent); color: #fff; }
.dlrow.match .dlmeta { color: #fff; opacity: .9; }
.dlicon { font-size: 18px; }
.dlname { flex: 1; font-weight: 600; overflow-wrap: anywhere; }
.dlmeta { font-size: 13px; color: var(--muted); white-space: nowrap; }
.dlhint { font-size: 13px; color: var(--muted); padding: 2px 4px; }

/* sessions */
.sessbtn { margin: 0 12px 10px; background: var(--code); text-align: start; font-size: 14px; }
.srow { padding: 12px; border-radius: 10px; border: 1px solid var(--line); margin-bottom: 8px; cursor: pointer; }
.srow:hover { background: var(--code); }
.stop { display: flex; gap: 8px; align-items: center; }
.sname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sstate { font-size: 11px; padding: 2px 7px; border-radius: 6px; background: var(--line); white-space: nowrap; }
.sstate[data-s="working"] { background: var(--accent); color: #fff; }
.sstate[data-s="waiting"] { background: var(--accent2); color: #fff; }
.smeta { font-size: 12px; color: var(--muted); margin: 4px 0; overflow-wrap: anywhere; }
.slast { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sev { margin: 8px 0; overflow-wrap: anywhere; font-size: 14px; }
.sev.you { background: var(--user); border-radius: 12px; padding: 8px 12px; white-space: pre-wrap; }
.sev.tool { font-size: 12px; color: var(--muted); font-family: ui-monospace, monospace; }
.sev.claude pre { background: var(--code); padding: 8px; border-radius: 8px; overflow-x: auto; font-size: 12.5px; }

/* files */
.files { position: fixed; inset: 0; background: var(--panel); display: flex; flex-direction: column; z-index: 30; }
.fhead { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.fhead b { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.flist { overflow-y: auto; padding: 8px; }
.frow { display: flex; gap: 10px; padding: 10px; border-radius: 8px; cursor: pointer; }
.frow:hover { background: var(--code); }
.frow .fn { flex: 1; overflow-wrap: anywhere; }
.frow .fs { color: var(--muted); font-size: 13px; }

@media (max-width: 760px) {
  #menu { display: block; }
  .top { gap: 4px; padding: 8px; }
  .top select { padding: 6px 4px; font-size: 14px; }
  .side { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: 84%; max-width: 320px; }
  .side.open { transform: none; }
  .side.open + .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 10; }
  .msgs { padding: 12px; }
}
