/* 
   css/refined-ui.css 
   Standardized UI Components for Stock Management System
*/

@import url('animate-ui.css');

:root {
  --modal-bg: rgba(0, 0, 0, 0.6);
  --modal-content-bg: #ffffff;
  --modal-radius: 16px;
  --modal-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  --modal-border: rgba(15, 23, 42, 0.08);
  --modal-title: #0f172a;
  --transition-speed: 0.3s;
}

/* ==========================================
   1. STANDARDIZED MODAL SYSTEM
   ========================================== */

.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: var(--modal-bg);
  z-index: 9999;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity var(--transition-speed) ease;
  padding: 16px;
  box-sizing: border-box;
  pointer-events: none;
}

.modal.show {
  display: flex !important;
  opacity: 1 !important;
  pointer-events: auto;
}

.modal-content {
  background: linear-gradient(180deg, var(--site-surface, #ffffff) 0%, var(--site-raised, #f8fafc) 100%);
  padding: clamp(20px, 5vw, 36px);
  border-radius: var(--modal-radius);
  width: 100%;
  max-width: 600px; /* Default max-width */
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: var(--modal-shadow);
  position: relative;
  border: 1px solid var(--modal-border);
  transform: translateY(16px) scale(0.98);
  transition: transform var(--transition-speed) ease, box-shadow var(--transition-speed) ease;
  box-sizing: border-box;
}

.modal.show .modal-content {
  transform: translateY(0) scale(1);
  box-shadow: 0 24px 64px rgba(15, 23, 42, 0.3);
}

/* Modal Variants */
.modal-content.wide {
  max-width: 900px;
}

.modal-content.narrow {
  max-width: 450px;
}

.modal-content.full {
  max-width: 1200px;
  width: 95%;
}

/* Close Button Standard */
.modal-close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: var(--ds-neutral-100, #f1f5f9);
  border: 1px solid rgba(148, 163, 184, 0.35);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
  color: var(--ds-neutral-600, #475569);
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.modal-close-btn:hover {
  background: var(--ds-surface, #ffffff);
  color: var(--ds-neutral-900, #0f172a);
  border-color: rgba(71, 85, 105, 0.35);
  transform: translateY(-1px) scale(1.04);
}

.modal-close-btn:focus-visible {
  outline: 3px solid rgba(58, 90, 64, 0.35);
  outline-offset: 2px;
}

.modal-content h2 {
  margin: 0 44px 20px 0;
  color: var(--modal-title);
  line-height: 1.3;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 24px;
  flex-wrap: wrap;
}

.modal-content::-webkit-scrollbar {
  width: 8px;
}

.modal-content::-webkit-scrollbar-track {
  background: var(--ds-neutral-200, #e2e8f0);
  border-radius: 999px;
}

.modal-content::-webkit-scrollbar-thumb {
  background: var(--ds-neutral-400, #94a3b8);
  border-radius: 999px;
}

.modal-content::-webkit-scrollbar-thumb:hover {
  background: #64748b;
}

/* ==========================================
   2. ENHANCED TABLE RESPONSIVENESS
   ========================================== */

.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 1rem;
  border-radius: 8px;
  position: relative;
}

/* Custom Scrollbar for better UI on Desktop */
.table-responsive::-webkit-scrollbar {
  height: 6px;
}

.table-responsive::-webkit-scrollbar-track {
  background: var(--site-raised, #f1f1f1);
  border-radius: 3px;
}

.table-responsive::-webkit-scrollbar-thumb {
  background: var(--ds-neutral-300, #cbd5e1);
  border-radius: 3px;
}

.table-responsive::-webkit-scrollbar-thumb:hover {
  background: var(--ds-neutral-400, #94a3b8);
}

/* Table Horizontal Hint */
@media (max-width: 768px) {
  .table-responsive::after {
    content: '← เลื่อนดูข้อมูล →';
    display: block;
    text-align: center;
    font-size: 0.8125rem;
    color: var(--gray-400, #9ca3af);
    padding: 8px 0;
    font-weight: 500;
  }
}

/* ==========================================
   3. MOBILE OPTIMIZATIONS
   ========================================== */

@media (max-width: 480px) {
  .modal-content {
    padding: 20px 16px;
    border-radius: 12px;
  }

  .modal-close-btn {
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    font-size: 21px;
  }
  
  .form-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  
  .btn-group-mobile {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  
  .btn-group-mobile .btn {
    width: 100%;
  }
}

/* Ensure fixed positioning works well with mobile virtual keyboard */
.modal-content input:focus {
  scroll-margin-top: 100px;
}

/* ==========================================
  4. USER MANAGEMENT MODAL
  ========================================== */
.user-modal-content {
  max-width: 760px;
  border-radius: 18px;
  overflow-x: hidden;
  overflow-y: auto;
}

.user-modal-header {
  background: linear-gradient(135deg, #2f5a3e 0%, #1f3f2f 100%);
  color: #fff;
  padding: 22px 24px 16px;
  margin: -36px -36px 24px -36px;
  border-radius: 16px 16px 0 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.user-modal-header h2 {
  margin: 0;
  font-size: 1.45rem;
  color: #fff;
}

.user-modal-header p {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.9);
}

.user-modal-header .modal-close-btn {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.32);
}

.user-modal-header .modal-close-btn:hover {
  background: rgba(255, 255, 255, 0.28);
  color: #fff;
}

.user-modal-section {
  padding: 18px;
  border-radius: 12px;
  margin-bottom: 16px;
  border-left: 4px solid;
}

.user-modal-section h3 {
  margin: 0 0 14px;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.section-account {
  background: var(--site-raised, #f0f7f4);
  border-left-color: #588157;
}

.section-account h3 {
  color: var(--site-text, #1a2e22);
}

.section-personal {
  background: var(--site-raised, #f0f7f4);
  border-left-color: #588157;
}

.section-personal h3 {
  color: var(--site-text, #2c4c3b);
}

.section-role {
  background: var(--ds-warning-soft, #fffbeb);
  border-left-color: #f59e0b;
}

.section-role h3 {
  color: var(--site-warning, #92400e);
}

.required {
  color: var(--site-danger, #ef4444);
}

.form-input,
.form-select {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--ds-neutral-300, #cbd5e1);
  border-radius: 10px;
  font-size: 0.95rem;
  /* Use the page's Thai-capable stack, not the browser's default form-control
     font — the UA default dropped/clipped Thai vowel & tone marks in the select
     ("ตัวอักษรไม่เต็ม"). */
  font-family: inherit;
  transition: all 0.2s ease;
  background-color: var(--ds-surface, #ffffff);
  color: var(--gray-800, #1f2937);
  /* height must grow with the content: a global `select{height:40px}` combined
     with this padding squeezed the text box and clipped Thai vowel/tone marks
     (top & bottom). Let min-height set the floor and line-height give the marks
     room instead of a fixed height. */
  height: auto;
  min-height: 46px;
  line-height: 1.5;
  /* Force the native option popup to render light (white bg + dark text) even
     when the OS is in dark mode — otherwise the popup is dark-on-dark. */
  color-scheme: light;
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: #3a5a40;
  box-shadow: 0 0 0 3px rgba(58, 90, 64, 0.12);
}

.form-select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 0.55rem center;
  background-repeat: no-repeat;
  background-size: 1.35em 1.35em;
  padding-right: 2.3rem;
}

.grid-2-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.permissions-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
  padding: 12px;
  background: var(--ds-surface, #ffffff);
  border-radius: 8px;
  border: 1px solid var(--ds-neutral-200, #e2e8f0);
  max-height: 230px;
  overflow-y: auto;
}

/* Feature flags grouped by area — the heading must break the parent grid flow */
#featuresContainer .feature-group,
#rolePermFeatures .feature-group {
  grid-column: 1 / -1;
}

#featuresContainer .feature-group-title,
#rolePermFeatures .feature-group-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--ds-neutral-600, #475569);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 4px 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--ds-neutral-200, #e2e8f0);
}

#featuresContainer .feature-group > .checkbox-label,
#rolePermFeatures .feature-group > .checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: auto;
  margin: 0 8px 8px 0;
}

/* Enum dropdowns inside the feature lists — force readable contrast + spacing
   (bug: text was hard to read because these selects got no explicit colour). */
#featuresContainer select,
#rolePermFeatures select {
  border: 1px solid var(--ds-neutral-300, #cbd5e1);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
  font-family: inherit;
  background: var(--ds-surface, #ffffff);
  color: var(--gray-800, #1f2937);
  height: auto;
  min-height: 36px;
  line-height: 1.5;
  max-width: 100%;
  /* keep the native option popup light even under OS dark mode */
  color-scheme: light;
}

#featuresContainer select option,
#rolePermFeatures select option {
  color: var(--gray-800, #1f2937);
  background: var(--ds-surface, #ffffff);
}

/* ── Role permission editor layout ── */
.role-perm-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  gap: 16px;
  align-items: start;
}

.role-perm-grid .form-group { margin-bottom: 0; }

#rolePermPages,
#rolePermFeatures {
  max-height: 340px;
}

@media (max-width: 820px) {
  .role-perm-grid { grid-template-columns: 1fr; }
}

/* ── 🔐 Role permission editor — refined visual (scoped to #rolePermCard) ── */

/* launcher bar that opens the modal */
.role-perm-launch {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding: 16px 20px;
  background: linear-gradient(135deg, var(--site-raised, #f0fdf4) 0%, var(--site-raised, #ecfdf5) 100%);
  border: 1px solid var(--site-border, #d1fae5);
  border-left: 4px solid #3a5a40;
  border-radius: 12px;
}
.role-perm-launch__text { display: flex; flex-direction: column; gap: 2px; flex: 1 1 240px; }
.role-perm-launch__text strong { font-size: 15px; color: var(--ds-neutral-800, #1e293b); }
.role-perm-launch__text span { font-size: 13px; color: var(--ds-neutral-500, #64748b); }
.role-perm-launch__btn { flex: 0 0 auto; }

/* modal shell — near-full-screen so the two columns get room */
#rolePermCard.role-perm-modal-content {
  padding: 0;
  overflow: hidden;
  width: 95vw;
  max-width: 1600px;
  max-height: calc(100dvh - 40px);
  display: flex;
  flex-direction: column;
  border-radius: 16px;
}

/* let the permission lists use the taller modal */
#rolePermCard .permissions-container {
  max-height: min(56vh, 620px);
}

/* close button inside the gradient header */
#rolePermCard .role-perm-close {
  margin-left: auto;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease;
}
#rolePermCard .role-perm-close:hover { background: rgba(255, 255, 255, 0.32); }

/* footer actions */
#rolePermCard .role-perm-actions {
  padding: 14px 24px 18px;
  border-top: 1px solid var(--site-border, #eef2f7);
  flex: 0 0 auto;
}
#rolePermCard .role-perm-title { flex: 0 0 auto; }

#rolePermCard .role-perm-title {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0;
  padding: 18px 24px;
  font-size: 1.1rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, #3a5a40 0%, #2f7a57 100%);
  letter-spacing: 0.01em;
}
#rolePermCard .role-perm-title__icon {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.18);
  font-size: 1rem;
  flex: 0 0 auto;
}

#rolePermCard .role-perm-body {
  padding: 20px 24px 20px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

#rolePermCard .role-perm-desc {
  margin: 0 0 18px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.65;
  color: var(--ds-neutral-600, #475569);
  background: var(--site-raised, #f0fdf4);
  border-left: 3px solid #3a5a40;
  border-radius: 8px;
}
#rolePermCard .role-perm-desc strong { color: var(--site-text, #2f6d4f); }

#rolePermCard .role-perm-toolbar {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding: 16px;
  background: var(--ds-neutral-50, #f8fafc);
  border: 1px solid var(--ds-neutral-200, #e2e8f0);
  border-radius: 12px;
}
#rolePermCard .role-perm-toolbar__select {
  margin: 0;
  flex: 1 1 260px;
  min-width: 220px;
  max-width: 380px;
}
#rolePermCard .role-perm-toolbar__save { flex: 0 0 auto; }

#rolePermCard .role-perm-col__label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  color: var(--ds-neutral-800, #1e293b);
  margin-bottom: 8px;
}

/* scrollable permission lists */
#rolePermCard .permissions-container {
  background: var(--site-raised, #fbfdfc);
  border: 1px solid var(--ds-neutral-200, #e2e8f0);
  border-radius: 12px;
  padding: 14px;
  gap: 8px;
}

/* base pill look inside the panel */
#rolePermCard .checkbox-label {
  border-radius: 10px;
  border-color: var(--gray-200, #e5e7eb);
}
#rolePermCard .checkbox-label:hover {
  border-color: #3a5a40;
  background: var(--site-raised, #f0fdf4);
}
/* ✅ selected-state highlight — the cue that was missing */
#rolePermCard .checkbox-label:has(input[type="checkbox"]:checked) {
  background: linear-gradient(135deg, var(--site-raised, #ecfdf5) 0%, var(--site-raised, #dcfce7) 100%);
  border-color: #3a5a40;
  box-shadow: inset 0 0 0 1px #3a5a40;
}
#rolePermCard .checkbox-label:has(input[type="checkbox"]:checked) span {
  color: var(--site-text, #14532d);
  font-weight: 600;
}

/* feature groups become soft cards */
#rolePermCard #rolePermFeatures .feature-group {
  padding: 12px 14px;
  margin-bottom: 12px;
  background: var(--ds-surface, #ffffff);
  border: 1px solid var(--site-border, #eef2f7);
  border-radius: 10px;
}
#rolePermCard #rolePermFeatures .feature-group:last-child { margin-bottom: 0; }
#rolePermCard #rolePermFeatures .feature-group-title {
  color: var(--site-text, #3a5a40);
  border-bottom-color: var(--site-border, #d1fae5);
}

.checkbox-label {
  cursor: pointer;
  background: var(--ds-surface, #ffffff);
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--gray-200, #e5e7eb);
  transition: all 0.2s ease;
  width: 100%;
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox-label:hover {
  background: var(--ds-neutral-50, #f8fafc);
  border-color: var(--ds-neutral-400, #94a3b8);
}

.checkbox-label input[type="checkbox"] {
  width: 17px;
  height: 17px;
  accent-color: var(--site-text, #3a5a40);
}

.btn-block {
  flex: 1;
}

#userModal.show {
  align-items: flex-start;
  padding-top: 20px;
  padding-bottom: 20px;
}

#userModal .modal-content {
  max-height: calc(100dvh - 40px);
}

/* ── User administration dialogs: consistent, formal workspace ── */
#userModal .user-modal-content{
  border:1px solid var(--site-border, #d9e4dc);
  background:linear-gradient(180deg,var(--site-surface, #fff) 0%,var(--site-raised, #f8faf8) 100%);
  box-shadow:0 24px 70px rgba(24,48,33,.24),0 4px 14px rgba(24,48,33,.08);
}
#userModal .user-modal-header{
  background:linear-gradient(125deg,#1f4932 0%,#356d4a 60%,#477e58 100%);
  padding:23px 24px 18px;
  border-bottom:1px solid rgba(255,255,255,.15);
}
#userModal .user-modal-eyebrow{margin:0 0 4px;color:rgba(255,255,255,.7);font-size:.69rem;font-weight:800;letter-spacing:.08em;text-transform:uppercase}
#userModal .user-modal-header h2{font-size:1.32rem;letter-spacing:-.01em}
#userModal .user-modal-section{position:relative;padding:17px 18px 18px;border:1px solid var(--site-border, #dfe8e1);border-left-width:4px;box-shadow:0 2px 7px rgba(30,54,38,.035)}
#userModal .section-account{background:linear-gradient(135deg,var(--site-raised, #f5fbf6),var(--site-raised, #edf6ef));border-left-color:#3a6d49}
#userModal .section-personal{background:linear-gradient(135deg,var(--site-raised, #f7faf9),var(--site-raised, #f0f6f2));border-left-color:#648a70}
#userModal .section-role{background:linear-gradient(135deg,var(--site-raised, #fffdf6),var(--site-raised, #fff9e9));border-left-color:#b88224}
#userModal .user-modal-section h3{padding-bottom:10px;border-bottom:1px solid rgba(91,116,98,.16);font-size:.94rem}
#userModal .section-role h3{border-bottom-color:var(--site-warning-border, #eadab5)}
#userModal .form-group>label{color:var(--site-text, #334b3a);font-size:.82rem;font-weight:750}
#userModal .form-input,#userModal .form-select{border-color:var(--site-border, #cbd9cf);box-shadow:inset 0 1px 1px rgba(25,55,35,.02)}
#userModal .form-input:focus,#userModal .form-select:focus{border-color:#3a6d49;box-shadow:0 0 0 3px rgba(58,109,73,.13)}
#userModal .modal-actions{display:flex;align-items:center;justify-content:flex-end;gap:10px;margin:22px 0 0;padding:15px 0 1px;border-top:1px solid var(--site-border, #e2e9e4)}
#userModal .modal-actions .btn{min-height:40px;padding-inline:16px}
#userModal .modal-actions .btn-success{box-shadow:0 3px 8px rgba(44,87,59,.16)}

#rolePermCard.role-perm-modal-content{border:1px solid var(--site-border, #d8e4db);box-shadow:0 26px 80px rgba(24,48,33,.26);background:var(--site-raised, #f7faf8)}
#rolePermCard .role-perm-title{padding:17px 24px;background:linear-gradient(125deg,#1f4932,#3c7750)}
#rolePermCard .role-perm-title__copy{display:flex;min-width:0;flex-direction:column;gap:2px}
#rolePermCard .role-perm-title__text{font-size:1.06rem;font-weight:800}
#rolePermCard .role-perm-title__copy small{color:rgba(255,255,255,.73);font-size:.72rem;font-weight:500}
#rolePermCard .role-perm-body{background:linear-gradient(180deg,var(--site-raised, #f7faf8),var(--site-surface, #fff))}
#rolePermCard .role-perm-toolbar{background:var(--ds-surface, #ffffff);border-color:var(--site-border, #dce6df);box-shadow:0 2px 7px rgba(31,73,50,.035)}
#rolePermCard .role-perm-col{margin-bottom:0;padding:15px;border:1px solid var(--site-border, #dfe8e1);border-radius:12px;background:var(--ds-surface, #ffffff)}
#rolePermCard .role-perm-col__label{padding-bottom:9px;border-bottom:1px solid var(--site-border, #e5ece7);color:var(--site-text, #294a34)}
#rolePermCard .role-perm-actions{display:flex;justify-content:flex-end;gap:10px;background:var(--ds-surface, #ffffff);padding:14px 24px 17px}
@media(max-width:640px){#userModal .modal-actions{align-items:stretch;flex-direction:column-reverse}#userModal .modal-actions .btn{width:100%}#rolePermCard .role-perm-title{padding:15px 16px}#rolePermCard .role-perm-body{padding:14px 16px}#rolePermCard .role-perm-actions{padding:12px 16px;flex-wrap:wrap}}

@media (max-width: 640px) {
  .user-modal-header {
    margin: -20px -16px 16px -16px;
    padding: 16px 16px 12px;
  }

  .grid-2-col {
    grid-template-columns: 1fr;
  }

  .permissions-container {
    grid-template-columns: 1fr;
    max-height: 210px;
  }
}
