:root{
  --bg:#fff; --fg:#111; --muted:#6b7280; --border:#e5e7eb; --surface:#f7f7f8;
  --accent:#10a37f; --danger:#d83a3a; --code:#111827; --code-bg:#f3f4f6;
  --font-size:16px;
}
body.dark{
  --bg:#1f1f1f; --fg:#f3f3f3; --muted:#9ca3af; --border:#333; --surface:#232323;
  --code:#e5e7eb; --code-bg:#111315;
}
*{box-sizing:border-box}
html,body{height:100%}
body{
  margin:0; font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto;
  background:var(--bg); color:var(--fg); transition:.2s background,.2s color;
  font-size:var(--font-size); line-height:1.5;
}
.app{display:flex; height:100vh; width:100vw; margin:0; position:relative; overflow:hidden;}
.sidebar{
  width:300px; min-width:220px; max-width:60vw;
  border-right:1px solid var(--border);
  display:flex; flex-direction:column; padding:12px; gap:10px; background:var(--bg);
  position:relative; z-index:1000; overflow:hidden;
}
.resizer{
  width:6px; cursor:col-resize; background:transparent; position:relative;
}
.resizer::after{
  content:""; position:absolute; left:2px; top:0; bottom:0; width:2px; background:var(--border);
}
.main{flex:1; display:flex; flex-direction:column; min-width:0; align-items:center;}
.topbar{
  padding:12px 16px; border-bottom:1px solid var(--border);
  display:flex; justify-content:space-between; align-items:center; gap:10px; width:100%;
  max-width:680px; margin:0 auto;
}
.topbar h2{margin:0; font-size:1.1rem; font-weight:700; white-space:nowrap; overflow:hidden; text-overflow:ellipsis;}
.switch{display:flex; align-items:center; gap:6px; font-size:.9rem; color:var(--muted);}
.chat{
  flex:1; overflow-y:auto; display:flex; flex-direction:column; gap:10px; min-width:0;
  align-items:center; width:100%; padding:28px 0 28px 0;
}
.chat-centered{ max-width:680px; margin:0 auto; width:100%; }
.message{
  max-width:96%; width:auto; min-width:0; padding:12px 16px; border:1px solid var(--border);
  border-radius:12px; background:var(--surface); animation:fade .2s ease;
  margin:0.5em 0;
  word-break:break-word;
}
.message.user{
  align-self:flex-end; background:var(--accent); color:#fff; border-color:var(--accent);
}
.message .asst{white-space:pre-wrap;}
.message code{font-family:ui-monospace,SFMono-Regular,Menlo,monospace; background:var(--code-bg); color:var(--code); padding:.15em .35em; border-radius:4px;}
pre{background:var(--code-bg); color:var(--code); padding:12px; border-radius:8px; overflow:auto; border:1px solid var(--border);}
pre .copy-btn{float:right; margin:-4px -4px 8px 0; border:1px solid var(--border); padding:4px 8px; border-radius:6px; background:transparent; cursor:pointer;}
.composer{
  border-top:1px solid var(--border); padding:12px 16px; background:var(--bg); width:100%;
  max-width:680px; margin:0 auto;
}
.input-box{
  display:flex; align-items:center; background:var(--surface);
  border:1px solid var(--border); border-radius:999px; padding:6px 10px;
}
#user-input{
  flex:1; background:transparent; border:none; color:inherit; font-size:1rem; padding:8px; resize:none; max-height:160px; overflow:auto;
}
#user-input:focus{outline:none}
.send{
  background:var(--accent); color:#fff; border:none; border-radius:50%;
  width:40px; height:40px; cursor:pointer; font-size:1rem;
}
.hints{color:var(--muted); font-size:.85rem; padding:6px 6px 0;}
/* Sidebar y tree */
.sidebar-header{ display:flex; justify-content:space-between; align-items:center; padding:4px 0 10px; border-bottom:1px solid var(--border);}
.brand{font-weight:700; letter-spacing:.5px}
.icon-btn{
  background:none; border:none; cursor:pointer; font-size:1.3rem; color:var(--fg);
}
.sidebar-search input{ width:100%; padding:8px 10px; border:1px solid var(--border); border-radius:8px; background:var(--surface); color:var(--fg);}
.sidebar-actions button{width:100%; margin-bottom:6px}
.primary{ background:var(--accent); color:#fff; border:none; padding:10px 12px; border-radius:8px; cursor:pointer; font-weight:600;}
.ghost{ background:transparent; border:1px solid var(--border); color:var(--fg); padding:10px 12px; border-radius:8px; cursor:pointer; font-weight:600;}
.danger{ background:transparent; border:1px solid var(--danger); color:var(--danger); padding:10px 12px; border-radius:8px; cursor:pointer; width:100%; font-weight:600;}
.import-label{display:inline-block; border:1px solid var(--border); padding:8px 10px; border-radius:8px; cursor:pointer; margin-left:6px;}
.sidebar-footer{margin-top:auto; padding-top:8px; border-top:1px solid var(--border); display:flex; gap:6px; flex-wrap:wrap;}
.tree{flex:1; overflow:auto; padding:6px 0;}
.section-sep{margin:10px 4px; font-size:.92em; color:var(--muted); text-transform:uppercase; letter-spacing:.06em;}
.tree-group{margin:6px 0}
.tree-folder{
  display:flex; align-items:center; justify-content:space-between; padding:6px 8px; margin:4px 0; border-radius:8px; cursor:pointer; font-weight:700;
}
.tree-folder .label{display:flex; align-items:center; gap:6px; white-space:normal; word-break:break-word;}
.tree-topic{
  display:flex; align-items:center; justify-content:space-between;
  padding:8px 10px; margin:2px 0; border-radius:8px; cursor:pointer; white-space:normal; word-break:break-word;
}
.tree-topic.active{background:var(--surface);}
.arrow-btn{background:none; border:none; cursor:pointer; font-size:1.1em; color:var(--muted); margin-left:8px; transition:.2s color;}
.arrow-btn:hover{color:var(--accent);}
.inline-rename{width:100%; border:none; background:transparent; color:inherit; font:inherit; padding:2px 4px; border-bottom:1px dashed var(--border);}
.menu{position:absolute; z-index:9999; background:var(--surface); border:1px solid var(--border); border-radius:8px; box-shadow:0 8px 24px 0 rgba(0,0,0,0.12); padding:4px 0; min-width:180px;}
.menu button{background:none; border:none; width:100%; text-align:left; font-size:1em; padding:8px 16px; cursor:pointer; color:var(--fg);}
.menu button:hover{background:var(--bg);}
@keyframes fade{from{opacity:0; transform:translateY(4px)}to{opacity:1; transform:translateY(0)}}
/* MOBILE */
@media(max-width:900px){
  .sidebar{
    position:fixed; left:-320px; top:0; bottom:0;
    transition:left 0.3s ease;
    box-shadow:2px 0 8px rgba(0,0,0,0.25);
  }
  .sidebar.open{left:0;}
  #menu-btn{display:inline-block;}
  #close-sidebar{display:inline-block;}
  .resizer{display:none;}
  .chat-centered, .composer, .topbar{max-width:100vw;}
}
#close-sidebar{display:none;}
#menu-btn{display:none;}
