:root {
  --bg: #f5f7fa; --surface: #ffffff; --surface-2: #eef2f7; --text: #172033; --muted: #5b6678;
  --border: #d9e0ea; --primary: #0b5cad; --primary-ink: #ffffff; --accent: #e8f1fb;
  --danger: #b42318; --ok: #1f7a4d; --warn: #b26b00; --radius: 12px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 4px 16px rgba(16,24,40,.06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c; --surface: #161d28; --surface-2: #1d2633; --text: #e6ebf2; --muted: #9aa6b8;
    --border: #2a3545; --primary: #4b9bea; --primary-ink: #08131f; --accent: #1a2b40;
    --danger: #f97066; --ok: #47c98a; --warn: #f5b13d; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-size: 15px; line-height: 1.5; }
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.25rem; } h2 { font-size: 1.1rem; margin-top: 1.5rem; } h3 { font-size: 1rem; }
.muted { color: var(--muted); } .small { font-size: .85rem; } .error { color: var(--danger); min-height: 1.2em; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .55rem .7rem; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: .5rem .9rem; }
a.button { display: inline-block; text-decoration: none; border: 1px solid var(--border); border-radius: 8px; padding: .5rem .9rem; }
button.primary, a.button.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 600; }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.link { border: none; background: none; color: var(--primary); padding: .2rem 0; }
button:disabled { opacity: .55; cursor: default; }
.wide { width: 100%; }
.row { display: flex; gap: .5rem; align-items: center; } .row.end { justify-content: flex-end; } .row.wrap { flex-wrap: wrap; }
.row > input { flex: 1; }
.stack { display: flex; flex-direction: column; gap: .75rem; }
label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.notice { background: var(--accent); border-radius: 8px; padding: .6rem .8rem; white-space: pre-wrap; }
.sub-banner { margin: .5rem 1rem 0; }
.pw-wrap { position: relative; display: block; }
.pw-wrap input { width: 100%; padding-right: 2.6rem; }
.pw-toggle { position: absolute; right: .3rem; top: 50%; transform: translateY(-50%); border: none; background: none; padding: .3rem; color: var(--muted, #667); line-height: 0; }
.pw-toggle:hover, .pw-toggle:focus-visible { color: var(--primary); }
.sub-banner.danger { background: #fde8e8; color: #8a1c1c; }
.badge { display: inline-block; padding: .1rem .45rem; border-radius: 99px; font-size: .78rem; background: var(--accent); white-space: nowrap; }
.badge.ok { background: #e3f4e8; color: #1d6b38; }
.badge.warn { background: #fff3d6; color: #7a5300; }
.badge.bad { background: #fde8e8; color: #8a1c1c; }
@media (prefers-color-scheme: dark) {
  .sub-banner.danger, .badge.bad { background: #4a1d1d; color: #ffd0d0; }
  .badge.ok { background: #173a24; color: #bfeccc; }
  .badge.warn { background: #43350f; color: #ffe3a3; }
}

.center-view { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(400px, 100%); padding: 1.75rem; display: flex; flex-direction: column; gap: 1rem; }
.login-card p { margin: 0; }
.login-card .error:empty, .login-card [role="status"]:empty { display: none; }
.brand { display: flex; gap: .75rem; align-items: center; }
.brand p { margin: 0; }
.logo { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; background: var(--primary); color: var(--primary-ink); font-weight: 700; }
.small-brand .logo { width: 30px; height: 30px; font-size: .8rem; border-radius: 8px; }

dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); width: min(420px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }

.layout { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; gap: .75rem; padding: 1rem; min-height: 0; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv-item { display: flex; align-items: center; gap: .25rem; border-radius: 8px; }
.conv-item button.open { flex: 1; text-align: left; border: none; background: none; padding: .5rem .6rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item.active, .conv-item:hover { background: var(--surface-2); }
.conv-item .del { border: none; background: none; color: var(--muted); padding: .2rem .4rem; visibility: hidden; }
.conv-item:hover .del { visibility: visible; }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: .75rem; display: flex; flex-direction: column; gap: .25rem; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.topbar { display: flex; align-items: center; gap: 1rem; padding: .5rem 1rem; border-bottom: 1px solid var(--border); background: var(--surface); }
.tabs { display: flex; gap: .25rem; overflow-x: auto; }
.tab { border: none; background: none; padding: .45rem .8rem; border-radius: 8px; color: var(--muted); white-space: nowrap; }
.tab.active { background: var(--accent); color: var(--primary); font-weight: 600; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: .45rem .6rem; display: inline-grid; place-items: center; cursor: pointer; line-height: 1; }
#toggle-sidebar { display: none; }
.meter { margin-left: auto; display: flex; align-items: center; gap: .5rem; min-width: 0; }
.meter-bar { width: 120px; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.meter-bar span { display: block; height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.meter.low .meter-bar span { background: var(--warn); } .meter.blocked .meter-bar span { background: var(--danger); }

.tab-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.scroll { overflow-y: auto; }
.panel { max-width: 960px; width: 100%; margin: 0 auto; padding: 1.25rem 16px 3rem; }

.messages { flex: 1; overflow-y: auto; padding: 1.25rem 16px; display: flex; flex-direction: column; gap: 1rem; }
.msg { max-width: 860px; width: 100%; margin: 0 auto; display: flex; }
.msg .bubble { padding: .75rem 1rem; border-radius: var(--radius); overflow-x: auto; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--primary); color: var(--primary-ink); max-width: 80%; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); width: 100%; }
.bubble table { border-collapse: collapse; margin: .5rem 0; font-size: .9rem; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: .35rem .55rem; text-align: left; vertical-align: top; }
.bubble th { background: var(--surface-2); }
.bubble p { margin: .4rem 0; } .bubble ul, .bubble ol { margin: .3rem 0; padding-left: 1.3rem; }
.bubble h3, .bubble h4 { margin: .8rem 0 .3rem; }
.bubble code { background: var(--surface-2); padding: 0 .25rem; border-radius: 4px; }
.status-line { color: var(--muted); font-size: .85rem; font-style: italic; }
.empty { max-width: 720px; margin: auto; text-align: center; padding: 2rem 0; }
.suggestions { display: flex; flex-direction: column; gap: .5rem; margin-top: 1rem; }
.chip { text-align: left; border-radius: 10px; background: var(--surface); }

.composer { border-top: 1px solid var(--border); background: var(--surface); padding: .75rem 16px; }
.composer-row { display: flex; gap: .5rem; align-items: flex-end; max-width: 860px; margin: 0 auto; }
.composer textarea { flex: 1; resize: none; max-height: 200px; }
#composer-status { max-width: 860px; margin: .35rem auto 0; min-height: 1em; }
#mic.recording { background: var(--danger); color: #fff; border-color: var(--danger); }
.attach-list { max-width: 860px; margin: 0 auto .4rem; display: flex; flex-wrap: wrap; gap: .4rem; }
.attach-list:empty { display: none; }
.attach { background: var(--surface-2); border-radius: 99px; padding: .2rem .6rem; font-size: .85rem; display: flex; gap: .3rem; align-items: center; }
.attach button { border: none; background: none; padding: 0 .2rem; }

.list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.list li { display: flex; justify-content: space-between; gap: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .55rem .75rem; }
.table-wrap { overflow-x: auto; margin: .5rem 0 1rem; }
.table-wrap table { width: 100%; border-collapse: collapse; background: var(--surface); font-size: .9rem; }
.table-wrap th, .table-wrap td { border-bottom: 1px solid var(--border); padding: .45rem .6rem; text-align: left; white-space: nowrap; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; margin: 1rem 0; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem; }
.kpi .v { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.packages { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: .75rem; }
.package { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; display: flex; flex-direction: column; gap: .5rem; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 110px; padding: .5rem 0; border-bottom: 1px solid var(--border); }
.bars div { flex: 1; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px; }

.between { justify-content: space-between; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: .75rem; align-items: end; }
.fs { padding: 1rem; border: 1px solid var(--border); }
.fs legend { font-weight: 600; padding: 0 .4rem; }
label.check { flex-direction: row; align-items: center; gap: .5rem; }
.ing-row { border: 1px dashed var(--border); border-radius: 10px; padding: .75rem; margin-bottom: .75rem; }
.ing-row header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .5rem; font-weight: 600; }
.kpi.good .v { color: var(--ok); } .kpi.bad .v { color: var(--danger); }
.result-table td:last-child, .result-table th:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.result-table tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
.badge { display: inline-block; background: var(--accent); color: var(--primary); border-radius: 99px; padding: 0 .5rem; font-size: .75rem; margin-left: .3rem; }
@media print {
  .sidebar, .topbar, #renta-form, .tabs, #r-print { display: none !important; }
  .layout { display: block; } .tab-panel { overflow: visible; }
}

@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 30% 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  #toggle-sidebar { display: inline-grid; }
  .meter-bar { width: 60px; }
  #meter-label { display: none; }
  .msg.user .bubble { max-width: 92%; }
}
