/* ====================================================================
   Data Definitions v3 — UI styles
   - Floating numbered badge (color by category: KPI/Chart/Table/Card)
   - Popup with params (fixed vs dynamic)
   - Toggle button (bottom-left)
   ==================================================================== */

/* Badge */
.def-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: #2196f3;             /* default = KPI = blue */
  color: #fff;
  border: 2px solid var(--bg-2, #1e1e1e);
  font-size: 11px;
  font-weight: 700;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  line-height: 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
  transition: transform 0.15s ease, background 0.15s ease, opacity 0.2s ease;
}
.def-badge:hover { background: #1976d2; transform: scale(1.2); }
.def-badge:active { transform: scale(0.95); }

/* Category colors */
.def-badge.def-cat-kpi    { background: #2196f3; }   /* blue   - KPI */
.def-badge.def-cat-chart  { background: #9c27b0; }   /* purple - Chart */
.def-badge.def-cat-table  { background: #009688; }   /* teal   - Table */
.def-badge.def-cat-card   { background: #ff9800; }   /* orange - Card/Section */
.def-badge.def-cat-filter { background: #4caf50; }   /* green  - Filter */
.def-badge.def-cat-kpi:hover    { background: #1976d2; }
.def-badge.def-cat-chart:hover  { background: #7b1fa2; }
.def-badge.def-cat-table:hover  { background: #00796b; }
.def-badge.def-cat-card:hover   { background: #f57c00; }
.def-badge.def-cat-filter:hover { background: #388e3c; }

body.defs-hidden .def-badge { display: none; }
body.defs-hidden .def-popup { display: none; }

/* Popup */
.def-popup {
  position: fixed;
  z-index: 10000;
  max-width: 420px;
  min-width: 320px;
  max-height: 80vh;
  overflow-y: auto;
  background: #2a2a2a;
  border: 1px solid #2196f3;
  border-radius: 10px;
  padding: 48px 18px 14px;   /* top = 48px เผื่อ action row ไม่ทับ content */
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
  animation: defPopupFadeIn 0.15s ease;
}
@keyframes defPopupFadeIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.def-popup-num {
  font-size: 11px; color: #2196f3; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px;
}
.def-popup-label {
  font-size: 15px; font-weight: 700; margin-bottom: 10px; color: #fff;
}
.def-popup-def { color: #ddd; margin-bottom: 10px; }

.def-popup-section {
  background: #1a1a1a; padding: 10px 12px; border-radius: 6px;
  margin: 10px 0 8px; font-size: 12px; border-left: 3px solid #2196f3;
}
.def-popup-section b {
  display: block; color: #2196f3; font-size: 11px;
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.3px;
}
.def-popup-section pre {
  margin: 0; font-family: 'Menlo', 'Monaco', monospace; font-size: 11px;
  color: #e0e0e0; white-space: pre-wrap; word-break: break-word;
}

/* Params */
.def-popup-params { border-left-color: #ff9800; }
.def-popup-params b { color: #ff9800; }
.def-param {
  background: #222; border-radius: 5px;
  padding: 8px 10px; margin: 6px 0; font-size: 12px;
  border-left: 3px solid #555;
}
.def-param.def-param-fixed   { border-left-color: #9e9e9e; }
.def-param.def-param-dynamic { border-left-color: #4caf50; }
.def-param-head {
  display: flex; align-items: center; gap: 6px; margin-bottom: 4px;
}
.def-param-icon { font-size: 14px; }
.def-param-name { font-weight: 700; color: #fff; }
.def-param-type {
  margin-left: auto; font-size: 10px; color: #aaa;
  padding: 2px 6px; border-radius: 3px; background: #1a1a1a;
}
.def-param-val { color: #ccc; font-size: 11px; padding-left: 20px; }
.def-param-val code {
  background: #1a1a1a; padding: 1px 5px; border-radius: 3px;
  color: #4caf50; font-family: 'Menlo', monospace;
}
.def-param-control { color: #888; font-size: 10px; }

.def-popup-source { color: #aaa; font-size: 11px; margin-top: 6px; }
.def-popup-source b { color: #ccc; }
.def-popup-notes {
  margin-top: 8px; padding: 8px 10px; background: rgba(255, 193, 7, 0.1);
  border-left: 3px solid #ffc107; border-radius: 4px;
  color: #ddd; font-size: 12px;
}

/* (.def-popup-close rule ใหม่อยู่ใน .def-popup-actions section ด้านล่าง) */

/* Toggle */
#defToggle {
  position: fixed; bottom: 24px; left: 24px;
  width: 44px; height: 44px; border-radius: 50%;
  background: #2196f3; color: #fff; border: none;
  font-size: 22px; font-weight: 700; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25); z-index: 9998;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, opacity 0.2s;
}
#defToggle:hover { background: #1976d2; }
body.defs-hidden #defToggle { opacity: 0.5; background: #555; }
body.defs-hidden #defToggle:hover { opacity: 1; }

[data-def] { position: relative; }

/* Mini badge (inline · for sub-defs like "5.1") */
.def-badge-mini {
  position: static;            /* inline, not corner */
  display: inline-flex;
  vertical-align: super;
  width: auto;
  min-width: 0;
  height: 16px;
  padding: 0 5px;
  font-size: 9px;
  line-height: 14px;
  border-radius: 8px;
  border-width: 1px;
  margin: 0 2px;
  top: auto;
  right: auto;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.def-badge-mini:hover { transform: scale(1.3); }

/* Sub-defs list inside popup */
.def-popup-subs { border-left-color: #00bcd4; }
.def-popup-subs b { color: #00bcd4; }
.def-sub-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  margin: 4px 0;
  background: #222;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.15s;
}
.def-sub-item:hover { background: #2a2a2a; }
.def-sub-num {
  background: #00bcd4;
  color: #fff;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 700;
  min-width: 30px;
  text-align: center;
}
.def-sub-label { color: #ddd; font-size: 12px; }

/* Actions row — อยู่แถวบนสุดของ popup · ไม่ทับ content (padding-top ของ popup เปิดที่ไว้) */
.def-popup-actions {
  position: absolute;
  top: 10px;
  right: 12px;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  z-index: 2;
  box-sizing: border-box;
}
.def-popup-actions button {
  height: 28px;
  padding: 0 10px;
  background: #3a3a3a;
  border: 1px solid #555;
  border-radius: 5px;
  color: #ddd;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.def-popup-actions button:hover {
  background: #4a4a4a;
  color: #fff;
  border-color: #777;
}
.def-popup-copy {
  color: #4caf50 !important;
  border-color: #4caf50 !important;
}
.def-popup-copy:hover { background: rgba(76, 175, 80, 0.2) !important; }
.def-popup-reset {
  color: #ff9800 !important;
  border-color: #ff9800 !important;
}
.def-popup-reset:hover { background: rgba(255, 152, 0, 0.2) !important; }

/* Close — เล็ก subtle ไม่ทับ Copy · ไม่มี border สี */
.def-popup-close {
  width: 28px !important;
  height: 28px !important;
  padding: 0 !important;
  font-size: 16px !important;
  line-height: 1 !important;
  background: transparent !important;
  border-color: transparent !important;
  color: #888 !important;
}
.def-popup-close:hover {
  background: #444 !important;
  color: #fff !important;
  border-color: #555 !important;
}

/* Hint banner */
.def-popup-hint {
  background: rgba(33, 150, 243, 0.1);
  border-left: 3px solid #2196f3;
  border-radius: 4px;
  padding: 6px 10px;
  margin: 4px 0 12px;
  font-size: 11px;
  color: #90caf9;
}
.def-popup-hint b { color: #bbdefb; }

/* Editable areas — มี hint ชัดเจน */
.editable {
  cursor: text;
  border: 1px dashed transparent;
  border-radius: 4px;
  padding: 4px 6px;
  margin: -4px -6px;
  outline: none;
  transition: background 0.15s, border-color 0.15s;
}
.editable:hover {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(76, 175, 80, 0.4);
}
.editable:focus {
  background: rgba(76, 175, 80, 0.12);
  border-color: #4caf50;
  border-style: solid;
}
.editable[contenteditable="true"]:empty::before {
  content: "(พิมพ์ที่นี่)";
  color: #555;
  font-style: italic;
}

/* Footnote section */
.def-popup-foot { border-left-color: #4caf50; }
.def-popup-foot b { color: #4caf50; }
.def-foot-item {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 5px 8px;
  margin: 3px 0;
  background: #222;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.5;
}
.def-foot-item.edited {
  border-left: 2px solid #4caf50;
  background: rgba(76, 175, 80, 0.08);
}
.def-foot-term {
  color: #4caf50;
  font-weight: 700;
  white-space: nowrap;
  font-family: 'Menlo', monospace;
}
.def-foot-eq {
  color: #666;
  font-weight: 700;
}
.def-foot-expl {
  flex: 1;
  color: #ddd;
}

/* Edited flag */
.def-popup-edited-flag {
  margin-top: 10px;
  padding: 6px 10px;
  background: rgba(76, 175, 80, 0.1);
  border-left: 3px solid #4caf50;
  border-radius: 4px;
  color: #a5d6a7;
  font-size: 11px;
}

/* ====================================================================
   Verified SQL section (links to ai-data-context/sql/*.sql)
   ==================================================================== */
.def-popup-verified {
  margin-top: 10px;
  padding: 10px 12px;
  background: linear-gradient(135deg, rgba(26, 74, 46, 0.4), rgba(45, 110, 71, 0.25));
  border-left: 3px solid #5dd58e;
  border-radius: 4px;
  font-size: 11.5px;
}
/* Panel tint matches status — set via :has() selector on inner badge */
.def-popup-verified:has(.def-status-badge.ver-caveat) {
  background: linear-gradient(135deg, rgba(120, 88, 14, 0.4), rgba(154, 128, 38, 0.18));
  border-left-color: #f4c447;
}
.def-popup-verified:has(.def-status-badge.ver-issue) {
  background: linear-gradient(135deg, rgba(120, 30, 22, 0.4), rgba(154, 50, 38, 0.18));
  border-left-color: #e74c3c;
}
.def-verified-badge {
  display: inline-block;
  background: #5dd58e;
  color: #0d3320;
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
  margin-right: 8px;
}
.def-verified-text {
  color: #d4f4dc;
  font-weight: 600;
}
.def-verified-row {
  margin-top: 6px;
  color: #c8e6cf;
  line-height: 1.5;
}
.def-verified-row b {
  color: #fff;
  font-weight: 600;
  margin-right: 4px;
}
.def-verified-row code {
  background: rgba(0, 0, 0, 0.35);
  color: #b5e0c5;
  padding: 1px 6px;
  border-radius: 3px;
  font-family: 'Menlo', monospace;
  font-size: 10.5px;
  word-break: break-word;
}
.def-verified-meta {
  margin-top: 6px;
  font-size: 10px;
  color: #88c69a;
  font-style: italic;
}

/* Verified section — head row with multiple badges */
.def-verified-head {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
}

/* Data source badge */
.def-source-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.3px;
}
.def-source-badge.src-event  { background: #5b8def; color: #fff; }
.def-source-badge.src-main   { background: #8d6def; color: #fff; }
.def-source-badge.src-hybrid { background: #ef9b5b; color: #1f1f1f; }

/* Single status badge (replaces VERIFIED+CAVEAT pair) */
.def-status-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.2px;
}
.def-status-badge.ver-clean  { background: #5dd58e; color: #0d3320; }
.def-status-badge.ver-caveat { background: #f4c447; color: #3a2900; }
.def-status-badge.ver-issue  { background: #e74c3c; color: #fff; }

.def-status-desc {
  margin-top: 8px;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  font-size: 11px;
  color: #d4e6dc;
  font-style: italic;
  line-height: 1.5;
}

/* Verification note (highlighted) */
.def-verified-note {
  background: rgba(244, 196, 71, 0.12);
  border-left: 2px solid #f4c447;
  padding: 6px 10px;
  border-radius: 3px;
  margin-top: 8px;
}
