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

:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-light: #eef2ff;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --yellow: #d97706;
  --yellow-bg: #fef3c7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --grade-a: #16a34a;
  --grade-b: #65a30d;
  --grade-c: #d97706;
  --grade-d: #ea580c;
  --grade-f: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ========== Layout ========== */
#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
}

header {
  padding: 32px 0 8px;
  text-align: center;
}

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.025em;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.95rem;
  margin-top: 2px;
}

main { padding-bottom: 60px; }

footer {
  text-align: center;
  padding: 24px 0;
  color: var(--text-secondary);
  font-size: 0.8rem;
}

/* ========== Search ========== */
.search-section {
  margin: 20px 0 24px;
}

.search-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  background: var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
}

.tab {
  flex: 1;
  padding: 8px;
  border: none;
  background: transparent;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-secondary);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.search-box {
  display: flex;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}

.search-box:focus-within {
  border-color: var(--primary);
}

.search-box input {
  flex: 1;
  border: none;
  outline: none;
  padding: 14px 16px;
  font-size: 1rem;
  background: transparent;
  color: var(--text);
}

.search-box input::placeholder { color: #9ca3af; }

.search-box button {
  padding: 12px 16px;
  border: none;
  background: var(--primary);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: background 0.15s;
}

.search-box button:hover { background: #4338ca; }

/* ========== Loading ========== */
#loading {
  text-align: center;
  padding: 48px 0;
  color: var(--text-secondary);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 0 auto 12px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Error ========== */
.error-card {
  background: var(--red-bg);
  color: var(--red);
  padding: 16px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
}

/* ========== Search Results Grid ========== */
#search-results h2 {
  font-size: 1rem;
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: 12px;
}

.results-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.result-item:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-md);
}

.result-thumb {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg);
  flex-shrink: 0;
}

.result-info {
  flex: 1;
  min-width: 0;
}

.result-info .name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-info .meta {
  font-size: 0.82rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-arrow {
  color: var(--text-secondary);
  font-size: 1.2rem;
  flex-shrink: 0;
}

/* ========== Product Detail ========== */
.back-button {
  border: none;
  background: none;
  color: var(--primary);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 0;
  margin-bottom: 8px;
}

.product-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.product-header {
  display: flex;
  gap: 16px;
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.product-image-container {
  width: 96px;
  height: 96px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--bg);
  flex-shrink: 0;
}

.product-image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  min-width: 0;
}

.product-info h2 {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.3;
}

.brand {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 2px;
}

.category {
  font-size: 0.8rem;
  color: var(--primary);
  background: var(--primary-light);
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  margin-top: 6px;
}

.barcode-label {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* ========== KIP Score Section ========== */
.kip-section,
.risk-section,
.ingredients-section {
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.ingredients-section { border-bottom: none; }

.kip-section h3,
.risk-section h3,
.ingredients-section h3 {
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.kip-main {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  margin-bottom: 20px;
}

.kip-circle {
  position: relative;
  width: 120px;
  height: 120px;
}

.kip-circle svg {
  transform: rotate(-90deg);
  width: 100%;
  height: 100%;
}

.kip-ring-bg {
  fill: none;
  stroke: var(--border);
  stroke-width: 8;
}

.kip-ring {
  fill: none;
  stroke: var(--grade-a);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 326.73;
  stroke-dashoffset: 326.73;
  transition: stroke-dashoffset 0.8s ease, stroke 0.3s;
}

.kip-value {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.kip-number {
  font-size: 2rem;
  font-weight: 800;
  line-height: 1;
}

.kip-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.kip-grade {
  font-size: 3.5rem;
  font-weight: 900;
  line-height: 1;
  color: var(--grade-a);
  transition: color 0.3s;
}

/* KIP Breakdown Bars */
.kip-breakdown {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.kip-bar-header {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.kip-bar-value { font-weight: 600; }

.kip-bar-track {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.kip-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.8s ease;
  width: 0;
}

.nutrition-fill { background: #6366f1; }
.additive-fill { background: #8b5cf6; }

/* High Risk Warning */
.high-risk-warning {
  margin-top: 16px;
  padding: 10px 14px;
  background: var(--red-bg);
  color: var(--red);
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 500;
  text-align: center;
}

/* ========== Risk Badges ========== */
.risk-badges {
  display: flex;
  gap: 8px;
}

.risk-badge {
  flex: 1;
  text-align: center;
  padding: 12px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
}

.risk-badge span {
  display: block;
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 2px;
}

.risk-badge.safe { background: var(--green-bg); color: var(--green); }
.risk-badge.caution { background: var(--yellow-bg); color: var(--yellow); }
.risk-badge.avoid { background: var(--red-bg); color: var(--red); }

/* ========== Ingredients List ========== */
.ingredients-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ingredient-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.ingredient-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-top: 5px;
  flex-shrink: 0;
}

.ingredient-dot.safe { background: var(--green); }
.ingredient-dot.caution { background: var(--yellow); }
.ingredient-dot.avoid { background: var(--red); }

.ingredient-info {
  flex: 1;
  min-width: 0;
}

.ingredient-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.ingredient-risk {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 1px;
}

/* ========== Calculation Details ========== */
.calc-details {
  margin-top: 20px;
}

.calc-toggle {
  display: block;
  width: 100%;
  padding: 10px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--primary);
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.calc-toggle:hover {
  background: var(--primary-light);
  border-color: var(--primary);
}

.calc-toggle.open {
  border-style: solid;
  border-color: var(--primary);
  background: var(--primary-light);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  border-bottom: none;
}

.calc-body {
  border: 1.5px solid var(--primary);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  padding: 16px;
  background: var(--primary-light);
}

.calc-step {
  margin-bottom: 16px;
}

.calc-step:last-child { margin-bottom: 0; }

.calc-step h4,
.calc-formula h4 {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--primary);
  margin-bottom: 6px;
}

.calc-step p {
  font-size: 0.88rem;
  color: var(--text);
  line-height: 1.6;
}

.formula-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 16px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.85rem;
  line-height: 1.8;
  color: var(--text);
  margin-bottom: 16px;
}

.formula-box .formula-line {
  display: block;
}

.formula-box .formula-highlight {
  color: var(--primary);
  font-weight: 600;
}

.formula-box .formula-dim {
  color: var(--text-secondary);
}

.formula-box .formula-result {
  font-weight: 700;
  color: var(--text);
}

.formula-box .formula-cap {
  color: var(--red);
  font-weight: 600;
}

/* Additive penalty table */
.additive-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin-top: 8px;
}

.additive-table th {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 2px solid var(--border);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
}

.additive-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}

.additive-table tfoot td {
  font-weight: 700;
  border-bottom: none;
  border-top: 2px solid var(--border);
}

.cat-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 600;
}

.cat-badge.cat-1 { background: var(--green-bg); color: var(--green); }
.cat-badge.cat-2 { background: #eff6ff; color: #2563eb; }
.cat-badge.cat-3 { background: var(--yellow-bg); color: var(--yellow); }
.cat-badge.cat-4 { background: var(--red-bg); color: var(--red); }

.penalty-val { font-weight: 600; font-family: 'SF Mono', 'Fira Code', monospace; }
.penalty-zero { color: var(--text-secondary); }
.penalty-nonzero { color: var(--red); }

.no-additives-msg {
  font-size: 0.88rem;
  color: var(--text-secondary);
  font-style: italic;
  margin-top: 4px;
}

/* ========== Utility ========== */
.hidden { display: none !important; }

/* ========== Responsive ========== */
@media (max-width: 480px) {
  header h1 { font-size: 1.4rem; }
  .product-header { flex-direction: column; align-items: center; text-align: center; }
  .kip-main { flex-direction: column; gap: 12px; }
  .risk-badges { flex-direction: column; }
}
