:root {
  --bg: #f4f2ec;
  --paper: #fffdf8;
  --ink: #17201d;
  --muted: #6c746f;
  --line: #dddcd4;
  --green: #16775d;
  --green-soft: #dcece6;
  --blue: #285f83;
  --blue-soft: #e0ebf2;
  --violet: #66548e;
  --violet-soft: #ece7f5;
  --coral: #b45042;
  --coral-soft: #f4e1dd;
  --amber: #9a6a19;
  --amber-soft: #f4ead5;
  --shadow: 0 22px 60px rgba(37, 47, 42, .09);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: Tahoma, Arial, sans-serif;
  background:
    radial-gradient(circle at 4% 0%, rgba(102, 84, 142, .13), transparent 30rem),
    radial-gradient(circle at 92% 8%, rgba(22, 119, 93, .14), transparent 34rem),
    var(--bg);
}

button, input, select { font: inherit; }
a { color: inherit; }

.app-shell {
  width: min(1560px, calc(100% - 36px));
  margin: 0 auto;
  padding: 26px 0 60px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 24px;
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand h1 { margin: 0; font-size: 1.65rem; letter-spacing: -.04em; }
.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, #17201d, #416b5f);
  font: 800 .9rem Georgia, serif;
  letter-spacing: .08em;
  box-shadow: 0 10px 30px rgba(23, 32, 29, .18);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.top-actions { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.ghost-link, .status-pill, .count-chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 253, 248, .75);
  padding: 9px 14px;
  text-decoration: none;
  color: var(--muted);
  font-size: .82rem;
}
.ghost-link:hover { border-color: var(--ink); color: var(--ink); }
.status-pill.success { color: var(--green); border-color: rgba(22, 119, 93, .35); background: var(--green-soft); }
.status-pill.partial { color: var(--amber); border-color: rgba(154, 106, 25, .35); background: var(--amber-soft); }
.status-pill.failed { color: var(--coral); border-color: rgba(180, 80, 66, .35); background: var(--coral-soft); }

.hero-panel {
  min-height: 250px;
  padding: clamp(26px, 5vw, 62px);
  border-radius: 32px;
  color: #f7faf8;
  background:
    linear-gradient(115deg, rgba(255,255,255,.06), transparent 45%),
    linear-gradient(135deg, #15251f 0%, #24483d 55%, #345d50 100%);
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 40px;
  overflow: hidden;
  position: relative;
}
.hero-panel::after {
  content: "";
  position: absolute;
  width: 330px;
  height: 330px;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 50%;
  left: -80px;
  top: -130px;
  box-shadow: 0 0 0 55px rgba(255,255,255,.025), 0 0 0 110px rgba(255,255,255,.018);
}
.hero-panel > * { position: relative; z-index: 1; }
.hero-panel h2 {
  margin: 0;
  max-width: 760px;
  font-size: clamp(2rem, 5vw, 4.7rem);
  line-height: 1.08;
  letter-spacing: -.05em;
}
.hero-panel .eyebrow { color: #a9c9bd; }
.hero-copy { margin: 18px 0 0; color: #c7d8d2; max-width: 760px; line-height: 1.8; }
.source-card {
  min-width: 215px;
  padding: 20px;
  border-radius: 20px;
  background: rgba(255,255,255,.09);
  border: 1px solid rgba(255,255,255,.16);
  backdrop-filter: blur(8px);
}
.source-card span, .source-card small { display: block; color: #b9cec6; font-size: .75rem; }
.source-card strong { display: block; font: 700 1.45rem Georgia, serif; margin: 7px 0; direction: ltr; text-align: right; }

.controls {
  margin: 18px 0;
  padding: 16px;
  background: rgba(255, 253, 248, .86);
  border: 1px solid var(--line);
  border-radius: 22px;
  display: grid;
  grid-template-columns: minmax(260px, 1.7fr) repeat(2, minmax(150px, .7fr)) minmax(220px, 1fr);
  gap: 12px;
  box-shadow: 0 10px 35px rgba(37,47,42,.04);
}
.controls label { display: grid; gap: 7px; color: var(--muted); font-size: .74rem; font-weight: 700; }
.controls select, .controls input, .country-row select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #fff;
  color: var(--ink);
  padding: 11px 12px;
  outline: none;
}
.controls select:focus, .controls input:focus, .country-row select:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(22,119,93,.1); }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 14px;
  margin: 18px 0;
}
.kpi-card {
  min-height: 148px;
  padding: 20px;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: 0 10px 30px rgba(37,47,42,.045);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.kpi-card span, .kpi-card small { color: var(--muted); font-size: .75rem; }
.kpi-card strong { font: 700 clamp(2rem, 4vw, 3.2rem) Georgia, serif; direction: ltr; text-align: right; }
.kpi-card.violet { background: linear-gradient(145deg, var(--paper), var(--violet-soft)); }
.kpi-card.blue { background: linear-gradient(145deg, var(--paper), var(--blue-soft)); }
.kpi-card.green { background: linear-gradient(145deg, var(--paper), var(--green-soft)); }
.kpi-card.coral { background: linear-gradient(145deg, var(--paper), var(--coral-soft)); }
.kpi-card.amber { background: linear-gradient(145deg, var(--paper), var(--amber-soft)); }

.data-panel {
  border: 1px solid var(--line);
  border-radius: 27px;
  background: rgba(255,253,248,.92);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.panel-heading {
  padding: 22px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}
.panel-heading h3 { margin: 0; font-size: 1.4rem; }
.count-chip { background: var(--violet-soft); color: var(--violet); }
.table-wrap { overflow: auto; max-height: 690px; }
table { width: 100%; min-width: 1280px; border-collapse: collapse; font-size: .82rem; }
th {
  position: sticky;
  top: 0;
  z-index: 2;
  text-align: right;
  padding: 13px 14px;
  color: var(--muted);
  background: #f4f2ec;
  border-bottom: 1px solid var(--line);
  font-size: .7rem;
}
td { padding: 13px 14px; border-bottom: 1px solid #eceae3; vertical-align: middle; }
tbody tr { cursor: pointer; transition: background .15s ease; }
tbody tr:hover { background: #f5f6f0; }
.keyword-cell { font-weight: 700; max-width: 290px; }
.url-cell { max-width: 270px; direction: ltr; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--blue); }
.num { direction: ltr; text-align: right; font-variant-numeric: tabular-nums; }
.empty-cell { text-align: center; color: var(--muted); padding: 50px; }
.tag, .movement {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 5px 9px;
  font-size: .69rem;
  white-space: nowrap;
}
.tag.primary { background: var(--violet-soft); color: var(--violet); }
.tag.secondary { background: var(--blue-soft); color: var(--blue); }
.movement.improving { background: var(--green-soft); color: var(--green); }
.movement.declining { background: var(--coral-soft); color: var(--coral); }
.movement.stable { background: #ecece8; color: #616761; }
.movement.new { background: var(--blue-soft); color: var(--blue); }
.movement.not_observed { background: var(--amber-soft); color: var(--amber); }
.delta-up { color: var(--green); font-weight: 700; }
.delta-down { color: var(--coral); font-weight: 700; }

.detail-backdrop { position: fixed; inset: 0; background: rgba(18,24,21,.35); backdrop-filter: blur(3px); z-index: 20; }
.detail-panel {
  position: fixed;
  z-index: 21;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(680px, 94vw);
  padding: 30px;
  background: var(--paper);
  box-shadow: 30px 0 80px rgba(17,28,22,.2);
  transform: translateX(-105%);
  transition: transform .28s ease;
  overflow: auto;
}
.detail-panel.open { transform: translateX(0); }
.detail-panel h3 { margin: 0; font-size: clamp(1.5rem, 4vw, 2.4rem); }
.close-button { position: absolute; left: 20px; top: 18px; border: 0; background: #eeeae1; width: 38px; height: 38px; border-radius: 50%; cursor: pointer; font-size: 1.5rem; }
.detail-meta { display: flex; gap: 8px; margin: 12px 0 24px; color: var(--muted); font-size: .78rem; }
.detail-meta span { padding: 6px 9px; border-radius: 999px; background: #efede7; }
.chart-wrap { border: 1px solid var(--line); border-radius: 20px; padding: 10px; background: #fff; direction: ltr; }
#historyChart { width: 100%; height: auto; display: block; }
.chart-grid { stroke: #e5e4de; stroke-width: 1; }
.chart-line { fill: none; stroke: var(--green); stroke-width: 3; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { fill: rgba(22,119,93,.08); }
.chart-dot { fill: var(--paper); stroke: var(--green); stroke-width: 2; }
.chart-label { fill: var(--muted); font-size: 11px; }
.history-summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; margin: 14px 0; }
.history-summary div { padding: 13px; border-radius: 14px; background: #efede7; }
.history-summary span { display: block; color: var(--muted); font-size: .7rem; }
.history-summary strong { display: block; margin-top: 5px; direction: ltr; text-align: right; }
.country-row { display: grid; grid-template-columns: 100px 1fr; align-items: center; gap: 10px; margin: 18px 0; }
.history-list { border-top: 1px solid var(--line); }
.history-item { display: grid; grid-template-columns: 110px 90px 1fr; gap: 10px; padding: 12px 2px; border-bottom: 1px solid #eceae3; font-size: .78rem; }
.history-item a { color: var(--blue); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: ltr; }

.toast { position: fixed; z-index: 30; bottom: 24px; right: 24px; max-width: 420px; padding: 13px 17px; border-radius: 14px; background: #17201d; color: #fff; box-shadow: var(--shadow); }
.muted { color: var(--muted); }

.login-page { display: grid; place-items: center; padding: 20px; }
.login-shell { width: min(460px, 100%); }
.login-card { padding: 38px; border: 1px solid var(--line); border-radius: 28px; background: rgba(255,253,248,.93); box-shadow: var(--shadow); }
.login-card .brand-mark { margin-bottom: 22px; }
.login-card h1 { margin: 0; font-size: 3rem; letter-spacing: -.05em; }
.login-card > .muted { line-height: 1.7; margin: 10px 0 24px; }
.login-form { display: grid; gap: 10px; }
.login-form label { color: var(--muted); font-size: .78rem; font-weight: 700; }
.login-form input { border: 1px solid var(--line); border-radius: 13px; padding: 12px; background: #fff; outline: none; margin-bottom: 7px; }
.login-form input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(22,119,93,.1); }
.login-form button { border: 0; border-radius: 13px; padding: 13px; color: #fff; background: #1f493c; cursor: pointer; font-weight: 700; margin-top: 6px; }
.login-error { padding: 11px; border-radius: 11px; background: var(--coral-soft); color: var(--coral); margin-bottom: 16px; font-size: .8rem; }

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .controls { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 720px) {
  .app-shell { width: min(100% - 20px, 1560px); padding-top: 14px; }
  .topbar, .hero-panel { align-items: flex-start; flex-direction: column; }
  .hero-panel { border-radius: 24px; padding: 28px 22px; }
  .source-card { width: 100%; }
  .controls, .kpi-grid { grid-template-columns: 1fr; }
  .kpi-card { min-height: 122px; }
  .top-actions { width: 100%; }
  .data-panel { border-radius: 20px; }
  .detail-panel { padding: 25px 18px; }
  .history-summary { grid-template-columns: 1fr; }
}

