/* Target Path: style.css */
/**
 * 🌌 ValueCompare Design System — Single Source CSS (100% SOT)
 * 🚀 Mobile-First Responsive, Noto Sans Thai, Smooth Micro-interactions
 */

@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@300;400;500;600;700&display=swap');

:root {
  --font-main: 'Noto Sans Thai', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  /* Color Palette (Teal & Emerald Primary) */
  --primary: #0d9488;
  --primary-hover: #0f766e;
  --primary-soft: #f0fdfa;
  --primary-border: #ccfbf1;
  --primary-text: #115e59;

  --winner-bg: #ecfdf5;
  --winner-border: #10b981;
  --winner-text: #065f46;
  --winner-solid: #059669;

  --warn-bg: #fffbeb;
  --warn-border: #fde68a;
  --warn-text: #92400e;

  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --danger-text: #b91c1c;

  /* Neutrals */
  --bg-page: #f8fafc;
  --bg-card: #ffffff;
  --bg-subtle: #f1f5f9;
  --border-default: #e2e8f0;
  --border-focus: #0d9488;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;

  /* Shadows (Beautiful Neutral Layers) */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-card: 0 4px 20px -4px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.04);
  --shadow-winner: 0 8px 24px -6px rgba(16, 185, 129, 0.18), 0 2px 6px rgba(16, 185, 129, 0.08);
  --shadow-lg: 0 10px 25px -5px rgba(15, 23, 42, 0.08), 0 8px 10px -6px rgba(15, 23, 42, 0.04);

  --radius-sm: 0.5rem;
  --radius-md: 0.75rem;
  --radius-card: 1rem;
  --radius-pill: 9999px;

  --transition-fast: 0.18s ease;
  --transition-normal: 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

/* Dark Mode Tokens (รองรับ Phase 3) */
[data-theme="dark"] {
  --bg-page: #0b1120;
  --bg-card: #1e293b;
  --bg-subtle: #0f172a;
  --border-default: #334155;
  --border-focus: #14b8a6;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-subtle: #64748b;
  --primary-soft: #134e4a;
  --primary-border: #2dd4bf;
  --primary-text: #ccfbf1;
  --winner-bg: #064e3b;
  --winner-border: #10b981;
  --winner-text: #a7f3d0;
  --warn-bg: #451a03;
  --warn-border: #78350f;
  --warn-text: #fde68a;
  --danger-soft: #4c0519;
  --danger-border: #881337;
  --danger-text: #fecdd3;
  --shadow-card: 0 4px 20px -4px rgba(0, 0, 0, 0.5);
}

/* ─────────────────────────────────────────────
 * Base Reset & Typography
 * ───────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  font-family: var(--font-main);
  background-color: var(--bg-page);
  color: var(--text-main);
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
}

body {
  background-image:
    radial-gradient(at 10% 0%, rgba(13, 148, 136, 0.06) 0px, transparent 50%),
    radial-gradient(at 90% 10%, rgba(56, 189, 248, 0.06) 0px, transparent 45%);
}

.tabular-nums {
  font-variant-numeric: tabular-nums;
}

/* 🎯 Outline Reset: ลบขอบดำเมื่อคลิก แต่คงขอบ Focus เมื่อกด Tab */
button:focus:not(:focus-visible),
input:focus:not(:focus-visible),
select:focus:not(:focus-visible) {
  outline: none !important;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Remove number spinners */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type="number"] {
  -moz-appearance: textfield;
}

/* ─────────────────────────────────────────────
 * Layout & Shell
 * ───────────────────────────────────────────── */
.app-shell {
  max-width: 64rem;
  margin: 0 auto;
  padding: 1.25rem 1rem 3rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

@media (min-width: 640px) {
  .app-shell {
    padding: 2rem 1.5rem 4rem 1.5rem;
  }
}

/* ─────────────────────────────────────────────
 * Header
 * ───────────────────────────────────────────── */
.app-header {
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-card);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 768px) {
  .app-header {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 1.5rem;
  }
}

.brand-wrapper {
  display: flex;
  align-items: center;
  gap: 0.875rem;
}

.brand-icon-box {
  width: 3rem;
  height: 3rem;
  border-radius: 1rem;
  background: linear-gradient(135deg, #0d9488 0%, #059669 100%);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  box-shadow: 0 4px 12px rgba(13, 148, 136, 0.25);
  flex-shrink: 0;
}

.brand-title-group h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-main);
  letter-spacing: -0.02em;
}

@media (min-width: 640px) {
  .brand-title-group h1 {
    font-size: 1.5rem;
  }
}

.brand-title-group p {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin-top: 0.125rem;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

/* ─────────────────────────────────────────────
 * Buttons & Badges
 * ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 44px;
  padding: 0.5rem 0.875rem;
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  transition: var(--transition-normal);
  user-select: none;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-secondary {
  background: #ffffff;
  border-color: var(--border-default);
  color: var(--text-main);
}

.btn-secondary:hover {
  background: var(--bg-subtle);
  border-color: #cbd5e1;
}

.btn-danger {
  background: var(--danger-soft);
  border-color: var(--danger-border);
  color: var(--danger-text);
}

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

.btn-update-available {
  background: var(--warn-bg);
  border-color: #fcd34d;
  color: var(--warn-text);
  font-weight: 600;
}

.btn-update-available:hover {
  background: #fef3c7;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.25rem 0.625rem;
  border-radius: var(--radius-pill);
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.badge-brand {
  background: var(--primary-soft);
  color: var(--primary-text);
  border: 1px solid var(--primary-border);
}

.badge-neutral {
  background: var(--bg-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border-default);
}

.badge-winner {
  background: var(--winner-solid);
  color: #ffffff;
  box-shadow: 0 2px 6px rgba(16, 185, 129, 0.3);
}

.badge-diff {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
}

/* ─────────────────────────────────────────────
 * Form & Product Rows
 * ───────────────────────────────────────────── */
.form-container {
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
}

.warning-banner {
  display: flex;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  font-size: 0.8125rem;
  line-height: 1.4;
}

.warning-banner.hidden {
  display: none;
}

.item-row {
  background: var(--bg-card);
  border: 1.5px solid var(--border-default);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 0.875rem 1rem;
  transition: transform var(--transition-normal),
              border-color var(--transition-fast),
              box-shadow var(--transition-fast),
              background-color var(--transition-fast);
}

@media (min-width: 640px) {
  .item-row {
    padding: 1rem 1.25rem;
  }
}

.item-row:focus-within {
  border-color: #99f6e4;
  box-shadow: 0 8px 24px -6px rgba(13, 148, 136, 0.12);
}

.item-row.is-winner {
  background: linear-gradient(180deg, #ffffff 0%, var(--winner-bg) 100%);
  border-color: var(--winner-border);
  box-shadow: var(--shadow-winner);
}

.item-row.is-invalid {
  border-color: #fca5a5;
}

.row-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  align-items: flex-end;
}

@media (min-width: 768px) {
  .row-grid {
    grid-template-columns: 5fr 3fr 4fr;
    gap: 0.875rem;
  }
}

.amount-unit-grid {
  display: grid;
  grid-template-columns: 7fr 5fr;
  gap: 0.625rem;
}

.field-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-subtle);
  margin-bottom: 0.25rem;
}

.app-input, .app-select {
  width: 100%;
  min-height: 44px;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  font-family: inherit;
  background: var(--bg-subtle);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-main);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast), background-color var(--transition-fast);
}

.app-input::placeholder {
  color: var(--text-subtle);
}

.app-input:focus, .app-select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(13, 148, 136, 0.14);
  background-color: #ffffff;
}

.input-with-icon {
  position: relative;
  display: flex;
  align-items: center;
}

.input-row-badge {
  position: absolute;
  left: 0.5rem;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: var(--radius-sm);
  background: #e2e8f0;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.input-with-icon .app-input {
  padding-left: 2.75rem;
}

.input-suffix-box {
  position: relative;
}

.input-suffix {
  position: absolute;
  right: 0.875rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.75rem;
  color: var(--text-muted);
  pointer-events: none;
}

.input-suffix-box .app-input {
  padding-right: 2.75rem;
}

/* Inline Result Bar */
.row-result-slot {
  margin-top: 0.75rem;
  padding-top: 0.625rem;
  border-top: 1px solid var(--border-default);
}

.row-result-slot.hidden {
  display: none;
}

.result-content {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.result-price-box {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.375rem;
}

.result-price-number {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--text-main);
}

.result-price-number.winner {
  color: var(--winner-text);
  font-size: 1.25rem;
}

.form-helper-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.25rem 0.25rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ─────────────────────────────────────────────
 * History Section
 * ───────────────────────────────────────────── */
.history-section {
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-card);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 640px) {
  .history-section {
    padding: 1.25rem 1.5rem;
  }
}

.history-header {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

@media (min-width: 640px) {
  .history-header {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

.history-title-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.history-title {
  font-size: 1.0625rem;
  font-weight: 700;
  color: var(--text-main);
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.history-toolbar {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.625rem;
}

@media (min-width: 640px) {
  .history-toolbar {
    grid-template-columns: 6fr 3fr 3fr;
  }
}

.history-list-container {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.history-card {
  background: var(--bg-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 0.875rem 1rem;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.history-card:hover {
  border-color: #99f6e4;
  box-shadow: var(--shadow-sm);
}

.history-card-top {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
}

.history-best-info {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-main);
  margin-top: 0.125rem;
}

.history-item-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.375rem 0.625rem;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
}

.history-item-winner {
  background: var(--winner-bg);
  border: 1px solid #a7f3d0;
  color: var(--winner-text);
  font-weight: 600;
}

.history-item-regular {
  background: var(--bg-subtle);
  border: 1px solid var(--border-default);
  color: var(--text-main);
}

.history-empty-state {
  padding: 2.5rem 1rem;
  text-align: center;
  border: 1.5px dashed var(--border-default);
  border-radius: var(--radius-md);
  background: var(--bg-subtle);
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

/* ─────────────────────────────────────────────
 * Animations
 * ───────────────────────────────────────────── */
@keyframes fadeSlideIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes badgePop {
  0% {
    opacity: 0;
    transform: scale(0.88);
  }
  70% {
    transform: scale(1.04);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateY(-12px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(-10px) scale(0.95);
  }
}

.animate-result {
  animation: fadeSlideIn 0.24s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.animate-badge {
  animation: badgePop 0.28s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.toast-enter {
  animation: toastIn 0.25s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.toast-exit {
  animation: toastOut 0.22s ease forwards;
}

/* Toast Container */
#toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  left: 1rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
}

@media (min-width: 640px) {
  #toast-container {
    left: auto;
    width: 24rem;
  }
}

.toast-card {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: var(--shadow-lg);
  font-size: 0.8125rem;
  font-weight: 500;
  line-height: 1.4;
}

/* SweetAlert2 Thai Font Override */
.swal2-popup {
  font-family: var(--font-main) !important;
  border-radius: 1.25rem !important;
}

.app-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-subtle);
  padding-bottom: 1rem;
}
