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

:root {
  --wine: #722F37;
  --wine-dark: #4A1C22;
  --gold: #C9A84C;
  --gold-light: #E8D59E;
  --cream: #FAF6EE;
  --ink: #1A1A1A;
  --stone: #8B8578;
}

body {
  font-family: 'DM Sans', sans-serif;
  background: var(--cream);
  color: var(--ink);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Header ---- */
header {
  background: var(--wine-dark);
  color: var(--cream);
  padding: 20px 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 3px solid var(--gold);
  position: relative;
  z-index: 1000;
}

header::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--gold-light);
  opacity: 0.4;
}

.title-group h1 {
  font-family: 'Playfair Display', serif;
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.title-group h1 span {
  color: var(--gold);
}

.title-group p {
  font-size: 0.78rem;
  color: var(--gold-light);
  margin-top: 2px;
  font-weight: 300;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ---- Legend ---- */
.legend {
  display: flex;
  gap: 20px;
  font-size: 0.75rem;
  align-items: center;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(255,255,255,0.15);
}

.swatch-hex { background: rgba(114, 47, 55, 0.45); border: 2px solid var(--gold); }
.swatch-village { background: var(--gold); border-radius: 50%; width: 10px; height: 10px; }
.swatch-vertex { background: #fff; border: 2px solid var(--wine); border-radius: 50%; width: 10px; height: 10px; }

/* ---- Main Content (chat sidebar + map) ---- */
.main-content {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* ---- Map ---- */
#map {
  flex: 1;
  z-index: 1;
}

#map.add-vertex-cursor {
  cursor: crosshair !important;
}

/* ---- Sidebar ---- */
.sidebar {
  position: absolute;
  top: 90px;
  right: 16px;
  z-index: 999;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(12px);
  border-radius: 10px;
  padding: 20px;
  width: 320px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12), 0 1px 3px rgba(0,0,0,0.08);
  border: 1px solid rgba(201, 168, 76, 0.25);
  font-size: 0.82rem;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

.sidebar h2 {
  font-family: 'Playfair Display', serif;
  font-size: 1rem;
  color: var(--wine-dark);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1.5px solid var(--gold-light);
}

/* ---- Coordinate Table ---- */
.coord-table {
  width: 100%;
  border-collapse: collapse;
}

.coord-table th {
  text-align: left;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--stone);
  padding: 4px 6px 6px;
  border-bottom: 1px solid #e5e0d5;
}

.coord-table td {
  padding: 5px 6px;
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
  border-bottom: 1px solid #f0ece4;
}

.coord-table tr:hover td {
  background: rgba(201, 168, 76, 0.08);
}

.btn-remove {
  background: none;
  border: none;
  color: var(--stone);
  font-size: 1rem;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  border-radius: 3px;
  opacity: 0.4;
  transition: opacity 0.15s, color 0.15s;
}
.btn-remove:hover { opacity: 1; color: #c0392b; }

.popup-remove-btn {
  display: block;
  margin-top: 6px;
  padding: 4px 10px;
  background: #c0392b;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  width: 100%;
}
.popup-remove-btn:hover { background: #96281b; }

/* ---- Info Rows ---- */
.info-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid #f0ece4;
}

.info-row:last-child { border: none; }

.info-label {
  color: var(--stone);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.info-value {
  font-weight: 500;
  color: var(--wine-dark);
}

/* ---- Notes ---- */
.note {
  margin-top: 14px;
  padding: 10px 12px;
  background: rgba(114, 47, 55, 0.06);
  border-left: 3px solid var(--wine);
  border-radius: 0 6px 6px 0;
  font-size: 0.72rem;
  line-height: 1.5;
  color: var(--stone);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: none;
  border-radius: 6px;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-primary {
  background: var(--wine);
  color: #fff;
}

.btn-primary:hover {
  background: var(--wine-dark);
}

.btn-secondary {
  background: var(--gold);
  color: var(--wine-dark);
}

.btn-secondary:hover {
  background: var(--gold-light);
}

.btn-danger {
  background: #c0392b;
  color: #fff;
}

.btn-danger:hover {
  background: #96281b;
}

.btn-active {
  background: #27ae60;
  color: #fff;
  animation: pulse-glow 1.5s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(39, 174, 96, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(39, 174, 96, 0); }
}

.btn-row {
  display: flex;
  gap: 8px;
  margin: 12px 0;
  flex-wrap: wrap;
}

.btn .icon {
  font-size: 1rem;
}

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--wine-dark);
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.82rem;
  font-family: 'DM Sans', sans-serif;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  z-index: 10000;
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
  pointer-events: none;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ---- Import Area ---- */
.import-area {
  width: 100%;
  min-height: 80px;
  border: 1.5px solid var(--gold-light);
  border-radius: 6px;
  padding: 8px 10px;
  font-family: 'DM Sans', monospace;
  font-size: 0.72rem;
  resize: vertical;
  background: #fff;
  color: var(--ink);
  line-height: 1.5;
}

.import-area:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(201, 168, 76, 0.2);
}

.section-gap {
  margin-top: 16px;
}

/* ---- Chat Sidebar (left) ---- */
.chat-sidebar {
  width: 360px;
  min-width: 360px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
  border-right: 1px solid rgba(201, 168, 76, 0.3);
  display: flex;
  flex-direction: column;
  z-index: 10;
  transition: margin-left 0.3s ease, opacity 0.3s ease;
}

.chat-sidebar.collapsed {
  margin-left: -360px;
  opacity: 0;
  pointer-events: none;
}

.chat-header {
  padding: 14px 16px;
  background: var(--wine-dark);
  color: var(--cream);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 2px solid var(--gold);
  flex-shrink: 0;
}

.chat-header-title {
  font-family: 'Playfair Display', serif;
  font-size: 0.95rem;
  font-weight: 700;
}

.chat-header-title span {
  color: var(--gold);
}

.chat-collapse {
  background: none;
  border: none;
  color: var(--gold-light);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 4px;
  line-height: 1;
}

.chat-collapse:hover {
  background: rgba(255,255,255,0.1);
}

/* Expand tab (visible when sidebar is collapsed) */
.chat-expand-tab {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 11;
  background: var(--wine-dark);
  color: var(--gold);
  border: 2px solid var(--gold);
  border-left: none;
  border-radius: 0 8px 8px 0;
  padding: 14px 6px;
  cursor: pointer;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-family: 'Playfair Display', serif;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  transition: opacity 0.3s ease, transform 0.3s ease;
  opacity: 0;
  pointer-events: none;
}

.chat-expand-tab.visible {
  opacity: 1;
  pointer-events: auto;
}

.chat-expand-tab:hover {
  background: var(--wine);
  padding-right: 10px;
}

.chat-expand-label {
  display: inline;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.chat-msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.82rem;
  line-height: 1.55;
  word-wrap: break-word;
}

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

.chat-msg.assistant {
  align-self: flex-start;
  background: #f0ece4;
  color: var(--ink);
  border-bottom-left-radius: 4px;
}

.chat-msg.system {
  align-self: center;
  background: rgba(201, 168, 76, 0.15);
  color: var(--stone);
  font-size: 0.72rem;
  padding: 6px 12px;
  border-radius: 8px;
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
}

.typing-logo {
  animation: pulse-logo 1.4s ease-in-out infinite;
  opacity: 0.7;
}

.typing-status {
  font-size: 0.72rem;
  color: var(--stone);
  font-style: italic;
}

@keyframes pulse-logo {
  0%, 100% { opacity: 0.3; transform: scale(0.95); }
  50% { opacity: 1; transform: scale(1.05); }
}

.chat-apply-btn {
  display: block;
  margin-top: 8px;
  padding: 6px 12px;
  background: var(--wine);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}
.chat-apply-btn:hover { background: var(--wine-dark); }
.chat-apply-btn:disabled { opacity: 0.6; cursor: default; background: var(--stone); }

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid #e5e0d5;
  background: #faf8f4;
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  padding: 8px 12px;
  border: 1.5px solid var(--gold-light);
  border-radius: 8px;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.82rem;
  background: #fff;
  color: var(--ink);
}

.chat-input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(201, 168, 76, 0.15);
}

.chat-send {
  padding: 8px 14px;
  background: var(--wine);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-family: 'DM Sans', sans-serif;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.chat-send:hover {
  background: var(--wine-dark);
}

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

/* ---- Leaflet Popup Overrides ---- */
.leaflet-popup-content-wrapper {
  border-radius: 8px;
  font-family: 'DM Sans', sans-serif;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}

.leaflet-popup-content {
  margin: 10px 14px;
  font-size: 0.82rem;
  line-height: 1.5;
}

.popup-label {
  font-family: 'Playfair Display', serif;
  font-weight: 700;
  color: var(--wine-dark);
  font-size: 0.9rem;
}

.popup-coord {
  color: var(--stone);
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
}

/* ---- Responsive ---- */
@media (max-width: 640px) {
  .sidebar { display: none; }
  header { padding: 14px 16px; }
  .title-group h1 { font-size: 1.2rem; }
  .legend { gap: 12px; font-size: 0.68rem; }
  .chat-sidebar { width: 280px; min-width: 280px; }
  .chat-sidebar.collapsed { margin-left: -280px; }
}
