:root {
  color-scheme: light;
  --ink: #172033;
  --muted: #6e7582;
  --navy: #10182b;
  --navy-soft: #1b263e;
  --paper: #f7f5ef;
  --white: #fffefa;
  --accent: #f16b3a;
  --accent-dark: #d95426;
  --line: #dedbd2;
  --shadow: 0 18px 55px rgba(22, 27, 39, .11);

  /* Tokens derivados para poder invertir el tema sin tocar cada regla. */
  --sidebar-bg: var(--navy);
  --sidebar-text: #f5f2e9;
  --sidebar-muted: #9199a9;
  --sidebar-line: #293248;
  --sidebar-item: #aeb5c2;
  --sidebar-item-hover-bg: #171f33;
  --sidebar-item-active-bg: var(--navy-soft);
  --panel-bg: var(--paper);
  --topbar-bg: rgba(247, 245, 239, .9);
  --icon-hover-bg: #ece9e1;
  --composer-bg: var(--white);
  --composer-line: #d3cfc5;
  --code-inline-bg: #e9e5dc;
  --chip-bg: rgba(255, 255, 255, .36);
}

[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e7e9ee;
  --muted: #9aa2b1;
  --navy: #0c111c;
  --navy-soft: #1a2334;
  --paper: #12161f;
  --white: #1b212d;
  --accent: #f4794c;
  --accent-dark: #e0663a;
  --line: #2a3140;
  --shadow: 0 18px 55px rgba(0, 0, 0, .45);

  --sidebar-bg: #0c111c;
  --sidebar-text: #e7e9ee;
  --sidebar-muted: #7c8698;
  --sidebar-line: #232c3c;
  --sidebar-item: #a9b1c1;
  --sidebar-item-hover-bg: #1a2334;
  --sidebar-item-active-bg: #202a3d;
  --panel-bg: #12161f;
  --topbar-bg: rgba(18, 22, 31, .9);
  --icon-hover-bg: #222b3a;
  --composer-bg: #1b212d;
  --composer-line: #313a4b;
  --code-inline-bg: #2a3242;
  --chip-bg: rgba(255, 255, 255, .04);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--panel-bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, textarea, input { font: inherit; }
button { cursor: pointer; }

.app-shell { display: grid; grid-template-columns: 300px minmax(0, 1fr); height: 100dvh; overflow: hidden; }
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 27px 20px 22px;
  color: var(--sidebar-text);
  background: var(--sidebar-bg);
}
.brand-row { display: flex; align-items: center; gap: 12px; padding: 0 8px 25px; }
.brand-row strong { display: block; font-family: Georgia, serif; font-size: 25px; letter-spacing: .2px; }
.brand-row small, .sidebar-foot small { display: block; margin-top: 2px; color: var(--sidebar-muted); font-size: 11px; letter-spacing: .07em; text-transform: uppercase; }
.brand-mark { position: relative; width: 37px; height: 37px; border: 1px solid #495166; border-radius: 50%; }
.brand-mark::before { content: ""; position: absolute; inset: 7px; border-radius: 50%; background: var(--accent); }
.brand-mark span { position: absolute; z-index: 1; top: 4px; right: 1px; width: 19px; height: 26px; border-radius: 50%; background: var(--sidebar-bg); transform: rotate(-18deg); }
.new-chat {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 12px 15px; border: 1px solid #3a4359; border-radius: 9px;
  color: var(--sidebar-text); background: transparent; transition: .2s ease;
}
.new-chat:hover { border-color: var(--accent); background: rgba(241, 107, 58, .08); }
.new-chat span { color: var(--accent); font-size: 23px; line-height: 15px; }
.history-label { margin: 29px 10px 10px; color: var(--sidebar-muted); font-size: 10px; font-weight: 700; letter-spacing: .15em; text-transform: uppercase; }
.session-list { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: thin; scrollbar-color: #3b455b transparent; }
.session-item {
  width: 100%; margin: 2px 0; padding: 12px 11px; border: 0; border-radius: 8px;
  color: var(--sidebar-item); background: transparent; text-align: left;
}
.session-item:hover { color: var(--sidebar-text); background: var(--sidebar-item-hover-bg); }
.session-item.active { color: var(--sidebar-text); background: var(--sidebar-item-active-bg); box-shadow: inset 3px 0 var(--accent); }
.session-item strong { display: block; overflow: hidden; font-size: 13px; font-weight: 550; text-overflow: ellipsis; white-space: nowrap; }
.session-item span { display: block; margin-top: 4px; color: var(--sidebar-muted); font-size: 11px; }
.sidebar-foot { display: flex; align-items: center; gap: 10px; padding: 18px 8px 0; border-top: 1px solid var(--sidebar-line); }
.sidebar-foot strong { display: block; overflow: hidden; max-width: 205px; font-size: 12px; font-weight: 550; text-overflow: ellipsis; white-space: nowrap; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #63c18c; box-shadow: 0 0 0 4px rgba(99, 193, 140, .1); }

.chat-panel { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-width: 0; min-height: 0; height: 100dvh; overflow: hidden; background: var(--panel-bg); }
.topbar { display: flex; align-items: center; gap: 14px; min-height: 84px; padding: 17px clamp(22px, 4vw, 62px); border-bottom: 1px solid var(--line); background: var(--topbar-bg); }
.topbar > .title-block { min-width: 0; flex: 1; }
.topbar h1 { overflow: hidden; margin: 0; font-family: Georgia, serif; font-size: clamp(20px, 2.4vw, 27px); font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.topbar p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.icon-button { display: grid; place-items: center; width: 38px; height: 38px; padding: 0; border: 0; border-radius: 8px; color: var(--muted); background: transparent; }
.icon-button:hover { color: var(--ink); background: var(--icon-hover-bg); }
.icon-button svg { width: 20px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.7; }
.icon-button.active { color: var(--accent); background: rgba(241, 107, 58, .12); }
.menu-button, .close-sidebar { display: none; }
.delete-button:disabled { opacity: .35; cursor: default; }

.messages { min-height: 0; overflow-y: auto; padding: 35px clamp(22px, 6vw, 90px) 25px; scroll-behavior: smooth; }
.messages-inner { width: min(850px, 100%); margin: 0 auto; }
.welcome { display: grid; place-content: center; min-height: 100%; text-align: center; animation: rise .45s ease both; }
.welcome-orbit { position: relative; width: 82px; height: 82px; margin: 0 auto 24px; border: 1px solid #ccc7ba; border-radius: 50%; }
.welcome-orbit::before { content: ""; position: absolute; inset: 15px; border-radius: 50%; background: var(--accent); box-shadow: 0 12px 28px rgba(241, 107, 58, .22); }
.welcome-orbit::after { content: ""; position: absolute; top: 7px; right: 7px; width: 41px; height: 57px; border-radius: 50%; background: var(--panel-bg); transform: rotate(-20deg); }
.welcome h2 { margin: 0; font-family: Georgia, serif; font-size: clamp(32px, 5vw, 48px); font-weight: 400; letter-spacing: -.02em; }
.welcome p { max-width: 500px; margin: 13px auto 0; color: var(--muted); line-height: 1.65; }
.suggestions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: min(670px, 100%); margin: 34px auto 0; }
.suggestion { padding: 14px 15px; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: var(--chip-bg); font-size: 12px; line-height: 1.4; text-align: left; transition: .2s ease; }
.suggestion:hover { border-color: #bab4a7; background: var(--white); transform: translateY(-2px); box-shadow: 0 8px 20px rgba(20,25,36,.06); }
.message { display: grid; grid-template-columns: 34px minmax(0, 1fr); gap: 13px; margin-bottom: 29px; animation: rise .3s ease both; }
.avatar { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; color: white; background: var(--navy); font-family: Georgia, serif; font-size: 14px; }
.message.user .avatar { background: var(--accent); }
.message-head { display: flex; align-items: baseline; gap: 9px; margin: 1px 0 7px; }
.message-head strong { font-size: 13px; }
.message-head time { color: var(--muted); font-size: 10px; }
.message-body { color: var(--ink); font-size: 15px; line-height: 1.7; overflow-wrap: anywhere; }
.message-body p { margin: 0 0 11px; }
.message-body p:last-child { margin-bottom: 0; }
.message-body pre { overflow-x: auto; padding: 15px; border-radius: 8px; color: #e8eaf0; background: #151d2e; font: 12px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; }
.message-body code:not(pre code) { padding: 2px 5px; border-radius: 4px; background: var(--code-inline-bg); font: 13px ui-monospace, SFMono-Regular, Consolas, monospace; }
.message-body a { color: var(--accent-dark); }
.message-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 10px; }
.attachment-chip { display: inline-flex; align-items: center; gap: 7px; max-width: 260px; padding: 7px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--chip-bg); font-size: 12px; }
.attachment-chip svg { width: 15px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; opacity: .7; }
.attachment-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-chip img { width: 34px; height: 34px; border-radius: 5px; object-fit: cover; }
.artifact { margin: 14px 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--white); box-shadow: 0 6px 18px rgba(20,25,36,.06); }
.artifact-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px 8px 13px; border-bottom: 1px solid var(--line); background: var(--panel-bg); }
.artifact-label { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.artifact-label svg { width: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.artifact-actions { display: flex; gap: 6px; }
.artifact-frame { display: block; width: 100%; height: 420px; border: 0; background: white; }
.mini-button { padding: 5px 11px; border: 1px solid var(--line); border-radius: 7px; color: var(--muted); background: transparent; font-size: 11px; font-weight: 600; transition: .15s ease; }
.mini-button:hover { color: var(--ink); border-color: #bab4a7; background: var(--icon-hover-bg); }
.message-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; opacity: 0; transition: opacity .2s ease; }
.message.assistant:hover .message-actions, .message.assistant:focus-within .message-actions { opacity: 1; }
@media (hover: none) { .message-actions { opacity: 1; } }
.typing { display: flex; gap: 5px; padding-top: 10px; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: #8c9099; animation: pulse 1s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
.sources { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); }
.sources-label { color: var(--muted); font-size: 10px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; }
.sources ol { margin: 7px 0 0; padding-left: 18px; }
.sources li { margin: 3px 0; font-size: 12px; }

.composer-wrap { min-width: 0; padding: 12px clamp(22px, 6vw, 90px) 17px; background: linear-gradient(transparent, var(--panel-bg) 18%); }
.composer-tools { display: flex; align-items: center; gap: 8px; width: min(850px, 100%); margin: 0 auto 8px; }
.tool-toggle { display: inline-flex; align-items: center; gap: 7px; padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); background: transparent; font-size: 12px; transition: .2s ease; }
.tool-toggle:hover { color: var(--ink); border-color: #bab4a7; }
.tool-toggle.active { color: white; border-color: var(--accent); background: var(--accent); }
.tool-toggle svg { width: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.pending-attachments { display: flex; flex-wrap: wrap; gap: 8px; width: min(850px, 100%); margin: 0 auto 8px; }
.pending-attachments:empty { display: none; }
.pending-chip { display: inline-flex; align-items: center; gap: 8px; padding: 6px 8px 6px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--white); font-size: 12px; }
.pending-chip.error { border-color: #c8503f; color: #c8503f; }
.pending-chip span { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pending-chip button { display: grid; place-items: center; width: 18px; height: 18px; padding: 0; border: 0; border-radius: 50%; color: var(--muted); background: transparent; font-size: 15px; }
.pending-chip button:hover { color: var(--ink); background: var(--icon-hover-bg); }
.composer { display: flex; align-items: flex-end; gap: 10px; width: min(850px, 100%); margin: 0 auto; padding: 10px 10px 10px 12px; border: 1px solid var(--composer-line); border-radius: 13px; background: var(--composer-bg); box-shadow: var(--shadow); }
.composer:focus-within { border-color: #aaa497; box-shadow: 0 18px 55px rgba(22,27,39,.13), 0 0 0 3px rgba(241,107,58,.08); }
.composer .attach-button { display: grid; flex: 0 0 auto; place-items: center; width: 42px; height: 42px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; }
.composer .attach-button:hover { color: var(--ink); background: var(--icon-hover-bg); }
.composer .attach-button svg { width: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.composer textarea { flex: 1; max-height: 160px; padding: 8px 0; resize: none; border: 0; outline: 0; color: var(--ink); background: transparent; line-height: 1.45; }
.composer textarea::placeholder { color: var(--muted); }
.composer .send-button { display: grid; flex: 0 0 auto; place-items: center; width: 42px; height: 42px; border: 0; border-radius: 9px; color: white; background: var(--accent); transition: .2s ease; }
.composer .send-button:hover { background: var(--accent-dark); transform: translateY(-1px); }
.composer .send-button:disabled { opacity: .5; cursor: default; transform: none; }
.composer .send-button svg { width: 21px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.composer-hint { margin: 8px 0 0; color: var(--muted); font-size: 10px; text-align: center; }
.scrim { display: none; }
.toast { position: fixed; z-index: 20; right: 24px; bottom: 24px; max-width: 360px; padding: 12px 16px; border-radius: 8px; color: white; background: #9b342c; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transform: translateY(10px); transition: .25s ease; }
.toast.show { opacity: 1; transform: translateY(0); }

.dialog { width: min(540px, calc(100% - 32px)); max-height: calc(100dvh - 48px); padding: 0; border: 0; border-radius: 14px; color: var(--ink); background: var(--white); box-shadow: 0 28px 80px rgba(9,14,25,.28); }
.dialog::backdrop { background: rgba(9,14,25,.58); backdrop-filter: blur(3px); }
.dialog form, .dialog .dialog-inner { padding: 28px; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 26px; }
.dialog-head span { color: var(--accent); font-size: 10px; font-weight: 800; letter-spacing: .16em; text-transform: uppercase; }
.dialog-head h2 { margin: 4px 0 0; font-family: Georgia, serif; font-size: 27px; font-weight: 500; }
.dialog-head .icon-button { font-size: 25px; }
.dialog label { display: flex; justify-content: space-between; margin: 18px 0 7px; font-size: 12px; font-weight: 700; }
.dialog label span { color: var(--muted); font-size: 10px; font-weight: 500; text-transform: uppercase; }
.dialog input[type="text"], .dialog input:not([type]), .dialog input[type="password"] { width: 100%; height: 45px; padding: 0 13px; border: 1px solid var(--line); border-radius: 8px; outline: none; color: var(--ink); background: var(--panel-bg); }
.dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(241,107,58,.09); }
.field-help { margin: 7px 0 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 18px; }
.field-row > div { min-width: 0; }
.field-row strong { display: block; font-size: 13px; }
.field-row small { color: var(--muted); font-size: 11px; }
.switch { position: relative; flex: 0 0 auto; width: 46px; height: 26px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: .2s ease; }
.switch-track::before { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: white; transition: .2s ease; }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::before { transform: translateX(20px); }
.secret-field { position: relative; }
.secret-field input { padding-right: 75px; }
.secret-field button { position: absolute; top: 5px; right: 5px; height: 35px; padding: 0 10px; border: 0; color: var(--accent-dark); background: transparent; font-size: 11px; font-weight: 700; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 27px; padding-top: 19px; border-top: 1px solid var(--line); }
.dialog-actions button { padding: 10px 15px; border-radius: 8px; font-size: 12px; font-weight: 700; }
.secondary-button { border: 1px solid var(--line); color: var(--ink); background: transparent; }
.primary-button { border: 1px solid var(--accent); color: white; background: var(--accent); }
.primary-button:hover { background: var(--accent-dark); }

.sources-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; max-height: 320px; overflow-y: auto; }
.sources-list:empty::after { content: "Aún no has añadido fuentes."; color: var(--muted); font-size: 12px; }
.source-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; }
.source-row > div { min-width: 0; flex: 1; }
.source-row strong { display: block; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.source-row small { color: var(--muted); font-size: 11px; }
.source-row button { flex: 0 0 auto; }
.upload-drop { margin-top: 10px; padding: 22px; border: 1.5px dashed var(--line); border-radius: 10px; color: var(--muted); text-align: center; font-size: 13px; transition: .2s ease; }
.upload-drop.drag { border-color: var(--accent); color: var(--ink); background: rgba(241,107,58,.06); }
.upload-drop button { margin-top: 8px; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
@keyframes pulse { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

@media (max-width: 760px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; z-index: 12; inset: 0 auto 0 0; width: min(310px, 86vw); transform: translateX(-100%); transition: transform .25s ease; }
  .sidebar.open { transform: translateX(0); }
  .close-sidebar { display: grid; margin-left: auto; color: var(--sidebar-item); font-size: 25px; }
  .close-sidebar:hover { color: var(--sidebar-text); background: #202a40; }
  .menu-button { display: grid; }
  .scrim { position: fixed; z-index: 11; inset: 0; display: block; background: rgba(9,14,25,.45); opacity: 0; pointer-events: none; transition: opacity .25s ease; }
  .scrim.show { opacity: 1; pointer-events: auto; }
  .chat-panel { height: 100dvh; }
  .topbar { min-height: 72px; padding: 12px 14px; }
  .messages { padding: 25px 16px 18px; }
  .composer-wrap { padding: 10px 12px 12px; }
  .composer-hint { display: none; }
  .suggestions { grid-template-columns: 1fr; margin-top: 25px; }
  .suggestion:nth-child(3) { display: none; }
  .message { grid-template-columns: 30px minmax(0, 1fr); gap: 10px; }
  .avatar { width: 30px; height: 30px; }
}
