:root {
  --bg: #0f0f12;
  --surface: #17171c;
  --surface-2: #1f1f26;
  --border: #2a2a33;
  --text: #ececf1;
  --muted: #8b8b97;
  --accent: #ff7a3d;
  --accent-2: #ffb347;
  --ok: #3ecf8e;
  --error: #ff5c5c;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 Inter, system-ui, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 24px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 16px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 12px 24px; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 10;
}
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { color: var(--muted); padding: 6px 12px; border-radius: 8px; }
.topbar nav a:hover, .topbar nav a.active { color: var(--text); background: var(--surface-2); }
.topbar .user { display: flex; align-items: center; gap: 12px; color: var(--muted); }
.brand { font-weight: 700; letter-spacing: .08em; color: var(--text); display: inline-flex; align-items: center; gap: 8px; }
.brand.big { font-size: 26px; }
.brand .dot { width: 12px; height: 12px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2)); }

.container { max-width: 1200px; margin: 0 auto; padding: 28px 24px; }
.page-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 20px; }

/* Cards / layout */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.grid-2 { display: grid; grid-template-columns: 1.6fr 1fr; gap: 20px; align-items: start; }
.grid-2.tight { grid-template-columns: 1fr 1fr; gap: 16px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; }
.row input { flex: 1; }
.settings { display: flex; flex-direction: column; gap: 20px; }
.settings .card > label, .settings .card .grid-2 { margin-bottom: 14px; }
.sticky-actions { position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 40%); }

/* Forms */
label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; }
input, select, textarea {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 12px; font: inherit; font-weight: 400;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
.check { display: inline-flex; gap: 6px; align-items: center; font-weight: 400; font-size: 12px; color: var(--muted); }
.check input { padding: 0; }
.test-result { font-size: 12px; font-weight: 500; }
.test-result.ok { color: var(--ok); }
.test-result.error { color: var(--error); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid transparent; border-radius: 8px;
  padding: 9px 16px; font: inherit; font-weight: 600; cursor: pointer;
  color: var(--text); background: var(--surface-2); white-space: nowrap;
}
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #1a0d05; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: transparent; border-color: #4a2626; color: var(--error); }
.btn.sm { padding: 5px 10px; font-size: 12px; }

/* Table */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 0 8px 10px; }
.table td { padding: 12px 8px; border-top: 1px solid var(--border); vertical-align: top; }
.table tr.inactive td { opacity: .5; }
.table .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.table .actions form { margin: 0; }
.badge { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.superadmin { color: var(--accent); background: rgba(255,122,61,.12); }
.badge.off { color: var(--error); }

/* Flash */
.flash { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; border: 1px solid; }
.flash.ok { border-color: rgba(62,207,142,.4); background: rgba(62,207,142,.08); }
.flash.error { border-color: rgba(255,92,92,.4); background: rgba(255,92,92,.08); }

/* Auth */
body.auth .container { min-height: 100vh; display: grid; place-items: center; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 32px; }
.auth-card .muted { margin: 4px 0 24px; }

.empty-state { text-align: center; padding: 80px 0; }

@media (max-width: 900px) {
  .grid-2, .grid-2.tight { grid-template-columns: 1fr; }
  .topbar { flex-wrap: wrap; gap: 12px; padding: 12px 16px; }
  .container { padding: 20px 16px; }
  .table .actions { justify-content: flex-start; }
}

/* ================= Estudio ================= */
body.studio .container { max-width: none; padding: 0; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.only-mobile { display: none; }
.pad { padding: 8px 12px; }

.studio-wrap { display: grid; grid-template-columns: 270px 1fr; height: calc(100vh - 57px); position: relative; }

.sidebar { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; padding: 14px; gap: 12px; min-height: 0; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 0 -6px; }
.conv { display: flex; align-items: center; gap: 6px; padding: 9px 10px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.conv:hover, .conv.active { background: var(--surface-2); color: var(--text); }
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-actions { display: none; gap: 2px; }
.conv:hover .conv-actions { display: flex; }
.conv-actions button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 2px 4px; font-size: 12px; }
.conv-actions button:hover { color: var(--text); }
.quota .bar { height: 4px; background: var(--surface-2); border-radius: 4px; margin-top: 6px; overflow: hidden; }
.quota .bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); }

.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 12px 24px; border-bottom: 1px solid var(--border); }
.chat-head h2 { margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { background: var(--accent); color: #1a0d05; border-radius: 99px; padding: 0 7px; margin-left: 6px; font-size: 11px; }

.messages { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 22px; }
.msg { display: flex; gap: 12px; max-width: 900px; width: 100%; margin: 0 auto; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--surface-2); border-radius: 16px 16px 4px 16px; padding: 10px 14px; max-width: 75%; }
.msg.assistant .body { flex: 1; min-width: 0; }
.msg .bubble p { margin: 0 0 6px; }
.msg .bubble p:last-child { margin-bottom: 0; }
.msg .bubble ul { margin: 4px 0 8px; padding-left: 20px; }
.avatar { width: 28px; height: 28px; flex: none; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.tag { display: inline-block; font-size: 11px; color: var(--accent); background: rgba(255,122,61,.12); padding: 1px 8px; border-radius: 99px; margin-bottom: 4px; }

.typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.gen-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; margin-top: 12px; }
.gen-card { margin: 0; position: relative; border-radius: 12px; overflow: hidden; background: var(--surface-2); border: 2px solid transparent; max-height: 520px; }
.gen-card.active { border-color: var(--accent); }
.gen-card img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.gen-card figcaption { position: absolute; inset: auto 0 0 0; display: flex; justify-content: space-between; align-items: center; gap: 6px;
  padding: 24px 8px 8px; background: linear-gradient(transparent, rgba(0,0,0,.75)); font-size: 11px; }
.gen-actions { display: flex; gap: 4px; opacity: 0; transition: opacity .15s; }
.gen-card:hover .gen-actions, .gen-card.active .gen-actions { opacity: 1; }
.gen-actions button, .gen-actions a { background: rgba(0,0,0,.6); border: 1px solid rgba(255,255,255,.2); color: #fff; border-radius: 6px;
  padding: 3px 7px; font-size: 11px; cursor: pointer; font-family: inherit; }
.gen-actions button:hover, .gen-actions a:hover { background: var(--accent); color: #1a0d05; }
.gen-state { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center; color: var(--muted); text-align: center; padding: 16px; }
.gen-state.error { color: var(--error); }
.spinner { width: 26px; height: 26px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.composer { padding: 12px 24px 20px; max-width: 948px; width: 100%; margin: 0 auto; }
.composer-box { display: flex; gap: 8px; align-items: flex-end; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 8px; }
.composer-box:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; background: transparent; resize: none; max-height: 220px; padding: 8px; }
.active-chip { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.active-chip img { width: 34px; height: 34px; object-fit: cover; border-radius: 6px; }
.active-chip.explicit { color: var(--text); }
.active-chip.explicit img { outline: 2px solid var(--accent); }
.active-chip button { background: none; border: 0; color: var(--muted); cursor: pointer; }

.welcome { margin: auto; text-align: center; max-width: 560px; }
.welcome p { color: var(--muted); font-size: 15px; }
.suggestions { display: flex; flex-direction: column; gap: 8px; margin-top: 20px; }
.chip { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 10px; padding: 10px 14px; cursor: pointer; font: inherit; text-align: left; }
.chip:hover { border-color: var(--accent); }

/* Panel de referencias */
.refs { position: absolute; top: 0; right: 0; bottom: 0; width: 400px; max-width: 100%; background: var(--surface); border-left: 1px solid var(--border);
  padding: 16px; overflow-y: auto; z-index: 5; box-shadow: -20px 0 40px rgba(0,0,0,.4); }
.refs[hidden] { display: none; }
.refs-head { display: flex; justify-content: space-between; align-items: center; }
.refs-head h2 { margin: 0; }
.dropzone { border: 2px dashed var(--border); border-radius: 12px; padding: 22px; text-align: center; cursor: pointer; margin: 12px 0 16px; align-items: center; }
.dropzone.over, .dropzone:hover { border-color: var(--accent); background: rgba(255,122,61,.05); }
.ref-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ref-card { border-radius: 10px; overflow: hidden; background: var(--surface-2); cursor: pointer; border: 1px solid var(--border); }
.ref-card:hover { border-color: var(--accent); }
.ref-card img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; background: #fff; }
.ref-card.loading { aspect-ratio: 1; display: flex; flex-direction: column; gap: 8px; align-items: center; justify-content: center; text-align: center; color: var(--muted); padding: 8px; }
.ref-info { padding: 6px 8px; display: flex; flex-direction: column; gap: 3px; }
.ref-title { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ref-info .badge { align-self: flex-start; }

/* Modales y avisos */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.75); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal[hidden] { display: none; }
.modal-inner { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 20px; width: 100%; max-width: 480px; max-height: 92vh; overflow-y: auto; }
.modal-inner.wide { max-width: 1100px; display: flex; flex-direction: column; gap: 12px; }
.modal-inner.wide img { max-width: 100%; max-height: 78vh; object-fit: contain; margin: 0 auto; border-radius: 8px; }
.modal .close { position: absolute; top: 10px; right: 10px; z-index: 2; }
.ref-preview { max-height: 240px; object-fit: contain; border-radius: 8px; background: #fff; }
.row.center { justify-content: center; flex-wrap: wrap; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--surface-2); border: 1px solid var(--error);
  padding: 12px 18px; border-radius: 10px; z-index: 60; max-width: 90vw; box-shadow: 0 10px 30px rgba(0,0,0,.5); }
.toast.warn { border-color: var(--accent-2); }

@media (max-width: 900px) {
  .only-mobile { display: inline-flex; }
  .studio-wrap { grid-template-columns: 1fr; height: calc(100vh - 100px); }
  .sidebar { position: absolute; inset: 0 auto 0 0; width: 280px; z-index: 6; transform: translateX(-100%); transition: transform .2s; }
  body.sidebar-open .sidebar { transform: none; box-shadow: 20px 0 40px rgba(0,0,0,.5); }
  .messages, .composer, .chat-head { padding-left: 16px; padding-right: 16px; }
  .gen-actions { opacity: 1; }
  .conv-actions { display: flex; }
}
.badge.admin { color: var(--accent-2); background: rgba(255,179,71,.12); }
.ref-card { position: relative; }
.doc-icon { aspect-ratio: 1; display: grid; place-items: center; font-weight: 700; font-size: 20px; letter-spacing: .05em;
  color: var(--accent); background: repeating-linear-gradient(0deg, var(--surface-2) 0 14px, var(--surface) 14px 16px); }
.doc-flag { position: absolute; top: 6px; right: 6px; font-size: 10px; font-weight: 700; background: var(--accent); color: #1a0d05; padding: 1px 6px; border-radius: 4px; }
