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

:root {
  --green-dark: #1b5e20;
  --green-mid: #2e7d32;
  --green-light: #4caf50;
  --green-pale: #e8f5e9;
  --green-user: #1b8a2a;
  --bg: #f5f5f0;
  --white: #ffffff;
  --text: #212121;
  --text-light: #616161;
  --text-muted: #9e9e9e;
  --border: #e0e0e0;
  --shadow: rgba(0, 0, 0, 0.08);
  --font-latin: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-arabic: 'Noto Naskh Arabic', 'Traditional Arabic', 'Scheherazade New', serif;
  --radius: 16px;
  --radius-sm: 10px;
}

html, body {
  height: 100%;
  font-family: var(--font-latin);
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 720px;
  margin: 0 auto;
  background: var(--white);
  box-shadow: 0 0 40px var(--shadow);
}

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--green-dark);
  color: var(--white);
  flex-shrink: 0;
  z-index: 10;
}

.header-title h1 {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.2;
}

.header-title .subtitle {
  font-size: 11px;
  opacity: 0.75;
  letter-spacing: 0.5px;
}

.header-controls {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-end;
}

.language-selector {
  display: flex;
  gap: 4px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 3px;
}

.lang-btn {
  padding: 5px 10px;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  font-family: var(--font-latin);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.lang-btn[data-lang="urdu"] {
  font-family: var(--font-arabic);
  font-size: 13px;
}

.lang-btn.active {
  background: rgba(255, 255, 255, 0.25);
  color: var(--white);
  font-weight: 600;
}

/* ===== Chat Area ===== */
#chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px 10px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

#messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100%;
  justify-content: flex-end;
}

/* Welcome */
.welcome-message {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-light);
  font-size: 14px;
  line-height: 1.6;
}

.welcome-icon {
  font-size: 40px;
  color: var(--green-light);
  margin-bottom: 12px;
}

/* ===== Message Bubbles ===== */
.message {
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.25s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  align-self: flex-end;
  max-width: 88%;
}

.message.bot {
  align-self: stretch;
  max-width: 100%;
}

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .bubble {
  background: var(--green-user);
  color: var(--white);
  border-bottom-right-radius: 4px;
  font-size: 14px;
}

.message.bot .bubble {
  background: var(--green-pale);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* ===== Masala Card ===== */
.masala-card {
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-top: 8px;
  overflow: hidden;
  animation: cardFadeIn 0.28s ease both;
}

@keyframes cardFadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Re-trigger a brief crossfade on the content area when partial→done
   swaps the snippet for the canonical highlight. */
.message.bot.content-fade .masala-card {
  animation: cardCrossfade 0.32s ease both;
}

@keyframes cardCrossfade {
  from { opacity: 0.5; }
  to   { opacity: 1; }
}

.masala-card + .masala-card {
  margin-top: 10px;
}

.masala-citation {
  background: var(--green-dark);
  color: var(--white);
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.3px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.copy-citation-btn {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  transition: color 0.15s;
}

.copy-citation-btn:hover {
  color: var(--white);
}

.masala-heading {
  padding: 8px 12px 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--green-mid);
}

.masala-body {
  padding: 8px 12px 12px;
}

.masala-text-urdu {
  direction: rtl;
  text-align: right;
  font-family: var(--font-arabic);
  font-size: 16px;
  line-height: 2;
  color: var(--text);
  margin-bottom: 10px;
  white-space: pre-wrap;
}

.masala-text-english {
  direction: ltr;
  text-align: left;
  font-family: var(--font-latin);
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text);
  white-space: pre-wrap;
}

.masala-text-urdu + .masala-text-english {
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.ruling-badge {
  display: inline-block;
  padding: 2px 8px;
  margin: 8px 12px 10px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 4px;
  background: #fff3e0;
  color: #e65100;
  text-transform: capitalize;
}

.masala-snippet {
  direction: rtl;
  text-align: right;
  font-family: var(--font-arabic);
  font-size: 16px;
  line-height: 2;
  color: var(--text);
  white-space: pre-wrap;
  background: #f1f8e9;
  border-right: 3px solid var(--green-mid);
  padding: 10px 14px;
  border-radius: 4px;
  margin-bottom: 8px;
  transition: background-position 0.3s ease;
}

/* While we wait for Claude to finish picking highlights, the snippet is
   sourced from the start of the body and we mark it loading. Subtle
   shimmer hints to the user that the canonical highlight is on its way. */
.masala-snippet.is-loading {
  background: linear-gradient(
    100deg,
    #f1f8e9 0%,
    #f1f8e9 40%,
    #e6f3d6 50%,
    #f1f8e9 60%,
    #f1f8e9 100%
  );
  background-size: 200% 100%;
  animation: snippet-shimmer 1.6s linear infinite;
}

@keyframes snippet-shimmer {
  from { background-position: 200% 0; }
  to   { background-position: -200% 0; }
}

.masala-expand-btn {
  display: inline-block;
  background: none;
  border: none;
  color: var(--green-mid);
  font-family: var(--font-arabic);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 0;
  direction: rtl;
}

.masala-expand-btn:hover {
  text-decoration: underline;
}

.masala-full-text {
  display: none;
}

.masala-full-text.expanded {
  display: block;
}

.snippet-highlight {
  background: #f1f8e9;
  border-radius: 2px;
  padding: 2px 0;
}

/* Sub-masalas (fatwa ranges) */
.sub-masail-list {
  direction: rtl;
}

.sub-masala {
  border-bottom: 1px solid var(--border);
}

.sub-masala:last-child {
  border-bottom: none;
}

.sub-masala-header {
  width: 100%;
  text-align: right;
  background: none;
  border: none;
  padding: 10px 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  direction: rtl;
  font-family: var(--font-arabic);
  font-size: 14px;
  color: var(--text);
}

.sub-masala-header:hover {
  background: var(--green-pale);
}

.sub-masala-number {
  font-weight: 700;
  color: var(--green-dark);
  flex-shrink: 0;
  font-size: 13px;
}

.sub-masala-title {
  flex: 1;
  text-align: right;
}

.toggle-icon {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--text-muted);
}

.sub-masala-content {
  display: none;
  padding: 0 12px 12px;
}

.sub-masala-content.expanded {
  display: block;
}

.no-results {
  padding: 12px;
  font-size: 13.5px;
  color: var(--text-light);
  font-style: italic;
}

/* Source citations */
.source-citations {
  padding: 6px 12px 10px;
  border-top: 1px solid var(--border);
}

.source-citations summary {
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  font-weight: 500;
}

.source-citations ul {
  margin-top: 4px;
  padding-left: 16px;
}

.source-citations li {
  font-size: 11px;
  color: var(--text-light);
  line-height: 1.5;
}

/* ===== Loading ===== */
.loading-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
}

.loading-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green-light);
  animation: pulse 1.2s infinite ease-in-out;
}

.loading-dot:nth-child(2) { animation-delay: 0.2s; }
.loading-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* ===== Error ===== */
.error-bubble {
  background: #ffebee;
  color: #c62828;
  padding: 10px 14px;
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  font-size: 13px;
  line-height: 1.5;
}

/* ===== Source Picker ===== */
.source-picker {
  position: relative;
  flex-shrink: 0;
}

#source-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: none;
  background: transparent;
  color: var(--text-light);
  font-family: var(--font-latin);
  font-size: 12px;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

#source-toggle:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--text);
}

#source-toggle svg {
  flex-shrink: 0;
  opacity: 0.6;
}

.source-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  min-width: 240px;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.06);
  z-index: 100;
  overflow: hidden;
  animation: menuIn 0.15s ease;
}

.source-menu.hidden {
  display: none;
}

@keyframes menuIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.source-menu-header {
  padding: 10px 14px 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.source-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.12s;
}

.source-option:hover {
  background: var(--green-pale);
}

.source-option input[type="checkbox"] {
  display: none;
}

.source-option-check {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, background 0.15s;
}

.source-option-check::after {
  content: '';
  width: 10px;
  height: 6px;
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--white);
  transform: rotate(-45deg) scale(0);
  transition: transform 0.15s;
  margin-top: -2px;
}

.source-option input[type="checkbox"]:checked ~ .source-option-check {
  background: var(--green-mid);
  border-color: var(--green-mid);
}

.source-option input[type="checkbox"]:checked ~ .source-option-check::after {
  transform: rotate(-45deg) scale(1);
}

.source-option-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.source-option-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.source-option-desc {
  font-size: 11px;
  color: var(--text-muted);
}

/* ===== Input Area ===== */
#input-area {
  flex-shrink: 0;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--white);
  border-top: 1px solid var(--border);
  z-index: 10;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--bg);
  border-radius: 24px;
  padding: 6px 6px 6px 4px;
  border: 1px solid var(--border);
  transition: border-color 0.2s;
}

.input-container:focus-within {
  border-color: var(--green-light);
}

#question-input {
  flex: 1;
  border: none;
  background: transparent;
  font-family: var(--font-latin);
  font-size: 15px;
  line-height: 1.4;
  resize: none;
  outline: none;
  max-height: 120px;
  padding: 6px 0;
  color: var(--text);
}

#question-input::placeholder {
  color: var(--text-muted);
}

#send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--green-mid);
  color: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s;
}

#send-btn:hover {
  background: var(--green-dark);
}

#send-btn:active {
  transform: scale(0.93);
}

#send-btn:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
}

/* ===== Desktop ===== */
@media (min-width: 720px) {
  #app {
    height: 100vh;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .message {
    max-width: 75%;
  }

  .masala-text-urdu {
    font-size: 17px;
  }

  .masala-text-english {
    font-size: 14px;
  }
}
