/* ============================================================
   Nazara ERP — Base Styles
   ============================================================ */

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

:root {
  --bg:        #f5f6fa;
  --surface:   #ffffff;
  --border:    #e2e6ef;
  --primary:   #2563eb;
  --primary-h: #1d4ed8;
  --danger:    #dc2626;
  --danger-h:  #b91c1c;
  --text:      #1e293b;
  --muted:     #64748b;
  --sidebar-w: 220px;
  --radius:    8px;
  --shadow:    0 1px 4px rgba(0,0,0,.08);
}

html, body { overscroll-behavior: none; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---- Sidebar ---- */
#sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

#sidebar .brand {
  padding: 18px 16px 12px;
  font-size: 16px;
  font-weight: 700;
  color: var(--primary);
  border-bottom: 1px solid var(--border);
  letter-spacing: -.3px;
}

#sidebar .nav-section {
  padding: 10px 8px 4px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--muted);
}

#sidebar .nav-item {
  display: block;
  padding: 8px 12px;
  margin: 1px 6px;
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text);
  font-size: 13.5px;
  transition: background .12s;
  user-select: none;
}

#sidebar .nav-item:hover  { background: var(--bg); }
#sidebar .nav-item.active { background: #eff6ff; color: var(--primary); font-weight: 600; }

/* ---- Main ---- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#topbar {
  min-height: 48px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: env(safe-area-inset-top, 0px) 20px 0;
  gap: 8px;
  flex-shrink: 0;
}

#topbar .page-title {
  font-size: 15px;
  font-weight: 600;
}

#workspace {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  transition: opacity .15s ease;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
}
#workspace.fade-out { opacity: 0; }

/* ---- Card ---- */
@keyframes fade-up {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
  animation: fade-up .25s ease both;
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 14px;
  color: var(--text);
}

/* ---- Table ---- */
.tbl-wrap { overflow-x: auto; }
#jc-lines-section .tbl-wrap { overflow: visible; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

thead th {
  text-align: left;
  padding: 9px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .4px;
}

tbody td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8faff; }

/* ---- Form ---- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.form-group { display: flex; flex-direction: column; gap: 5px; }

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .4px;
}

input[type=text], input[type=number], input[type=date], input[type=password], select, textarea {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 16px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color .15s;
  width: 100%;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37,99,235,.12);
}

textarea { resize: vertical; min-height: 72px; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .12s, border-color .12s;
}

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

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

.btn-outline  { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover  { background: var(--bg); }

.btn-sm { padding: 4px 10px; font-size: 12.5px; }

/* Touch feedback */
.btn:active { transform: scale(0.97); transition: transform 0.1s ease; }
#sidebar .nav-item:active { background: #e0e7ff; }
tbody tr:active { background: #eff6ff; }

/* Prevent text selection on interactive elements */
.btn, #hamburger-btn, .dash-filter-bar .btn, .signout-link {
  -webkit-user-select: none;
  user-select: none;
}

/* ---- Misc ---- */
.flex-row   { display: flex; align-items: center; gap: 10px; }
.flex-end   { justify-content: flex-end; }
.mt-12      { margin-top: 12px; }
.text-muted { color: var(--muted); }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  background: #eff6ff;
  color: var(--primary);
}

/* ---- Empty state ---- */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
  font-size: 13.5px;
}

/* ---- Toast ---- */
#toast-container {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom, 20px));
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 9999;
}

.toast {
  background: #1e293b;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13.5px;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  animation: slide-in .2s ease;
}

.toast.error { background: var(--danger); }

@keyframes slide-in {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---- Auth mode (login / setup pages) ---- */
body.auth-mode #sidebar { display: none; }
body.auth-mode #topbar  { display: none; }
body.auth-mode #workspace { padding: 0; }

.login-center {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100%;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,.1);
  padding: 36px 32px;
  width: 100%;
  max-width: 380px;
}

.login-brand {
  font-size: 17px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
  letter-spacing: -.3px;
}

.login-card h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 20px;
  color: var(--text);
}

.setup-hint {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 16px;
  margin-top: -12px;
  line-height: 1.5;
}

.login-error {
  background: #fef2f2;
  border: 1px solid #fca5a5;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--danger);
  margin-top: 10px;
}

/* ---- User info bar (bottom of sidebar) ---- */
#sidebar {
  /* ensure flex-column is set for margin-top:auto on user-info-bar */
}

.user-info-bar {
  margin-top: auto;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.user-info-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.signout-link {
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
}

.signout-link:hover { color: var(--danger); }

.notif-toggle {
  display: flex; align-items: center; gap: 4px; cursor: pointer;
  -webkit-user-select: none; user-select: none;
}
.notif-toggle input { width: 14px; height: 14px; cursor: pointer; accent-color: var(--primary); }
.notif-bell { font-size: 14px; }

/* ---- Permission badges ---- */
.perm-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  margin: 1px;
}
.perm-badge.admin    { background: #fef3c7; color: #92400e; }
.perm-badge.perm-yes { background: #d1fae5; color: #065f46; }
.perm-badge.perm-no  { background: #f3f4f6; color: #9ca3af; }

/* ---- Permission form (categories module) ---- */
.perm-section {
  margin-top: 16px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}

.perm-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  margin-bottom: 10px;
}

.perm-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.perm-check-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13.5px;
  font-weight: normal;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

.perm-check-label input[type=checkbox] {
  width: 15px;
  height: 15px;
  cursor: pointer;
  border: 1px solid var(--border);
  border-radius: 3px;
  accent-color: var(--primary);
}

/* ---- Stitch Log alert badge + unread row ---- */
.alert-badge {
  background: #e74c3c;
  color: #fff;
  border-radius: 10px;
  padding: 1px 7px;
  font-size: 0.75rem;
  margin-left: 6px;
  vertical-align: middle;
}

.alert-unread {
  background: #fff8e1;
}

/* ---- Job Card combo-box ---- */
.jc-combo { position: relative; display: inline-block; width: 100%; }
.jc-combo input {
  width: 100%; padding: 4px 6px; font-size: 13px;
  border: 1px solid var(--border); border-radius: 4px;
  background: var(--surface); color: var(--text);
}
.jc-combo input:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 2px rgba(37,99,235,.12); }
.jc-combo-list {
  position: absolute; top: 100%; left: 0; right: 0;
  max-height: 180px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 0 0 6px 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,.12);
  z-index: 100; display: none;
}
.jc-combo-list.open { display: block; }
.jc-combo-item { padding: 5px 8px; font-size: 13px; cursor: pointer; }
.jc-combo-item:hover, .jc-combo-item.highlighted { background: #eff6ff; color: var(--primary); }
.jc-combo-item.no-match { color: var(--muted); font-style: italic; pointer-events: none; }

/* Compact job card lines table */
#jc-lines-section table { font-size: 13px; }
#jc-lines-section th { padding: 5px 6px; font-size: 11px; white-space: nowrap; }
#jc-lines-section td { padding: 4px 4px; vertical-align: middle; }
#jc-lines-section .jc-sr { width: 32px; text-align: center; color: var(--muted); font-weight: 600; }
#jc-lines-section .jc-meters { width: 72px; text-align: right; font-variant-numeric: tabular-nums; }
#jc-lines-section .jc-tq-col { border-right: 1px solid var(--border); }
#jc-lines-section th.jc-color-group { text-align: center; border-bottom: none; }
#jc-lines-section th.jc-sub-col { font-size: 10px; text-align: center; padding: 3px 4px; }

/* Compact color matching lines table */
#cm-lines-section table { font-size: 13px; }
#cm-lines-section th { padding: 5px 6px; font-size: 11px; white-space: nowrap; }
#cm-lines-section td { padding: 4px 4px; vertical-align: middle; }
#cm-lines-section .cm-sr { width: 32px; text-align: center; color: var(--muted); font-weight: 600; }
#cm-lines-section .jc-tq-col { border-right: 1px solid var(--border); }
#cm-lines-section th.jc-color-group { text-align: center; border-bottom: none; }
#cm-lines-section th.jc-sub-col { font-size: 10px; text-align: center; padding: 3px 4px; }
#cm-lines-section .tbl-wrap { overflow: visible; }

/* CM line picker in JC form */
#jc-cm-line-picker input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }

/* Dashboard */
.dash-filter-bar { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.dash-filter-bar .btn.active { background: var(--primary); color: #fff; }
.dash-kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.dash-kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 14px; text-align: center; }
.dash-kpi-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text); line-height: 1.2; }
.dash-kpi-label { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin-top: 4px; }
.dash-charts-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 20px; }
.dash-chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-height: 280px; }
.dash-chart-card h3 { margin: 0 0 12px; font-size: 14px; color: var(--text); }
@media (max-width: 768px) {
  .dash-kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .dash-charts-row { grid-template-columns: 1fr; }
}

/* ---- Hamburger button ---- */
#hamburger-btn {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  line-height: 1;
}

/* ---- Sidebar overlay ---- */
#sidebar-overlay {
  display: block;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 999;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#sidebar-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

/* ---- Mobile responsive ---- */
@media (max-width: 768px) {
  #hamburger-btn { display: block; }

  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform .25s ease;
    width: var(--sidebar-w);
  }
  #sidebar.open { transform: translateX(0); }

  /* sidebar-overlay uses opacity-based transition now */

  #workspace { padding: 12px; }

  .tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tbl-wrap table { min-width: 600px; }
  #jc-lines-section .tbl-wrap table,
  #cm-lines-section .tbl-wrap table { min-width: 900px; }

  .form-grid { grid-template-columns: 1fr; }

  .card { padding: 14px; }

  .login-card { margin: 16px; padding: 24px 20px; }

  body.auth-mode #hamburger-btn { display: none; }
  body.auth-mode #sidebar-overlay { display: none; }
}

@media (max-width: 400px) {
  #workspace { padding: 8px; }
  .card { padding: 10px; }
  .login-card { padding: 20px 16px; }
}

/* ---- Offline Banner ---- */
#offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #f59e0b;
  color: #78350f;
  text-align: center;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  z-index: 10000;
}

/* ---- Install Banner ---- */
#install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(0,0,0,.1);
  padding: 12px 20px;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 10000;
  font-size: 13.5px;
}
#install-banner span { flex: 1; }

/* ---- Update Banner ---- */
#update-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #eff6ff;
  border-top: 1px solid #bfdbfe;
  padding: 10px 20px;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 10000;
  font-size: 13.5px;
  color: var(--primary);
  font-weight: 500;
}
#update-banner span { flex: 1; }
