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

:root {
  --bg:         #f8f9fa;
  --bg-2:       #ffffff;
  --bg-3:       #f0f2f6;
  --border:     #e0e3e9;
  --text:       #1a1a2e;
  --text-muted: #6b7280;
  --accent:     #ff4b4b;
  --accent-dim: #ffeaea;
  --green:      #21c55d;
  --green-bg:   #f0fdf4;
  --green-bdr:  #bbf7d0;
  --red:        #ef4444;
  --blue:       #3b82f6;
  --warning-bg: #fffbeb;
  --warning-bdr:#fde68a;
  --warning-tx: #92400e;
  --radius:     6px;
  --sidebar-w:  300px;
  --font:       -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono:  'SF Mono', 'Fira Code', 'Consolas', monospace;
  --shadow-sm:  0 1px 3px rgba(0,0,0,0.08);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

.layout { display: flex; min-height: 100vh; }

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
  box-shadow: var(--shadow-sm);
}
.sidebar-inner { padding: 20px 16px 40px; }

.brand { display: flex; align-items: center; gap: 10px; padding-bottom: 18px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.brand-icon { font-size: 26px; }
.brand-title { font-size: 15px; font-weight: 700; color: var(--text); }
.brand-sub { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.6px; }

.sidebar-section { margin-bottom: 16px; }
.section-label { font-size: 11px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.8px; margin-bottom: 8px; }
.field-label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }

.input {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  padding: 7px 10px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(255,75,75,0.1); }

.toggle-row { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 3px 0; font-size: 13px; color: var(--text); user-select: none; }
.toggle-row input[type="checkbox"] { accent-color: var(--accent); width: 14px; height: 14px; cursor: pointer; }
.toggle-row input[type="radio"] { accent-color: var(--accent); cursor: pointer; }

.country-panel { margin-top: 10px; }
.tier1-toggle { margin-bottom: 8px; }
.country-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 8px; background: var(--bg-3); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.tier-label { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.6px; margin-bottom: 4px; font-weight: 600; }

.scan-mode { display: flex; gap: 16px; margin-bottom: 8px; font-size: 13px; }

.slider { width: 100%; accent-color: var(--accent); cursor: pointer; }
.slider-val { font-size: 11px; color: var(--text-muted); text-align: right; margin-top: 2px; }

.run-btn {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  padding: 11px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.15s, opacity 0.15s;
  box-shadow: 0 2px 6px rgba(255,75,75,0.3);
}
.run-btn:hover { background: #e03c3c; }
.run-btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

.divider { height: 1px; background: var(--border); margin: 14px 0; }

.api-status.ok { font-size: 12px; color: #166534; background: var(--green-bg); border: 1px solid var(--green-bdr); border-radius: var(--radius); padding: 6px 10px; }
.hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; line-height: 1.5; }

/* ── Main ── */
.main { flex: 1; padding: 32px 40px; overflow-y: auto; }

.main-header { margin-bottom: 28px; border-bottom: 1px solid var(--border); padding-bottom: 18px; }
.main-title { font-size: 26px; font-weight: 700; color: var(--text); letter-spacing: -0.3px; }
.main-sub { color: var(--text-muted); margin-top: 4px; font-size: 13px; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-container {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  width: 90%;
  max-width: 900px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 28px;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

.modal-close:hover { color: var(--text); }

/* ── Idle ── */
.idle-state { text-align: center; padding: 40px 0; }
.idle-icon { font-size: 52px; margin-bottom: 16px; }
.idle-state h2 { font-size: 17px; color: var(--text-muted); font-weight: 400; margin-bottom: 28px; }
.idle-state h2 em { color: var(--accent); font-style: normal; font-weight: 600; }

.how-it-works { text-align: left; max-width: 560px; margin: 0 auto; background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; padding: 24px 28px; box-shadow: var(--shadow-sm); }
.how-it-works h3 { font-size: 12px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.8px; margin-bottom: 10px; margin-top: 18px; }
.how-it-works h3:first-child { margin-top: 0; }
.how-it-works ol, .how-it-works ul { padding-left: 18px; }
.how-it-works li { margin-bottom: 6px; color: var(--text); font-size: 13px; }
.how-it-works strong { color: var(--accent); }
.excl-note { font-size: 11px; color: var(--text-muted); margin-top: 10px; font-style: italic; }

/* ── Scan state ── */
.scan-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 14px; }
.scan-title { font-size: 18px; font-weight: 700; color: var(--text); }
.scan-status { color: var(--text-muted); font-size: 12px; }

.progress-wrap { margin-bottom: 10px; }
.progress-bar-bg { height: 8px; background: var(--bg-3); border-radius: 99px; overflow: hidden; margin-bottom: 6px; border: 1px solid var(--border); }
.progress-bar-fill { height: 100%; background: var(--accent); border-radius: 99px; transition: width 0.3s ease; }
.progress-label { font-size: 14px; color: var(--text-muted); font-family: var(--font-mono); }

.warnings { margin-top: 10px; }
.warning-item { font-size: 12px; color: var(--warning-tx); background: var(--warning-bg); border: 1px solid var(--warning-bdr); border-radius: var(--radius); padding: 5px 10px; margin-bottom: 4px; }

/* ── Results ── */
.results-header { display: flex; align-items: baseline; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.results-header h2 { font-size: 20px; font-weight: 700; color: var(--text); }
.results-meta { color: var(--text-muted); font-size: 12px; }

.stats-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 12px; margin-bottom: 24px; }
.stat-card { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; text-align: center; box-shadow: var(--shadow-sm); }
.stat-val { font-size: 22px; font-weight: 700; color: var(--text); }
.stat-label { font-size: 11px; color: var(--text-muted); margin-top: 2px; text-transform: uppercase; letter-spacing: 0.5px; }

.table-wrap {
 overflow-x: auto;
 border: 1px solid var(--border);
 border-radius: var(--radius);
 margin-bottom: 28px;
 box-shadow: var(--shadow-sm);
 max-width: 100%;
 }

.table-wrap::-webkit-scrollbar {
  height: 6px;
}
.table-wrap::-webkit-scrollbar-track {
  background: var(--bg-3);
}
.table-wrap::-webkit-scrollbar-thumb {
  background: var(--text-muted);
  border-radius: 99px;
}
.table-wrap::-webkit-scrollbar-thumb:hover {
  background: var(--text);
}

.table-scroll-top {
  overflow-x: auto;
  height: 12px;
  margin-bottom: 4px;
  border-radius: var(--radius);
}
.table-scroll-top-inner {
  height: 1px;
}

table { width: 100%; border-collapse: collapse; background: var(--bg-2); }
th { background: var(--bg-3); color: var(--text-muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; border-right: 1px solid var(--border); }
td { padding: 9px 14px; border-bottom: 1px solid var(--border); font-size: 13px; white-space: nowrap; font-family: var(--font-mono); border-right: 1px solid var(--border); }
td.desc-cell { white-space: normal !important; font-family: var(--font); overflow-wrap: break-word; max-width: 0; }
tr:last-child td { border-bottom: none; }
tr.alt td { background: var(--bg-3); }
tr:hover td { background: var(--accent-dim); }

.chart-section { background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; box-shadow: var(--shadow-sm); }
.chart-section h3 { font-size: 14px; font-weight: 600; color: var(--text); margin-bottom: 14px; }
#scatter-plot { width: 100%; height: 420px; }

.download-btn { background: var(--bg-2); border: 1px solid var(--border); color: var(--text); border-radius: var(--radius); font-family: var(--font); font-size: 13px; font-weight: 500; padding: 9px 20px; cursor: pointer; transition: background 0.15s; margin-bottom: 32px; box-shadow: var(--shadow-sm); }
.download-btn:hover { background: var(--bg-3); }

.error-box { background: #fef2f2; border: 1px solid #fecaca; border-radius: var(--radius); padding: 20px 24px; display: flex; gap: 12px; align-items: flex-start; }
.error-icon { font-size: 20px; }
#error-message { color: var(--red); font-size: 13px; line-height: 1.6; }

.financials-btn {
  font-size: 11px;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 3px 10px;
  cursor: pointer;
  font-family: var(--font);
  float: right;
  letter-spacing: 0.03em;
}
.financials-btn:hover {
  background: var(--bg-3);
  color: var(--text);
}
.hidden { display: none !important; }
.skip-stats {
    display: grid;
    grid-template-columns: 80px 1fr;
    gap: 2px 8px;
    color: #888;
    font-size: 0.85em;
}
.stop-btn {
  padding: 6px 14px;
}
.skip-label {
    text-align: right;
}

.tab-bar { display: flex; gap: 4px; margin-bottom: 24px; border-bottom: 1px solid var(--border); }
.tab { background: none; border: none; padding: 8px 16px; font-family: var(--font); font-size: 14px; font-weight: 500; color: var(--text-muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Single Ticker ──────────────────────────────────────────────────────── */

.ticker-search-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
}
.ticker-input {
  width: 160px;
  font-family: var(--font-mono);
  font-size: 15px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.ticker-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 18px;
  cursor: pointer;
  height: 36px;
  transition: background 0.15s;
}
.ticker-btn:hover { background: #e03c3c; }
.ticker-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.ticker-error { font-size: 12px; color: var(--red); }

.ticker-idle {
  text-align: center;
  padding: 48px 0;
  color: var(--text-muted);
  font-size: 14px;
}
.ticker-idle .idle-icon { font-size: 40px; margin-bottom: 12px; }

@keyframes spin { to { transform: rotate(360deg); } }
.spin { display: inline-block; animation: spin 1s linear infinite; }

/* Header */
.tk-header {
  display: flex;
  gap: 18px;
  align-items: flex-start;
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}
.tk-logo-wrap { flex-shrink: 0; }
.tk-logo {
  width: 52px;
  height: 52px;
  border-radius: 10px;
  border: 1px solid var(--border);
  object-fit: contain;
  background: #fafafa;
}
.tk-logo-fallback {
  width: 52px;
  height: 52px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  color: var(--text-muted);
  font-family: var(--font-mono);
}
.tk-header-text { flex: 1; min-width: 0; }
.tk-name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}
.tk-name-row h2 { font-size: 20px; font-weight: 700; color: var(--text); margin: 0; }
.tk-ticker-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid #fca5a5;
  border-radius: 4px;
  padding: 1px 7px;
  letter-spacing: 0.05em;
}
.tk-exchange-badge {
  font-size: 11px;
  background: var(--bg-3);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 7px;
}
.tk-meta-row {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 8px;
}
.tk-dot { color: var(--border); }
.tk-description {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.6;
  max-height: 54px;
  overflow: hidden;
  position: relative;
}
.tk-description.expanded { max-height: none; }
.tk-desc-toggle {
  font-size: 11px;
  color: var(--accent);
  cursor: pointer;
  background: none;
  border: none;
  padding: 2px 0;
  font-family: var(--font);
}
.desc-toggle {
  font-size: 11px;
  color: var(--accent);
  cursor: pointer;
  background: none;
  border: none;
  padding: 2px 0;
  font-family: var(--font);
  display: block;
  margin-top: 4px;
}
tr.desc-row td {
  background: var(--bg-3) !important;
}
.desc-cell {
  padding: 0.75rem 1rem;
  background: var(--bg-3);
  border-top: 1px solid var(--border);
}

.desc-text {
  font-size: 13px;
  color: var(--text);
  line-height: 1.6;
  white-space: normal;
  word-wrap: break-word;
  font-family: var(--font);
}

.desc-content {
  font-size: 13px;
  color: var(--text);
  line-height: 1.6;
  white-space: normal;
  word-wrap: break-word;
  font-family: var(--font);
}

.desc-content.expanded {
  max-height: none;
}

/* MF rank block */
.tk-rank-block {
  flex-shrink: 0;
  text-align: right;
  min-width: 80px;
}
.tk-rank-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-muted);
  margin-bottom: 2px;
}
.tk-rank-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}
.tk-rank-sub { font-size: 10px; color: var(--text-muted); margin-top: 3px; }

/* Stat cards */
.tk-cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
  margin-bottom: 24px;
}
@media (max-width: 900px) {
  .tk-cards { grid-template-columns: repeat(3, 1fr); }
}

/* Sections */
.tk-sections {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
@media (max-width: 900px) {
  .tk-sections { grid-template-columns: 1fr; }
}
.tk-section {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.tk-section-title {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  background: var(--bg-3);
  padding: 7px 14px;
  border-bottom: 1px solid var(--border);
}
.tk-table { width: 100%; border-collapse: collapse; }
.tk-td-label {
  padding: 7px 14px;
  font-size: 12px;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  width: 55%;
}
.tk-td-val {
  padding: 7px 14px;
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--text);
  border-bottom: 1px solid var(--border);
  text-align: right;
}
.tk-table tr:last-child td { border-bottom: none; }
.tk-table tr:hover td { background: var(--bg-3); }

.tk-val-positive { color: #166534; }
.tk-val-negative { color: var(--red); }

.tk-source-note {
  font-size: 11px;
  color: var(--text-muted);
  text-align: right;
  padding-top: 4px;
}

.tk-refresh-btn {
  font-size: 11px;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px 8px;
  cursor: pointer;
  margin-left: 4px;
  font-family: var(--font);
  transition: background 0.15s;
  vertical-align: middle;
  line-height: 1.6;
}
.tk-refresh-btn:hover { background: var(--bg-3); }

.tk-last-updated {
  font-size: 11px;
  color: var(--text-muted);
  align-self: center;
}

.infotip {
  font-size: 14px;
  color: var(--text-muted);
  cursor: help;
  margin-left: 3px;
}
@media (max-width: 768px) {
  .desktop-only { display: none; }
  .layout { flex-direction: column; }

  .sidebar {
    width: 100%;
    min-width: 100%;
    height: auto;
    position: relative;
    border-right: none;
    border-bottom: 1px solid var(--border);
    display: block;
  }

  .sidebar-inner { padding: 16px; }
  .main { padding: 16px; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
  .tk-cards { grid-template-columns: repeat(2, 1fr); }
  .tk-sections { grid-template-columns: 1fr; }

  .tk-header { flex-direction: column; gap: 12px; }

  .tk-rank-block {
    text-align: left;
    margin-left: 0;
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .tk-rank-label { margin-bottom: 0; }
  .tk-rank-value { font-size: 20px; }
  .tk-name-row h2 { font-size: 16px; }
  .tk-name-row { flex-wrap: wrap; }
}

.advanced-toggle {
  display: none;
  width: 100%;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-align: left;
  cursor: pointer;
  margin-bottom: 8px;
}

  .advanced-toggle { display: block; }

@media (max-width: 768px) {
  #advanced-drawer { display: block; }
  #advanced-drawer.hidden { display: none !important; }
}

#show-all-btn {
  padding: 4px 12px;
  font-family: var(--font);
  font-size: 12px;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}
#show-all-btn:hover {
  background: var(--bg-3);
}