/* ===== Asistente Casa Kiwi ===== */
:root {
  --bg: #F3F5EE;          /* pulpa de kiwi muy pálida */
  --surface: #FFFFFF;
  --ink: #202020;
  --ink-2: #5B6152;
  --line: #DDE2D2;
  --green: #31572C;       /* mensajes del usuario, marca */
  --kiwi: #C9DB32;        /* acento: actividad y selección */
  --orange: #F15A3A;      /* acción principal */
  --orange-ink: #FFFFFF;
  --error: #B42318;
  --k-skin: #6B4F2A; --k-flesh: #C9DB32; --k-seed: #202020; --k-core: #F3F5EE;
  --s1: #3F7A36; --s2: #2A86B8;   /* series de gráfico (validadas CVD) */
  --radius-bubble: 18px;
  --font: "Bricolage Grotesque", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171B14; --surface: #20261C; --ink: #ECEFE4; --ink-2: #A3AB96; --line: #323A2B;
    --green: #3E6B37; --kiwi: #C9DB32; --orange: #F26B4E;
    --k-skin: #8A6A3D; --k-core: #20261C; --error: #F97066;
    --s1: #7A9F35; --s2: #2A86B8;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 400 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--kiwi); outline-offset: 2px; border-radius: 6px; }
.muted { color: var(--ink-2); }
svg { flex: none; }
.icon-btn svg, .btn-new svg, .send svg, .mic svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ----- Kiwi ----- */
.kiwi-xs { width: 22px; height: 22px; }
.kiwi-sm { width: 56px; height: 56px; }
.kiwi-hero { width: 132px; height: 132px; margin-bottom: 8px; }
@media (prefers-reduced-motion: no-preference) {
  .welcome .kiwi-hero { animation: roll .9s cubic-bezier(.2,.8,.2,1) both; }
  @keyframes roll { from { transform: rotate(-120deg) scale(.6); opacity: 0; } to { transform: none; opacity: 1; } }
}

/* ----- Login ----- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 24px; padding: 32px 28px; display: grid; gap: 14px;
}
.login-card h1 { font-size: 34px; line-height: 1.02; font-weight: 800; letter-spacing: -.02em; margin: 4px 0 0; }
.login-card p { margin: 0 0 6px; }
.login-card label { display: grid; gap: 6px; font-weight: 500; font-size: 14px; }
.login-card input {
  border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 12px 14px; font-size: 16px;
}
.error { color: var(--error); margin: 0; font-size: 14px; }
.btn-primary {
  border: 0; border-radius: 12px; padding: 13px; background: var(--orange); color: var(--orange-ink);
  font-weight: 700; font-size: 16px; margin-top: 6px;
}
.btn-primary:disabled { opacity: .6; }

/* ----- Layout ----- */
.app { height: 100dvh; display: grid; grid-template-columns: 280px 1fr; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 18px 10px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 19px; letter-spacing: -.01em; }
.btn-new {
  display: flex; align-items: center; gap: 8px; margin: 6px 14px 10px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: transparent; font-weight: 500;
}
.btn-new:hover { border-color: var(--green); }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv {
  display: flex; align-items: center; gap: 4px; border-radius: 10px;
}
.conv a {
  flex: 1; min-width: 0; padding: 9px 10px; color: inherit; text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px;
}
.conv:hover { background: var(--bg); }
.conv.active { background: color-mix(in srgb, var(--kiwi) 35%, transparent); }
.conv .del { opacity: 0; border: 0; background: none; color: var(--ink-2); padding: 6px 8px; border-radius: 8px; }
.conv:hover .del, .conv .del:focus-visible { opacity: 1; }
.conv-group { font-size: 13px; color: var(--ink-2); padding: 14px 10px 4px; }
.sidebar-foot {
  border-top: 1px solid var(--line); padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 14px;
}
.sidebar-foot div { display: grid; }
.link-btn { border: 0; background: none; color: var(--ink-2); text-decoration: underline; padding: 6px; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.topbar h2 { flex: 1; margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center; color: var(--ink);
}
.icon-btn:hover { background: color-mix(in srgb, var(--ink) 7%, transparent); }
.only-mobile { display: none; }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 9; }

/* ----- Mensajes ----- */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px 12px; scroll-behavior: smooth; }
.messages > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.welcome { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 12px 0 24px; }
.welcome h1 { font-size: clamp(38px, 9vw, 64px); line-height: .98; font-weight: 800; letter-spacing: -.035em; margin: 6px 0 8px; }
.welcome p { margin: 0 0 26px; font-size: 18px; }
.chips { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: 100%; }
.chip {
  text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 14px;
  padding: 14px 16px; font-size: 15px; line-height: 1.35;
}
.chip:hover { border-color: var(--green); }

.msg { margin-bottom: 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--green); color: #fff; border-radius: var(--radius-bubble) var(--radius-bubble) 6px var(--radius-bubble);
  padding: 10px 15px; max-width: 85%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant { display: grid; grid-template-columns: 28px 1fr; gap: 12px; align-items: start; }
.msg.assistant .kiwi { width: 28px; height: 28px; margin-top: 2px; }
.msg .body { min-width: 0; overflow-wrap: anywhere; }
.msg .body > :first-child { margin-top: 0; }
.msg .body > :last-child { margin-bottom: 0; }
.msg .body p { margin: 0 0 10px; }
.msg .body ul, .msg .body ol { margin: 0 0 10px; padding-left: 22px; }
.msg .body li { margin: 2px 0; }
.msg .body h3, .msg .body h4 { font-size: 17px; margin: 16px 0 6px; font-weight: 700; }
.msg .body code { font-size: .92em; background: color-mix(in srgb, var(--ink) 8%, transparent); padding: 1px 5px; border-radius: 5px; }
.msg .body a { color: var(--green); }
@media (prefers-color-scheme: dark) { .msg .body a { color: var(--kiwi); } }
.table-wrap { overflow-x: auto; margin: 4px 0 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.msg table { border-collapse: collapse; width: 100%; font-size: 14px; font-variant-numeric: tabular-nums; }
.msg th, .msg td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.msg th { font-weight: 600; color: var(--ink-2); background: color-mix(in srgb, var(--kiwi) 14%, transparent); }
.msg tr:last-child td { border-bottom: 0; }
.tools-used { margin-top: 8px; font-size: 13px; color: var(--ink-2); }
.tools-used .bad { color: var(--error); }

.thinking { display: flex; align-items: center; gap: 10px; color: var(--ink-2); font-size: 15px; }
.thinking .kiwi { width: 28px; height: 28px; }
@media (prefers-reduced-motion: no-preference) {
  .thinking .kiwi { animation: spin 1.6s linear infinite; }
  @keyframes spin { to { transform: rotate(360deg); } }
}
.msg.error .body { color: var(--error); }
.retry { margin-top: 6px; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 6px 12px; font-size: 14px; }

/* ----- Gráficos ----- */
.chart { margin: 6px 0 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.chart figcaption { font-weight: 700; margin-bottom: 6px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend .sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.chart-scroll { overflow-x: auto; }
.chart-svg { width: 100%; min-width: 320px; height: auto; display: block; }
.chart-svg .grid { stroke: var(--line); stroke-width: 1; }
.chart-svg .grid.base { stroke: var(--ink-2); }
.chart-svg .axis { fill: var(--ink-2); font-size: 11px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart-svg .bar:hover { opacity: .8; }
.s1 { fill: var(--s1); background: var(--s1); } .s2 { fill: var(--s2); background: var(--s2); }
.chart-total { margin: 6px 0 0; font-size: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.chart-data summary { cursor: pointer; font-size: 13px; color: var(--ink-2); margin-top: 6px; }
.msg .body pre.code { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px; overflow-x: auto; font-size: 13px; }
.tools-used .bad { display: block; }
.tools-used span.bad { display: inline; }

/* ----- Tarjeta de confirmación ----- */
.action-card {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--line);
  border-left: 4px solid var(--green); border-radius: 14px; padding: 14px 16px;
}
.action-card.danger { border-left-color: var(--orange); }
.action-card h4 { margin: 0 0 10px; font-size: 17px; }
.action-card dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; font-size: 15px; }
.action-card dt { color: var(--ink-2); }
.action-card dd { margin: 0; font-variant-numeric: tabular-nums; }
.action-card .warn {
  margin: 10px 0 0; padding: 8px 10px; border-radius: 10px; font-size: 14px;
  background: color-mix(in srgb, var(--orange) 12%, transparent);
}
.action-foot { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.btn-confirm {
  border: 0; border-radius: 12px; padding: 11px 18px; font-weight: 700;
  background: var(--green); color: #fff;
}
.action-card.danger .btn-confirm { background: var(--orange); }
.btn-discard { border: 1px solid var(--line); background: transparent; border-radius: 12px; padding: 11px 16px; }
.btn-confirm:disabled, .btn-discard:disabled { opacity: .6; }
.action-state { margin: 0; font-weight: 600; }
.action-state.done { color: var(--green); }
@media (prefers-color-scheme: dark) { .action-state.done { color: var(--kiwi); } }
.action-state.failed { color: var(--error); }
.action-state.discarded, .action-state.expired { color: var(--ink-2); }
@media (max-width: 480px) { .action-card dl { grid-template-columns: 1fr; gap: 0; } .action-card dd { margin-bottom: 6px; } }

/* ----- Composer ----- */
.composer { padding: 8px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer-box {
  max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 6px 6px 6px 16px;
}
.composer-box:focus-within { border-color: var(--green); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; padding: 9px 0;
  font-size: 16px; line-height: 1.45; max-height: 180px;
}
.send {
  width: 42px; height: 42px; border-radius: 14px; border: 0; background: var(--orange); color: var(--orange-ink);
  display: grid; place-items: center;
}
.send:disabled { opacity: .45; cursor: default; }
.mic.listening { background: color-mix(in srgb, var(--orange) 18%, transparent); color: var(--orange); }
.hint { max-width: 760px; margin: 6px auto 0; font-size: 12.5px; text-align: center; }

/* ----- Móvil ----- */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 10; inset: 0 auto 0 0; width: min(86vw, 320px);
    transform: translateX(-102%); transition: transform .22s ease;
  }
  .app.menu-open .sidebar { transform: none; }
  .only-mobile { display: grid; }
  .chips { grid-template-columns: 1fr; }
  .chip { padding: 12px 14px; }
  .kiwi-hero { width: 104px; height: 104px; }
  .conv .del { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } .messages { scroll-behavior: auto; } }
