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

:root {
  --brand: #363095;
  --brand-dark: #2a2575;
  --brand-light: #f4f3fa;
  --accent: #429CE0;
  --accent-light: #B6E38F;
  --text: #2c2c2c;
  --text-muted: #6c757d;
  --bg: #f5f6fa;
  --bg-card: #ffffff;
  --border: #e5e7eb;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
  --radius: 10px;
  --radius-sm: 6px;
  --tap: 48px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app { min-height: 100vh; min-height: 100dvh; }
a { color: var(--brand); text-decoration: none; }

button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }

input, select, textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  transition: border-color 0.15s, box-shadow 0.15s;
  min-height: var(--tap);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(54,48,149,0.12);
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 20px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 15px;
  transition: background 0.15s, transform 0.05s, box-shadow 0.15s;
  white-space: nowrap;
  min-height: var(--tap);
  min-width: var(--tap);
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--brand); color: white; box-shadow: 0 2px 8px rgba(54,48,149,0.25); }
.btn-primary:hover { background: var(--brand-dark); }
.btn-secondary { background: var(--bg-card); color: var(--text); border: 1.5px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: white; }
.btn-success { background: var(--success); color: white; }
.btn-sm { padding: 8px 14px; font-size: 13px; min-height: 36px; }
.btn-xs { padding: 4px 10px; font-size: 12px; min-height: 28px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ===== LAYOUT ===== */
.loading-screen {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; min-height: 100vh; min-height: 100dvh; gap: 24px;
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border); border-top-color: var(--brand);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.layout { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--bg-card);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50;
}
.topbar-logo { height: 38px; }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.topbar-user .name { font-weight: 600; }
.topbar-user .role { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }

.main { flex: 1; padding: 16px; padding-bottom: 90px; }

.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; background: var(--bg-card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom); z-index: 40;
}
.bottom-nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 10px 4px; font-size: 11px;
  color: var(--text-muted); text-decoration: none; text-align: center;
  min-height: 56px;
}
.bottom-nav a.active { color: var(--brand); }
.bottom-nav .icon { font-size: 22px; line-height: 1; margin-bottom: 3px; }

/* ===== VIEW STRUCTURE ===== */
.view-header {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 16px; flex-wrap: wrap;
}
.view-header h2 { font-size: 20px; font-weight: 700; flex: 1; }

.filters-bar {
  display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; align-items: center;
}
.filters-bar select, .filters-bar input { flex: 1; min-width: 120px; }

/* ===== CARDS ===== */
.card {
  background: var(--bg-card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
}

.pres-card { cursor: pointer; transition: box-shadow 0.15s, transform 0.1s; }
.pres-card:active { transform: scale(0.99); box-shadow: var(--shadow); }

.card-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; flex-wrap: wrap; }
.card-row--top { justify-content: space-between; margin-bottom: 8px; }
.card-row--meta { font-size: 12px; color: var(--text-muted); margin-bottom: 8px; }

.card-actions {
  display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap;
  border-top: 1px solid var(--border); padding-top: 10px;
}
.card-actions .btn { flex: 1; }

.pres-numero { font-weight: 700; font-size: 15px; color: var(--brand); }
.pres-cliente { font-weight: 500; flex: 1; }
.pres-total { font-weight: 700; font-size: 16px; color: var(--brand); }
.cliente-nombre { font-weight: 600; font-size: 15px; }

/* ===== BADGES ===== */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px;
}
.estado-borrador, .badge-borrador { background: #f3f4f6; color: #4b5563; }
.estado-enviado, .badge-enviado { background: #dbeafe; color: #1e40af; }
.estado-aceptado, .badge-aceptado { background: #d1fae5; color: #065f46; }
.estado-rechazado, .badge-rechazado { background: #fee2e2; color: #991b1b; }
.estado-vencido, .badge-vencido { background: #fef3c7; color: #92400e; }
.badge--version { background: var(--brand-light); color: var(--brand); }
.badge--admin { background: #fef3c7; color: #92400e; }
.badge--inactivo { background: #f3f4f6; color: #9ca3af; }

.vencido { color: var(--danger); font-weight: 600; }

/* ===== ALERTS ===== */
.alert { padding: 12px 14px; border-radius: var(--radius-sm); margin-bottom: 14px; font-size: 14px; }
.alert-error { background: #fef2f2; color: var(--danger); border: 1px solid #fecaca; }
.alert-ok, .alert-success { background: #f0fdf4; color: var(--success); border: 1px solid #bbf7d0; }
.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

/* ===== FORM ===== */
.form-section { margin-bottom: 20px; }
.form-section label { margin-bottom: 8px; }

.form-row { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .form-row { grid-template-columns: 1fr 1fr 1fr; } }

.form-actions {
  display: flex; gap: 10px; flex-wrap: wrap;
  padding-top: 16px; border-top: 1px solid var(--border); margin-top: 8px;
}
.form-actions .btn { flex: 1; min-width: 140px; }

.estado-btns { display: flex; gap: 8px; flex-wrap: wrap; width: 100%; }
.estado-btns .btn { flex: 1; }

/* ===== AUTOCOMPLETE / DROPDOWN ===== */
.autocomplete-wrap, .search-wrap { position: relative; }

.dropdown {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  background: var(--bg-card); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  z-index: 200; max-height: 240px; overflow-y: auto;
}
.drop-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px; cursor: pointer; border-bottom: 1px solid var(--border);
  font-size: 14px; min-height: var(--tap);
}
.drop-item:last-child { border-bottom: none; }
.drop-item:hover, .drop-item:active { background: var(--brand-light); color: var(--brand); }
.drop-precio { font-weight: 700; color: var(--brand); white-space: nowrap; }

/* ===== ITEMS ===== */
.manual-row { display: flex; gap: 8px; align-items: flex-start; flex-wrap: wrap; }
.manual-row input { flex: 1; min-width: 120px; }
.manual-row .btn { flex-shrink: 0; align-self: flex-end; }

.item-row {
  background: var(--bg-card); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px; margin-bottom: 10px;
}
.item-nombre { font-weight: 600; margin-bottom: 10px; font-size: 14px; }
.sku { font-size: 11px; color: var(--text-muted); font-weight: 400; margin-left: 6px; }

.item-controls {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px;
}
.inp-label { font-size: 11px; font-weight: 600; color: var(--text-muted); white-space: nowrap; margin: 0; text-transform: uppercase; }
.inp-xs { width: 70px !important; min-height: 38px; padding: 6px 8px; text-align: right; }
.inp-sm { width: 120px !important; }
.btn-del { color: var(--danger); font-size: 16px; padding: 4px 8px; }

.item-subtotal { text-align: right; font-weight: 700; color: var(--brand); font-size: 15px; }

/* ===== TOTALES ===== */
.totales-box {
  background: var(--brand-light); border-left: 4px solid var(--brand);
  border-radius: var(--radius-sm); padding: 14px; margin-top: 12px;
}
.total-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 5px 0; font-size: 14px;
}
.total-row--final {
  border-top: 2px solid var(--brand); margin-top: 8px; padding-top: 10px;
  font-size: 20px; font-weight: 800; color: var(--brand);
}

/* ===== VIGENCIA ===== */
.vigencia-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.vigencia-opt { border: 1.5px solid var(--border); color: var(--text); background: var(--bg-card); border-radius: var(--radius-sm); }
.vigencia-opt.active { border-color: var(--brand); background: var(--brand); color: white; }

/* ===== TOGGLE IVA ===== */
.toggle { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; margin: 0; }
.toggle input[type="checkbox"] { width: 0; height: 0; opacity: 0; position: absolute; }
.slider {
  position: relative; display: inline-block;
  width: 52px; height: 28px; border-radius: 14px;
  background: var(--border); transition: background 0.2s;
  flex-shrink: 0;
}
.slider::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: white; box-shadow: var(--shadow);
  transition: transform 0.2s;
}
.toggle input:checked + .slider { background: var(--brand); }
.toggle input:checked + .slider::after { transform: translateX(24px); }

/* ===== MODAL ===== */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  z-index: 300; display: flex; align-items: flex-end; justify-content: center;
  padding: 16px;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--bg-card); width: 100%; max-width: 500px;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px; max-height: 85vh; overflow-y: auto;
}
.modal-box h3 { margin-bottom: 16px; font-size: 18px; }
.modal-box input, .modal-box select, .modal-box textarea { margin-bottom: 12px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

@media (min-width: 600px) {
  .modal { align-items: center; }
  .modal-box { border-radius: var(--radius); }
}

/* ===== TABS ===== */
.tabs { display: flex; gap: 4px; margin-bottom: 16px; background: var(--bg); padding: 4px; border-radius: var(--radius-sm); }
.tab {
  flex: 1; padding: 10px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: 13px; color: var(--text-muted);
  background: transparent; transition: background 0.15s, color 0.15s;
  min-height: 42px;
}
.tab.active { background: var(--bg-card); color: var(--brand); box-shadow: var(--shadow-sm); }
.tab-header { display: flex; justify-content: flex-end; margin-bottom: 12px; }

/* ===== PAGINATION ===== */
.pagination { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 16px 0; }

/* ===== EMPTY / LOADING ===== */
.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }
.loading { text-align: center; padding: 32px; color: var(--text-muted); }

/* ===== UTILS ===== */
.hidden { display: none !important; }
.mt-xs { margin-top: 6px; }
.mt-s { margin-top: 12px; }
.mt-m { margin-top: 20px; }
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-sm { font-size: 13px; }

/* ===== DARK MODE ===== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #5b54d1;
    --brand-dark: #3d36a8;
    --brand-light: #1f1d3a;
    --accent: #5fb5f0;
    --text: #e5e7eb;
    --text-muted: #9ca3af;
    --bg: #0f1115;
    --bg-card: #1a1d23;
    --border: #2d3139;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.4);
    --shadow: 0 2px 6px rgba(0,0,0,0.5);
    --shadow-lg: 0 10px 20px rgba(0,0,0,0.5);
  }
}

:root[data-theme="dark"] {
  --brand: #5b54d1;
  --brand-dark: #3d36a8;
  --brand-light: #1f1d3a;
  --accent: #5fb5f0;
  --text: #e5e7eb;
  --text-muted: #9ca3af;
  --bg: #0f1115;
  --bg-card: #1a1d23;
  --border: #2d3139;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.4);
  --shadow: 0 2px 6px rgba(0,0,0,0.5);
  --shadow-lg: 0 10px 20px rgba(0,0,0,0.5);
}

:root[data-theme="dark"] input,
:root[data-theme="dark"] select,
:root[data-theme="dark"] textarea,
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) input,
  :root:not([data-theme="light"]) select,
  :root:not([data-theme="light"]) textarea { background: #1a1d23; color: var(--text); }
  :root:not([data-theme="light"]) .estado-borrador { background: #2d3139; color: #cbd5e1; }
  :root:not([data-theme="light"]) .estado-enviado { background: #1e3a8a; color: #bfdbfe; }
  :root:not([data-theme="light"]) .estado-aceptado { background: #064e3b; color: #a7f3d0; }
  :root:not([data-theme="light"]) .estado-rechazado { background: #7f1d1d; color: #fecaca; }
}
