:root {
  --bg:        #F4EFE6;
  --bg-2:      #EFE8DC;
  --bg-3:      #E8DFCE;
  --paper:     #FBF7EE;
  --ink:       #1A1714;
  --ink-2:     #3B342B;
  --muted:     #7A6F5F;
  --muted-2:   #A89A85;
  --line:      #DCD2BE;
  --line-soft: #E8DFCE;
  --gold:      #B08A3E;
  --warn:      #C26A2D;
  --danger:    #B23A2E;
  --ok:        #5C7A3A;
  --r-sm: 10px; --r: 16px; --r-lg: 22px;
  --sidebar-w: 252px;
  --topbar-h: 64px;
  --tabbar-h: 72px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top:    env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
body { font-family: 'Inter Tight', system-ui, sans-serif; min-height: 100vh; }

.serif { font-family: 'Newsreader', Georgia, serif; letter-spacing: -0.012em; }
.mono  { font-family: 'JetBrains Mono', monospace; }

button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
a { color: var(--ink); text-decoration: none; }
a:hover { text-decoration: underline; }

.boot {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-family: 'JetBrains Mono', monospace; font-size: 12px;
  letter-spacing: 0.14em; text-transform: uppercase;
}

.noscroll::-webkit-scrollbar { display: none; }
.noscroll { scrollbar-width: none; }

.scr-enter { animation: scrEnter 220ms cubic-bezier(.2,.7,.2,1) both; }
@keyframes scrEnter { from { opacity:0; transform: translateY(6px); } to { opacity:1; transform: none; } }
.fade-in { animation: fadeIn 240ms ease both; }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }

/* ───────── Login ───────── */
.login-bg {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background:
    radial-gradient(900px 600px at 20% 0%, #EFE2C8 0%, transparent 60%),
    radial-gradient(900px 600px at 100% 100%, #E8DFCE 0%, transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%; max-width: 420px; background: var(--paper);
  border: 1px solid var(--line); border-radius: 22px; padding: 32px;
  box-shadow: 0 30px 80px rgba(26, 23, 20, 0.12);
}
.login-logo { display: flex; align-items: center; gap: 12px; margin-bottom: 28px; }
.login-logo b { font-size: 18px; font-weight: 700; letter-spacing: .02em; }
.login-logo span { font-size: 13px; color: var(--muted); }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .12em; padding-left: 4px; }
.field input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); outline: none; font-size: 14px; transition: border .12s;
}
.field input:focus { border-color: var(--ink); background: var(--paper); }

.btn-primary {
  width: 100%; padding: 13px; border-radius: 12px; border: 0;
  background: var(--ink); color: var(--bg); font-size: 14px; font-weight: 600;
  cursor: pointer; margin-top: 8px;
}
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-secondary {
  padding: 11px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); font-size: 13.5px; font-weight: 500; cursor: pointer;
}

.err { color: var(--danger); font-size: 12.5px; margin-top: 8px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 14px; line-height: 1.5; text-align: center; }

/* ───────── App layout ───────── */
.app {
  min-height: 100vh; min-height: 100dvh;
  display: grid; grid-template-columns: var(--sidebar-w) 1fr;
}

.sidebar {
  background: var(--bg-2); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 22px 14px;
  position: sticky; top: 0; height: 100vh; height: 100dvh;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 0 6px 22px; }
.sidebar-brand b { font-weight: 700; font-size: 15px; letter-spacing: .02em; }
.sidebar-brand small { font-weight: 500; font-size: 11px; color: var(--muted); display: block; margin-top: -2px; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }
.nav-btn {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px;
  border-radius: 12px; border: 0; background: transparent; color: var(--muted);
  font-size: 13.5px; font-weight: 500; text-align: left; transition: all .12s;
}
.nav-btn:hover { background: rgba(26,23,20,.04); color: var(--ink); }
.nav-btn.on {
  background: var(--paper); color: var(--ink); font-weight: 600;
  box-shadow: 0 1px 2px rgba(0,0,0,.04), inset 0 0 0 1px var(--line);
}
.nav-btn .badge {
  margin-left: auto; min-width: 18px; height: 18px; padding: 0 6px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 10.5px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.sidebar-foot { margin-top: auto; padding-top: 16px; border-top: 1px solid var(--line); }
.user-card {
  display: flex; align-items: center; gap: 10px; padding: 8px;
  border-radius: 12px; cursor: pointer; transition: background .12s;
}
.user-card:hover { background: rgba(26,23,20,.04); }
.avatar {
  width: 36px; height: 36px; border-radius: 999px;
  background: linear-gradient(135deg, #E9D6A6, #B89E7B);
  display: flex; align-items: center; justify-content: center;
  font-family: 'Newsreader', serif; font-weight: 600; font-size: 13.5px; color: #3B342B;
}
.user-card .nm { font-size: 13px; font-weight: 600; line-height: 1.2; }
.user-card .ml { font-size: 11px; color: var(--muted); }

.main { min-width: 0; overflow-x: hidden; }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding: 14px 26px; display: flex; align-items: center; gap: 16px;
}
.topbar h1 { margin: 0; font-family: 'Newsreader', serif; font-size: 22px; font-weight: 600; letter-spacing: -.012em; }
.topbar .ctx { color: var(--muted); font-size: 12.5px; }
.topbar .right { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.icon-btn {
  width: 38px; height: 38px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--paper); display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; position: relative;
}
.icon-btn:hover { background: var(--bg-2); }
.icon-btn .dot {
  position: absolute; top: 6px; right: 6px; width: 8px; height: 8px;
  border-radius: 999px; background: var(--danger);
}

.content { padding: 24px 26px 64px; max-width: 1200px; }

/* Cards */
.card {
  background: var(--paper); border: 1px solid var(--line); border-radius: var(--r);
  padding: 16px;
}
.card.tight { padding: 12px 14px; }
.card.flat { box-shadow: none; }
.card-h {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .14em; margin-bottom: 10px;
}
.card-t { font-size: 13.5px; font-weight: 600; margin-bottom: 10px; }

/* KPI grid */
.kpis {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px;
}
.kpi { display: flex; flex-direction: column; gap: 6px; }
.kpi .lbl { font-size: 11.5px; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.kpi .val { font-family: 'Newsreader', serif; font-size: 28px; font-weight: 600; line-height: 1; }
.kpi .val small { font-size: 12px; color: var(--muted); margin-left: 4px; font-family: 'Inter Tight', sans-serif; font-weight: 400; }
.kpi .hint { font-size: 11px; color: var(--muted); }
.kpi .hint.ok { color: var(--ok); }
.kpi .hint.warn { color: var(--warn); }
.kpi .hint.danger { color: var(--danger); }

.disc {
  width: 28px; height: 28px; border-radius: 999px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center; color: var(--ink-2);
}
.disc.gold   { background: #F1E0BC; color: #6B5430; }
.disc.blush  { background: #F7DCC8; color: #7B3F1F; }
.disc.sage   { background: #DCE3CB; color: #3B5827; }
.disc.cream  { background: #F1E8D2; color: #5C5036; }
.disc.stone  { background: #E0DACA; color: #4A4338; }

/* Topic */
.topics { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.topic { display: flex; flex-direction: column; gap: 6px; padding: 12px; border-radius: 12px; background: var(--bg-2); }
.topic .pct { font-size: 13px; }
.topic .pct b { font-size: 14px; }

/* Alerts */
.alert {
  display: flex; align-items: center; gap: 12px; padding: 12px;
  border-radius: 14px; border: 1px solid var(--line-soft); background: #FAF4E8;
  cursor: pointer; transition: border-color .12s;
}
.alert:hover { border-color: var(--ink); }
.alert .ic {
  width: 26px; height: 26px; border-radius: 999px; flex-shrink: 0; font-weight: 700; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center;
}
.alert.danger .ic { background: #FBE2D5; color: var(--danger); }
.alert.warn .ic   { background: #F4E5C5; color: var(--warn); }
.alert .body { flex: 1; min-width: 0; }
.alert .body .t { font-size: 13.5px; font-weight: 500; line-height: 1.3; }
.alert .body .h { font-size: 11.5px; margin-top: 2px; }
.alert.danger .body .h { color: var(--danger); }
.alert.warn .body .h   { color: var(--warn); }

/* Decision card */
.decision {
  padding: 14px; border-radius: 14px; background: var(--paper); border: 1px solid var(--line);
  display: flex; gap: 12px; align-items: flex-start; cursor: pointer; transition: border-color .12s;
}
.decision:hover { border-color: var(--ink); }
.decision .body { flex: 1; }
.decision .title { font-size: 14px; font-weight: 500; line-height: 1.3; margin-bottom: 6px; }
.tags { display: flex; gap: 8px; flex-wrap: wrap; }
.tag { font-size: 11px; padding: 3px 9px; border-radius: 999px; background: var(--bg-3); color: var(--ink-2); }
.tag.danger { background: #FBE2D5; color: var(--danger); }
.tag.warn   { background: #F4E5C5; color: #8A4A1B; }
.tag.muted  { background: var(--bg-2); color: var(--muted); }

.section-h {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 22px 0 12px;
}
.section-h h2 { margin: 0; font-family: 'Newsreader', serif; font-size: 22px; font-weight: 600; letter-spacing: -.012em; }

.grid-2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
.grid-2 > div { display: flex; flex-direction: column; gap: 14px; }

/* Donut */
.donut-wrap { display: flex; align-items: center; gap: 16px; }
.donut-num { font-family: 'Newsreader', serif; font-weight: 600; }

/* Chat */
.chat-page { display: grid; grid-template-columns: 1fr; height: calc(100vh - var(--topbar-h)); }
.chat-page.with-list { grid-template-columns: 320px 1fr; }
.chat-list { border-right: 1px solid var(--line); overflow-y: auto; padding: 14px; background: var(--bg); }
.chat-list .item {
  padding: 12px; border-radius: 12px; cursor: pointer; margin-bottom: 6px;
  border: 1px solid transparent;
}
.chat-list .item:hover { background: var(--bg-2); }
.chat-list .item.on { background: var(--paper); border-color: var(--line); }
.chat-list .item .q { font-size: 13px; line-height: 1.35; }
.chat-list .item .meta { font-size: 11px; color: var(--muted); margin-top: 4px; }

.chat-main { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-stream { flex: 1; overflow-y: auto; padding: 22px 28px; }
.bubble { display: flex; margin-bottom: 14px; }
.bubble.user { justify-content: flex-end; }
.bubble .b {
  max-width: 78%; padding: 12px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.5; white-space: pre-wrap;
  background: var(--paper); border: 1px solid var(--line);
}
.bubble.user .b { background: var(--bg-3); border-color: transparent; }
.chat-input {
  padding: 14px 22px; border-top: 1px solid var(--line); background: var(--bg);
  display: flex; gap: 10px; align-items: flex-end;
}
.chat-input textarea {
  flex: 1; padding: 11px 14px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--paper); resize: none; outline: none; font-size: 14px; line-height: 1.4; max-height: 160px;
}
.chat-input textarea:focus { border-color: var(--ink); }

.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing span { width: 6px; height: 6px; border-radius: 999px; background: var(--muted); animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

/* Mobile bottom tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--paper); border-top: 1px solid var(--line);
  display: flex; justify-content: space-around; align-items: stretch;
}
.tabbar button {
  flex: 1; border: 0; background: transparent; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 4px; color: var(--muted); padding: 8px 0;
  font-size: 10.5px; position: relative;
}
.tabbar button.on { color: var(--ink); }
.tabbar button .badge {
  position: absolute; top: 6px; right: 25%;
  min-width: 16px; height: 16px; padding: 0 5px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 9.5px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
}

/* ───────── Responsive ───────── */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .topbar { padding: 14px 18px; }
  .topbar h1 { font-size: 18px; }
  .content { padding: 18px 18px calc(var(--tabbar-h) + 24px + var(--safe-bottom)); }
  .grid-2 { grid-template-columns: 1fr; }
  .chat-page.with-list { grid-template-columns: 1fr; }
  .chat-page .chat-list { display: none; }
  .chat-stream { padding: 18px; }
  .chat-input { padding: 12px 16px calc(12px + var(--safe-bottom)); }
}
@media (min-width: 901px) {
  .tabbar { display: none; }
}

/* Selector poble */
.poble-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-top: 18px; }
.poble-card {
  padding: 18px; border-radius: 16px; background: var(--paper); border: 1px solid var(--line);
  cursor: pointer; transition: all .12s;
}
.poble-card:hover { border-color: var(--ink); }
.poble-card .nm { font-family: 'Newsreader', serif; font-size: 18px; font-weight: 600; }
.poble-card .meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Reports */
.report-row {
  display: flex; gap: 12px; align-items: center; padding: 12px 14px;
  border-radius: 12px; background: var(--paper); border: 1px solid var(--line);
  margin-bottom: 8px; cursor: pointer; transition: border-color .12s;
}
.report-row:hover { border-color: var(--ink); }
.report-row .body { flex: 1; min-width: 0; }
.report-row .body .t { font-size: 13.5px; font-weight: 600; }
.report-row .body .m { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

/* Empty/Loading */
.empty {
  text-align: center; padding: 32px 16px; color: var(--muted); font-size: 13px;
  border: 1px dashed var(--line); border-radius: 14px; background: var(--paper);
}

/* Agent layout */
.agent-layout {
  display: grid; grid-template-columns: 1fr 1fr; height: calc(100vh - var(--topbar-h)); height: calc(100dvh - var(--topbar-h));
}
.agent-layout.solo { grid-template-columns: 1fr; }
.agent-chat { display: flex; flex-direction: column; min-width: 0; background: var(--bg); border-right: 1px solid var(--line); }
.agent-doc { display: flex; flex-direction: column; min-width: 0; background: var(--paper); }
.agent-doc-head {
  padding: 14px 18px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.agent-doc-body {
  flex: 1; overflow-y: auto; padding: 22px 26px; font-size: 14.5px; line-height: 1.7;
  white-space: pre-wrap; font-family: 'Newsreader', Georgia, serif;
}

@media (max-width: 900px) {
  .agent-layout { grid-template-columns: 1fr; height: calc(100dvh - var(--topbar-h) - var(--tabbar-h)); }
  .agent-doc { display: none; }
  .agent-layout.show-doc .agent-chat { display: none; }
  .agent-layout.show-doc .agent-doc { display: flex; }
}

/* List rows amb accions */
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 12px;
  margin-bottom: 8px;
}
.row .body { flex: 1; min-width: 0; }
.row .title { font-size: 13.5px; font-weight: 500; line-height: 1.35; }
.row .meta { font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.row .actions { display: flex; gap: 6px; }
.row.done { opacity: .55; }
.row.done .title { text-decoration: line-through; }

.iconmini-btn {
  width: 32px; height: 32px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--paper); display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.iconmini-btn:hover { background: var(--bg-2); }
.iconmini-btn.danger { color: var(--danger); }
.iconmini-btn.danger:hover { background: #FBE2D5; }

/* Form inline */
.form-inline {
  display: grid; gap: 10px; padding: 16px; background: var(--paper);
  border: 1px solid var(--line); border-radius: 14px; margin-bottom: 14px;
}
.form-inline.cols { grid-template-columns: 1fr 1fr; }
.form-inline input, .form-inline textarea, .form-inline select {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); outline: none; font-size: 13.5px;
}
.form-inline label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .1em; }
.form-inline .row-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 8px; }

/* Modal/Drawer */
.drawer-bg {
  position: fixed; inset: 0; z-index: 50; background: rgba(26, 23, 20, .42);
  display: flex; align-items: stretch; justify-content: flex-end;
}
.drawer {
  width: 100%; max-width: 540px; background: var(--bg); overflow-y: auto;
  padding: 24px; animation: drawerIn .2s ease both;
}
@keyframes drawerIn { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (max-width: 600px) {
  .drawer-bg { align-items: flex-end; }
  .drawer { max-width: 100%; max-height: 90vh; border-top-left-radius: 22px; border-top-right-radius: 22px; }
  @keyframes drawerIn { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }
}
.drawer .close { float: right; }
