:root {
  --bg: #faf9f6;
  --text: #1a1a1a;
  --muted: #666;
  --accent: #2c5f8a;
  --border: #ddd;
  --card-bg: #fff;
  --radius: 6px;
}

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

body {
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.6;
}

/* RTL default */
html { direction: rtl; }
html[lang="en"] { direction: ltr; }

nav {
  background: var(--accent);
  color: #fff;
  padding: 0.75rem 1.5rem;
  display: flex;
  gap: 1.5rem;
  align-items: center;
}
nav a { color: #fff; text-decoration: none; font-weight: 500; }
nav .brand { font-size: 1.2rem; font-weight: 700; margin-inline-end: auto; }

.container { max-width: 1200px; margin: 0 auto; padding: 1.5rem; }

/* Card grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  transition: box-shadow 0.15s;
  text-decoration: none;
  color: inherit;
  display: block;
}
.card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
.card-thumb { width: 100%; height: 140px; object-fit: cover; border-radius: 4px; margin-bottom: 0.75rem; background: #eee; }
.card-thumb--placeholder {
  height: 140px;
  border-radius: 4px;
  margin-bottom: 0.75rem;
  background: linear-gradient(135deg, #dce8f5 0%, #c8dced 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.8rem;
}
.card-thumb--placeholder::before { content: attr(data-icon); }

.detail-thumb-placeholder {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 6px;
  margin-bottom: 1rem;
  background: linear-gradient(135deg, #dce8f5 0%, #c8dced 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 4rem;
}
.detail-thumb-placeholder::before { content: attr(data-icon); }
.card-title { font-weight: 600; margin-bottom: 0.25rem; }
.card-meta { font-size: 0.82rem; color: var(--muted); }
.badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  background: #e8f0f8;
  color: var(--accent);
  margin-inline-end: 0.25rem;
}

/* ── Timeline ───────────────────────────────────────────────────────────── */
.timeline {
  margin-bottom: 1.5rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem 0.5rem;
}
.tl-bars {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 64px;
  overflow: visible;
  padding-top: 28px; /* room for tooltip above tallest bar */
}
.tl-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  flex: 1 1 0;
  min-width: 28px;
  max-width: 52px;
  height: 100%;
  cursor: pointer;
  border-radius: 3px 3px 0 0;
  position: relative;
}
.tl-bar:hover .tl-bar-fill { background: var(--accent); }
.tl-bar--active .tl-bar-fill { background: var(--accent); }
.tl-bar-fill {
  width: 100%;
  background: #93b8d8;
  border-radius: 3px 3px 0 0;
  transition: background 0.15s, height 0.2s;
  min-height: 4px;
}
.tl-bar-label {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 3px;
  white-space: nowrap;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  line-height: 1;
  padding-bottom: 2px;
}
.tl-bar--active .tl-bar-label { color: var(--accent); font-weight: 700; }
/* count tooltip on hover */
.tl-bar-count {
  position: absolute;
  bottom: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  font-size: 0.68rem;
  padding: 1px 5px;
  border-radius: 3px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
}
.tl-bar:hover .tl-bar-count { opacity: 1; }

.tl-months {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}
.tl-month {
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: transparent;
  font-size: 0.8rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  color: var(--text);
}
.tl-month small { color: var(--muted); font-size: 0.72rem; }
.tl-month:hover { border-color: var(--accent); color: var(--accent); }
.tl-month--active { background: var(--accent); color: #fff; border-color: var(--accent); }
.tl-month--active small { color: #cde; }

/* Type icon badge */
.card-thumb-wrap {
  position: relative;
  margin-bottom: 0.75rem;
  min-height: 2rem; /* room for badge even without a thumbnail */
}
.type-badge {
  position: absolute;
  top: 0.4rem;
  inset-inline-end: 0.4rem;
  font-size: 1.15rem;
  line-height: 1;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border-radius: 4px;
  padding: 0.2rem 0.35rem;
  cursor: default;
  user-select: none;
  /* tooltip */
  --tip-offset: calc(100% + 6px);
}
/* no thumbnail: use accent background instead */
.card-thumb-wrap:not(:has(img)) .type-badge {
  position: static;
  display: inline-flex;
  background: var(--accent);
  font-size: 1rem;
  margin-bottom: 0.5rem;
}
.type-badge::after {
  content: attr(data-label);
  position: absolute;
  bottom: var(--tip-offset);
  left: 50%;
  transform: translateX(-50%);
  background: #222;
  color: #fff;
  font-size: 0.72rem;
  font-family: inherit;
  padding: 0.2rem 0.55rem;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}
.type-badge:hover::after { opacity: 1; }

/* Large icon variant for detail page h1 */
.type-badge--lg {
  position: static;
  font-size: 1.4rem;
  background: var(--accent);
  --tip-offset: calc(100% + 6px);
  flex-shrink: 0;
}

/* Sidebar layout */
.layout { display: grid; grid-template-columns: 220px 1fr; gap: 1.5rem; }
.sidebar { border-inline-end: 1px solid var(--border); padding-inline-end: 1rem; }
.sidebar h3 { font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 0.5rem; }

/* Chat */
.chat-wrap { display: flex; flex-direction: column; height: calc(100vh - 80px); }
.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem; }
.msg { max-width: 75%; padding: 0.75rem 1rem; border-radius: var(--radius); }
.msg.user { background: var(--accent); color: #fff; align-self: flex-end; }
.msg.assistant { background: var(--card-bg); border: 1px solid var(--border); align-self: flex-start; }
.chat-input { display: flex; gap: 0.5rem; padding: 1rem; border-top: 1px solid var(--border); }
.chat-input input { flex: 1; padding: 0.6rem 1rem; border: 1px solid var(--border); border-radius: var(--radius); font-size: 1rem; }
.chat-input button { padding: 0.6rem 1.25rem; background: var(--accent); color: #fff; border: none; border-radius: var(--radius); cursor: pointer; }

/* ── Homepage ────────────────────────────────────────────────────────────── */
.home-layout {
  display: grid;
  grid-template-columns: 1fr 40%;
  grid-template-areas: "main chat";
  gap: 0;
  height: calc(100vh - 56px);
  overflow: hidden;
}
.home-main {
  grid-area: main;
  overflow-y: auto;
  padding: 1.5rem 2rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  border-inline-start: 1px solid var(--border);
}
.home-chat-sidebar {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 56px);
  padding: 1rem;
  background: var(--card-bg);
  border-inline-start: 1px solid var(--border);
  overflow: hidden;
}
.home-chat-sidebar .messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--bg);
  min-height: 0;
}
.home-chat-sidebar .chat-input {
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  background: var(--card-bg);
  flex-shrink: 0;
}
.home-desc {
  font-size: 1rem;
  margin-bottom: 0.25rem;
}
.home-stat {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.75rem;
}
.how-to {
  list-style: none;
  margin-top: 0.75rem;
  font-size: 0.9rem;
}
.how-to li { padding: 0.2rem 0; }
.home-section-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.75rem;
}
/* Latest items */
.home-latest-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.home-latest-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: inherit;
  transition: box-shadow 0.15s;
}
.home-latest-item:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.home-latest-item img {
  width: 48px;
  height: 36px;
  object-fit: cover;
  border-radius: 3px;
  flex-shrink: 0;
}
.home-latest-icon {
  width: 48px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  background: linear-gradient(135deg, #dce8f5 0%, #c8dced 100%);
  border-radius: 3px;
  flex-shrink: 0;
}
.home-latest-title {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.home-latest-date {
  font-size: 0.78rem;
  color: var(--muted);
  flex-shrink: 0;
}
@media (max-width: 680px) {
  .home-layout {
    grid-template-columns: 1fr;
    grid-template-areas: "main" "chat";
    height: auto;
  }
  .home-main { padding: 1rem; }
  .home-chat-sidebar {
    border-inline-start: none;
    border-top: 1px solid var(--border);
    height: 60vh;
  }
}

/* Map */
#map { height: calc(100vh - 80px); width: 100%; }

/* Utilities */
.search-bar { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; }
.search-bar input { flex: 1; padding: 0.6rem 1rem; border: 1px solid var(--border); border-radius: var(--radius); font-size: 1rem; }
.search-bar button { padding: 0.6rem 1.25rem; background: var(--accent); color: #fff; border: none; border-radius: var(--radius); cursor: pointer; }

/* ── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  /* Timeline: horizontal scroll, count inline under year */
  .timeline { padding: 0.6rem 0.75rem 0.5rem; }
  .tl-bars {
    overflow-x: auto;
    overflow-y: hidden;
    padding-top: 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--accent) #e8f0f8;
    height: 72px;
    padding-bottom: 6px; /* room for scrollbar */
  }
  .tl-bars::-webkit-scrollbar { height: 4px; }
  .tl-bars::-webkit-scrollbar-track { background: #e8f0f8; border-radius: 2px; }
  .tl-bars::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 2px; }

  /* Fade gradient on left edge to hint there's more content */
  .timeline { position: relative; }
  .timeline::after {
    content: '';
    position: absolute;
    top: 0; left: 0;
    width: 24px;
    height: calc(100% - 2.5rem); /* above month row */
    background: linear-gradient(to right, var(--card-bg), transparent);
    pointer-events: none;
  }
  .tl-bar {
    flex: 0 0 auto;        /* don't shrink — scroll instead */
    min-width: 38px;
    max-width: 38px;
  }
  /* Replace floating tooltip with inline count below year */
  .tl-bar-count {
    position: static;
    transform: none;
    bottom: auto;
    left: auto;
    background: none;
    color: var(--muted);
    font-size: 0.58rem;
    padding: 0;
    border-radius: 0;
    opacity: 1;            /* always visible */
    writing-mode: horizontal-tb;
    margin-top: 1px;
    line-height: 1;
  }
  .tl-bar-label {
    writing-mode: horizontal-tb;
    transform: none;
    font-size: 0.6rem;
    padding-bottom: 0;
  }
  .tl-months { gap: 0.3rem; }
  .tl-month  { font-size: 0.75rem; padding: 0.2rem 0.4rem; }
}

@media (max-width: 680px) {
  /* Nav */
  nav {
    padding: 0.6rem 1rem;
    gap: 0.75rem 1.25rem;
    flex-wrap: wrap;
  }
  nav .brand {
    font-size: 1rem;
    margin-inline-end: auto;
  }

  /* Container */
  .container { padding: 1rem; }

  /* Sidebar layout: stack vertically */
  .layout {
    grid-template-columns: 1fr;
  }
  .sidebar {
    border-inline-end: none;
    border-bottom: 1px solid var(--border);
    padding-inline-end: 0;
    padding-bottom: 1rem;
  }

  /* On detail page: push related articles below the article */
  .layout > article { order: -1; }

  /* Card grid: single column */
  .grid {
    grid-template-columns: 1fr;
  }
  .card-thumb { height: 180px; }

  /* Bigger touch targets */
  .search-bar button,
  .chat-input button {
    min-height: 44px;
    padding: 0.6rem 1rem;
  }
  .search-bar input,
  .chat-input input {
    min-height: 44px;
  }

  /* Map: full viewport height minus nav */
  #map { height: calc(100vh - 92px); }
  #map-status { font-size: 0.8rem; }
}
