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

:root {
  --wine-red: #722F37;
  --wine-dark: #4A1C24;
  --wine-light: #A45A64;
  --gold: #C9A84C;
  --gold-light: #E8D5A3;
  --bg: #1A1A2E;
  --bg-card: #16213E;
  --bg-card-hover: #1A2744;
  --text: #E8E8E8;
  --text-muted: #9A9AB0;
  --border: #2A2A4A;
  --white: #FFFFFF;
  --green: #4CAF50;
  --orange: #FF9800;
  --red-badge: #E53935;
}

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

header {
  background: linear-gradient(135deg, var(--wine-dark), var(--wine-red));
  padding: 1.5rem 2rem;
  border-bottom: 2px solid var(--gold);
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

h1 {
  font-size: 1.8rem;
  color: var(--gold-light);
  letter-spacing: 0.5px;
}

.sync-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sync-indicator {
  font-size: 0.75rem;
  padding: 0.25rem 0.6rem;
  border-radius: 12px;
}

.sync-live {
  background: rgba(76, 175, 80, 0.2);
  color: #81C784;
}

.sync-offline {
  background: rgba(255, 152, 0, 0.2);
  color: #FFB74D;
}

.sync-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: var(--gold-light);
  font-size: 1.2rem;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.sync-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.sync-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.download-wrapper {
  position: relative;
}

.download-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.4rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  z-index: 100;
  min-width: 100px;
}

.download-menu.open {
  display: block;
}

.download-menu button {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  background: none;
  border: none;
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
}

.download-menu button:hover {
  background: rgba(255, 255, 255, 0.1);
}

.stats {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.stat {
  text-align: center;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--gold);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--gold-light);
  text-transform: uppercase;
  letter-spacing: 1px;
}

main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
}

.controls {
  margin-bottom: 1.5rem;
}

.search-bar {
  margin-bottom: 1rem;
}

.search-bar input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s;
}

.search-bar input:focus {
  border-color: var(--gold);
}

.filters {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.filters select {
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  cursor: pointer;
  min-width: 160px;
}

.filters select:focus {
  border-color: var(--gold);
}

.results-info {
  padding: 0.5rem 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.wine-grid {
  /* container for table or card grid */
}

.wine-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
  transition: transform 0.15s, border-color 0.15s;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.wine-card:hover {
  transform: translateY(-2px);
  border-color: var(--wine-light);
}

.wine-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
}

.wine-name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--white);
  line-height: 1.3;
  flex: 1;
}

.wine-vintage {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gold);
  white-space: nowrap;
}

.wine-badges {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.badge {
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-rouge { background: rgba(114, 47, 55, 0.6); color: #F4A0A8; }
.badge-blanc { background: rgba(201, 168, 76, 0.3); color: var(--gold-light); }
.badge-rose { background: rgba(255, 152, 0, 0.3); color: #FFB74D; }
.badge-orange { background: rgba(255, 120, 0, 0.3); color: #FFAB76; }
.badge-bottles {
  background: rgba(76, 175, 80, 0.2);
  color: var(--green);
}

.wine-origin {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.wine-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.wine-price {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gold);
}

.wine-maturity {
  font-size: 0.8rem;
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
}

.maturity-not-ready {
  background: rgba(33, 150, 243, 0.2);
  color: #64B5F6;
}

.maturity-ready {
  background: rgba(76, 175, 80, 0.2);
  color: #81C784;
}

.maturity-peak {
  background: rgba(76, 175, 80, 0.4);
  color: #A5D6A7;
  font-weight: 700;
}

.maturity-past {
  background: rgba(229, 57, 53, 0.2);
  color: #EF9A9A;
}

.wine-format {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.empty-state {
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
  grid-column: 1 / -1;
}

/* View toggle */
.view-toggle {
  display: flex;
  gap: 2px;
  margin-left: auto;
}

.view-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 0.5rem 0.7rem;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}

.view-btn:first-child { border-radius: 8px 0 0 8px; }
.view-btn:last-child { border-radius: 0 8px 8px 0; }

.view-btn.active {
  background: var(--wine-red);
  color: var(--white);
  border-color: var(--wine-red);
}

/* Card grid wrapper */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 1rem;
}

/* Table styles */
.table-wrapper {
  overflow-x: auto;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.wine-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.wine-table thead {
  background: var(--bg-card);
  position: sticky;
  top: 0;
  z-index: 10;
}

.wine-table th {
  padding: 0.75rem 0.75rem;
  text-align: left;
  font-weight: 600;
  color: var(--gold-light);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.th-sortable {
  cursor: pointer;
  user-select: none;
}

.th-sortable:hover {
  color: var(--gold);
}

.th-center { text-align: center; }
.th-right { text-align: right; }

.sort-arrows {
  display: inline-flex;
  flex-direction: column;
  vertical-align: middle;
  margin-right: 0.3rem;
  line-height: 0;
  gap: 1px;
}

.arrow-up, .arrow-down {
  font-size: 0.5rem;
  color: var(--text-muted);
  opacity: 0.3;
  line-height: 1;
}

.arrow-up.active, .arrow-down.active {
  color: var(--gold);
  opacity: 1;
}

.wine-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

.wine-table tbody tr:hover {
  background: var(--bg-card-hover);
}

.wine-table td {
  padding: 0.6rem 0.75rem;
  vertical-align: middle;
}

.td-name {
  font-weight: 500;
  color: var(--white);
  max-width: 300px;
}

.td-center { text-align: center; }
.td-right { text-align: right; }
.td-price { color: var(--gold); font-weight: 600; }

.td-regions {
  color: var(--text-muted);
  font-size: 0.8rem;
}

.format-tag {
  font-size: 0.7rem;
  color: var(--text-muted);
  background: rgba(255,255,255,0.05);
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
}

.badge-sm {
  padding: 0.15rem 0.5rem;
  border-radius: 20px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.maturity-pill {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-size: 0.75rem;
  white-space: nowrap;
}

.maturity-years {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Login screen */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.5rem;
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.login-box h2 {
  color: var(--gold-light);
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
}

.login-box input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  outline: none;
  margin-bottom: 1rem;
}

.login-box input:focus {
  border-color: var(--gold);
}

.login-box button {
  width: 100%;
  padding: 0.75rem;
  border: none;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--wine-dark), var(--wine-red));
  color: var(--gold-light);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.login-box button:hover {
  opacity: 0.9;
}

.login-error {
  color: var(--red-badge);
  font-size: 0.85rem;
  margin-top: 0.75rem;
  min-height: 1.2em;
}

/* API section */
.api-section {
  margin-top: 2rem;
  padding: 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.api-section h3 {
  color: var(--gold-light);
  font-size: 1rem;
  margin-bottom: 1rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.api-url-box {
  margin-bottom: 0.75rem;
}

.api-url-box label {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: block;
  margin-bottom: 0.3rem;
}

.api-url-row {
  display: flex;
  gap: 0.5rem;
}

.api-url-row input {
  flex: 1;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  font-family: monospace;
  outline: none;
}

.api-url-row button, .rotate-btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.api-url-row button:hover, .rotate-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.rotate-btn {
  margin-top: 0.5rem;
  border-color: var(--wine-light);
  color: var(--wine-light);
}

/* Chat panel */
.chat-toggle {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--wine-dark), var(--wine-red));
  color: var(--gold-light);
  font-size: 1.5rem;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 900;
  transition: transform 0.2s, box-shadow 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-toggle:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

.chat-toggle-icon {
  line-height: 1;
}

.chat-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 400px;
  height: 100vh;
  background: var(--bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 950;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}

.chat-panel.open {
  transform: translateX(0);
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  background: linear-gradient(135deg, var(--wine-dark), var(--wine-red));
  border-bottom: 2px solid var(--gold);
  flex-shrink: 0;
}

.chat-header h3 {
  color: var(--gold-light);
  font-size: 1.1rem;
  margin: 0;
}

.chat-close {
  background: none;
  border: none;
  color: var(--gold-light);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.25rem;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.chat-close:hover {
  opacity: 1;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.chat-bubble {
  padding: 0.75rem 1rem;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.5;
  max-width: 90%;
  word-wrap: break-word;
}

.chat-bubble-user {
  background: var(--wine-red);
  color: var(--white);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.chat-bubble-ai {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.chat-bubble-ai strong {
  color: var(--gold);
}

.chat-typing {
  color: var(--text-muted);
  font-style: italic;
}

.chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--bg-card);
  flex-shrink: 0;
}

.chat-form input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}

.chat-form input:focus {
  border-color: var(--gold);
}

.chat-send {
  background: linear-gradient(135deg, var(--wine-dark), var(--wine-red));
  border: none;
  color: var(--gold-light);
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s;
  flex-shrink: 0;
}

.chat-send:hover {
  opacity: 0.85;
}

.chat-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  header {
    padding: 1rem;
  }

  .header-content {
    flex-direction: column;
    align-items: flex-start;
  }

  h1 {
    font-size: 1.4rem;
  }

  main {
    padding: 1rem;
  }

  .card-grid {
    grid-template-columns: 1fr;
  }

  .wine-table { font-size: 0.8rem; }
  .wine-table th, .wine-table td { padding: 0.5rem; }

  .filters select {
    min-width: 0;
    flex: 1;
  }

  .chat-panel {
    width: 100%;
  }

  .chat-toggle {
    bottom: 1rem;
    right: 1rem;
    width: 48px;
    height: 48px;
    font-size: 1.3rem;
  }
}
