/* Stylesheet for Rx Portal - Improved Clinical Theme */
/* Based on tasteskill design directives: Minimalist/Editorial, Variance: 6, Motion: 5, Density: 5 */

:root {
  color-scheme: light;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-hover: #fafcfd;
  --surface-muted: #f1f5f9;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-hover: #cbd5e1;
  --primary: #0d9488; /* Clinical Teal Accent */
  --primary-hover: #0f766e;
  --primary-light: #f0fdfa;
  --primary-border: #99f6e4;
  --ok: #10b981; /* Emerald */
  --ok-light: #ecfdf5;
  --ok-border: #a7f3d0;
  --warn: #f59e0b; /* Amber */
  --warn-light: #fffbeb;
  --warn-border: #fef3c7;
  --bad: #f43f5e; /* Rose */
  --bad-light: #fff1f2;
  --bad-border: #ffe4e6;
  
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  --font-mono: "Cascadia Mono", Consolas, "SFMono-Regular", monospace;
  
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.02);
  --shadow-md: 0 4px 20px -2px rgba(15, 23, 42, 0.04), 0 2px 8px -1px rgba(15, 23, 42, 0.02);
  --shadow-lg: 0 12px 30px -4px rgba(15, 23, 42, 0.06), 0 4px 12px -2px rgba(15, 23, 42, 0.03);
  --shadow-primary: 0 10px 20px -4px rgba(13, 148, 136, 0.15);
  
  --radius: 12px;
  --radius-sm: 8px;
  --radius-pill: 9999px;
  
  --transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  --topbar-sticky-top: 12px;
  --topbar-sticky-height: 66px;
  --content-sticky-top: calc(var(--topbar-sticky-top) + var(--topbar-sticky-height) + 12px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  font-size: 15px;
}

body {
  font-family: var(--font-sans);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Scroll Margins for anchors */
#search-section,
#results-section,
#history-section {
  scroll-margin-top: calc(var(--content-sticky-top) + 24px);
}

/* Headings */
h1, h2, h3, h4 {
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

h1 { font-size: 1.8rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.2rem; }

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--line-hover);
  border-radius: var(--radius-pill);
}
::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}

/* Button Base styles */
button {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  font-weight: 500;
  border: 0;
  border-radius: var(--radius-sm);
  min-height: 40px;
  padding: 8px 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: var(--transition);
  user-select: none;
}

button:active {
  transform: scale(0.98);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

/* Button Themes */
.btn-primary {
  background: var(--primary);
  color: #ffffff;
  box-shadow: var(--shadow-primary);
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  box-shadow: 0 12px 24px -4px rgba(13, 148, 136, 0.25);
  transform: translateY(-1px);
}

.btn-secondary {
  background: #ffffff;
  border: 1px solid var(--line);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}
.btn-secondary:hover:not(:disabled) {
  border-color: var(--line-hover);
  background: var(--surface-hover);
  transform: translateY(-1px);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
}
.btn-ghost:hover:not(:disabled) {
  background: var(--surface-muted);
  color: var(--ink);
}

.btn-sm {
  min-height: 32px;
  font-size: 0.8rem;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
}

/* Inputs & Form Elements */
input, select {
  font-family: var(--font-sans);
  font-size: 0.95rem;
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  color: var(--ink);
  background: #ffffff;
  outline: none;
  transition: var(--transition);
}

input::placeholder {
  color: var(--muted);
  opacity: 0.6;
}

input:hover, select:hover {
  border-color: var(--line-hover);
  background: var(--surface-hover);
}

input:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(13, 148, 136, 0.08);
}

/* Field Container */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.field > span {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ink);
  opacity: 0.8;
}

.field-hint {
  min-height: 1.15rem;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.2;
}

.form-message {
  font-size: 0.85rem;
  color: var(--bad);
  margin-top: 4px;
  font-weight: 500;
}

.form-message.ok {
  color: var(--good);
}

.hidden {
  display: none !important;
}

/* Auth View */
.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  position: relative;
  isolation: isolate;
  overflow: hidden;
  padding: 24px;
  background:
    linear-gradient(90deg, rgba(246, 252, 253, 0.76), rgba(250, 253, 254, 0.3) 54%, rgba(246, 251, 253, 0.76)),
    url('/assets/pharma-research-background.png') center / cover no-repeat;
}

.login-view::before,
.login-view::after {
  content: '';
  position: absolute;
  pointer-events: none;
  z-index: -1;
}

.login-view::before {
  inset: 0;
  background: radial-gradient(circle at 50% 45%, rgba(255, 255, 255, 0.24), transparent 42%);
}

.login-view::after {
  width: min(42vw, 560px);
  height: min(42vw, 560px);
  top: -16%;
  right: -9%;
  border-radius: 50%;
  border: 1px solid rgba(13, 148, 136, 0.1);
  box-shadow: 0 0 0 42px rgba(13, 148, 136, 0.025), 0 0 0 84px rgba(13, 148, 136, 0.018);
}

.login-card {
  width: min(432px, 100%);
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.82);
  border-radius: 20px;
  padding: 34px;
  box-shadow: 0 24px 60px rgba(15, 65, 80, 0.14), 0 4px 18px rgba(15, 65, 80, 0.08);
  backdrop-filter: blur(14px);
  animation: login-card-in 0.48s cubic-bezier(0.16, 1, 0.3, 1);
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--primary), var(--primary-hover));
  color: #ffffff;
  font-weight: 700;
  font-size: 1.2rem;
  box-shadow: var(--shadow-primary);
  user-select: none;
}

.brand-row h1 {
  font-size: 1.45rem;
  color: var(--ink);
}

.brand-row p {
  font-size: 0.85rem;
  color: var(--muted);
  max-width: 260px;
  line-height: 1.45;
}

.login-signals {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 24px;
}

.login-signals span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 9px;
  border: 1px solid rgba(13, 148, 136, 0.16);
  border-radius: var(--radius-pill);
  background: rgba(240, 253, 250, 0.72);
  color: #0f766e;
  font-size: 0.72rem;
  font-weight: 650;
}

.login-signals span::before {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  content: '';
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-form .btn-primary {
  margin-top: 4px;
}

.login-security-note {
  margin-top: 18px;
  color: var(--muted);
  font-size: 0.72rem;
  text-align: center;
}

/* App View & Shell */
.app-view {
  max-width: 1440px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

/* Top bar navigation */
.topbar {
  position: sticky;
  top: var(--topbar-sticky-top);
  z-index: 50;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(226, 232, 240, 0.85);
  border-radius: var(--radius);
  padding: 14px 22px;
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(16px);
  min-height: 64px;
  transition: var(--transition);
}

.topbar:hover {
  border-color: rgba(203, 213, 225, 0.9);
  box-shadow: var(--shadow-lg);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand .brand-mark {
  width: 38px;
  height: 38px;
  font-size: 1rem;
}

.brand strong {
  display: block;
  font-size: 0.95rem;
  font-weight: 600;
}

.brand span {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
}

.topnav {
  display: flex;
  gap: 4px;
}

.nav-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.nav-link:hover {
  background: var(--surface-muted);
  color: var(--ink);
}

.nav-link.active {
  background: var(--primary-light);
  color: var(--primary);
  box-shadow: inset 0 0 0 1px rgba(13, 148, 136, 0.15);
}

.nav-link.disabled {
  color: var(--line-hover);
  cursor: not-allowed;
  opacity: 0.5;
}

.nav-link.disabled:hover {
  background: transparent;
  color: var(--line-hover);
}

.user-box {
  display: flex;
  align-items: center;
  gap: 12px;
}

.badge-user {
  font-size: 0.85rem;
  font-weight: 500;
  background: var(--surface-muted);
  color: var(--ink);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
}

/* Layout Grid */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 320px);
  gap: 24px;
  margin-top: 24px;
  align-items: start;
}

.main-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  min-width: 0;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: static;
  align-self: start;
  min-width: 0;
}

.results-section {
  grid-column: 1 / -1;
  position: relative;
  z-index: 10;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 2px);
  background: var(--surface);
}

.admin-section {
  align-self: start;
}

.admin-user-form {
  margin: 18px 0 20px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.checkbox-field .inline-check {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
}

.inline-check input {
  width: auto;
  min-height: auto;
}

.admin-form-actions {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.admin-form-actions .btn-primary,
.admin-form-actions .btn-secondary {
  min-height: 42px;
  white-space: nowrap;
}

.admin-table-wrap {
  margin-top: 10px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 0.8rem;
  font-weight: 700;
  background: var(--surface-muted);
}

.status-pill.ok {
  color: var(--good);
  background: var(--primary-light);
}

.status-pill.error {
  color: var(--bad);
  background: rgba(244, 63, 94, 0.08);
}

.table-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Bento Style Panel */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 24px;
  transition: var(--transition);
}

.panel:hover {
  border-color: var(--line-hover);
  box-shadow: var(--shadow-md);
}

.results-section {
  grid-column: 1 / -1;
}

.section-head, .panel-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 18px;
  border-bottom: 1px solid var(--surface-muted);
  padding-bottom: 12px;
}

.results-section > .panel-head {
  position: static;
  z-index: auto;
  margin: 0 0 18px;
  padding: 0 0 12px;
  background: transparent;
  border-bottom: 1px solid var(--surface-muted);
  border-radius: 0;
  backdrop-filter: none;
}

.panel-head h3 {
  margin: 0;
  font-size: 1rem;
}

.panel-head .button-row,
.panel-head .result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: flex-end;
}

.result-actions {
  width: 100%;
}

@media (max-width: 1100px) {
  .result-actions {
    justify-content: flex-start;
  }
}

.eyebrow {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  margin-bottom: 4px;
  letter-spacing: 0.12em;
}

/* Grid helper classes */
.col-span-4 { grid-column: span 4; }
.col-span-6 { grid-column: span 6; }
.col-span-12 { grid-column: span 12; }

/* Form layout inside Panel */
.search-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 16px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  border-top: 1px solid var(--surface-muted);
  padding-top: 16px;
  margin-top: 4px;
}

/* Search status panel */
.run-state {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px 10px;
  min-width: min(100%, 286px);
  max-width: 340px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
  color: var(--muted);
  text-align: left;
  transition: background .2s ease, border-color .2s ease, color .2s ease;
}

.run-state-icon {
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--line-hover);
}

.run-state-copy {
  min-width: 0;
}

.run-state-copy strong,
.run-state-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-state-copy strong {
  color: var(--ink);
  font-size: .78rem;
  font-weight: 700;
}

.run-state-copy small {
  margin-top: 2px;
  color: var(--muted);
  font-size: .69rem;
}

.run-state-meta {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .68rem;
  font-weight: 600;
  white-space: nowrap;
}

.run-state-progress {
  grid-column: 1 / -1;
  height: 3px;
  overflow: hidden;
  border-radius: var(--radius-pill);
  background: rgba(100, 116, 139, .2);
}

.run-state-progress span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width .35s ease;
}

.run-state.running,
.run-state.normalizing {
  background: var(--warn-light);
  color: var(--warn);
  border-color: var(--warn-border);
}

.run-state.running .run-state-icon,
.run-state.normalizing .run-state-icon {
  border: 2px solid rgba(217, 119, 6, .22);
  border-top-color: var(--warn);
  background: transparent;
  animation: run-state-spin .85s linear infinite;
}

.run-state.running .run-state-progress span,
.run-state.normalizing .run-state-progress span {
  background: var(--warn);
}

.run-state.reconnecting {
  background: var(--warn-light);
  color: var(--warn);
  border-color: var(--warn-border);
}

.run-state.reconnecting .run-state-icon::before {
  content: '!';
  color: var(--warn);
  font-weight: 800;
}

.run-state.done {
  background: var(--ok-light);
  color: var(--ok);
  border-color: var(--ok-border);
}

.run-state.done .run-state-icon {
  background: var(--ok);
  animation: run-state-pop .34s ease-out both;
}

.run-state.done .run-state-icon::before {
  content: '✓';
  color: #fff;
  font-size: .75rem;
  font-weight: 800;
}

.run-state.done .run-state-progress span {
  background: var(--ok);
}

.run-state.error {
  background: var(--bad-light);
  color: var(--bad);
  border-color: var(--bad-border);
}

.run-state.error .run-state-icon {
  background: var(--bad);
}

.run-state.error .run-state-icon::before {
  content: '!';
  color: #fff;
  font-size: .75rem;
  font-weight: 800;
}

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

@keyframes run-state-pop {
  0% { transform: scale(.45); opacity: .2; }
  75% { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

/* Source Selection Grid */
.sources-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-auto-rows: 36px;
  gap: 6px;
  align-items: stretch;
}

.source-option {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  height: 36px;
  min-height: 36px;
  max-height: 36px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-left-width: 2px;
  border-radius: 6px;
  background: #ffffff;
  cursor: pointer;
  transition: var(--transition);
  font-size: 0.78rem;
  user-select: none;
}

.source-option:hover {
  border-top-color: var(--line-hover);
  border-right-color: var(--line-hover);
  border-bottom-color: var(--line-hover);
  box-shadow: var(--shadow-sm);
}

.source-option input[type="checkbox"] {
  width: 15px;
  height: 15px;
  min-height: 15px;
  accent-color: var(--primary);
  cursor: pointer;
  flex: 0 0 auto;
  margin: 0;
}

.source-name {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 500;
  color: var(--ink);
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.source-indicator {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  margin-left: auto;
  border: 1.5px solid var(--ok);
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 2px var(--ok-light);
}

.source-option.source-ok {
  border-color: var(--line);
  border-left-color: var(--ok);
  background: #ffffff;
}

.source-option.source-ok.source-partial-excipients {
  border-color: var(--line);
  border-left-color: var(--warn);
  background: #ffffff;
}

.source-option.source-ok.source-partial-excipients .source-indicator {
  border-color: var(--warn);
  background: linear-gradient(90deg, var(--warn) 50%, transparent 50%);
  box-shadow: 0 0 0 2px var(--warn-light);
}

.source-option.source-ok.source-no-excipients {
  border-color: var(--line);
  border-left-color: #94a3b8;
  background: #ffffff;
}

.source-option.source-ok.source-no-excipients .source-indicator {
  border-color: #94a3b8;
  background: transparent;
  box-shadow: none;
}

.source-option:not(.source-ok) {
  border-left-color: var(--bad);
  background: var(--bad-light);
}

.source-option:not(.source-ok) .source-indicator {
  border-color: var(--bad);
  border-radius: 2px;
  background: var(--bad);
  box-shadow: none;
  transform: rotate(45deg);
}

.source-toggle {
  display: block;
  width: auto;
  min-height: 30px;
  margin: 8px 0 0 auto;
  padding: 5px 10px;
  font-size: 0.76rem;
}

@media (max-width: 1100px) {
  .sources-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 800px) {
  .sources-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 600px) {
  .sources-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* Progress Section */
.progress-summary-badge {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-light);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
}

.normalized-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.chip {
  font-size: 0.8rem;
  font-weight: 500;
  padding: 4px 12px;
  background: var(--surface-muted);
  color: var(--ink);
  border-radius: var(--radius-pill);
  border: 1px solid var(--line);
}

.chip.ok {
  background: var(--ok-light);
  border-color: var(--ok-border);
  color: var(--ok);
}

.chip.warn {
  background: var(--warn-light);
  border-color: var(--warn-border);
  color: var(--warn);
}

.chip.error {
  background: var(--bad-light);
  border-color: var(--bad-border);
  color: var(--bad);
}

.progress-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #ffffff;
  transition: var(--transition);
}

.progress-item:hover {
  border-color: var(--line-hover);
  box-shadow: var(--shadow-sm);
}

.progress-item .dot {
  width: 8px;
  height: 8px;
  min-width: 8px;
  border-radius: var(--radius-pill);
  background: var(--line-hover);
}

.progress-item.running .dot {
  background: var(--warn);
  animation: blink 1.5s infinite;
}

.progress-item.done .dot {
  background: var(--ok);
}

.progress-item.error .dot {
  background: var(--bad);
}

.progress-item div {
  flex-grow: 1;
  min-width: 0;
}

.progress-item strong {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.progress-item small {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.3;
}

.progress-item em {
  font-style: normal;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
}

/* Stats Panel & cards */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.stat-card {
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid rgba(203, 213, 225, 0.7);
  border-radius: calc(var(--radius) + 4px);
  padding: 18px 16px;
  text-align: center;
  transition: var(--transition);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.5);
}

.stat-card:hover {
  transform: translateY(-1px);
}

.stats-panel {
  display: grid;
  gap: 18px;
  max-height: none;
  overflow: visible;
}

.stats-panel .panel-head {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.stat-card span {
  display: block;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 4px;
}

.stat-card strong {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--ink);
}

.stat-card.success strong { color: var(--ok); }
.stat-card.empty strong { color: var(--muted); }
.stat-card.error strong { color: var(--bad); }

.country-stats-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
  max-height: min(360px, 40vh);
  overflow-y: auto;
  padding-right: 4px;
  overscroll-behavior: contain;
}

.country-row {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  min-width: 0;
  min-height: 34px;
  padding: 10px 12px;
  background: #ffffff;
  border: 1px solid rgba(226, 232, 240, 0.9);
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  line-height: 1.25;
  overflow: hidden;
}

.country-row span {
  z-index: 2;
  font-weight: 600;
  color: var(--ink);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.country-row strong {
  z-index: 2;
  font-family: var(--font-mono);
  color: var(--ink);
}

.country-row i {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.country-row.error i { background: var(--bad); }
.country-row.running i { background: var(--warn); }

/* History Panel */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 350px;
  overflow-y: auto;
  padding-right: 4px;
}

.history-item {
  width: 100%;
  display: flex;
  flex-direction: column;
  text-align: left;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #ffffff;
  transition: var(--transition);
  box-shadow: none;
  min-height: auto;
}

.history-item:hover {
  transform: translateY(-1px);
  border-color: var(--line-hover);
  box-shadow: var(--shadow-sm);
  background: var(--surface-hover);
}

.history-item strong {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--ink);
}

.history-item span {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 2px;
}

.history-item small {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 6px;
  opacity: 0.8;
}

.history-owner {
  margin-top: 6px;
  color: var(--primary-hover);
  font-size: 0.72rem;
  font-style: normal;
  font-weight: 650;
}

/* Results section & tables */
.results-title-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.badge-count {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--primary-light);
  color: var(--primary);
  border: 1px solid var(--primary-border);
  padding: 2px 10px;
  border-radius: var(--radius-pill);
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.formula-analysis-bar {
  display: grid;
  grid-template-columns: minmax(210px, 0.75fr) minmax(540px, 2fr);
  gap: 12px 20px;
  margin-bottom: 16px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--surface) 0%, var(--surface-hover) 100%);
}

.formula-analysis-copy {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.formula-analysis-copy strong {
  font-size: 0.9rem;
}

.formula-analysis-copy span,
.formula-ai-model-description {
  color: var(--muted);
  font-size: 0.76rem;
}

.formula-analysis-settings {
  display: grid;
  grid-template-columns: minmax(130px, 0.7fr) minmax(190px, 1fr) minmax(210px, 1.1fr);
  gap: 10px;
}

.formula-analysis-settings label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.formula-analysis-settings label > span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
}

.formula-analysis-settings input,
.formula-analysis-settings select {
  min-height: 36px;
  padding: 6px 9px;
  font-size: 0.8rem;
}

.formula-ai-model-description {
  grid-column: 2;
  margin-top: -6px;
}

.formula-analysis-status {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 4px 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.formula-analysis-status-icon {
  grid-row: 1;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 2px solid var(--line-hover);
  border-radius: 50%;
}

.formula-analysis-status-copy {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  align-items: baseline;
  min-width: 0;
}

.formula-analysis-status-copy strong {
  font-size: 0.82rem;
}

.formula-analysis-status-copy span {
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 0.75rem;
}

.formula-analysis-progress {
  grid-column: 1 / -1;
  height: 4px;
  overflow: hidden;
  border-radius: var(--radius-pill);
  background: var(--surface-muted);
}

.formula-analysis-progress span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--primary);
  transition: width 0.35s ease;
}

.formula-analysis-status.preparing .formula-analysis-status-icon,
.formula-analysis-status.ai_analyzing .formula-analysis-status-icon,
.formula-analysis-status.validating .formula-analysis-status-icon,
.formula-analysis-status.aggregating .formula-analysis-status-icon,
.formula-analysis-status.rendering_report .formula-analysis-status-icon {
  border-color: var(--primary-border);
  border-top-color: var(--primary);
  animation: formula-analysis-spin 0.8s linear infinite;
}

.formula-analysis-status.completed {
  border-color: var(--ok-border);
  background: var(--ok-light);
}

.formula-analysis-status.completed .formula-analysis-status-icon {
  border-color: var(--ok);
  background: var(--ok);
}

.formula-analysis-status.completed .formula-analysis-status-icon::after {
  content: '';
  width: 8px;
  height: 4px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: translateY(-1px) rotate(-45deg);
}

.formula-analysis-status.failed {
  border-color: var(--bad-border);
  background: var(--bad-light);
}

.formula-analysis-status.failed .formula-analysis-status-icon {
  border-color: var(--bad);
}

.formula-analysis-status.failed .formula-analysis-status-icon::after {
  content: '!';
  color: var(--bad);
  font-size: 0.78rem;
  font-weight: 800;
}

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

.table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.88rem;
}

.data-table th {
  background: var(--surface-muted);
  color: var(--ink);
  font-weight: 600;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}

.data-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  line-height: 1.4;
  vertical-align: top;
}

.product-name-cell {
  display: flex;
  align-items: flex-start;
  gap: 7px;
}

.row-normalization {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  min-width: 18px;
  margin-top: 1px;
  border-radius: 50%;
  font-size: .68rem;
  font-weight: 800;
}

.row-normalization.pending {
  background: var(--surface-muted);
  border: 1px solid var(--line);
}

.row-normalization.running {
  border: 2px solid rgba(217, 119, 6, .22);
  border-top-color: var(--warn);
  animation: row-normalization-spin .85s linear infinite;
}

.row-normalization.done {
  background: var(--ok);
  color: #fff;
}

.row-normalization.error {
  background: var(--bad-light);
  color: var(--bad);
  border: 1px solid var(--bad-border);
}

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

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

.data-table tr:last-child td {
  border-bottom: 0;
}

.data-table tr:hover td {
  background: var(--surface-hover);
}

.data-table td.empty-row {
  text-align: center;
  color: var(--muted);
  padding: 36px;
  font-style: italic;
}

.data-table a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
  transition: var(--transition);
}

.data-table a:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* Toast Notification styling */
.app-toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  background: var(--ink);
  color: #ffffff;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  font-size: 0.9rem;
  font-weight: 500;
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
}

.app-toast.show {
  transform: translateY(0);
  opacity: 1;
}

.app-toast.ok { background: var(--ok); }
.app-toast.warn { background: var(--warn); }
.app-toast.error { background: var(--bad); }

/* Keyframe animations */
@keyframes login-card-in {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .login-card,
  .run-state.running .run-state-icon,
  .run-state.normalizing .run-state-icon,
  .run-state.done .run-state-icon {
    animation: none;
  }

  .row-normalization.running {
    animation: none;
  }

  .run-state-progress span {
    transition: none;
  }

  .formula-analysis-status-icon,
  .formula-analysis-progress span {
    animation: none !important;
    transition: none;
  }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* Responsiveness */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
  .main-content { order: 1; }
  .results-section { order: 2; }
  .side-panel { order: 3; }
  .admin-section { order: 4; }
  .side-panel {
    position: static;
  }
  .country-stats-list {
    max-height: 280px;
  }
}

@media (max-width: 860px) {
  .formula-analysis-bar {
    grid-template-columns: 1fr;
  }
  .formula-analysis-settings {
    grid-template-columns: 1fr 1fr;
  }
  .formula-analysis-settings label:last-child {
    grid-column: 1 / -1;
  }
  .formula-ai-model-description {
    grid-column: 1;
    margin-top: 0;
  }
  .data-table th,
  .data-table td {
    padding: 10px 12px;
  }
  .table-wrap {
    border-radius: var(--radius-sm);
  }
  .result-actions {
    justify-content: center;
  }
}

@media (max-width: 768px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
  .col-span-4, .col-span-6, .col-span-12 {
    grid-column: span 12;
  }
  .topbar {
    position: static;
    flex-direction: column;
    height: auto;
    padding: 16px;
    gap: 12px;
  }
  .results-section > .panel-head {
    position: static;
    margin: 0 0 18px;
    padding: 0 0 12px;
    background: transparent;
    backdrop-filter: none;
  }
  .topnav {
    width: 100%;
    flex-wrap: wrap;
    justify-content: center;
  }
  .user-box {
    width: 100%;
    justify-content: center;
  }
  .data-table th,
  .data-table td {
    min-width: 120px;
  }
  .section-head,
  .panel-head {
    flex-direction: column;
  }
  .section-head .run-state {
    width: 100%;
    max-width: none;
  }
  .panel-head .button-row,
  .panel-head .result-actions,
  .form-actions {
    width: 100%;
    justify-content: stretch;
  }
  .panel-head .button-row button,
  .panel-head .result-actions button,
  .form-actions button {
    flex: 1 1 140px;
  }
  .app-toast {
    right: 16px;
    bottom: 16px;
    left: 16px;
    text-align: center;
  }
}

@media (max-width: 520px) {
  .sources-grid {
    grid-template-columns: 1fr;
  }
  .formula-analysis-settings {
    grid-template-columns: 1fr;
  }
  .formula-analysis-settings label:last-child {
    grid-column: auto;
  }
  .login-view {
    align-items: center;
    padding: 18px;
    background-position: 38% center;
  }
  .login-card {
    padding: 28px 22px;
  }
  .brand-row {
    gap: 12px;
  }
  .login-signals {
    margin-bottom: 20px;
  }
}
