/* ===================================================================== */
/*  UX/UI Improvements — Design Tokens, A11y, Dark Mode, Mobile, etc.    */
/*  ไฟล์นี้ override บางส่วนของ custom.css เพื่อปรับปรุง UX/UI ทั้งระบบ   */
/* ===================================================================== */

/* ---------- Design Tokens ---------- */
:root {
  /* Spacing scale (4px base) */
  --space-1: 0.25rem;   /* 4 */
  --space-2: 0.5rem;    /* 8 */
  --space-3: 0.75rem;   /* 12 */
  --space-4: 1rem;      /* 16 */
  --space-5: 1.5rem;    /* 24 */
  --space-6: 2rem;      /* 32 */
  --space-8: 3rem;      /* 48 */

  /* Font-size scale */
  --fs-xs:   0.75rem;   /* 12 */
  --fs-sm:   0.875rem;  /* 14 */
  --fs-base: 1rem;      /* 16 */
  --fs-lg:   1.125rem;  /* 18 */
  --fs-xl:   1.25rem;   /* 20 */
  --fs-2xl:  1.5rem;    /* 24 */
  --fs-3xl:  1.875rem;  /* 30 */
  --fs-4xl:  2.25rem;   /* 36 */

  /* Border radius scale */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-full: 9999px;

  /* Z-index scale */
  --z-sticky: 1055;
  --z-modal: 1080;
  --z-toast: 1090;

  /* Semantic price colors (only for price-direction indicators, not buy/sell labels) */
  --price-up: #047857;       /* darker emerald — better contrast */
  --price-down: #b91c1c;     /* darker red */
  --price-neutral: #6b7280;

  /* Neutral table cell colors (replaces wrong price-up/price-down on buy/sell) */
  --buy-color: #4a3c1f;      /* dark gold-bronze for "รับซื้อ" */
  --sell-color: #1f2937;     /* charcoal for "ขายออก" */

  /* Surface/text colors */
  --surface-1: #ffffff;
  --surface-2: #fafaf8;
  --surface-3: #f4f1e8;
  --text-primary: #1f2937;
  --text-secondary: #4b5563;
  --text-muted: #6b7280;
  --border-soft: rgba(212, 175, 55, 0.18);
  --border-medium: rgba(212, 175, 55, 0.35);

  /* Override gold-primary for text usage to ensure WCAG AA on white (≥4.5:1) */
  --gold-text: #85652d;       /* same as gold-dark, used for text */
  --gold-bg: #d4af37;         /* used for backgrounds where contrast doesn't matter */
}

/* ---------- Dark Mode (auto from system) ---------- */
@media (prefers-color-scheme: dark) {
  :root[data-theme-auto], :root:not([data-theme]) {
    --surface-1: #1a1a1a;
    --surface-2: #232323;
    --surface-3: #2d2d2d;
    --text-primary: #f1f1f1;
    --text-secondary: #c4c4c4;
    --text-muted: #9ca3af;
    --border-soft: rgba(212, 175, 55, 0.22);
    --border-medium: rgba(212, 175, 55, 0.4);
    --buy-color: #f3e1a3;
    --sell-color: #e5e7eb;
    --price-up: #10b981;
    --price-down: #f87171;
    --gold-text: #f3e1a3;
  }
}

/* Manual dark mode toggle */
:root[data-theme="dark"] {
  --surface-1: #1a1a1a;
  --surface-2: #232323;
  --surface-3: #2d2d2d;
  --text-primary: #f1f1f1;
  --text-secondary: #c4c4c4;
  --text-muted: #9ca3af;
  --border-soft: rgba(212, 175, 55, 0.22);
  --border-medium: rgba(212, 175, 55, 0.4);
  --buy-color: #f3e1a3;
  --sell-color: #e5e7eb;
  --price-up: #10b981;
  --price-down: #f87171;
  --gold-text: #f3e1a3;
}

[data-theme="dark"] body { background: var(--surface-1) !important; color: var(--text-primary); }
[data-theme="dark"] .card,
[data-theme="dark"] .table-gold { background: var(--surface-2) !important; color: var(--text-primary); }
[data-theme="dark"] table.table tbody tr,
[data-theme="dark"] table.table-hover tbody tr { background: var(--surface-2); color: var(--text-primary); }
[data-theme="dark"] .text-muted { color: var(--text-muted) !important; }

/* ---------- Accessibility: focus-visible ---------- */
*:focus { outline: none; }
*:focus-visible {
  outline: 3px solid var(--gold-bg);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
a:focus-visible, button:focus-visible, .btn:focus-visible, .nav-link:focus-visible,
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--gold-bg);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(212, 175, 55, 0.2);
}

/* Skip link for keyboard users */
.skip-to-content {
  position: absolute;
  top: -100px;
  left: 0;
  background: var(--gold-text);
  color: #fff;
  padding: var(--space-3) var(--space-4);
  z-index: var(--z-toast);
  text-decoration: none;
  font-weight: 600;
  border-radius: 0 0 var(--radius-md) 0;
}
.skip-to-content:focus { top: 0; }

/* ---------- Price color semantics: ใช้สีกลางสำหรับ buy/sell labels ---------- */
.price-buy   { color: var(--buy-color)  !important; font-weight: 600; }
.price-sell  { color: var(--sell-color) !important; font-weight: 600; }
.price-up    { color: var(--price-up)   !important; }
.price-down  { color: var(--price-down) !important; }
.price-neutral { color: var(--price-neutral) !important; }

/* ใช้ผ่าน data-direction บนราคาที่ flash อัปเดต */
[data-direction="up"]   { color: var(--price-up); }
[data-direction="down"] { color: var(--price-down); }

/* ---------- Skeleton Loader ---------- */
.skeleton {
  display: inline-block;
  background: linear-gradient(90deg, var(--surface-3) 25%, var(--surface-2) 50%, var(--surface-3) 75%);
  background-size: 200% 100%;
  animation: skeletonShimmer 1.4s ease-in-out infinite;
  border-radius: var(--radius-sm);
  color: transparent;
  user-select: none;
}
.skeleton-line { width: 100%; height: 1em; }
.skeleton-line.w-50 { width: 50%; }
.skeleton-line.w-75 { width: 75%; }
.skeleton-price { width: 6ch; height: 1.2em; }
@keyframes skeletonShimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .skeleton { animation: none; }
}

/* ---------- Focus state for live-updating prices ---------- */
.price-live {
  transition: background-color 0.7s ease;
}

/* ---------- Mobile: stacked card layout for tables (data-label pattern) ---------- */
@media (max-width: 575.98px) {
  .table-stack-mobile thead {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }
  .table-stack-mobile tr {
    display: block;
    margin-bottom: var(--space-3);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-md);
    background: var(--surface-1);
  }
  .table-stack-mobile td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-2) var(--space-3) !important;
    border-top: 1px solid var(--border-soft);
    text-align: right;
  }
  .table-stack-mobile td:first-child { border-top: 0; }
  .table-stack-mobile td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-secondary);
    text-align: left;
    margin-right: var(--space-3);
  }
}

/* ---------- Indicator Card (analysis pages) ---------- */
.indicator-card {
  background: var(--surface-1);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  height: 100%;
}
.indicator-card .indicator-label {
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}
.indicator-card .indicator-value {
  font-size: var(--fs-2xl);
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.indicator-card .indicator-meta {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.indicator-card .indicator-bar {
  height: 6px;
  background: var(--surface-3);
  border-radius: var(--radius-full);
  overflow: hidden;
  position: relative;
}
.indicator-card .indicator-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--price-up), var(--gold-bg));
  border-radius: var(--radius-full);
  transition: width 0.4s ease;
}
.indicator-card .signal-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--fs-xs);
  font-weight: 600;
}
.signal-badge.signal-bullish    { background: rgba(4, 120, 87, 0.12);  color: var(--price-up); }
.signal-badge.signal-bearish    { background: rgba(185, 28, 28, 0.12); color: var(--price-down); }
.signal-badge.signal-neutral    { background: rgba(107, 114, 128, 0.12); color: var(--price-neutral); }
.signal-badge.signal-overbought { background: rgba(245, 158, 11, 0.15); color: #b45309; }
.signal-badge.signal-oversold   { background: rgba(99, 102, 241, 0.15); color: #4338ca; }

/* ---------- Composite Score Card ---------- */
.composite-card {
  background: linear-gradient(135deg, #fffdf5 0%, #fff9ed 100%);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
[data-theme="dark"] .composite-card { background: linear-gradient(135deg, #2d2d2d 0%, #1a1a1a 100%); }
.composite-score-bar {
  position: relative;
  height: 12px;
  background: linear-gradient(90deg, var(--price-down) 0%, var(--price-neutral) 50%, var(--price-up) 100%);
  border-radius: var(--radius-full);
}
.composite-score-bar .score-marker {
  position: absolute;
  top: -4px;
  width: 4px;
  height: 20px;
  background: var(--text-primary);
  border-radius: 2px;
  transform: translateX(-50%);
}

/* ---------- Price Alert CTA ---------- */
.price-alert-card {
  background: linear-gradient(135deg, #fffdf5 0%, #fef3c7 100%);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
}
.price-alert-card h6 { margin: 0; font-weight: 600; color: var(--gold-text); }
.price-alert-card .form-control {
  border-color: var(--border-medium);
  border-radius: var(--radius-md);
}
.price-alert-card .btn-alert {
  background: var(--gold-bg);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-4);
  font-weight: 600;
}
.price-alert-card .btn-alert:hover { background: var(--gold-text); color: #fff; }

/* ---------- Yearly History Chips (unified component) ---------- */
.yearly-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}
.yearly-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--fs-sm);
  font-weight: 500;
  background: var(--surface-1);
  color: var(--gold-text);
  border: 1px solid var(--border-soft);
  text-decoration: none;
  transition: all 0.15s ease;
}
.yearly-chip:hover {
  background: var(--surface-3);
  border-color: var(--border-medium);
  color: var(--gold-text);
  transform: translateY(-1px);
}
.yearly-chip .delta-up   { color: var(--price-up); font-size: var(--fs-xs); }
.yearly-chip .delta-down { color: var(--price-down); font-size: var(--fs-xs); }

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------- Better focus on flash animations ---------- */
.sb-flash-up   { background: rgba(4, 120, 87, 0.18) !important; }
.sb-flash-down { background: rgba(185, 28, 28, 0.18) !important; }

/* ---------- Visually hidden utility ---------- */
.visually-hidden, .sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* ---------- Theme toggle button ---------- */
.theme-toggle {
  background: transparent;
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-full);
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--gold-text);
  cursor: pointer;
  transition: all 0.2s ease;
}
.theme-toggle:hover { background: var(--surface-3); }
[data-theme="dark"] .theme-toggle { color: var(--gold-text); border-color: var(--border-medium); }
