:root {
  --bg:#0f172a; --surface:#1e293b; --surface2:#243044; --border:#334155;
  --text:#e2e8f0; --muted:#94a3b8; --green:#22c55e; --yellow:#eab308;
  --red:#ef4444; --blue:#3b82f6; --accent:#6366f1; --sidebar-w:220px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  display: flex; min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Sidebar */
#sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w); background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  position: fixed; top: 0; left: 0; height: 100vh; overflow-y: auto; z-index: 100;
}
#sidebar .logo { padding: 1.25rem 1rem 1rem; border-bottom: 1px solid var(--border); }
#sidebar .logo h2 { font-size: 1rem; font-weight: 700; color: var(--text); }
#sidebar .logo p { font-size: 0.72rem; color: var(--muted); margin-top: 0.15rem; }
#sidebar nav { padding: 0.75rem 0; flex: 1; }
#sidebar nav a {
  display: flex; align-items: center; gap: 0.6rem; padding: 0.6rem 1rem;
  color: var(--muted); text-decoration: none; font-size: 0.88rem;
  border-left: 3px solid transparent; transition: all .15s;
}
#sidebar nav a:hover { color: var(--text); background: rgba(255,255,255,.04); text-decoration: none; }
#sidebar nav a.active { color: var(--accent); border-left-color: var(--accent); background: rgba(99,102,241,.08); }
#sidebar nav a .ico { width: 20px; text-align: center; }
#sidebar nav a .badge-count {
  margin-left: auto; background: var(--red); color: #fff;
  border-radius: 9999px; font-size: 0.68rem; font-weight: 700;
  padding: 0.1rem 0.45rem; min-width: 22px; text-align: center;
}
#sidebar nav a .badge-count.muted { background: var(--surface2); color: var(--muted); }
#sidebar nav a .badge-count.whatsapp { background: #22c55e; color: #fff; }
#sidebar nav a .badge-count.whatsapp.muted { background: var(--surface2); color: var(--muted); }
#sidebar .nav-section {
  padding: 1rem 1rem 0.4rem 1rem; font-size: 0.62rem; font-weight: 700;
  letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted);
  border-top: 1px solid var(--border); margin-top: 0.6rem;
}
#sidebar nav a.my-case-link {
  background: rgba(99,102,241,0.04); border-left-color: transparent;
}
#sidebar nav a.my-case-link.active { background: rgba(99,102,241,0.12); }

/* Modal preview */
#ticket-modal {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.65);
  z-index: 200; align-items: flex-start; justify-content: center; overflow-y: auto;
  padding: 3rem 1rem;
}
#ticket-modal.open { display: flex; }
#ticket-modal-content {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  width: 760px; max-width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  position: relative;
}
#ticket-modal-close {
  position: absolute; top: 0.75rem; right: 0.75rem; background: var(--surface2);
  color: var(--muted); border: 1px solid var(--border); border-radius: 9999px;
  width: 28px; height: 28px; cursor: pointer; font-size: 1rem; line-height: 1;
}
#ticket-modal-close:hover { color: var(--text); border-color: var(--accent); }
.ticket-preview { padding: 1.5rem 1.75rem; }
.ticket-preview-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 1rem; padding-bottom: 1rem; border-bottom: 1px solid var(--border);
}
.ticket-preview-meta {
  display: flex; flex-wrap: wrap; gap: 0.65rem 1.25rem; padding: 0.85rem 0;
  font-size: 0.85rem; color: var(--text); border-bottom: 1px solid var(--border);
}
.ticket-preview-meta b { color: var(--muted); font-weight: 600; margin-right: 0.25rem; }
.ticket-preview-section { padding: 1rem 0; border-bottom: 1px solid var(--border); }
.ticket-preview-section:last-child { border-bottom: none; }
.ticket-preview-section h3 {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); font-weight: 700; margin-bottom: 0.6rem;
}
.conversation { display: flex; flex-direction: column; gap: 0.6rem; }
.msg { padding: 0.65rem 0.85rem; border-radius: 10px; font-size: 0.85rem; line-height: 1.45; max-width: 90%; }
.msg.user { background: var(--surface2); align-self: flex-start; border-radius: 10px 10px 10px 2px; }
.msg.agent { background: rgba(99,102,241,0.12); border: 1px solid rgba(99,102,241,0.2); align-self: flex-end; border-radius: 10px 10px 2px 10px; }
.msg-meta { font-size: 0.7rem; color: var(--muted); margin-bottom: 0.25rem; display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.msg-body { white-space: pre-wrap; word-break: break-word; color: var(--text); }
.ticket-link { cursor: pointer; }
.ticket-link:hover { text-decoration: underline; }
.card { transition: transform 0.1s, border-color 0.1s; }
a.card:hover { border-color: var(--accent); transform: translateY(-1px); text-decoration: none !important; }
#sidebar .footer {
  padding: 0.75rem 1rem; border-top: 1px solid var(--border);
  font-size: 0.7rem; color: var(--muted);
}
#sidebar .footer .who { font-size: 0.78rem; color: var(--text); margin-bottom: 0.25rem; font-weight: 600; }
#sidebar .footer form { display: inline; }
#sidebar .footer button {
  background: transparent; border: none; color: var(--muted); padding: 0; cursor: pointer;
  font-size: 0.72rem;
}
#sidebar .footer button:hover { color: var(--text); }

/* Main */
#main { margin-left: var(--sidebar-w); flex: 1; padding: 1.75rem 2rem; min-width: 0; }
body:has(#main.no-sidebar) { align-items: center; justify-content: center; }
#main.no-sidebar {
  margin: 0 auto; max-width: 420px; width: 100%; padding: 2rem;
  display: flex; flex-direction: column; justify-content: center;
  min-height: 100vh;
}
.login-box .brand { text-align: center; margin-bottom: 1.5rem; }
.login-box .brand .logo-emoji { font-size: 2.5rem; }
.login-box .brand h1 { font-size: 1.4rem; font-weight: 700; margin-top: 0.5rem; }
.login-box .brand p { color: var(--muted); font-size: 0.82rem; }
h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 0.2rem; }
h2 { font-size: 1.05rem; font-weight: 600; margin-bottom: 0.9rem; color: var(--accent); letter-spacing: .02em; }
h3 { font-size: 0.95rem; font-weight: 600; margin: 0.75rem 0 0.5rem; color: var(--text); }
.subtitle { color: var(--muted); font-size: 0.82rem; margin-bottom: 1.5rem; }

/* Filters */
.filters {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 1rem 1.25rem; margin-bottom: 1.5rem; display: flex; flex-wrap: wrap; gap: 1rem;
  align-items: flex-end;
}
.fg { display: flex; flex-direction: column; gap: 0.25rem; }
.fg label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.fg input, .fg select {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 7px; padding: 0.38rem 0.65rem; font-size: 0.86rem; min-width: 130px;
}
.fg input:focus, .fg select:focus { outline: none; border-color: var(--accent); }

/* Buttons */
.btn, button, input[type="submit"] {
  border-radius: 7px; padding: 0.38rem 0.9rem; font-size: 0.84rem; cursor: pointer;
  border: 1px solid var(--border); background: var(--accent); color: #fff; border-color: var(--accent);
  font: inherit; font-size: 0.84rem;
}
.btn:hover, button:hover { opacity: .9; }
.btn-ghost, button.secondary {
  background: var(--surface2); color: var(--muted); border-color: var(--border);
}
.btn-ghost:hover, button.secondary:hover { color: var(--text); border-color: var(--accent); }
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.78rem; }

/* Cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0.9rem; margin-bottom: 1.75rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 1rem 1.1rem; }
.card .val, .card .value { font-size: 1.8rem; font-weight: 700; margin-bottom: 0.15rem; }
.card .lbl, .card .label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.card .delta { font-size: 0.75rem; color: var(--green); margin-top: 0.2rem; }
.card .delta.down { color: var(--red); }
.card .muted { color: var(--muted); font-size: 0.72rem; margin-top: 0.2rem; }

/* Tables */
.section { margin-bottom: 2rem; }
.tbl-wrap { overflow-x: auto; width: 100%; }
table {
  width: 100%; border-collapse: collapse; font-size: 0.86rem;
  background: var(--surface); border-radius: 12px; overflow: hidden;
  border: 1px solid var(--border); table-layout: auto;
}
th {
  background: var(--surface2); border-bottom: 2px solid var(--border);
  padding: 0.55rem 0.75rem; text-align: left; color: var(--muted); font-weight: 600;
  text-transform: uppercase; font-size: 0.68rem; letter-spacing: .04em; white-space: nowrap;
}
td {
  padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--border);
  vertical-align: middle; color: var(--text); white-space: nowrap;
}
/* La 2a columna (típicamente Asunto) absorbe el espacio sobrante y permite wrap. */
table tbody td:nth-child(2) { white-space: normal; word-break: break-word; min-width: 240px; }
table thead th:nth-child(2) { width: auto; }
tr:hover td { background: rgba(255,255,255,.025); }
tr:last-child td { border-bottom: none; }
table a { color: var(--accent); }
table a:hover { color: var(--text); }
.tbl-wrap td .badge { white-space: nowrap; }

/* Badges */
.badge {
  display: inline-block; padding: .16rem .5rem; border-radius: 9999px;
  font-size: .71rem; font-weight: 600;
}
.badge.g, .badge.success { background: rgba(34,197,94,.15); color: var(--green); }
.badge.y, .badge.warn, .badge.high { background: rgba(234,179,8,.15); color: var(--yellow); }
.badge.r, .badge.danger, .badge.urgent { background: rgba(239,68,68,.15); color: var(--red); }
.badge.n, .badge.normal { background: rgba(99,102,241,.15); color: var(--accent); }
.badge.low { background: rgba(148,163,184,.1); color: var(--muted); }
.tag {
  display: inline-block; padding: 2px 8px; background: rgba(99,102,241,.12);
  border-radius: 12px; font-size: 12px; margin-right: 4px; color: var(--text);
}

/* Login */
.login-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 2rem;
}
.login-box h2 { color: var(--text); margin-bottom: 1.5rem; }
.login-box label { display: block; margin-bottom: 1rem; font-size: 0.82rem; color: var(--muted); }
.login-box input {
  width: 100%; margin-top: 0.3rem; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); border-radius: 7px; padding: 0.5rem 0.75rem; font-size: 0.9rem;
}
.login-box input:focus { outline: none; border-color: var(--accent); }
.login-box button { width: 100%; padding: 0.55rem; font-size: 0.95rem; font-weight: 600; margin-top: 0.5rem; }

/* Alerts and notices */
.alert {
  background: rgba(234,179,8,.1); border: 1px solid rgba(234,179,8,.3);
  color: var(--yellow); padding: 0.85rem 1.1rem; border-radius: 10px; margin-bottom: 1rem;
  font-size: 0.86rem;
}
.alert.info { background: rgba(99,102,241,.08); border-color: rgba(99,102,241,.3); color: var(--accent); }
.notice { background: rgba(34,197,94,.08); border: 1px solid rgba(34,197,94,.3); color: var(--green); padding: 0.65rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: 0.85rem; }
.error { color: var(--red); margin-bottom: 1rem; font-size: 0.85rem; }
.muted { color: var(--muted); font-size: 0.85rem; }

/* Scrollbar dark */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
