/* --- CSS Variables (Theme Colors) --- */
:root {
  --bg-color: #f4f5f7;
  --card-bg: #ffffff;
  --text-color: #172b4d;
  --text-muted: #6b778c;
  --border-color: #ebecf0;
  --accent-color: #60a5fa;
  --btn-ghost-hover: #f1f2f4;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  --sidebar-width: 380px; 
}

body.dark-mode {
  --bg-color: #1d2125;
  --card-bg: #282c34;
  --text-color: #d1d5db;
  --text-muted: #9ca3af;
  --border-color: #374151;
  --accent-color: #60a5fa;
  --btn-ghost-hover: #374151;
}

body.dark-mode .card,
body.dark-mode .table,
body.dark-mode .table-head,
body.dark-mode .table-pagination {
  border-color: #4b5563;
  background: #242a34;
}

body.dark-mode .chart-container {
  border: 1px solid #4b5563;
  background: #1f2430;
}

/* --- Base Styles --- */
* { box-sizing: border-box; }
body {
  font-family: var(--font-family);
  margin: 0;
  background: var(--bg-color);
  color: var(--text-color);
  transition: background 0.2s, color 0.2s;
  font-size: 14px;
  font-weight: var(--font-weight-normal);
  line-height: 1.5;
}
body.resizing {
  user-select: none;
  -webkit-user-select: none;
}

/* --- Layout (Sidebar + Main) --- */
#mainContent {
  padding: 0 20px; 
  transition: margin-right 0.3s ease, width 0.3s ease;
}

#detailSidebar {
  position: fixed;
  top: 0;
  right: 0;
  height: 100vh;
  width: var(--sidebar-width); 
  max-width: 90vw;
  background: var(--card-bg);
  border-left: 1px solid var(--border-color);
  z-index: 100;
  display: flex; 
  flex-direction: column; 
  overflow-y: hidden; 
  transform: translateX(100%);
  transition: transform 0.3s ease, width 0s; 
}
#detailSidebar.visible { transform: translateX(0); }
body.sidebar-visible #mainContent { 
  margin-right: var(--sidebar-width); 
  width: calc(100% - var(--sidebar-width));
}

/* === ที่จับ (Handle) สำหรับลาก === */
.resize-handle {
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 24px;
  height: 24px;
  cursor: nwse-resize;
  z-index: 2;
  background: rgba(36,36,36,0.18);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, box-shadow 0.2s;
}
.resize-handle:hover,
body.resizing .resize-handle {
  background: #60a5fa;
  box-shadow: 0 0 0 2px #2563eb;
  opacity: 0.7;
}
.resize-handle::after {
  content: '';
  display: block;
  width: 18px;
  height: 18px;
  background-image: url('data:image/svg+xml;utf8,<svg width="18" height="18" viewBox="0 0 18 18" xmlns="http://www.w3.org/2000/svg"><polyline points="5,15 15,15 15,5" stroke="%2360a5fa" stroke-width="2.5" fill="none"/></svg>');
  background-size: contain;
  background-repeat: no-repeat;
}
/* ============================= */

/* ปรับ handle ของ widget chart: ซ่อนปกติ, โชว์ตอน hover เท่านั้น และให้สีอ่อนลง */
.charts .card .resize-handle {
  background: transparent;
  opacity: 0;
}
.charts .card .resize-handle:hover,
.charts .card.resizing .resize-handle {
  opacity: 1;
  background: rgba(96,165,250,0.16);
  box-shadow: 0 0 0 1px rgba(37,99,235,0.4);
}


.sidebar-header {
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}
.sidebar-header h3 { margin: 0; }
.sidebar-header .close-btn { font-size: 24px; padding: 0 8px; }

#detailSidebarContent {
  padding: 20px;
  overflow-y: auto; 
  flex-grow: 1;
}
#detailSidebarContent .detail-item { margin-bottom: 12px; }
#detailSidebarContent .detail-item label { font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }
#detailSidebarContent .detail-item p { margin: 0; white-space: pre-wrap; word-wrap: break-word; }

/* --- Header --- */
header {
  padding: 15px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
  position: relative;
}
header h1 {
  font-size: 2.4rem;
  font-weight: var(--font-weight-bold);
  color: var(--accent-color);
  margin: 0;
  font-family: var(--font-family);
}
.corner-actions {
  position: absolute;
  top: 15px;
  right: 15px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
}
.corner-top {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.corner-actions .status-block {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
 
}
.corner-actions .status-block .label {
  font-size: 11px;
  letter-spacing: 0.3px;
  color: var(--text-muted);
  text-transform: uppercase;
  display: none;
}
.corner-actions .latest-value {
  font-weight: 600;
}
.corner-actions .status-block .btn.small-btn {
  padding: 4px 12px;
  font-size: 13px;
}
.sync-inline{
  display:flex;
  align-items:center;
  gap:8px;
  font-weight:600;
  color:var(--text-muted);
}
.sync-inline .btn{
  min-width:70px;
}
.icon-actions {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}
.icon-actions .btn {
  min-width: 40px;
}
.icon-actions button {
  color: #071d4d !important;
}
.icon-actions button#btnSettings {
  margin-left: 8px;
}
.controls { 
  display: flex; 
  flex-wrap: wrap; 
  gap: 15px; 
  align-items: flex-end; 
  position: relative; /* สำหรับให้ control บางอัน float ขวา */
}
.control { display: flex; flex-direction: column; }
.control label { 
  font-weight: var(--font-weight-semibold); 
  margin-bottom: 5px; 
  color: var(--text-muted);
  font-family: var(--font-family);
}
.control.grow { flex-grow: 1; }
.control.flex-end { /* <<< ใหม่: จัดให้ไปอยู่ขวาสุด */
  margin-left: auto; 
  margin-right: 0;
}
.hidden-label { /* <<< ใหม่: ซ่อน label สำหรับปุ่มที่ไม่มี */
  visibility: hidden;
  height: 0;
  margin: 0;
  padding: 0;
}

.range { display: flex; align-items: center; gap: 5px; }
.loaders { display: flex; gap: 5px; }
.view-buttons { display: flex; gap: 5px; }
.muted { color: var(--text-muted); }
.controls .controls-sync {
  margin-left: auto;
  display: flex;
  align-items: flex-end;
  gap: 20px;
}
.controls .controls-sync .sync-block,
.controls .controls-sync .latest-inline {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.controls .controls-sync .latest-inline label {
  margin-bottom: 0;
}

/* --- Forms (Buttons, Inputs) --- */
input[type="text"], input[type="date"], input[type="file"], select, input[id="csvUrl"] {
  background: var(--bg-color);
  border: 1px solid var(--border-color);
  color: var(--text-color);
  border-radius: 4px;
  padding: 8px 10px;
  font-size: 14px;
}
input[id="csvUrl"] { width: 100%; }
.btn {
  background: var(--accent-color);
  color: white;
  border: 0;
  padding: 8px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-weight: var(--font-weight-semibold);
  font-family: var(--font-family);
  transition: background 0.2s, opacity 0.2s;
}
.btn:hover {
  opacity: 0.9;
}
.btn:disabled { background: #ccc; }
.btn.ghost {
  background: transparent;
  color: var(--text-color);
  border: 1px solid var(--border-color);
}
.btn.ghost:hover { background: var(--btn-ghost-hover); }
.btn.small-btn { padding: 4px 8px; font-size: 16px; }
.btn-icon { font-size: 16px; padding: 4px 8px; }
.btn.full-width { /* <<< ใหม่: ปุ่มเต็มความกว้างใน dropdown */
  width: 100%;
  justify-content: flex-start;
  text-align: left;
  margin-bottom: 8px;
}
.btn.full-width:last-child { margin-bottom: 0; }
.modal-filter {
  width: 100%;
  margin: 8px 0 10px;
}
.chip {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--btn-ghost-hover);
  color: var(--text-color);
  font-size: 12px;
}
.inline-option { margin: 6px 0 10px; font-size: 13px; }
.inline-option select { margin-left: 6px; }
.metric-card {
  padding: 6px 10px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--card-bg);
  min-width: 120px;
}
.metric-title {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.metric-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-color);
}


/* --- Cards & Charts --- */
.card {
  background: var(--card-bg);
  border-radius: 5px;
  border: 1px solid var(--border-color);
  margin-bottom: 20px;
}
.charts { 
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 20px; 
  align-items: stretch; 
  justify-content: center;
  transition: none;
}
.charts .card.w4 {
  min-width: 240px;
  flex: 1 1 240px;
}
body.layout-2 .charts .card.w4 { width: calc(50% - 14px); }
body.layout-3 .charts .card.w4 { width: calc(33.333% - 14px); }
body.layout-4 .charts .card.w4 { width: calc(25% - 14px); }
.charts .card.w-full { width: 100%; min-width: 0; }
.charts .card { 
  padding: 15px; 
  display: flex; 
  flex-direction: column; 
  flex: 1 1 240px;
  min-width: 0;
  position: relative;
  max-width: 100%;
  box-sizing: border-box;
  cursor: move; /* แสดงว่า drag ได้ */
}

.charts .card.resizing {
  cursor: nwse-resize;
  user-select: none;
}

.charts .card.resizing * {
  pointer-events: none;
}
.charts .card h3 { flex-shrink: 0; }
.card-tools {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}
.card-tools .small-label {
  font-size: 12px;
  color: var(--text-muted);
}
.card-tools select {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--bg-color);
  color: var(--text-color);
}
.chart-container {
  flex-grow: 1;
  position: relative;
  min-height: 160px;
  height: 100%;
}
.charts .card canvas { position: relative; }
.charts .card .chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

/* กันไม่ให้ canvas โชว์เส้นประเวลา focus/click */
.charts canvas {
  outline: none;
}
.chart-container.short {
  height: 100%;
}
.w4 { flex: 1 1 240px; }
.w-full { flex: 1 1 100%; width: 100%; }
.inline-filter { margin-bottom: 10px; }

.charts .card.w4.dragging { opacity: 0.5; }
.charts .card.w4.drag-over { outline: 2px dashed var(--accent-color); }

.modern-gradient-bg {
  background: linear-gradient(135deg, #f9fafb 0%, #ffffff 100%);
  border-radius: 12px;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.04);
  border: 0;
  padding: 8px;
}
body.dark-mode .modern-gradient-bg {
  background: linear-gradient(135deg, #1f2933 0%, #111827 100%);
  border: 0;
}

/* --- Table --- */
.table { padding: 0; }
.table-head {
  padding: 15px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.table-title { display: flex; align-items: center; gap: 10px; }
.table-title h3 { margin: 0; }
.chart-filter {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--btn-ghost-hover);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 13px;
  color: var(--text-color);
}
.chart-filter .btn.small-btn {
  padding: 2px 8px;
  font-size: 12px;
}
.table-head .right { display: flex; gap: 10px; }
.table-wrap { overflow-x: hidden; max-height: 600px; width: 100%; }
.table-pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 8px 15px 12px;
  font-size: 12px;
}
.table-pagination.left {
  justify-content: flex-start;
}
table { width: 100%; min-width: 100%; border-collapse: collapse; table-layout: fixed; }
th, td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
th {
  background: var(--bg-color);
  font-size: 12px;
  text-transform: uppercase;
  color: var(--text-muted);
  position: relative;
}
th.dragging { opacity: 0.4; }
th.drop-target {
  outline: 2px dashed var(--accent-color);
  outline-offset: -4px;
}
td { }
tbody tr:hover {
  background: var(--btn-ghost-hover);
  cursor: pointer;
}
.resizer {
  position: absolute;
  right: 0;
  top: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
  border-left: 1px solid #d5d8dd;
  opacity: 0.5;
}
.sortable { cursor: pointer; }
.sortable .th-content { pointer-events: none; color: var(--text-color); }
.sortable.sorted.asc .th-content::after {
  content: '▲';
  font-size: 10px;
  margin-left: 4px;
}
.sortable.sorted.desc .th-content::after {
  content: '▼';
  font-size: 10px;
  margin-left: 4px;
}

/* --- Responsive (Mobile) --- */
@media (max-width: 768px) {
  header {
    padding: 12px;
    margin-left: 0;
  }
  header h1 { margin-bottom: 12px; }
  .corner-actions {
    position: static;
    align-items: flex-end;
    margin-bottom: 8px;
  }
  .controls {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .control { width: 100%; }
  .range { gap: 8px; }
  .view-buttons { width: 100%; justify-content: flex-start; }
  .table-head { flex-direction: column; align-items: flex-start; gap: 10px; }
  .table-head .right { width: 100%; justify-content: space-between; }
  .table-pagination { justify-content: space-between; }
  .charts { flex-direction: column; }
  .w4, .w-full { width: 100% !important; }
  .charts .card { 
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 100% !important;
  }
  .chart-container { height: 220px; }
  .chart-container.short { height: 260px; }
  .resize-handle { display: none; } /* ซ่อน resize handle ใน mobile */
  table { table-layout: auto; }
  th, td { white-space: nowrap; font-size: 12px; }
  .table-wrap { overflow-x: auto; }
  .table-head .right input { width: 150px; }
  .table-head .right button { white-space: nowrap; padding: 6px 10px; font-size: 12px; }
  .table-head { padding: 10px; }
  .table { padding: 0; }
  .dashboard-title-text { font-size: 1.3rem; }
  .dashboard-logo-blue {
    width: 55px;
    height: 55px;
    background: transparent;
    filter: brightness(1.25) saturate(1.1) sepia(0) hue-rotate(343deg) drop-shadow(0 0 0 #60A5FA);
  }
}

@media (max-width: 480px) {
  body { font-size: 13px; }
  .charts { flex-direction: column; }
  .chart-container { height: 220px; }
  .chart-container.short { height: 240px; }
  th, td { white-space: normal; font-size: 12px; padding: 6px; }
  table { table-layout: auto; }
  .table-head { padding: 8px; }
  .table-head .right { flex-direction: column; align-items: flex-start; gap: 6px; }
  .table-head .right input { width: 100%; }
  .table-head .right button { width: 100%; text-align: center; }
}

@media (max-width: 480px) {
  .modal-content.small-modal {
    max-width: 90%;
  }
  .settings-selects {
    grid-template-columns: 1fr;
  }
}

/* --- Modal (Column Picker) --- */
.modal-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal-content {
  width: 90%;
  max-width: 500px;
  padding: 20px;
}
.modal-content.small-modal { /* <<< ใหม่: สำหรับ dropdown setting */
  max-width: 300px; 
  padding: 12px 14px;
}
.modal-content.small-modal h3 {
  margin-top: 0;
  margin-bottom: 15px;
  text-align: center;
}
.checkbox-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  margin: 20px 0;
}
.checkbox-grid label { display: flex; align-items: center; gap: 5px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}
.settings-options { /* <<< ใหม่: สำหรับปุ่มใน dropdown */
  display: flex;
  flex-direction: column;
}
.settings-selects {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border-color);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 10px;
  row-gap: 8px;
}
.settings-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 8px;
}
.settings-row label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.settings-row select {
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--bg-color);
  color: var(--text-color);
}

footer { padding: 20px 0; }
footer .footer-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
footer .footer-right img {
  display: block;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
}
.card-header h3 {
  margin: 0;
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--text-color);
}
.charts .card { position: relative; }
.charts .card.resizing {
  box-shadow: 0 0 0 2px #2563eb;
  z-index: 10;
}
.chart-container {
  height: 260px;
  min-height: 220px;
  max-height: 320px;
}
.chart-container.short {
  height: 320px;
}
.charts .card .chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

/* --- Dashboard Title and Logo --- */
.dashboard-title {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
}
.dashboard-logo {
  width: 48px;
  height: 48px;
  background: transparent;
  border-radius: 50%;
  filter: hue-rotate(190deg) brightness(1.2) saturate(1.2);
}
.dashboard-title-text {
  color: #60a5fa;
  font-size: 2.2rem;
  font-weight: 700;
  margin: 0;
}
.dashboard-logo-blue {
  width: 55px;
  height: 55px;
  background: transparent;
  filter: brightness(1.25) saturate(1.1) sepia(0) hue-rotate(343deg) drop-shadow(0 0 0 #60A5FA);
}

/* Layout grid icons */
.layout-grid-icons {
  display: flex;
  gap: 12px;
  align-items: center;
}
.layout-icon {
  background: none;
  border: none;
  padding: 4px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  outline: none;
}
.layout-icon img {
  width: 32px;
  height: 32px;
  filter: grayscale(0) brightness(1);
  transition: filter 0.2s;
}
.layout-icon.active,
.layout-icon:hover {
  background: #e0e7ff;
  box-shadow: 0 0 0 2px #2563eb;
}
.layout-icon.active img {
  filter: drop-shadow(0 0 2px #2563eb) brightness(1.2);
}
@media (prefers-color-scheme: dark) {
  .layout-icon {
    background: none;
  }
  .layout-icon.active,
  .layout-icon:hover {
    background: #1e293b;
    box-shadow: 0 0 0 2px #2563eb;
  }
  .layout-icon img {
    filter: grayscale(0) brightness(0.8);
  }
  .layout-icon.active img {
    filter: drop-shadow(0 0 2px #2563eb) brightness(1.4);
  }
}
