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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #eff6ff;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --warning: #d97706;
  --warning-bg: #fffbeb;
  --error: #dc2626;
  --error-bg: #fef2f2;
  --dup-bg: #fef9c3;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.1);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html { font-size: 15px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* Layout */
.app-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  height: 56px;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}
.app-header .header-logo {
  height: 32px;
  width: auto;
  margin-right: 0.75rem;
}
.app-header h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--primary);
  margin-right: 2rem;
  white-space: nowrap;
}
.app-header h1 span { color: var(--text); font-weight: 400; }

.tab-nav { display: flex; gap: .25rem; height: 100%; }
.tab-nav button {
  background: none;
  border: none;
  padding: 0 1rem;
  font-size: .875rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all .15s;
  font-family: inherit;
}
.tab-nav button:hover { color: var(--text); }
.tab-nav button.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 1.5rem;
}

.tab-content { display: none; }
.tab-content.active { display: block; }

/* Sub-tabs */
.sub-tab-nav {
  display: flex;
  gap: .125rem;
  margin-bottom: 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .25rem;
  box-shadow: var(--shadow);
}
.sub-tab-nav button {
  flex: 1;
  background: none;
  border: none;
  padding: .5rem .75rem;
  font-size: .8125rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: calc(var(--radius) - 2px);
  transition: all .15s;
  font-family: inherit;
  text-align: center;
}
.sub-tab-nav button:hover { color: var(--text); background: var(--bg); }
.sub-tab-nav button.active {
  color: var(--primary);
  background: var(--primary-light);
}
.subtab-content { display: none; }
.subtab-content.active { display: block; }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

/* Form elements */
.form-group { margin-bottom: .875rem; }
.form-group:last-child { margin-bottom: 0; }

label {
  display: block;
  font-size: .8125rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: .375rem;
  text-transform: uppercase;
  letter-spacing: .03em;
}

select, input[type="text"], input[type="number"], textarea {
  width: 100%;
  padding: .5rem .75rem;
  font-size: .875rem;
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  transition: border-color .15s;
}
select:focus, input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
textarea {
  resize: vertical;
  min-height: 120px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: .8125rem;
  line-height: 1.6;
}

select { cursor: pointer; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .5rem 1rem;
  font-size: .875rem;
  font-weight: 600;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .15s;
  background: var(--card);
  color: var(--text);
}
.btn:hover { background: var(--bg); }
.btn:active { transform: scale(.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-success {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}
.btn-success:hover { background: #15803d; }

.btn-danger {
  background: var(--error);
  color: #fff;
  border-color: var(--error);
}
.btn-danger:hover { background: #b91c1c; }

.btn-sm { padding: .3rem .6rem; font-size: .8125rem; }
.btn-icon { padding: .3rem .5rem; font-size: .875rem; }

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.toolbar .spacer { flex: 1; }
.toolbar select, .toolbar input { width: auto; }

.summary-bar {
  display: flex;
  gap: 1rem;
  align-items: center;
  padding-top: 0.75rem;
  margin-top: 0.75rem;
  border-top: 1px solid var(--border);
  font-size: .8125rem;
  color: var(--text-muted);
}
.summary-bar strong { color: var(--text); font-weight: 700; }
.summary-divider { color: var(--border); }

/* Table */
.table-wrap {
  position: relative;
  overflow: visible;
}
.table-scroll {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8125rem;
}
thead { background: #f8fafc; }
th {
  padding: .6rem .75rem;
  text-align: left;
  font-weight: 600;
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
th.sortable {
  cursor: pointer;
  user-select: none;
  transition: color .15s;
}
th.sortable:hover { color: var(--text); }
.sort-icon {
  display: inline-block;
  width: 14px;
  height: 14px;
  vertical-align: middle;
  margin-left: 2px;
  color: var(--primary);
}
td {
  padding: .5rem .75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:hover { background: #f8fafc; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.dup {
  background: var(--dup-bg);
}
tbody tr.dup:hover { background: #fef08a; }

td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { white-space: nowrap; }
td.actions button { margin-right: .25rem; }

.row-action {
  position: absolute;
  z-index: 5;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: var(--card);
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  border-radius: 50%;
}
.row-action.visible {
  opacity: 1;
  pointer-events: auto;
}
.row-action:hover {
  background: #f1f5f9;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
}
.row-action-edit {
  right: calc(100% - 4px);
}
.row-action-delete {
  left: calc(100% - 4px);
}

/* Log */
.log-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem 1rem;
  font-size: .8125rem;
  line-height: 1.7;
}
.log-box .ok { color: var(--success); font-weight: 600; }
.log-box .warn { color: var(--warning); }
.log-box .err { color: var(--error); }
.log-box .line { padding-left: 1rem; font-family: 'SF Mono', 'Consolas', monospace; font-size: .75rem; }

/* Preview table inside parse tab */
.preview-table { margin: 1rem 0; }

/* Config margins table */
.margins-table input[type="number"] {
  width: 64px;
  height: 32px;
  padding: 0 .25rem;
  text-align: center;
  font-size: .8125rem;
  border: 1px solid transparent;
  border-radius: 2px;
  background: transparent;
  transition: background .15s, border-color .15s;
  -moz-appearance: textfield;
}
.margins-table input[type="number"]:hover {
  background: var(--bg);
  border-color: var(--border);
}
.margins-table input[type="number"]:focus {
  background: var(--card);
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37,99,235,.12);
}
.margins-table input[type="number"]::-webkit-outer-spin-button,
.margins-table input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.margins-table td:first-child { font-weight: 500; font-size: .8125rem; }

.margins-table .has-stock { font-weight: 700; color: var(--text); }
.margins-table .no-stock { font-weight: 400; color: var(--text-muted); }

.margins-table .col-usados { background: #eff6ff; border-right: 2px solid var(--primary); }
.margins-table .col-sellados { background: #f0fdf4; }
.margins-table thead tr:last-child th:nth-child(4) { border-right: 2px solid var(--primary); }
.margins-table tbody td:nth-child(4) { border-right: 2px solid var(--primary); }

.margins-table td.row-has-stock {
  background: #d4edda;
}
.margins-table td.row-has-stock input[type="number"] {
  background: #d4edda;
  border-color: transparent;
}
.margins-table td.row-has-stock input[type="number"]:hover {
  border-color: transparent;
  background: #d4edda;
}

.margen-row-action {
  position: absolute;
  z-index: 5;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: var(--card);
  box-shadow: 0 1px 3px rgba(0,0,0,.15);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  border-radius: 50%;
  left: calc(100% - 4px);
}
.margen-row-action.visible {
  opacity: 1;
  pointer-events: auto;
}
.margen-row-action:hover {
  background: #f1f5f9;
  box-shadow: 0 2px 6px rgba(0,0,0,.2);
}

/* Checkbox inline */
.checkbox-inline {
  display: inline-flex;
  align-items: center;
  gap: .25rem;
  font-size: .8125rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  margin: 0;
}
.checkbox-inline input[type="checkbox"] {
  width: auto;
  margin: 0;
  cursor: pointer;
}

/* Adjustments */
.adj-table input[type="number"] {
  width: 80px;
  padding: .25rem .4rem;
  text-align: center;
  font-size: .8125rem;
}
.adj-table td:first-child { font-weight: 500; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 1.5rem;
  width: 90%;
  max-width: 400px;
}
.modal h3 { margin-bottom: 1rem; font-size: 1rem; }
.modal .form-group { margin-bottom: .75rem; }
.modal .form-actions {
  display: flex;
  gap: .5rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}
.toast {
  background: var(--text);
  color: #fff;
  padding: .75rem 1rem;
  border-radius: var(--radius);
  font-size: .875rem;
  box-shadow: var(--shadow-lg);
  animation: toast-in .25s ease-out;
  max-width: 360px;
}
.toast-success { background: var(--success); }
.toast-error { background: var(--error); }
.toast-warning { background: var(--warning); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(1rem); }
  to { opacity: 1; transform: translateY(0); }
}

/* Badge / inline tag */
.badge {
  display: inline-block;
  padding: .1rem .4rem;
  border-radius: 4px;
  font-size: .7rem;
  font-weight: 600;
  text-transform: uppercase;
}
.badge-invoice {
  background: var(--primary-light);
  color: var(--primary);
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
}
.empty-state p { font-size: .875rem; margin-top: .5rem; }

/* Responsive */
@media (max-width: 640px) {
  .app-header { flex-wrap: wrap; height: auto; padding: .75rem 1rem; gap: .5rem; }
  .app-header h1 { font-size: 1rem; margin-right: 0; width: 100%; }
  .tab-nav { width: 100%; }
  .tab-nav button { flex: 1; text-align: center; font-size: .8125rem; padding: .5rem; }
  .container { padding: 1rem; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar select, .toolbar input { width: 100%; }
  .modal { width: 95%; padding: 1rem; }
}
