* { box-sizing: border-box; }
[hidden] { display: none !important; }
:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #e2e6ec;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --sidebar-bg: #14213d;
  --sidebar-text: #cbd5e1;
}
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: "Segoe UI", "Noto Sans Thai", Tahoma, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; }

/* ===== Login ===== */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #14213d, #2563eb);
}
.login-card {
  background: var(--card); border-radius: 12px; padding: 32px; width: 340px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.login-brand p { margin: 0; font-size: 12px; color: var(--muted); }
.login-brand strong { font-size: 15px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 10px; background: var(--primary);
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.field { margin-bottom: 14px; display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; width: 100%;
}
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
.btn {
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: #fff; cursor: pointer; font-weight: 600;
}
.btn:hover { background: #f3f4f6; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.login-error { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* ===== Shell ===== */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; padding: 16px 12px; flex-shrink: 0;
}
.brand-block { display: flex; align-items: center; gap: 10px; padding: 8px; margin-bottom: 16px; }
.brand-block p { margin: 0; font-size: 11px; opacity: .7; }
.brand-block strong { font-size: 13px; color: #fff; }
.nav-list { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 10px; text-align: left;
  background: transparent; border: none; color: var(--sidebar-text);
  padding: 10px 12px; border-radius: 8px; cursor: pointer; font-weight: 500;
  text-decoration: none; width: 100%;
}
.nav-item:hover { background: rgba(255,255,255,.08); }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-icon { width: 18px; text-align: center; }
.sidebar-user {
  display: flex; align-items: center; gap: 8px; padding: 10px; border-top: 1px solid rgba(255,255,255,.1);
}
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%; background: var(--primary);
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px;
}
.user-meta { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.user-meta strong { font-size: 12px; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta span { font-size: 11px; opacity: .7; }
.btn-logout { background: transparent; border: none; color: var(--sidebar-text); cursor: pointer; font-size: 16px; }

.main-area { flex: 1; padding: 18px 24px; overflow-x: hidden; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.topbar-filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.topbar-filters select, .topbar-filters input[type=text] { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.topbar-filters input[type=text] { width: 200px; }
.chk-overdue { font-size: 12px; display: flex; align-items: center; gap: 4px; color: var(--muted); }

.notice { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-size: 13px; }
.notice-info { background: #eff6ff; color: #1e40af; }
.notice-error { background: #fef2f2; color: #b91c1c; }
.notice-success { background: #f0fdf4; color: #15803d; }

.view[hidden] { display: none; }

/* ===== Kanban ===== */
.kanban-board { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 12px; align-items: flex-start; }
.kanban-col {
  background: #eceff3; border-radius: 10px; width: 260px; flex-shrink: 0; padding: 10px;
  min-height: 200px;
}
.kanban-col.drag-over { background: #dbeafe; }
.kanban-col-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 4px 10px; font-weight: 700; font-size: 13px; }
.kanban-col-count { background: #fff; border-radius: 10px; padding: 1px 8px; font-size: 11px; color: var(--muted); }
.kanban-card {
  background: #fff; border-radius: 8px; padding: 10px; margin-bottom: 8px; cursor: grab;
  box-shadow: 0 1px 3px rgba(0,0,0,.08); border-left: 4px solid var(--primary);
}
.kanban-card:active { cursor: grabbing; }
.kanban-card.overdue { border-left-color: var(--danger); }
.kc-type { font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 6px; color: #fff; display: inline-block; margin-bottom: 6px; }
.kc-title { font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.kc-meta { display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: var(--muted); }
.kc-priority-urgent { color: var(--danger); font-weight: 700; }
.kc-priority-high { color: var(--warn); font-weight: 700; }
.kc-checklist { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* ===== Table ===== */
.data-table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 8px; overflow: hidden; }
.data-table th, .data-table td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; font-size: 13px; }
.data-table th { background: #f9fafb; font-size: 12px; color: var(--muted); }
.data-table tr:hover td { background: #f9fafb; cursor: pointer; }

/* ===== Calendar ===== */
.calendar-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.cal-cell { background: var(--card); border-radius: 8px; min-height: 90px; padding: 6px; font-size: 11px; }
.cal-cell.out-month { opacity: .35; }
.cal-daynum { font-weight: 700; margin-bottom: 4px; }
.cal-item { background: #eff6ff; color: #1e40af; border-radius: 5px; padding: 2px 4px; margin-bottom: 2px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-item.overdue { background: #fef2f2; color: #b91c1c; }

/* ===== Dashboard ===== */
.dash-tiles { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.dash-tile { background: var(--card); border-radius: 10px; padding: 14px 18px; min-width: 130px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.dash-tile strong { display: block; font-size: 22px; }
.dash-tile span { font-size: 12px; color: var(--muted); }
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.dash-panel { background: var(--card); border-radius: 10px; padding: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.06); margin-bottom: 14px; }
.dash-panel h3 { margin: 0 0 10px; font-size: 13px; }
.dash-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex;
  align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal { background: #fff; border-radius: 12px; max-height: 90vh; display: flex; flex-direction: column; width: 100%; }
.modal-md { max-width: 480px; }
.modal-lg { max-width: 920px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-close { background: transparent; border: none; font-size: 16px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

.case-detail-body { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; }
.cd-title-input { font-size: 16px; font-weight: 700; border: none; width: 100%; padding: 4px 0; margin-bottom: 8px; }
.cd-title-input:focus { outline: none; border-bottom: 1px solid var(--primary); }
.cd-desc, #cd-desc { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px; margin-bottom: 8px; }
.cd-section { margin-top: 18px; }
.cd-section h4 { margin: 0 0 8px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.case-type-pill { font-size: 11px; font-weight: 700; color: #fff; padding: 2px 8px; border-radius: 6px; margin-right: 8px; }

.subtask-list { list-style: none; margin: 0; padding: 0; }
.subtask-list li { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 13px; }
.subtask-list li.done label { text-decoration: line-through; color: var(--muted); }
.subtask-list li button { margin-left: auto; background: transparent; border: none; color: var(--danger); cursor: pointer; }
.subtask-add { display: flex; gap: 6px; margin-top: 6px; }
.subtask-add input { flex: 1; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }

.product-search-field { position: relative; }
.product-search-field input[type=text] { width: 100%; padding: 8px; border: 1px solid var(--border); border-radius: 8px; }
.product-suggest {
  position: absolute; top: 100%; left: 0; right: 0; background: #fff; border: 1px solid var(--border);
  border-radius: 8px; max-height: 200px; overflow-y: auto; z-index: 10; box-shadow: 0 6px 20px rgba(0,0,0,.1);
}
.product-suggest-item { padding: 8px 10px; cursor: pointer; font-size: 12px; }
.product-suggest-item:hover { background: #f3f4f6; }

.comment-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 8px; }
.comment-item { background: #f9fafb; border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.comment-item .c-meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.comment-item .c-att { margin-top: 6px; font-size: 12px; }
.comment-add textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px; }
.comment-add-row { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }

.activity-log { list-style: none; margin: 0; padding: 0; font-size: 12px; }
.activity-log li { padding: 6px 0; border-bottom: 1px solid var(--border); color: var(--muted); }
.activity-log li strong { color: var(--text); }

.hint { font-size: 12px; color: var(--muted); }

#cd-attachments { display: flex; flex-wrap: wrap; gap: 10px; }
.c-att { display: flex; align-items: center; gap: 8px; }
.c-att.c-att-image {
  flex-direction: column; align-items: flex-start; background: #f9fafb;
  border: 1px solid var(--border); border-radius: 8px; padding: 6px;
}
.c-att-row { display: flex; align-items: center; gap: 6px; }
.att-thumb {
  width: 90px; height: 90px; object-fit: cover; border-radius: 6px;
  border: 1px solid var(--border); cursor: zoom-in; display: block;
}
.att-thumb-sm { width: 50px; height: 50px; }
.att-thumb-lg { width: 160px; height: 160px; }
.att-name { font-size: 11px; max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.img-lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.85); display: flex;
  align-items: center; justify-content: center; z-index: 9999; cursor: zoom-out;
}
.img-lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 6px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
.img-lightbox-close { position: absolute; top: 16px; right: 24px; color: #fff; font-size: 28px; cursor: pointer; line-height: 1; }
.admin-panel { background: var(--card); border-radius: 10px; padding: 18px; }
.member-form { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin-bottom: 16px; }
.member-form select { padding: 8px; border: 1px solid var(--border); border-radius: 8px; }
.table-wrap { overflow-x: auto; }
.visibility-toggle-row { margin-bottom: 14px; }
.visibility-toggle-row .chk-inline { font-size: 14px; gap: 8px; margin-bottom: 4px; }

.role-badge { font-size: 10px; padding: 1px 6px; border-radius: 6px; background: #e5e7eb; }
.watcher-chip {
  display: inline-flex; align-items: center; gap: 4px; background: #eff6ff; color: #1e40af;
  border-radius: 12px; padding: 2px 8px; font-size: 11px; margin: 2px 4px 2px 0;
}
.watcher-chip button { background: transparent; border: none; cursor: pointer; color: #1e40af; }

.branch-checks { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.chk-inline { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; font-weight: normal; }
.chk-inline input { width: auto; }
.promo-add-row { align-items: center; gap: 8px; margin-top: 8px; }
.promo-add-row .product-search-field { flex: 1; }
.promo-group { padding: 10px 12px; border-radius: 8px; border: 1px solid #e2e2e2; margin-bottom: 12px; }
.promo-group-buy { background: #f0f7ff; border-color: #bcdcff; }
.promo-group-free { background: #fff6ea; border-color: #ffdca8; }

@media (max-width: 900px) {
  .case-detail-body { grid-template-columns: 1fr; }
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; overflow-x: auto; }
  .nav-list { flex-direction: row; }
}
