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

body { font-family: 'Segoe UI', system-ui, sans-serif; background: #f0f4f8; color: #2d3748; font-size: 14px; }

.hidden { display: none !important; }

/* === LOGIN === */
#login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #1a2332 0%, #2d4a6e 100%); }
.login-card { background: #fff; border-radius: 12px; padding: 40px; width: 360px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.login-logo { font-size: 22px; font-weight: 700; color: #1a2332; text-align: center; margin-bottom: 24px; letter-spacing: -0.5px; }
.tab-bar { display: flex; border-bottom: 2px solid #eee; margin-bottom: 24px; }
.tab-btn { flex: 1; padding: 10px; background: none; border: none; cursor: pointer; color: #888; font-size: 14px; font-weight: 500; transition: color 0.2s; }
.tab-btn.active { color: #2d4a6e; border-bottom: 2px solid #2d4a6e; margin-bottom: -2px; }

/* === LAYOUT === */
#app-page { display: flex; height: 100vh; }

/* === SIDEBAR === */
.sidebar { width: 220px; background: #1a2332; display: flex; flex-direction: column; flex-shrink: 0; overflow: hidden; }
.sidebar-brand { padding: 20px 16px; font-size: 16px; font-weight: 700; color: #fff; border-bottom: 1px solid rgba(255,255,255,0.1); }
.sidebar-nav { flex: 1; padding: 8px 0; overflow-y: auto; }
.nav-link { display: block; padding: 11px 20px; color: rgba(255,255,255,0.7); cursor: pointer; font-size: 13.5px; transition: all 0.15s; border-left: 3px solid transparent; }
.nav-link:hover { color: #fff; background: rgba(255,255,255,0.07); }
.nav-link.active { color: #fff; background: rgba(255,255,255,0.12); border-left-color: #48bb78; }
.sidebar-footer { padding: 16px; border-top: 1px solid rgba(255,255,255,0.1); display: flex; flex-direction: column; gap: 6px; }
.sidebar-user { color: rgba(255,255,255,0.7); font-size: 12px; padding-bottom: 4px; }

/* === MAIN CONTENT === */
.main-content { flex: 1; overflow-y: auto; }
.section { padding: 24px; }

/* === CARDS === */
.card { background: #fff; border-radius: 8px; padding: 20px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); border: 1px solid #e8ecf0; }
.card-title { font-size: 16px; font-weight: 600; color: #1a2332; margin-bottom: 16px; }
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.section-header h2 { font-size: 20px; font-weight: 700; color: #1a2332; }

/* === TABLES === */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead tr { background: #f7fafc; }
th { padding: 10px 14px; text-align: left; font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: #718096; border-bottom: 2px solid #e8ecf0; }
td { padding: 11px 14px; border-bottom: 1px solid #f0f4f8; color: #4a5568; vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f7fafc; }
.clickable-row { cursor: pointer; }
.clickable-row:hover td { background: #ebf4ff; }

/* === BUTTONS === */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 13.5px; font-weight: 500; transition: all 0.15s; }
.btn.primary { background: #2d6a4f; color: #fff; }
.btn.primary:hover { background: #245a42; }
.btn.danger { background: #e53e3e; color: #fff; }
.btn.danger:hover { background: #c53030; }
.btn.ghost { background: #edf2f7; color: #4a5568; }
.btn.ghost:hover { background: #e2e8f0; }
.btn.warning { background: #dd6b20; color: #fff; }
.btn.warning:hover { background: #c05621; }
.btn.success { background: #276749; color: #fff; }
.btn.success:hover { background: #1e4f37; }
.btn.sm { padding: 5px 10px; font-size: 12px; border-radius: 4px; }
.btn.full { width: 100%; justify-content: center; margin-top: 8px; }
.btn.outline { background: transparent; border: 1px solid #2d6a4f; color: #2d6a4f; }
.btn.outline:hover { background: #f0fff4; }
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }

/* === FORMS === */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: #4a5568; margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.4px; }
.field input, .field select, .field textarea { width: 100%; padding: 9px 12px; border: 1px solid #e2e8f0; border-radius: 6px; font-size: 14px; color: #2d3748; background: #fff; transition: border-color 0.15s; outline: none; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: #2d6a4f; box-shadow: 0 0 0 3px rgba(45,106,79,0.1); }
.field textarea { resize: vertical; min-height: 70px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row.three { grid-template-columns: 1fr 1fr 1fr; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; padding-top: 16px; border-top: 1px solid #e8ecf0; }

/* === MODAL === */
#modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 1000; display: flex; align-items: center; justify-content: center; padding: 20px; pointer-events: none; }
.modal-box { background: #fff; border-radius: 10px; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; box-shadow: 0 25px 60px rgba(0,0,0,0.3); pointer-events: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 14px; border-bottom: 1px solid #e8ecf0; }
.modal-head span { font-size: 15px; font-weight: 600; color: #1a2332; }
.modal-x { background: none; border: none; font-size: 22px; cursor: pointer; color: #a0aec0; line-height: 1; padding: 0 4px; }
.modal-x:hover { color: #4a5568; }
#modal-body { padding: 20px; }

/* === CONFIRM === */
#confirm-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 2000; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.confirm-box { background: #fff; border-radius: 10px; padding: 28px; width: 340px; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,0.3); pointer-events: auto; }
.confirm-box p { margin-bottom: 20px; font-size: 15px; color: #4a5568; line-height: 1.5; }
.confirm-actions { display: flex; gap: 10px; justify-content: center; }

/* === TOAST === */
#toast-container { position: fixed; top: 16px; right: 16px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 12px 18px; border-radius: 8px; font-size: 13.5px; font-weight: 500; box-shadow: 0 4px 12px rgba(0,0,0,0.15); animation: slideIn 0.25s ease; max-width: 320px; }
.toast.success { background: #2d6a4f; color: #fff; }
.toast.error { background: #e53e3e; color: #fff; }
.toast.info { background: #2b6cb0; color: #fff; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* === BADGES === */
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.green { background: #c6f6d5; color: #276749; }
.badge.red { background: #fed7d7; color: #9b2c2c; }
.badge.blue { background: #bee3f8; color: #2b6cb0; }
.badge.yellow { background: #fefcbf; color: #744210; }
.badge.gray { background: #edf2f7; color: #718096; }

/* === OVERVIEW === */
.overview-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; margin-bottom: 24px; }
.overview-card { background: #fff; border-radius: 8px; padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); border: 1px solid #e8ecf0; }
.overview-card h3 { font-size: 13px; font-weight: 600; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 10px; }
.stat-row { display: flex; justify-content: space-between; align-items: center; padding: 5px 0; border-bottom: 1px solid #f0f4f8; font-size: 13px; }
.stat-row:last-child { border-bottom: none; }
.stat-row .label { color: #718096; }
.stat-row .val { font-weight: 600; color: #2d3748; }
.stat-row .val.green { color: #276749; }
.stat-row .val.red { color: #9b2c2c; }
.overview-section-title { font-size: 16px; font-weight: 700; color: #1a2332; margin: 24px 0 12px; }

/* === DETAIL VIEW === */
.detail-back { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; color: #2d6a4f; cursor: pointer; font-size: 13.5px; font-weight: 500; }
.detail-back:hover { text-decoration: underline; }
.detail-tabs { display: flex; border-bottom: 2px solid #e8ecf0; margin-bottom: 20px; gap: 0; }
.detail-tab { padding: 10px 20px; cursor: pointer; font-size: 13.5px; font-weight: 500; color: #718096; border-bottom: 2px solid transparent; margin-bottom: -2px; transition: all 0.15s; }
.detail-tab.active { color: #2d6a4f; border-bottom-color: #2d6a4f; }
.detail-tab:hover { color: #2d6a4f; }

/* === INGREDIENTS BUILDER === */
.ingredient-list { border: 1px solid #e8ecf0; border-radius: 6px; overflow: hidden; margin-bottom: 8px; }
.ingredient-row { display: grid; grid-template-columns: 1fr 120px 36px; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid #f0f4f8; }
.ingredient-row:last-child { border-bottom: none; }
.ingredient-row select, .ingredient-row input { border: 1px solid #e2e8f0; border-radius: 4px; padding: 5px 8px; font-size: 13px; }
.ingredient-row button { background: none; border: none; cursor: pointer; color: #e53e3e; font-size: 16px; padding: 0; display: flex; align-items: center; justify-content: center; }

/* === INFO PAIRS === */
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.info-pair { padding: 8px 0; border-bottom: 1px solid #f0f4f8; }
.info-pair .lbl { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px; color: #a0aec0; }
.info-pair .val { font-size: 14px; color: #2d3748; margin-top: 2px; font-weight: 500; }

/* === SUMMARY TOTALS === */
.totals-bar { background: #ebf8ff; border: 1px solid #bee3f8; border-radius: 8px; padding: 16px 20px; margin-bottom: 20px; display: flex; flex-wrap: wrap; gap: 24px; }
.total-item { }
.total-item .t-label { font-size: 11px; font-weight: 600; color: #2b6cb0; text-transform: uppercase; letter-spacing: 0.4px; }
.total-item .t-value { font-size: 18px; font-weight: 700; color: #1a2332; }

/* === EMPTY STATE === */
.empty { text-align: center; padding: 40px; color: #a0aec0; }
.empty p { margin-top: 8px; font-size: 13px; }

/* === COST DETAILS TOOLTIP === */
.cost-detail-list { font-size: 12px; color: #4a5568; }
.cost-detail-list div { padding: 2px 0; }

.profit-bar { height: 6px; border-radius: 3px; background: #e8ecf0; margin-top: 4px; overflow: hidden; }
.profit-bar-fill { height: 100%; background: #48bb78; border-radius: 3px; }

/* === ROLE-BASED VISIBILITY (CSS safety net, complements JS .hidden toggling) === */
body:not(.role-super-admin) .admin-only { display: none !important; }
body:not(.role-super-admin):not(.role-manager) .manager-only { display: none !important; }
body.role-branch-manager-only .no-branch-manager { display: none !important; }
body.role-storage-manager-only .no-storage-manager { display: none !important; }

/* === MOBILE HEADER (hidden on desktop) === */
.mobile-header { display: none; }
.sidebar-overlay { display: none; }

/* ============================================================
   MOBILE RESPONSIVE  ≤ 768px
   ============================================================ */
@media (max-width: 768px) {

  /* --- Top header bar --- */
  .mobile-header {
    display: flex;
    align-items: center;
    gap: 12px;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 52px;
    background: #1a2332;
    z-index: 400;
    padding: 0 16px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  }
  .hamburger {
    background: none;
    border: none;
    color: #fff;
    font-size: 22px;
    cursor: pointer;
    padding: 4px 6px;
    line-height: 1;
    flex-shrink: 0;
  }
  .mobile-brand {
    font-size: 15px;
    font-weight: 700;
    color: #fff;
    flex-shrink: 0;
  }
  .mobile-section-title {
    font-size: 13px;
    color: rgba(255,255,255,0.6);
    margin-left: auto;
    text-transform: capitalize;
  }

  /* --- Sidebar becomes a slide-in drawer --- */
  #app-page { display: block; padding-top: 52px; }

  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 260px;
    z-index: 500;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    overflow-y: auto;
  }
  body.sidebar-open .sidebar { transform: translateX(0); }

  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 499;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s;
  }
  body.sidebar-open .sidebar-overlay {
    opacity: 1;
    pointer-events: auto;
  }

  /* Bigger tap targets for nav links */
  .nav-link { padding: 14px 20px; font-size: 15px; }

  /* --- Main content --- */
  .main-content { width: 100%; min-height: calc(100vh - 52px); }
  .section { padding: 12px; }

  /* --- Section header --- */
  .section-header { flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
  .section-header h2 { font-size: 17px; }

  /* --- Cards --- */
  .card { padding: 14px; margin-bottom: 12px; }

  /* --- Tables: smaller text, allow scroll --- */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  th { padding: 8px 10px; font-size: 11px; }
  td { padding: 9px 10px; font-size: 13px; }
  .btn-group { flex-wrap: wrap; gap: 4px; }
  .btn.sm { padding: 6px 10px; font-size: 12px; }

  /* --- Forms: single column --- */
  .form-row { grid-template-columns: 1fr !important; gap: 0; }
  .form-row.three { grid-template-columns: 1fr !important; }
  .field input, .field select, .field textarea { font-size: 16px; padding: 11px 12px; }
  .form-actions { flex-direction: column-reverse; gap: 8px; }
  .form-actions .btn { width: 100%; justify-content: center; padding: 12px; font-size: 15px; }

  /* --- Modal: bottom sheet --- */
  #modal-overlay { align-items: flex-end; padding: 0; }
  .modal-box {
    border-radius: 16px 16px 0 0 !important;
    max-height: 92vh;
    max-width: 100% !important;
    width: 100%;
    margin: 0;
    position: relative;
  }
  .modal-head { padding: 14px 16px 12px; }
  #modal-body { padding: 14px 16px 24px; }

  /* --- Confirm dialog: full width at bottom --- */
  #confirm-overlay { align-items: flex-end; }
  .confirm-box { width: 100%; border-radius: 16px 16px 0 0; padding: 24px 20px 32px; }
  .confirm-actions { flex-direction: column; }
  .confirm-actions .btn { width: 100%; padding: 12px; font-size: 15px; }

  /* --- Toast: bottom on mobile --- */
  #toast-container { top: auto; bottom: 16px; right: 12px; left: 12px; align-items: stretch; }
  .toast { max-width: 100%; text-align: center; }

  /* --- Overview --- */
  .overview-grid { grid-template-columns: 1fr; gap: 12px; }
  .totals-bar { gap: 14px; padding: 12px 14px; }
  .total-item .t-value { font-size: 15px; }

  /* --- Detail tabs --- */
  .detail-tab { padding: 10px 14px; font-size: 13px; }

  /* --- Info grid: single column on very small screens --- */
  .info-grid { grid-template-columns: 1fr 1fr; }

  /* --- Buttons: larger touch targets --- */
  .btn { min-height: 38px; }
  .btn.primary, .btn.danger, .btn.ghost, .btn.warning, .btn.success { padding: 9px 16px; }

  /* --- Login card --- */
  .login-card { width: 92%; padding: 28px 20px; }
}

/* ============ RTL / Arabic — Desktop ============ */
[dir="rtl"] #app-page { flex-direction: row-reverse; }
[dir="rtl"] .nav-link { border-left: none; border-right: 3px solid transparent; }
[dir="rtl"] .nav-link.active { border-left-color: transparent; border-right-color: #48bb78; }
[dir="rtl"] .sidebar-footer { align-items: stretch; }
[dir="rtl"] .section-header { flex-direction: row-reverse; }
[dir="rtl"] .btn-group { flex-direction: row-reverse; }
[dir="rtl"] .form-actions { flex-direction: row-reverse; }
[dir="rtl"] .info-grid { direction: rtl; }
[dir="rtl"] .modal-head { flex-direction: row-reverse; }
[dir="rtl"] .table-wrap table { direction: rtl; }
[dir="rtl"] th { text-align: right; }
[dir="rtl"] td { text-align: right; }
[dir="rtl"] .totals-bar { flex-direction: row-reverse; flex-wrap: wrap; }
[dir="rtl"] .detail-tabs { flex-direction: row-reverse; flex-wrap: wrap; }
[dir="rtl"] .detail-back { direction: rtl; }
[dir="rtl"] input, [dir="rtl"] select, [dir="rtl"] textarea { text-align: right; }
[dir="rtl"] .field label { text-align: right; }
[dir="rtl"] .lbl { text-align: right; }
[dir="rtl"] .val { text-align: right; }
[dir="rtl"] .stat-row { flex-direction: row-reverse; }
[dir="rtl"] .confirm-actions { flex-direction: row-reverse; }
[dir="rtl"] #toast-container { right: auto; left: 16px; }
@keyframes slideInRtl { from { transform: translateX(-100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
[dir="rtl"] .toast { animation: slideInRtl 0.25s ease; }

/* ============ RTL / Arabic — Mobile ============ */
@media (max-width: 768px) {
  [dir="rtl"] .sidebar { left: auto; right: 0; transform: translateX(100%); }
  [dir="rtl"] body.sidebar-open .sidebar { transform: translateX(0); }
  [dir="rtl"] .mobile-section-title { margin-left: 0; margin-right: auto; }
  [dir="rtl"] #toast-container { right: 12px; left: 12px; }
  .col-hide-mobile { display: none; }

  /* --- Inventory sell lots: card layout --- */
  .inv-lots-table thead { display: none; }
  .inv-lots-table,
  .inv-lots-table tbody { display: block; }
  .inv-lots-table tbody tr {
    display: block;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    margin-bottom: 10px;
    padding: 12px 14px;
  }
  .inv-lots-table tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    border: none;
    font-size: 14px;
    text-align: left !important;
    min-width: auto !important;
  }
  .inv-lots-table tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #718096;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    flex-shrink: 0;
    margin-right: 8px;
  }
  .inv-lots-table tbody td:first-child {
    justify-content: flex-start;
    gap: 8px;
    border-bottom: 1px solid #e8ecf0;
    padding-bottom: 10px;
    margin-bottom: 4px;
  }
  .inv-lots-table tbody td:last-child {
    border-top: 1px solid #e8ecf0;
    padding-top: 10px;
    margin-top: 4px;
    font-weight: 700;
    color: #276749;
  }
  .inv-lots-table tbody td input[type="number"] {
    width: auto !important;
    flex: 1;
    max-width: 140px;
    min-width: 80px;
  }
  .inv-lots-table tfoot { display: block; }
  .inv-lots-table tfoot tr {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f0fff4;
    border-radius: 8px;
    padding: 10px 14px;
  }
  .inv-lots-table tfoot td {
    display: inline;
    border: none;
    padding: 0;
    font-weight: 700;
    color: #276749;
  }
  .inv-lots-table tfoot td:first-child {
    font-size: 13px;
    font-weight: 600;
  }
}
