:root {
  --cream-1: #fbf7ef;
  --cream-2: #f6ecd8;
  --cream-3: #efe0c4;
  --ink: #3a3026;
  --ink-soft: #7a6e5d;
  --line: #e7dcc7;
  --accent: #b07d3c;       /* warm gold/bronze */
  --accent-dark: #8c612b;
  --accent-soft: #f0e3cd;
  --teal: #2f6f6b;
  --danger: #b3463c;
  --danger-soft: #f6e0dd;
  --ok: #3f7d57;
  --white: #fffdf8;
  --shadow: 0 12px 32px rgba(78, 58, 26, 0.10);
  --shadow-sm: 0 4px 14px rgba(78, 58, 26, 0.08);
  --radius: 16px;
}

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

body {
  font-family: 'Inter', system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(135deg, var(--cream-1) 0%, var(--cream-2) 45%, var(--cream-3) 100%);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ============ AUTH ============ */
.auth-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 18px;
}
.auth-card {
  width: 100%;
  max-width: 410px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 34px 32px;
}
.auth-brand { text-align: center; margin-bottom: 22px; }
.brand-logo { width: 200px; max-width: 80%; height: auto; margin: 0 auto 16px; display: block; }
.sidebar-logo { width: 100%; max-width: 180px; height: auto; display: block; }
.logo-mark {
  width: 58px; height: 58px;
  margin: 0 auto 14px;
  display: grid; place-items: center;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: #fff; font-weight: 800; font-size: 22px;
  letter-spacing: 1px;
  box-shadow: 0 8px 18px rgba(176, 125, 60, 0.35);
}
.logo-mark.small { width: 40px; height: 40px; font-size: 15px; border-radius: 12px; margin: 0; }
.auth-brand h1 { font-size: 24px; font-weight: 800; line-height: 1.15; }
.auth-brand h1 span { color: var(--accent); }
.auth-tagline { color: var(--ink-soft); font-size: 13px; margin-top: 8px; }

.auth-tabs {
  display: flex; gap: 6px;
  background: var(--cream-2);
  padding: 5px; border-radius: 12px; margin-bottom: 20px;
}
.auth-tab {
  flex: 1; border: none; background: transparent; cursor: pointer;
  padding: 10px; border-radius: 9px; font-weight: 600; font-size: 14px;
  color: var(--ink-soft); transition: 0.18s;
}
.auth-tab.active { background: var(--white); color: var(--ink); box-shadow: var(--shadow-sm); }

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label, .modal label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; font-weight: 600; color: var(--ink-soft);
}
input, select, textarea {
  font-family: inherit; font-size: 14px; color: var(--ink);
  padding: 11px 13px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--white); transition: 0.15s; width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.hint { font-size: 12px; color: var(--ink-soft); text-align: center; }
.hint strong { color: var(--accent); }
.auth-foot { color: var(--ink-soft); font-size: 12px; }

.btn-primary {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  color: #fff; border: none; cursor: pointer;
  padding: 12px 18px; border-radius: 10px; font-weight: 600; font-size: 14px;
  transition: 0.16s; box-shadow: 0 6px 16px rgba(176,125,60,0.28);
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 9px 20px rgba(176,125,60,0.36); }
.btn-primary:active { transform: translateY(0); }
.btn-ghost {
  background: var(--cream-2); border: 1px solid var(--line); color: var(--ink);
  padding: 9px 14px; border-radius: 10px; font-weight: 600; cursor: pointer; font-size: 13px;
  transition: 0.15s; width: 100%;
}
.btn-ghost:hover { background: var(--cream-3); }
.btn-sm { padding: 7px 12px; font-size: 12.5px; border-radius: 8px; cursor: pointer; font-weight: 600; border: 1px solid var(--line); background: var(--white); color: var(--ink); transition: 0.14s; }
.btn-sm:hover { background: var(--cream-2); }
.btn-danger { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }
.btn-danger:hover { background: #f0cfca; }

.form-msg { font-size: 13px; min-height: 18px; color: var(--danger); font-weight: 500; }
.form-msg.ok { color: var(--ok); }

/* ============ APP SHELL ============ */
.app-shell { display: grid; grid-template-columns: 268px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--white);
  border-right: 1px solid var(--line);
  padding: 22px 18px;
  display: flex; flex-direction: column; gap: 22px;
  position: sticky; top: 0; height: 100vh;
}
.sidebar-brand { display: flex; align-items: center; gap: 12px; }
.sidebar-brand strong { display: block; font-size: 15px; }
.sidebar-brand span { font-size: 12px; color: var(--ink-soft); }

.side-nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  background: transparent; border: none; cursor: pointer;
  padding: 11px 13px; border-radius: 11px; font-size: 14px; font-weight: 600;
  color: var(--ink-soft); text-align: left; transition: 0.14s;
}
.nav-item:hover { background: var(--cream-2); color: var(--ink); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-dark); }
.nav-ico { font-size: 16px; }

.side-divisions { flex: 1; }
.side-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--ink-soft); margin-bottom: 10px; font-weight: 700; }
.division-chips { display: flex; flex-direction: column; gap: 4px; }
.chip {
  text-align: left; border: none; background: transparent; cursor: pointer;
  padding: 8px 12px; border-radius: 9px; font-size: 13px; color: var(--ink);
  display: flex; justify-content: space-between; align-items: center; transition: 0.14s;
}
.chip:hover { background: var(--cream-2); }
.chip.active { background: var(--accent); color: #fff; }
.chip .count { font-size: 11px; opacity: 0.7; background: rgba(0,0,0,0.06); padding: 1px 7px; border-radius: 20px; }
.chip.active .count { background: rgba(255,255,255,0.25); }

.sidebar-foot { border-top: 1px solid var(--line); padding-top: 16px; display: flex; flex-direction: column; gap: 12px; }
.user-badge { display: flex; align-items: center; gap: 11px; }
.avatar {
  width: 40px; height: 40px; border-radius: 11px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--teal) 0%, #244f4c 100%); color: #fff;
  font-weight: 700; font-size: 16px;
}
.user-meta { display: flex; flex-direction: column; gap: 3px; overflow: hidden; }
.user-meta strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role-pill {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px;
  padding: 2px 8px; border-radius: 20px; width: fit-content;
  background: var(--accent-soft); color: var(--accent-dark);
}
.role-pill.superadmin { background: #e7dcfb; color: #6b46c1; }
.role-pill.admin { background: #d6ece4; color: var(--teal); }

/* ============ MAIN ============ */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 22px 30px; border-bottom: 1px solid var(--line);
  background: rgba(255,253,248,0.7); backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 5;
}
.topbar h2 { font-size: 20px; font-weight: 700; }
.topbar-actions { display: flex; gap: 12px; align-items: center; }
.search-box input { width: 240px; }

.content { padding: 28px 30px; }

/* ============ SOP CARDS ============ */
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 18px; }
.sop-card {
  background: var(--white); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; display: flex; flex-direction: column; gap: 12px; transition: 0.16s;
  box-shadow: var(--shadow-sm);
}
.sop-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); }
.sop-top { display: flex; gap: 13px; align-items: flex-start; }
.pdf-ico {
  width: 44px; height: 44px; border-radius: 11px; flex-shrink: 0;
  background: var(--danger-soft); color: var(--danger);
  display: grid; place-items: center; font-weight: 800; font-size: 11px; letter-spacing: 0.5px;
}
.sop-info { min-width: 0; }
.sop-info h3 { font-size: 15px; font-weight: 700; line-height: 1.3; margin-bottom: 4px; }
.sop-info .desc { font-size: 12.5px; color: var(--ink-soft); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sop-tags { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tag { font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 20px; background: var(--accent-soft); color: var(--accent-dark); }
.sop-meta { font-size: 11px; color: var(--ink-soft); }
.sop-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: auto; padding-top: 6px; border-top: 1px solid var(--line); }
.sop-actions .btn-sm { flex: 1; min-width: 70px; }

/* ============ TABLE ============ */
.data-table { width: 100%; border-collapse: collapse; background: var(--white); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); border: 1px solid var(--line); }
.data-table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--ink-soft); padding: 14px 18px; background: var(--cream-2); font-weight: 700; }
.data-table td { padding: 14px 18px; border-top: 1px solid var(--line); font-size: 14px; vertical-align: middle; }
.data-table tr:hover td { background: var(--cream-1); }
.role-select { width: auto; min-width: 130px; padding: 7px 10px; }
.cell-actions { display: flex; gap: 8px; }

/* ============ EMPTY / LOADING ============ */
.empty, .loading { text-align: center; padding: 70px 20px; color: var(--ink-soft); }
.empty .big { font-size: 46px; margin-bottom: 12px; }
.empty h3 { color: var(--ink); font-size: 17px; margin-bottom: 6px; }

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(58, 48, 38, 0.45);
  backdrop-filter: blur(3px); display: flex; align-items: center; justify-content: center;
  padding: 24px; z-index: 50; animation: fade 0.15s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--white); border-radius: 20px; box-shadow: var(--shadow);
  width: 100%; max-width: 480px; max-height: 90vh; overflow: auto;
  padding: 26px; animation: pop 0.18s ease;
}
@keyframes pop { from { transform: scale(0.96); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-head h3 { font-size: 18px; font-weight: 700; }
.modal-close { background: var(--cream-2); border: none; width: 34px; height: 34px; border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--ink-soft); transition: 0.14s; }
.modal-close:hover { background: var(--cream-3); }
.modal form { display: flex; flex-direction: column; gap: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 6px; }
.modal-actions .btn-ghost { width: auto; }

/* PDF viewer modal */
.modal.viewer { max-width: 1000px; width: 95vw; height: 92vh; display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.viewer-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 22px; border-bottom: 1px solid var(--line); }
.viewer-head h3 { font-size: 16px; }
.viewer-head .vactions { display: flex; gap: 10px; align-items: center; }
.viewer iframe { flex: 1; width: 100%; border: none; background: #525659; }

/* Toast */
#toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 12px; font-size: 13.5px; font-weight: 500;
  box-shadow: var(--shadow); animation: pop 0.2s ease; max-width: 90vw;
}
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* Hamburger + drawer overlay (hidden on desktop) */
.hamburger { display: none; }
.drawer-overlay { display: none; }

/* ============ RESPONSIVE / MOBILE ============ */
@media (max-width: 860px) {
  /* Single column; sidebar becomes an off-canvas drawer */
  .app-shell { display: block; }

  .sidebar {
    position: fixed; top: 0; left: 0;
    width: 82%; max-width: 300px; height: 100vh;
    transform: translateX(-105%);
    transition: transform 0.26s ease;
    z-index: 60; box-shadow: var(--shadow);
    overflow-y: auto;
    flex-direction: column;
  }
  .sidebar.open { transform: translateX(0); }

  .drawer-overlay {
    position: fixed; inset: 0;
    background: rgba(58, 48, 38, 0.45);
    backdrop-filter: blur(2px);
    z-index: 55;
  }
  .drawer-overlay.show { display: block; }

  /* Hamburger button in the top bar */
  .hamburger {
    display: inline-flex; align-items: center; justify-content: center;
    width: 42px; height: 42px; flex-shrink: 0;
    border: 1px solid var(--line); background: var(--white);
    border-radius: 11px; font-size: 20px; color: var(--ink);
    cursor: pointer;
  }
  .hamburger:active { background: var(--cream-2); }

  /* Top bar: title row, then search + action row */
  .topbar {
    flex-wrap: wrap; gap: 10px; padding: 12px 16px;
  }
  .topbar h2 { font-size: 18px; flex: 1; min-width: 0; }
  .topbar-actions { width: 100%; gap: 10px; }
  .search-box { flex: 1; }
  .search-box input { width: 100%; }
  #upload-btn { white-space: nowrap; padding: 11px 14px; }

  .content { padding: 16px; }
  .card-grid { grid-template-columns: 1fr; }

  /* Tables scroll horizontally instead of breaking layout */
  .content > .data-table,
  .data-table { display: block; overflow-x: auto; white-space: nowrap; }

  /* Modals fill more of the small screen */
  .modal { max-width: 100%; padding: 20px; }
  .modal.viewer { width: 100vw; height: 100vh; max-width: 100vw; border-radius: 0; }
  .modal-overlay { padding: 0; }
}

@media (max-width: 420px) {
  .sop-actions .btn-sm { flex: 1 1 calc(50% - 4px); }
}
