*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1923;
  --bg-card: #1a2736;
  --bg-sidebar: #0d1520;
  --border: #2a3a4e;
  --text: #e2e8f0;
  --text-muted: #8899aa;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 8px;
}

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── Login Background ── */
#login-screen {
  position: relative; display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: radial-gradient(ellipse at 30% 20%, #132236 0%, #0b1520 50%, #060d14 100%); overflow: hidden;
}
.login-bg { position: absolute; inset: 0; z-index: 0; }
.bg-grid {
  position: absolute; inset: 0;
  background-image:
    linear-gradient(rgba(59,130,246,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59,130,246,0.06) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: gridPulse 12s ease-in-out infinite alternate;
}
@keyframes gridPulse {
  0% { opacity: 0.3; background-size: 50px 50px; }
  50% { opacity: 0.7; }
  100% { opacity: 0.4; background-size: 52px 52px; }
}
.bg-particles { position: absolute; inset: 0; }
.bg-num {
  position: absolute; font-family: 'Consolas', 'Courier New', monospace;
  font-size: 16px; color: rgba(59,130,246,0.18); font-weight: 600;
  animation: floatNum 14s ease-in-out infinite;
  pointer-events: none; user-select: none; text-shadow: 0 0 8px rgba(59,130,246,0.15);
}
@keyframes floatNum {
  0% { transform: translateY(0px) translateX(0px) rotate(0deg); opacity: 0.06; }
  20% { opacity: 0.25; }
  50% { transform: translateY(-40px) translateX(15px) rotate(5deg); opacity: 0.12; }
  70% { opacity: 0.28; }
  100% { transform: translateY(5px) translateX(-10px) rotate(-2deg); opacity: 0.06; }
}

/* Matrix-style falling numbers */
.bg-fall {
  position: absolute; font-family: 'Consolas', monospace;
  font-size: 14px; color: rgba(34,197,94,0.12); font-weight: 700;
  animation: fall 8s linear infinite; pointer-events: none; user-select: none;
}
@keyframes fall {
  0% { transform: translateY(-100%); opacity: 0; }
  10% { opacity: 0.15; }
  90% { opacity: 0.15; }
  100% { transform: translateY(100vh); opacity: 0; }
}

/* Glowing orbs */
.bg-orb {
  position: absolute; border-radius: 50%;
  filter: blur(80px); opacity: 0.08; pointer-events: none;
  animation: orbPulse 10s ease-in-out infinite alternate;
}
@keyframes orbPulse {
  0% { transform: scale(1); opacity: 0.05; }
  100% { transform: scale(1.3); opacity: 0.12; }
}

/* ── Login Box ── */
.login-box {
  position: relative; z-index: 1;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px;
  padding: 48px 40px 40px; width: 100%; max-width: 420px; text-align: center;
  box-shadow: 0 25px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(59,130,246,0.1);
  backdrop-filter: blur(8px);
}
.login-logo {
  width: 56px; height: 56px; border-radius: 14px; overflow: hidden;
  margin: 0 auto 16px; display: flex; align-items: center; justify-content: center;
  background: var(--bg-card);
}
.login-logo img { width: 100%; height: 100%; object-fit: contain; }
.login-box h1 { font-size: 22px; margin-bottom: 4px; color: var(--text); font-weight: 700; }
.login-sub { color: var(--text-muted); margin-bottom: 28px; font-size: 13px; line-height: 1.4; }
.login-box input {
  width: 100%; padding: 11px 14px; margin-bottom: 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.login-box input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59,130,246,0.15); }
.password-wrap { position: relative; width: 100%; }
.password-wrap input { width: 100%; padding-right: 40px; margin-bottom: 0; }
.toggle-pw {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 16px; padding: 4px;
  color: var(--text-muted); line-height: 1;
}
.toggle-pw:hover { color: var(--text); }
.login-switch { margin-top: 12px; font-size: 13px; color: var(--text-muted); }
.login-switch a { color: var(--primary); text-decoration: none; }
.login-switch a:hover { text-decoration: underline; }

/* ── Layout ── */
#main-app { display: flex; min-height: 100vh; }
#sidebar {
  width: 220px; min-width: 220px; background: var(--bg-sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 16px 0;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 20px 20px;
}
.sidebar-logo {
  width: 32px; height: 32px; border-radius: 8px;
  object-fit: contain;
  flex-shrink: 0;
}
.sidebar-title { font-size: 15px; font-weight: 700; color: var(--text); letter-spacing: -0.3px; }

/* ── Content Header ── */
#content-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 0 20px; margin-bottom: 8px;
}
.header-empresa {
  font-size: 15px; font-weight: 700; color: var(--text);
}
.header-empresa .empresa-rut {
  font-weight: 400; color: var(--text-muted); font-size: 12px; margin-left: 8px;
  font-family: 'Consolas', monospace;
}
.content-header-left { display: flex; align-items: center; gap: 10px; }
.content-header-right { display: flex; gap: 8px; }

/* ── Company Select Screen ── */
#company-select-screen {
  position: relative; display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--bg); overflow: hidden;
}
.company-select-box {
  position: relative; z-index: 1;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px;
  padding: 40px 40px 32px; width: 100%; max-width: 600px; text-align: center;
  box-shadow: 0 25px 60px rgba(0,0,0,0.5);
}
.company-select-box h1 { font-size: 20px; margin-bottom: 4px; }
.company-list { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; max-height: 300px; overflow-y: auto; }
.company-card {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px 20px; cursor: pointer; text-align: left; transition: all 0.15s;
  display: flex; align-items: center; gap: 14px;
}
.company-card:hover { border-color: var(--primary); background: rgba(59,130,246,0.06); }
.company-card-icon {
  width: 42px; height: 42px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), #1d4ed8);
  color: #fff; font-size: 18px; font-weight: 700; display: flex;
  align-items: center; justify-content: center; flex-shrink: 0;
}
.company-card-info { flex: 1; }
.company-card-name { font-size: 14px; font-weight: 600; color: var(--text); }
.company-card-rut { font-size: 12px; color: var(--text-muted); font-family: 'Consolas', monospace; }
.company-card-giro { font-size: 11px; color: var(--text-muted); }
.company-card-badge { font-size: 10px; }
#sidebar-nav { flex: 1; }
#sidebar-nav hr { border: none; border-top: 1px solid var(--border); margin: 8px 16px; }
.nav-item { display: block; padding: 10px 20px; color: var(--text-muted); text-decoration: none; font-size: 13px; transition: all 0.15s; border-left: 3px solid transparent; }
.nav-item:hover { color: var(--text); background: rgba(255,255,255,0.03); }
.nav-item.active { color: var(--primary); background: rgba(59,130,246,0.1); border-left-color: var(--primary); }
.sidebar-footer { padding: 12px 20px; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-muted); display: flex; align-items: center; justify-content: space-between; }

#content { flex: 1; padding: 28px; max-width: 1200px; overflow-y: auto; }
.view-section { display: none; }
.view-section.active { display: block; }

/* ── Cards ── */
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; margin-bottom: 20px; }
.card h2 { font-size: 18px; margin-bottom: 16px; }
.card h3 { font-size: 15px; margin-bottom: 12px; color: var(--text-muted); }
.header-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.header-row h2 { margin-bottom: 0; }
.filter-row { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }

/* ── Buttons ── */
.btn-primary {
  background: var(--primary); color: #fff; border: none; padding: 9px 18px;
  border-radius: var(--radius); cursor: pointer; font-size: 13px; font-weight: 600;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; border: none; padding: 9px 18px; border-radius: var(--radius); cursor: pointer; font-size: 13px; }
.btn-danger:hover { opacity: 0.85; }
.btn-sm { background: transparent; color: var(--text-muted); border: 1px solid var(--border); padding: 5px 12px; border-radius: var(--radius); cursor: pointer; font-size: 12px; }
.btn-sm:hover { color: var(--text); border-color: var(--text-muted); }

/* ── Forms ── */
form { display: flex; flex-direction: column; gap: 12px; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; }
.form-group { flex: 1; min-width: 180px; display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.form-input, select.form-input {
  padding: 8px 12px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 13px;
}
.form-input:focus { outline: none; border-color: var(--primary); }

/* ── Tables ── */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; padding: 10px 12px; border-bottom: 2px solid var(--border); color: var(--text-muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; }
td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
tr:hover td { background: rgba(255,255,255,0.02); }
.mono { font-family: 'Consolas', monospace; }
.amount { text-align: right; font-family: 'Consolas', monospace; }
.amount-neg { color: var(--danger); }

/* ── Badges ── */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600;
}
.badge-green { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-yellow { background: rgba(245,158,11,0.15); color: var(--warning); }
.badge-red { background: rgba(239,68,68,0.15); color: var(--danger); }
.badge-blue { background: rgba(59,130,246,0.15); color: var(--primary); }

/* ── Toast ── */
#toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 12px 20px; border-radius: var(--radius); font-size: 13px; animation: slideIn 0.3s ease; max-width: 400px; }
.toast-success { background: #065f46; color: #d1fae5; border: 1px solid #059669; }
.toast-error { background: #7f1d1d; color: #fee2e2; border: 1px solid #dc2626; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ── Utils ── */
.error-msg { background: #7f1d1d; color: #fca5a5; padding: 10px; border-radius: var(--radius); font-size: 13px; }
.empty-state { text-align: center; padding: 40px; color: var(--text-muted); font-size: 14px; }
.spaced { margin-top: 12px; }
.inline-group { display: flex; gap: 8px; align-items: center; }
.clickable { cursor: pointer; color: var(--primary); }
.clickable:hover { text-decoration: underline; }

/* ── Tree ── */
.tree { margin-top: 12px; }
.tree-node { margin-left: 20px; }
.tree-row { display: flex; align-items: center; gap: 8px; padding: 4px 8px; border-radius: 4px; cursor: pointer; }
.tree-row:hover { background: rgba(255,255,255,0.03); }
.tree-toggle { width: 18px; font-size: 11px; color: var(--text-muted); cursor: pointer; }
.tree-name { font-size: 13px; }
.tree-code { font-size: 12px; color: var(--text-muted); margin-right: 8px; font-family: 'Consolas', monospace; }
.tree-type { font-size: 10px; padding: 2px 6px; border-radius: 4px; }

/* ── Stats cards ── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
.stat-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.stat-value { font-size: 28px; font-weight: 700; margin: 4px 0; }
.stat-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; }

/* ── Emision DTE ── */
.dte-receptor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dte-items { margin-top: 8px; }
.dte-item-row { display: grid; grid-template-columns: 3fr 1fr 1fr 1fr 40px; gap: 8px; align-items: end; margin-bottom: 8px; }
@media (max-width: 768px) {
  .dte-receptor-grid { grid-template-columns: 1fr; }
  .dte-item-row { grid-template-columns: 1fr; }
}

/* ── Modal ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal-box { background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 30px; max-width: 600px; width: 95%; max-height: 80vh; overflow-y: auto; }
.modal-box h3 { margin-bottom: 16px; }

/* ── Responsive sidebar toggle ── */
@media (max-width: 768px) {
  #sidebar { position: fixed; z-index: 100; min-height: 100vh; transform: translateX(-100%); transition: transform 0.25s; }
  #sidebar.open { transform: translateX(0); }
  #content { padding: 16px; }
}
