/* --------------------------------------------------------------------------
 * Express-style Desktop-Class Web UI Stylesheet (Left Sidebar & Express Toolbar)
 * -------------------------------------------------------------------------- */

:root {
  --bg-app: #dbeafe;
  --bg-header: #1e3a8a;
  --header-text: #ffffff;
  --bg-sidebar: #f8fafc;
  --sidebar-border: #cbd5e1;
  --sidebar-active: #e2e8f0;
  --bg-panel: #ffffff;
  --border-color: #cbd5e1;
  --focus-ring: #2563eb;
  --status-bar-bg: #1e293b;
  --status-bar-text: #f8fafc;
  --highlight-row: #e0f2fe;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Tahoma, "Sarabun", sans-serif;
  font-size: 13px;
}

body {
  background-color: #94a3b8;
  color: #1e293b;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  user-select: none;
}

/* 1. Top Bar / App Title */
.app-topbar {
  background: linear-gradient(180deg, #1e3a8a 0%, #1e40af 100%);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 32px;
  font-weight: bold;
  font-size: 13px;
  border-bottom: 1px solid #172554;
  flex-shrink: 0;
}

/* 2. Main Container (Sidebar + Workspace) */
.app-container {
  flex: 1;
  display: flex;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  background-color: #cbd5e1;
}

/* Left Sidebar Navigation */
.app-sidebar {
  width: 240px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  box-shadow: 2px 0 5px rgba(0,0,0,0.05);
}

.sidebar-header {
  background: #e2e8f0;
  padding: 8px 12px;
  font-weight: bold;
  color: #334155;
  border-bottom: 1px solid var(--sidebar-border);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sidebar-menu {
  flex: 1;
  overflow-y: auto;
  list-style: none;
}

.menu-group-title {
  padding: 7px 12px;
  font-weight: bold;
  color: #0f172a;
  background-color: #f1f5f9;
  border-bottom: 1px solid #e2e8f0;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.menu-group-title:hover {
  background-color: #e2e8f0;
}

.menu-group-title .badge {
  font-size: 11px;
  color: #64748b;
}

.submenu-level-1 {
  list-style: none;
  background-color: #ffffff;
}

.submenu-item-1 {
  position: relative;
  border-bottom: 1px solid #f1f5f9;
}

.submenu-label-1 {
  padding: 6px 12px 6px 20px;
  color: #334155;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 500;
}

.submenu-label-1:hover, .submenu-item-1.active > .submenu-label-1 {
  background-color: #f0fdf4;
  color: #15803d;
}

.submenu-level-2 {
  list-style: none;
  background-color: #f8fafc;
  padding-left: 10px;
  border-top: 1px solid #e2e8f0;
  display: none;
}

.submenu-item-1.open > .submenu-level-2 {
  display: block;
}

.submenu-item-2 {
  padding: 5px 12px 5px 24px;
  color: #475569;
  cursor: pointer;
  font-size: 12.5px;
  display: flex;
  align-items: center;
}

.submenu-item-2:hover, .submenu-item-2.selected {
  background-color: #dbeafe;
  color: #1e40af;
  font-weight: bold;
}

.submenu-item-2::before {
  content: "•";
  margin-right: 8px;
  color: #94a3b8;
}

/* ชั้นที่ 3: เล่มเอกสารของหน้าที่อยู่ในเมนูย่อย (docs/132) */
.submenu-item-2.sb-group {
  display: block;
  padding: 0;
  font-weight: normal;
  background: none;
  color: #475569;
}
.submenu-item-2.sb-group::before { content: none; }
.sb-group-label {
  padding: 5px 12px 5px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.sb-group-label::before {
  content: "•";
  margin-right: 8px;
  color: #94a3b8;
}
.sb-group-label > span:first-child { flex: 1; }
.sb-group-label:hover {
  background-color: #dbeafe;
  color: #1e40af;
}
.submenu-level-3 {
  list-style: none;
  padding-left: 14px;
  background-color: #f1f5f9;
}
.submenu-item-3 {
  display: flex;
  align-items: center;
  padding: 4px 12px 4px 24px;
  font-size: 12px;
  color: #475569;
  cursor: pointer;
}
.submenu-item-3::before {
  content: "◦";
  margin-right: 6px;
  color: #94a3b8;
}
.submenu-item-3:hover, .submenu-item-3.selected {
  background-color: #dbeafe;
  color: #1e40af;
  font-weight: bold;
}

/* 3. Main Workspace */
.main-workspace {
  flex: 1;
  padding: 8px;
  min-height: 0;
  min-width: 0;
  overflow: auto !important;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 #f1f5f9;
}

/* Modern & Clean Scrollbars */
* {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 #f1f5f9;
}

/* Modern & Clean Scrollbars */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: #f1f5f9;
}

::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.document-window {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.window-header {
  background: #f1f5f9;
  border-bottom: 1px solid var(--border-color);
  padding: 6px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  color: #0f172a;
}

/* Express Action Toolbar (แถบเครื่องมือไอคอนสไตล์ Express) */
.express-toolbar {
  background: #f8fafc;
  border-bottom: 1px solid #cbd5e1;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  gap: 3px;
  flex-wrap: wrap;
}

.tool-btn {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 3px;
  padding: 3px 8px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  cursor: pointer;
  color: #334155;
  font-weight: 500;
  height: 28px;
}

.tool-btn:hover {
  background: #e2e8f0;
  border-color: #94a3b8;
}

.tool-btn:active {
  background: #cbd5e1;
}

.tool-btn.btn-danger {
  color: #dc2626;
}

.tool-btn.btn-danger:hover {
  background: #fee2e2;
  border-color: #f87171;
}

.tool-btn.btn-save {
  color: #16a34a;
  font-weight: bold;
}

.tool-btn.btn-save:hover {
  background: #dcfce7;
  border-color: #86efac;
}

.toolbar-separator {
  width: 1px;
  height: 22px;
  background-color: #cbd5e1;
  margin: 0 4px;
}

/* Form Sections */
.form-section {
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-color);
  background-color: #ffffff;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 6px 16px;
}

.form-group {
  display: flex;
  align-items: center;
  min-width: 0;
}

.form-group label {
  width: 90px;
  font-weight: 600;
  color: #475569;
  flex-shrink: 0;
  font-size: 11px;
}

.form-input {
  flex: 1;
  min-width: 0;
  width: 100%;
  padding: 3px 8px;
  border: 1px solid var(--border-color);
  border-radius: 3px;
  outline: none;
  background-color: #fff;
  height: 25px;
  box-sizing: border-box;
}

select.form-input {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.form-input:focus {
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
  background-color: #f0fdf4;
}

.form-input[readonly] {
  background-color: #f8fafc;
  color: #64748b;
}

/* Items Grid Panel */
.items-grid-container {
  flex: 1;
  overflow: auto;
  background-color: #fff;
  position: relative;
}

table.data-grid {
  width: 100%;
  border-collapse: collapse;
}

table.data-grid th {
  background-color: #f1f5f9;
  border-bottom: 2px solid var(--border-color);
  border-right: 1px solid var(--border-color);
  padding: 5px 8px;
  text-align: left;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 2;
}

table.data-grid td {
  border-bottom: 1px solid #e2e8f0;
  border-right: 1px solid #e2e8f0;
  padding: 2px 4px;
}

table.data-grid tr:hover {
  background-color: #f8fafc;
}

table.data-grid tr.selected-row {
  background-color: var(--highlight-row);
}

table.data-grid input {
  width: 100%;
  border: 1px solid transparent;
  padding: 3px 6px;
  outline: none;
  background: transparent;
  height: 23px;
}

table.data-grid input:focus {
  background-color: #fff;
  border: 1px solid var(--focus-ring);
  box-shadow: inset 0 0 0 1px var(--focus-ring);
}

/* Footer & Summary Panel */
.window-footer {
  background-color: #f8fafc;
  border-top: 1px solid var(--border-color);
  padding: 8px 16px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.summary-box {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 270px;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}

.summary-row.grand-total {
  font-size: 14px;
  color: #1e3a8a;
  border-top: 2px solid #cbd5e1;
  padding-top: 3px;
  margin-top: 3px;
}

/* 4. Status Bar */
.status-bar {
  background-color: var(--status-bar-bg);
  color: var(--status-bar-text);
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 11px;
  flex-shrink: 0;
}

.shortcut-hints span {
  margin-right: 12px;
  color: #cbd5e1;
}

.shortcut-hints kbd {
  background-color: #334155;
  color: #38bdf8;
  padding: 1px 5px;
  border-radius: 3px;
  font-weight: bold;
  font-size: 11px;
}

/* Common Classes */
.text-right {
  text-align: right !important;
}

.text-center {
  text-align: center !important;
}

/* Modal Dialog Styling */
.modal-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.65);
  z-index: 9999;
}

.modal-backdrop.show,
.modal-backdrop.active {
  display: flex !important;
}

dialog {
  border: 1px solid #64748b;
  border-radius: 6px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.3);
  padding: 0;
  margin: auto;
  background: #ffffff;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.modal-header {
  background: #f1f5f9;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-color);
  font-weight: bold;
}

.modal-body {
  padding: 12px;
}

.modal-footer {
  padding: 8px 14px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: #f8fafc;
}

button.btn-primary {
  background-color: #2563eb;
  color: #fff;
  border: none;
  padding: 5px 12px;
  border-radius: 3px;
  cursor: pointer;
  font-weight: 500;
}

button.btn-primary:hover {
  background-color: #1d4ed8;
}

button.btn-secondary {
  background-color: #e2e8f0;
  color: #334155;
  border: 1px solid #cbd5e1;
  padding: 5px 12px;
  border-radius: 3px;
  cursor: pointer;
}

button.btn-secondary:hover {
  background-color: #cbd5e1;
}

/* Autocomplete / Live Search Dropdown */
.autocomplete-container {
  position: relative;
  width: 100%;
}

.autocomplete-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  min-width: 340px;
  max-height: 250px;
  overflow-y: auto;
  background: #ffffff;
  border: 1px solid #3b82f6;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  margin-top: 2px;
}

.autocomplete-item {
  padding: 6px 10px;
  cursor: pointer;
  border-bottom: 1px solid #f1f5f9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.autocomplete-item:last-child {
  border-bottom: none;
}

.autocomplete-item:hover,
.autocomplete-item.active {
  background-color: #eff6ff;
  color: #1d4ed8;
}

.autocomplete-item .item-title {
  font-weight: 600;
  color: #1e293b;
}

.autocomplete-item.active .item-title {
  color: #1d4ed8;
}

.autocomplete-item .item-desc {
  color: #64748b;
  font-size: 11px;
}

/* Floating table autocomplete dropdown for data-grid inputs */
.floating-autocomplete-dropdown {
  position: fixed;
  background: #ffffff;
  border: 1px solid #2563eb;
  border-radius: 4px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
  z-index: 9999;
  min-width: 380px;
  max-height: 250px;
  overflow-y: auto;
  display: none;
}

/* Global Form Page Wrappers (Contacts, Products, Customers, Settings) */
.contact-page-wrapper,
.product-page-wrapper,
.customer-page-wrapper,
.settings-page-wrapper {
  max-width: 980px;
  width: 100%;
  margin: 16px auto;
  background: #ffffff;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  flex-shrink: 0;
}


/* ===== ปุ่มที่กดไม่ได้ต้องดูออก (เดิมสีเหมือนปุ่มปกติ — ผู้ใช้ไม่รู้ว่ากดไปแล้ว) ===== */
.tool-btn:disabled,
.tool-btn[disabled] {
  background: #f1f5f9;
  color: #94a3b8;
  border-color: #e2e8f0;
  cursor: not-allowed;
  opacity: 0.55;
  filter: grayscale(1);
  box-shadow: none;
}
.tool-btn:disabled:hover,
.tool-btn:disabled:active,
.tool-btn.btn-danger:disabled:hover,
.tool-btn.btn-save:disabled:hover {
  background: #f1f5f9;
  border-color: #e2e8f0;
}

/* ===== โหมดกรอก/แก้ไขเอกสาร (api.js DocMode) — ต้องจบด้วย บันทึก หรือ ยกเลิก ===== */
body.doc-editing #btnSaveDoc:not(:disabled) {
  background: #16a34a;
  color: #ffffff;
  border-color: #15803d;
  box-shadow: 0 0 0 2px #bbf7d0;
}
body.doc-editing #btnSaveDoc:not(:disabled) * { color: #ffffff !important; }
body.doc-editing #btnSaveDoc:not(:disabled):hover { background: #15803d; }
body.doc-editing #btnCancelDoc:not(:disabled),
body.doc-editing #btnCancelEdit:not(:disabled),
body.doc-editing #btnResetDoc:not(:disabled) {
  background: #fffbeb;
  color: #b45309;
  border-color: #f59e0b;
  box-shadow: 0 0 0 2px #fde68a;
}
.doc-mode-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 10px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  animation: docModePulse 2s ease-in-out infinite;
}
.doc-mode-badge.is-edit { background: #fef3c7; color: #92400e; border: 1px solid #f59e0b; }
.doc-mode-badge.is-new { background: #dbeafe; color: #1e40af; border: 1px solid #60a5fa; }
@keyframes docModePulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.7; } }
/* ระยะ 2: ปุ่มดู/เลื่อนบิล/พิมพ์/ประวัติ ถูกล็อกระหว่างแก้ไข (DocMode ใส่คลาส dm-lock) */
body.doc-editing .tool-btn.dm-lock,
body.doc-editing .tool-btn.dm-lock:hover {
  background: #f1f5f9;
  color: #94a3b8;
  border-color: #e2e8f0;
  cursor: not-allowed;
  opacity: 0.55;
  filter: grayscale(1);
  box-shadow: none;
}
.doc-mode-toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%) translateY(20px);
  background: #1e293b; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 14px; font-weight: 600;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); opacity: 0; pointer-events: none; z-index: 99999;
  transition: opacity .2s, transform .2s;
}
.doc-mode-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media print { .doc-mode-badge, .doc-mode-toast { display: none !important; } }

/* ===== เรียกดูข้อมูล Alt+L (doc_browser.js — docs/55) ===== */
.docbrowse-overlay { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: none; align-items: flex-start; justify-content: center; z-index: 9000; padding-top: 4vh; }
.docbrowse-overlay.show { display: flex; }
.docbrowse-box { background: #fff; width: min(1280px, 96vw); height: 86vh; border-radius: 8px; box-shadow: 0 20px 50px rgba(0,0,0,.35); display: flex; flex-direction: column; overflow: hidden; }
.docbrowse-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; background: #1e3a8a; color: #fff; font-weight: 700; font-size: 15px; }
.docbrowse-x { background: transparent; border: 0; color: #fff; font-size: 18px; cursor: pointer; }
.docbrowse-search { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid #e2e8f0; background: #f8fafc; }
.docbrowse-filter { display: flex; align-items: center; gap: 10px; padding: 6px 14px; border-bottom: 1px solid #e2e8f0; background: #f8fafc; }
.docbrowse-filter-label { font-size: 13px; font-weight: 600; color: #334155; white-space: nowrap; }
.docbrowse-filter-select { height: 30px; min-width: 260px; max-width: 420px; padding: 2px 8px; font-size: 13px; border: 1px solid #cbd5e1; border-radius: 6px; background: #fff; }
.docbrowse-filter-custom { width: 420px; }
.docbrowse-filter-hint { font-size: 11.5px; color: #64748b; }
.docbrowse-field { padding: 5px 10px; border-radius: 6px; background: #dbeafe; color: #1e40af; font-weight: 700; font-size: 13px; cursor: pointer; white-space: nowrap; border: 1px solid #93c5fd; }
.docbrowse-inputwrap { position: relative; width: 360px; }
.docbrowse-input { width: 100%; padding: 6px 10px; font-size: 14px; border: 1px solid #94a3b8; border-radius: 6px; }
.docbrowse-input:focus { outline: 2px solid #3b82f6; border-color: #3b82f6; }
.docbrowse-hint { color: #64748b; font-size: 12px; }
.docbrowse-sugg { position: absolute; left: 0; top: calc(100% + 2px); width: 640px; max-width: 90vw; background: #fff; border: 1px solid #94a3b8; border-radius: 6px; box-shadow: 0 10px 25px rgba(0,0,0,.2); display: none; z-index: 2; max-height: 360px; overflow-y: auto; }
.docbrowse-sugg.show { display: block; }
.docbrowse-sitem { display: grid; grid-template-columns: 120px 80px 1fr 110px; gap: 8px; padding: 6px 10px; font-size: 13px; cursor: pointer; border-bottom: 1px solid #f1f5f9; }
.docbrowse-sitem.sel, .docbrowse-sitem:hover { background: #2563eb; color: #fff; }
.docbrowse-sitem.none { display: block; color: #94a3b8; cursor: default; background: #fff; }
.docbrowse-gridwrap { flex: 1; overflow: auto; position: relative; }
.docbrowse-grid { width: 100%; border-collapse: collapse; font-size: 13px; }
.docbrowse-grid th { position: sticky; top: 0; background: #c7f0a8; color: #1f2937; text-align: left; padding: 6px 8px; border: 1px solid #a3d977; white-space: nowrap; z-index: 1; }
.docbrowse-grid th.active { background: #86efac; box-shadow: inset 0 -3px 0 #15803d; }
.docbrowse-grid th.active::after { content: ' 🔍'; font-size: 11px; }
.docbrowse-grid td { padding: 4px 8px; border-bottom: 1px solid #eef2f7; border-right: 1px solid #f1f5f9; white-space: nowrap; cursor: default; }
.docbrowse-grid td.name { white-space: normal; min-width: 220px; }
.docbrowse-grid .num, .docbrowse-sitem .num { text-align: right; font-variant-numeric: tabular-nums; }
.docbrowse-grid .mono, .docbrowse-sitem .mono { font-family: Consolas, 'Courier New', monospace; }
.docbrowse-grid tbody tr:hover { background: #eff6ff; }
.docbrowse-grid tbody tr.sel, .docbrowse-grid tbody tr.sel:hover { background: #2563eb; color: #fff; }
.docbrowse-grid tbody tr.void td, .docbrowse-sitem.void { color: #94a3b8; text-decoration: line-through; }
.docbrowse-grid tbody tr.sel.void td { color: #e2e8f0; }
.docbrowse-empty { padding: 30px; text-align: center; color: #94a3b8; }
.docbrowse-foot { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-top: 1px solid #e2e8f0; background: #f8fafc; }
.docbrowse-count { margin-left: auto; color: #475569; font-size: 12px; }
@media print { .docbrowse-overlay { display: none !important; } }
