/* ===== VARIABLES ===== */
:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface2: #f8f9fb;
  --border: #e2e5ea;
  --border2: #d0d5dd;
  --text: #1a1d23;
  --text2: #5a6070;
  --text3: #8b92a0;
  --accent: #2563eb;
  --accent-bg: #eff6ff;
  --accent-text: #1d4ed8;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --warning: #d97706;
  --warning-bg: #fffbeb;
  --topbar: 52px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.07);
  --shadow-md: 0 4px 16px rgba(0,0,0,.1);
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; -webkit-text-size-adjust: 100%; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; }

/* ===== DISPLAY HELPERS ===== */
.d-none { display: none !important; }
.d-sm-none { display: none; }
@media (min-width: 576px) { .d-sm-none { display: inline; } }
@media (min-width: 768px) { .d-md-none { display: none !important; } .d-none.d-md-flex { display: flex !important; } }

/* ===== TOPNAV ===== */
.topnav {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar);
  background: var(--surface); border-bottom: 1px solid var(--border);
  z-index: 100; display: flex; align-items: center; padding: 0 16px;
  gap: 12px; box-shadow: var(--shadow);
}
.topnav-brand { display: flex; align-items: center; gap: 8px; text-decoration: none; flex-shrink: 0; }
.topnav-logo { font-size: 20px; font-weight: 900; color: var(--accent); letter-spacing: -1px; }
.topnav-sub { font-size: 11px; color: var(--text3); }
.topnav-menu { display: flex; align-items: center; gap: 2px; flex: 1; list-style: none; }
.topnav-menu li { position: relative; }

/* Nav links */
.nav-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 7px; border: none;
  background: none; cursor: pointer; font-size: 13px; font-weight: 500;
  color: var(--text2); white-space: nowrap; transition: all .15s;
  text-decoration: none;
}
.nav-link:hover { background: var(--accent-bg); color: var(--accent-text); }
.nav-link.active { background: var(--accent); color: #fff; }
.nav-link.active:hover { background: #1d4ed8; }
.caret { transition: transform .2s; }

/* Dropdown */
.dd-toggle { cursor: pointer; }
.dd-toggle.dd-open .caret { transform: rotate(180deg); }
.dd-panel {
  display: none; position: absolute; top: calc(100% + 6px); left: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow-md);
  min-width: 210px; padding: 6px; z-index: 200;
}
.dd-panel.show { display: block; }
.dd-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: 6px; font-size: 13px; font-weight: 500; color: var(--text2);
  text-decoration: none; transition: all .15s; width: 100%;
}
.dd-item:hover { background: var(--bg); color: var(--text); }
.dd-item.active { background: var(--accent-bg); color: var(--accent-text); }

/* Hamburger */
.mob-hamburger {
  background: none; border: none; cursor: pointer; padding: 6px;
  border-radius: 7px; color: var(--text2); margin-left: auto;
  display: flex; align-items: center;
}
.mob-hamburger:hover { background: var(--bg); }

/* ===== MOBILE DRAWER (RIGHT) ===== */
.drawer-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.4); z-index: 150;
}
.drawer-overlay.show { display: block; }
.mob-drawer {
  position: fixed; top: 0; right: -290px; width: 280px; height: 100vh;
  background: var(--surface); z-index: 160; display: flex; flex-direction: column;
  transition: right .25s cubic-bezier(.4,0,.2,1);
  box-shadow: -4px 0 20px rgba(0,0,0,.15); overflow-y: auto;
}
.mob-drawer.show { right: 0; }
.drawer-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.drawer-close {
  background: none; border: none; cursor: pointer; color: var(--text3);
  padding: 4px; border-radius: 6px; display: flex;
}
.drawer-close:hover { background: var(--bg); }
.drawer-nav { flex: 1; padding: 10px 8px; }
.drawer-section {
  font-size: 10px; font-weight: 700; color: var(--text3);
  text-transform: uppercase; letter-spacing: .8px; padding: 10px 8px 4px;
}
.drawer-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 12px;
  border-radius: 8px; font-size: 14px; font-weight: 500; color: var(--text2);
  text-decoration: none; transition: all .15s; margin-bottom: 2px;
}
.drawer-item:hover { background: var(--bg); color: var(--text); }
.drawer-item.active { background: var(--accent-bg); color: var(--accent-text); }
.drawer-footer { padding: 14px 16px; border-top: 1px solid var(--border); font-size: 11px; color: var(--text3); }

/* ===== MAIN CONTENT ===== */
.main-content { margin-top: var(--topbar); padding: 20px; max-width: 1400px; }

/* ===== PAGE HEADER ===== */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; gap: 10px; }
.page-title { font-size: 20px; font-weight: 700; }
.page-sub { font-size: 12px; color: var(--text2); margin-top: 2px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ===== STATS GRID ===== */
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; }
.stat-label { font-size: 11px; font-weight: 600; color: var(--text3); text-transform: uppercase; letter-spacing: .5px; }
.stat-val { font-size: 26px; font-weight: 700; color: var(--text); margin-top: 4px; }
.stat-sub { font-size: 12px; color: var(--text3); margin-top: 2px; }

/* ===== CARD ===== */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px; box-shadow: var(--shadow); margin-bottom: 18px; }
.card-title { font-size: 14px; font-weight: 600; margin-bottom: 16px; display: flex; align-items: center; gap: 8px; }

/* ===== QUICK SEARCH ===== */
.qs-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.qs-wrap { position: relative; flex: 1; min-width: 180px; max-width: 420px; }
.qs-wrap svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text3); pointer-events: none; }
.qs-input { width: 100%; padding: 9px 36px 9px 34px; border: 1px solid var(--border2); border-radius: 8px; font-size: 14px; color: var(--text); background: var(--surface); outline: none; transition: border .15s; }
.qs-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
.qs-clear { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); background: none; border: none; cursor: pointer; color: var(--text3); font-size: 16px; line-height: 1; display: none; padding: 2px 4px; }
.qs-count { font-size: 12px; color: var(--text3); white-space: nowrap; align-self: center; }

/* ===== FILTER CHIPS ===== */
.filter-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.chip { padding: 5px 12px; border-radius: 20px; border: 1px solid var(--border2); background: var(--surface); font-size: 12px; font-weight: 500; color: var(--text2); cursor: pointer; transition: all .15s; text-decoration: none; display: inline-block; }
.chip:hover { background: var(--accent-bg); color: var(--accent-text); border-color: var(--accent); }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ===== TABLE ===== */
.table-responsive { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th { background: var(--surface2); padding: 9px 14px; text-align: left; font-weight: 600; font-size: 11px; color: var(--text2); text-transform: uppercase; letter-spacing: .4px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; color: var(--text); }
.table tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--surface2); }
.td-muted { color: var(--text2); }

/* ===== MOBILE CARDS ===== */
.mob-cards { display: none; }
.mob-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 10px; }
.mob-card-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; }
.mob-card-title { font-weight: 600; font-size: 14px; }
.mob-card-sub { font-size: 12px; color: var(--text2); margin-top: 2px; }
.mob-card-row { display: flex; justify-content: space-between; align-items: center; padding: 4px 0; font-size: 13px; }
.mob-card-label { font-size: 12px; color: var(--text3); }
.mob-card-actions { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); display: flex; gap: 8px; }

/* ===== BADGES ===== */
.badge { display: inline-flex; align-items: center; padding: 3px 9px; border-radius: 99px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.badge-green { background: var(--success-bg); color: var(--success); }
.badge-blue { background: var(--accent-bg); color: var(--accent-text); }
.badge-yellow { background: var(--warning-bg); color: var(--warning); }
.badge-red { background: var(--danger-bg); color: var(--danger); }

/* ===== BUTTONS ===== */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 8px; font-size: 13px; font-weight: 500; cursor: pointer; border: 1px solid transparent; transition: all .15s; text-decoration: none; white-space: nowrap; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #1d4ed8; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border2); }
.btn-secondary:hover { background: var(--bg); }
.btn-danger { background: var(--danger-bg); color: var(--danger); border-color: #fecaca; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon { padding: 7px 10px; }

/* ===== FORMS ===== */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.form-grid-3 { grid-template-columns: repeat(3, 1fr); }
.form-grid-4 { grid-template-columns: repeat(4, 1fr); }
.col-full { grid-column: 1 / -1; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-label { font-size: 12px; font-weight: 600; color: var(--text2); }
.form-control {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border2);
  border-radius: 8px; font-size: 14px; color: var(--text);
  background: var(--surface); outline: none; transition: border .15s;
  font-family: inherit; -webkit-appearance: none;
}
.form-control:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
.form-control[readonly] { background: var(--surface2); color: var(--text2); }
textarea.form-control { resize: vertical; min-height: 70px; }
select.form-control {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'%3E%3Cpath fill='%238b92a0' d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; padding-right: 30px;
}
.form-text { font-size: 11px; color: var(--text3); margin-top: 3px; }

/* ===== AUTOCOMPLETE ===== */
.ac-wrap { position: relative; }
.ac-list {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--surface); border: 1px solid var(--border2);
  border-radius: 8px; box-shadow: var(--shadow-md);
  z-index: 300; max-height: 250px; overflow-y: auto; margin-top: 3px; display: none;
}
.ac-list.show { display: block; }
.ac-item { padding: 10px 14px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); }
.ac-item:last-child { border-bottom: none; }
.ac-item:hover { background: var(--bg); }
.ac-sub { font-size: 11px; color: var(--text3); margin-top: 2px; }

/* ===== ORDER LINES ===== */
.lineas-section { margin: 16px 0; }
.linea-head-row, .linea-row {
  display: grid;
  grid-template-columns: minmax(0,2.5fr) 80px 100px 70px 90px 40px;
  gap: 6px; align-items: center;
}
.linea-head-row { padding: 4px 8px; font-size: 11px; font-weight: 600; color: var(--text3); text-transform: uppercase; letter-spacing: .4px; }
.linea-row { padding: 7px 8px; background: var(--surface2); border: 1px solid var(--border); border-radius: 7px; margin-bottom: 5px; }
.linea-row input { padding: 5px 8px; font-size: 12px; border-radius: 5px; border: 1px solid var(--border2); background: var(--surface); width: 100%; outline: none; }
.linea-row input:focus { border-color: var(--accent); }
.linea-importe { font-weight: 700; font-size: 13px; text-align: right; }
.linea-del { background: var(--danger-bg); color: var(--danger); border: 1px solid #fecaca; border-radius: 5px; padding: 5px 9px; cursor: pointer; font-size: 12px; font-weight: 700; width: 100%; text-align: center; }

/* Mobile line items */
.linea-mob { display: none; background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 8px; }
.linea-mob-name { font-weight: 600; font-size: 13px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; }
.linea-mob-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.linea-mob-total { margin-top: 8px; text-align: right; font-size: 14px; font-weight: 700; color: var(--accent-text); }

/* ===== TOTALS BOX ===== */
.totals-box { background: var(--surface2); border: 1px solid var(--border); border-radius: 9px; padding: 14px 16px; margin-top: 14px; }
.total-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; color: var(--text2); }
.total-row.grand { font-size: 16px; font-weight: 700; color: var(--text); border-top: 1px solid var(--border); padding-top: 10px; margin-top: 6px; }

/* ===== ALERTS ===== */
.alert { padding: 12px 16px; border-radius: 8px; font-size: 13px; margin-bottom: 16px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.alert-success { background: var(--success-bg); color: var(--success); border: 1px solid #bbf7d0; }
.alert-error { background: var(--danger-bg); color: var(--danger); border: 1px solid #fecaca; }
.alert-info { background: var(--accent-bg); color: var(--accent-text); border: 1px solid #bfdbfe; }
.alert-dismissible button { background: none; border: none; cursor: pointer; color: inherit; font-size: 16px; padding: 0; line-height: 1; }

/* ===== MISC ===== */
.loading { display: flex; align-items: center; justify-content: center; padding: 40px; color: var(--text3); flex-direction: column; gap: 10px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; padding: 48px 20px; color: var(--text3); }
.empty-icon { font-size: 40px; margin-bottom: 10px; }
.text-success { color: var(--success); font-weight: 600; }
.text-danger { color: var(--danger); font-weight: 600; }

/* ===== TICKET PRINT ===== */
@media print {
  body * { visibility: hidden !important; }
  #ticketArea, #ticketArea * { visibility: visible !important; }
  #ticketArea {
    position: fixed !important; top: 0 !important; left: 0 !important;
    width: 80mm !important; margin: 0 !important; padding: 0 !important;
    background: #fff !important;
  }
}
#ticketArea { display: none; }
.ticket {
  width: 80mm; font-family: 'Courier New', Courier, monospace;
  font-size: 9pt; color: #000; background: #fff; padding: 4mm 3mm; line-height: 1.4;
}
.tk-center { text-align: center; }
.tk-bold { font-weight: bold; }
.tk-big { font-size: 12pt; font-weight: bold; }
.tk-line { border-top: 1px dashed #000; margin: 4px 0; }
.tk-row { display: flex; justify-content: space-between; font-size: 8.5pt; }
.tk-total { display: flex; justify-content: space-between; font-size: 11pt; font-weight: bold; }
.tk-footer { text-align: center; font-size: 8pt; margin-top: 4px; }
.tk-item-name { font-size: 8.5pt; word-break: break-word; }
.tk-item-detail { display: grid; grid-template-columns: 1fr 32px 62px; gap: 2px; font-size: 8pt; margin-bottom: 4px; }
.tk-spacer { height: 10mm; }

/* ===== RESPONSIVE BREAKPOINTS ===== */
@media (max-width: 1200px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .form-grid-4 { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 768px) {
  .main-content { padding: 12px; padding-top: calc(var(--topbar) + 12px); }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat-val { font-size: 20px; }
  .form-grid, .form-grid-3, .form-grid-4 { grid-template-columns: 1fr; }
  .col-full { grid-column: 1; }
  /* Hide table, show cards on mobile */
  .table-responsive { display: none; }
  .mob-cards { display: block; }
  /* Hide desktop lineas, show mobile lineas */
  .linea-head-row, .linea-row { display: none; }
  .linea-mob { display: block; }
  /* Hide NEW/Add button in page header on mobile (replaced by FAB) */
  .page-actions .btn-primary { display: none !important; }
  /* ALWAYS show buttons inside form action rows */
  .form-actions .btn          { display: inline-flex !important; }
  .form-actions .btn-primary  { display: inline-flex !important; }
  .form-actions .btn-secondary{ display: inline-flex !important; }
  .page-header { margin-bottom: 14px; }
}
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .stat-card { padding: 12px; }
  .stat-val { font-size: 18px; }
  .card { padding: 14px; }
  .qs-bar { flex-direction: column; align-items: stretch; }
  .qs-wrap { max-width: 100%; }
}
@media (min-width: 769px) {
  .linea-mob { display: none !important; }
  .linea-head-row, .linea-row { display: grid !important; }
  .mob-cards { display: none !important; }
  .table-responsive { display: block !important; }
  .d-md-none { display: none !important; }
  .topnav-menu.d-none.d-md-flex { display: flex !important; }
}

/* ===== PRINTER STATUS ===== */
.topnav-right { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.printer-status {
  font-size: 12px; font-weight: 500; padding: 4px 10px;
  border-radius: 20px; white-space: nowrap;
  transition: all .3s;
}
.printer-status.connected {
  background: var(--success-bg); color: var(--success);
  border: 1px solid #bbf7d0;
}
.printer-status.disconnected {
  background: var(--surface2); color: var(--text3);
  border: 1px solid var(--border);
}
.btn-success-outline {
  background: var(--success-bg) !important;
  color: var(--success) !important;
  border-color: #bbf7d0 !important;
}

/* ===== MOBILE FAB (Floating Action Button) ===== */
.fab {
  display: none;
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 26px;
  line-height: 1;
  border: none;
  cursor: pointer;
  z-index: 90;
  box-shadow: 0 4px 16px rgba(37,99,235,.45);
  align-items: center;
  justify-content: center;
  text-decoration: none;
  transition: background .15s, transform .15s;
}
.fab:hover, .fab:active { background: #1d4ed8; transform: scale(1.08); }

@media (max-width: 768px) {
  /* Show FAB, hide desktop button in header */
  .fab { display: flex; }
  .page-actions .btn-primary { display: none !important; }
  /* But keep primary buttons INSIDE forms visible */
  .form-actions .btn-primary { display: inline-flex !important; }
  .card .btn-primary { display: inline-flex !important; }
}
