:root {
  --bg: #0d1117;
  --panel: #161b22;
  --border: #30363d;
  --text: #c9d1d9;
  --muted: #8b949e;
  --accent: #2f81f7;
  --green: #3fb950;
  --red: #f85149;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  height: 100%;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* --- Collapsible strategy sidebar --- */
.app-nav {
  --nav-rail: 44px;
  --nav-panel: 230px;
  position: relative;
  z-index: 20;
  flex: 0 0 var(--nav-rail);
  width: var(--nav-rail);
  height: 100%;
  background: #0a0e14;
  border-inline-end: 1px solid var(--border);
  transition: flex-basis 0.18s ease, width 0.18s ease;
}
.app-nav.expanded,
.app-nav.pinned {
  flex-basis: calc(var(--nav-rail) + var(--nav-panel));
  width: calc(var(--nav-rail) + var(--nav-panel));
}
.nav-rail {
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  width: var(--nav-rail);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  background: #111827;
  border-inline-end: 1px solid var(--border);
  z-index: 2;
}
.nav-toggle {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 8px 4px;
  width: 100%;
}
.nav-toggle:hover { color: var(--text); }
.nav-toggle-icon { font-size: 16px; line-height: 1; }
.nav-toggle-label {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.nav-pin {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 8px;
  font-size: 14px;
  opacity: 0.7;
}
.nav-pin:hover,
.nav-pin.active { color: var(--accent); opacity: 1; }
.nav-panel {
  position: absolute;
  inset-block: 0;
  inset-inline-start: var(--nav-rail);
  width: var(--nav-panel);
  padding: 14px 12px;
  overflow-y: auto;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-6px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}
html[dir="rtl"] .nav-panel { transform: translateX(6px); }
.app-nav.expanded .nav-panel,
.app-nav.pinned .nav-panel {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0);
}
.nav-brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 16px;
  padding: 0 6px;
}
.nav-brand-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.nav-brand-env {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--green);
  text-transform: uppercase;
}
.nav-items {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: start;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  padding: 10px 12px;
  text-decoration: none;
  font: inherit;
}
.nav-item:hover {
  background: #161b22;
  color: var(--text);
}
.nav-item.active {
  background: #1f2937;
  color: var(--text);
  box-shadow: inset 3px 0 0 var(--accent);
}
html[dir="rtl"] .nav-item.active {
  box-shadow: inset -3px 0 0 var(--accent);
}
.nav-item-title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
}
.nav-item-sub {
  font-size: 11px;
  color: var(--muted);
}
.nav-item.active .nav-item-sub { color: #9ca3af; }

.app-shell {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

h1 { font-size: 18px; margin: 0; font-weight: 600; }
.nav-link {
  color: inherit;
  text-decoration: none;
}
.nav-link:hover { color: var(--accent); }
.subtitle { color: var(--muted); font-weight: 400; font-size: 13px; margin-left: 8px; }

.controls { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.controls label { color: var(--muted); font-size: 12px; display: flex; gap: 8px; align-items: center; min-width: 0; }
.controls label > span {
  flex-shrink: 0;
  white-space: nowrap;
}
.controls select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  max-width: 100%;
  min-width: 0;
}
#run-select {
  width: auto;
  min-width: 11.5rem;
  max-width: 18rem;
  padding-right: 2rem;
  padding-left: 10px;
  font-size: 12px;
  line-height: 1.35;
  direction: ltr;
  text-align: left;
}
#order-by { width: 10rem; min-width: 8rem; }
#lang-select { width: 6.5rem; min-width: 5.5rem; }
.checkbox { cursor: pointer; }

main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 380px 1fr;
  overflow: hidden;
}

#list-panel {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  min-height: 0;
  overscroll-behavior: contain;
}

#results {
  table-layout: fixed;
  width: 100%;
}

.universe-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
}
.universe-tabs .tab {
  flex: 1;
  background: var(--panel);
  color: var(--muted);
  border: none;
  border-bottom: 2px solid transparent;
  padding: 10px 8px;
  font-size: 12px;
  cursor: pointer;
  line-height: 1.3;
}
.universe-tabs .tab:hover { color: var(--text); }
.universe-tabs .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  background: var(--bg);
}

.list-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px 8px 16px;
  border-bottom: 1px solid var(--border);
}
.meta { color: var(--muted); font-size: 12px; }

.market-banner {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 16px;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  border-left: 3px solid var(--border);
}
.market-banner.hidden { display: none; }
.market-banner .mkt-state { font-weight: 600; font-size: 13px; }
.market-banner .mkt-detail { color: var(--muted); font-variant-numeric: tabular-nums; }
.market-banner .mkt-advice { color: var(--muted); }
.market-banner.favorable { border-left-color: var(--green); }
.market-banner.favorable .mkt-state { color: var(--green); }
.market-banner.extended { border-left-color: var(--red); }
.market-banner.extended .mkt-state { color: var(--red); }

.tier-badge {
  display: inline-block;
  min-width: 1.4em;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  text-align: center;
}
.tier-a { background: #3d2e00; color: #f0b429; border: 1px solid #d29922; }
.tier-b { background: #1c2d3f; color: #58a6ff; border: 1px solid #388bfd; }
.tier-none { color: var(--muted); }

.toolbar-btns { display: flex; gap: 6px; }
.toolbar-btns button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.toolbar-btns button:hover { border-color: var(--accent); color: var(--accent); }
.toolbar-btns button.copied { border-color: var(--green); color: var(--green); }
#copy-clean { border-color: var(--green); color: var(--green); }
.warn-flag { color: var(--red); cursor: help; }

table { width: 100%; border-collapse: collapse; }
th, td {
  text-align: right;
  padding: 7px 5px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
th:first-child, td:first-child { text-align: left; }
#results th:nth-child(1), #results td:nth-child(1) { width: 14%; }
#results th:nth-child(3), #results td:nth-child(3),
#results th:nth-child(4), #results td:nth-child(4) { width: 15%; }
#results th:nth-child(5), #results td:nth-child(5),
#results th:nth-child(6), #results td:nth-child(6) { width: 12%; }
#results th:nth-child(7), #results td:nth-child(7) { width: 10%; }
#results th:last-child, #results td:last-child { width: 6%; padding: 4px 2px; }
thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--panel);
  color: var(--muted);
  font-weight: 500;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
tbody tr { border-bottom: 1px solid var(--border); cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: rgba(47, 129, 247, 0.25); }
tbody tr:hover { background: #1c2230; }
tbody tr.active { background: #1f2a44; }
tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.sym { font-weight: 600; color: var(--accent); }
.in-zone { color: var(--green); }

#chart-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.chart-header {
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}
.chart-header h2 { margin: 0; font-size: 16px; flex: 0 0 auto; }
.chart-interval {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  margin-inline: auto;
  flex: 0 0 auto;
}
.chart-interval select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
}
.chart-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  flex: 0 1 auto;
}
.toggles { display: flex; gap: 14px; flex-wrap: wrap; }
.chart-zoom { display: flex; gap: 4px; }
.chart-zoom button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  min-width: 32px;
  height: 28px;
  font-size: 14px;
  cursor: pointer;
  padding: 0 8px;
}
.chart-zoom button:hover { border-color: var(--accent); color: var(--accent); }
.chart-zoom #zoom-fit { font-size: 11px; min-width: 36px; }
.chart-draw { display: flex; gap: 4px; }
.chart-draw button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  height: 28px;
  font-size: 11px;
  cursor: pointer;
  padding: 0 10px;
}
.chart-draw button:hover { border-color: var(--accent); color: var(--accent); }
.chart-draw button.active {
  border-color: #e3b341;
  color: #e3b341;
  background: #2a2410;
}
.hidden { display: none; }
.col-kind, th.col-kind { font-size: 11px; color: var(--muted); text-transform: capitalize; }
.chart-body { flex: 1; display: flex; min-height: 0; }
#chart { flex: 1; min-height: 0; min-width: 0; }

.decision {
  width: 240px;
  flex: 0 0 240px;
  border-left: 1px solid var(--border);
  padding: 14px 16px;
  overflow-y: auto;
  font-size: 13px;
}
.decision-empty { color: var(--muted); }
.decision h3 {
  margin: 0 0 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.decision .row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 5px 0;
  border-bottom: 1px solid var(--border);
}
.decision .row .flow-label { min-width: 0; }
.decision .row .v {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: end;
  white-space: nowrap;
}
.decision .row.hint {
  display: block;
  border-bottom: none;
  padding: 2px 0 6px;
  text-align: start;
}
.decision .row.hint span,
.decision .hint { color: var(--muted); font-size: 11px; font-weight: 400; }
.decision .hint-inline { color: var(--muted); font-size: 11px; }
.decision .flow-pair { white-space: nowrap; }
.decision .group { margin-bottom: 14px; }
.decision .good { color: var(--green); }
.decision .bad { color: var(--red); }
.decision .warn {
  background: rgba(248, 81, 73, 0.12);
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 12px;
  font-weight: 600;
}

/* Numbers and tickers stay LTR in both languages */
.ltr-num { direction: ltr; unicode-bidi: isolate; }

/* --- Hebrew / RTL layout --- */
html[dir="rtl"] body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue",
    Arial, "Noto Sans Hebrew", "Rubik", sans-serif;
}

html[dir="rtl"] .subtitle {
  margin-left: 0;
  margin-right: 8px;
}

html[dir="rtl"] #list-panel {
  border-right: none;
  border-left: 1px solid var(--border);
  /* Keep scrollbar on the physical right edge (page margin), not toward the chart. */
  direction: ltr;
}

html[dir="rtl"] #results {
  direction: rtl;
}

html[dir="rtl"] .decision {
  border-left: none;
  border-right: 1px solid var(--border);
}

/* RTL: label on the right, control on the left — natural flex order, no row-reverse */
html[dir="rtl"] .controls label {
  flex-direction: row;
}

html[dir="rtl"] .controls select {
  padding: 6px 10px;
  text-align: right;
}

html[dir="rtl"] #run-select {
  direction: ltr;
  text-align: left;
  padding-right: 2rem;
  padding-left: 10px;
}

html[dir="rtl"] th,
html[dir="rtl"] td {
  text-align: left;
}

html[dir="rtl"] th:first-child,
html[dir="rtl"] td:first-child {
  text-align: right;
}

html[dir="rtl"] .sym {
  text-align: right;
  direction: ltr;
}

html[dir="rtl"] .decision .row {
  text-align: right;
}

html[dir="rtl"] .decision .row.hint {
  text-align: right;
}

html[dir="rtl"] .decision .row .v {
  text-align: start;
}

html[dir="rtl"] .decision h3 {
  text-align: right;
}

html[dir="rtl"] .decision-empty {
  text-align: right;
}

html[dir="rtl"] .meta {
  text-align: right;
}

html[dir="rtl"] h1 {
  text-align: right;
}

/* --- Mobile / phone layout --- */
@media (max-width: 900px) {
  html, body {
    height: auto;
    min-height: 100%;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }

  body {
    flex-direction: row;
  }

  .app-nav {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    z-index: 40;
    flex: none;
    width: 40px;
    height: 100%;
    border-inline-end: none;
    background: transparent;
    pointer-events: none;
  }
  .app-nav .nav-rail {
    width: 40px;
    background: #111827;
    border-inline-end: 1px solid var(--border);
    /* Only the hamburger catches taps — the rail strip used to steal row taps. */
    pointer-events: none;
  }
  .app-nav .nav-toggle {
    pointer-events: auto;
  }
  .app-nav .nav-panel {
    pointer-events: none;
  }
  .nav-toggle-label,
  .nav-pin {
    display: none;
  }
  .nav-toggle {
    padding-top: 14px;
  }
  .app-nav.expanded,
  .app-nav.pinned {
    width: 100%;
    background: rgba(0, 0, 0, 0.45);
    pointer-events: auto;
  }
  .app-nav.expanded .nav-panel,
  .app-nav.pinned .nav-panel {
    width: min(280px, 82vw);
    background: #0a0e14;
    border-inline-end: 1px solid var(--border);
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.45);
    pointer-events: auto;
  }

  .app-shell {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: 100dvh;
    padding-inline-start: 40px;
    overflow-x: hidden;
  }

  header {
    padding: 10px 12px;
    align-items: flex-start;
    flex-direction: column;
  }
  header h1 {
    font-size: 16px;
  }
  .subtitle {
    display: inline;
    font-size: 12px;
  }
  .controls {
    width: 100%;
    display: grid;
    /* Narrow language column, wider scan-run column. */
    grid-template-columns: minmax(4.5rem, 0.55fr) minmax(0, 1.45fr);
    gap: 8px 10px;
  }
  .controls label {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .controls select,
  #order-by,
  #tier-filter {
    width: 100%;
    max-width: none;
    min-width: 0;
  }
  #lang-select {
    width: 100%;
    max-width: 5.25rem;
    min-width: 0;
    padding-inline: 6px;
  }
  #run-select {
    width: 100%;
    max-width: none;
    min-width: 0;
    font-size: 12px;
  }

  main {
    display: flex;
    flex-direction: column;
    overflow: visible;
    min-height: 0;
  }

  #list-panel {
    border: none;
    /* Constrain width so .table-scroll (not the page) owns horizontal pan. */
    overflow-x: hidden;
    overflow-y: visible;
    max-height: none;
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }
  html[dir="rtl"] #list-panel {
    border: none;
    direction: rtl;
  }

  .universe-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    touch-action: pan-x;
  }
  .universe-tabs .tab {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 10px 14px;
    font-size: 12px;
  }

  .market-banner {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    padding: 8px 12px;
  }
  .market-banner .mkt-advice {
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .list-toolbar {
    flex-wrap: wrap;
    padding: 8px 12px;
  }
  .meta {
    width: 100%;
    white-space: normal;
  }

  .table-scroll {
    width: 100%;
    max-width: 100%;
    /* Own vertical scroll so thead sticky works (overflow-x alone breaks sticky). */
    max-height: min(70vh, 640px);
    overflow-x: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-top: 1px solid var(--border);
    overscroll-behavior: contain;
    touch-action: pan-x pan-y;
    /* LTR scroller fixes broken RTL horizontal pan on iOS/Chrome. */
    direction: ltr;
  }
  .table-scroll #results {
    direction: rtl;
    table-layout: auto;
    min-width: 720px;
    width: max-content;
    max-width: none;
  }
  .table-scroll thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--panel);
  }
  th, td {
    padding: 14px 12px;
    font-size: 14px;
    overflow: visible;
  }
  tbody tr {
    touch-action: manipulation;
  }

  #chart-panel {
    min-height: 75vh;
    border-top: 1px solid var(--border);
    scroll-margin-top: 8px;
  }
  .chart-header {
    padding: 10px 12px;
    gap: 10px;
  }
  .chart-interval {
    margin-inline: 0;
    width: 100%;
    justify-content: space-between;
  }
  .chart-controls {
    width: 100%;
    gap: 10px;
  }
  .toggles {
    gap: 10px 14px;
  }
  .chart-draw button,
  .chart-zoom button {
    min-height: 36px;
    min-width: 36px;
    padding: 0 12px;
  }
  .chart-body {
    flex-direction: column;
    min-height: 65vh;
  }
  #chart {
    min-height: 48vh;
  }
  .decision {
    width: 100%;
    flex: none;
    max-height: none;
    border: none;
    border-top: 1px solid var(--border);
  }
  html[dir="rtl"] .decision {
    border: none;
    border-top: 1px solid var(--border);
  }
}

/* Phone landscape: list + chart side by side */
@media (max-width: 900px) and (orientation: landscape) {
  html, body {
    height: 100%;
    overflow: hidden;
  }
  .app-shell {
    height: 100dvh;
    overflow: hidden;
  }
  header {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    max-height: 28vh;
    overflow-y: auto;
    padding: 6px 10px;
    gap: 6px 12px;
  }
  header h1 { font-size: 14px; }
  .controls {
    display: flex;
    flex-wrap: wrap;
    width: auto;
    flex: 1;
    gap: 6px 10px;
  }
  .controls label {
    width: auto;
    flex-direction: row;
    align-items: center;
  }
  .controls select,
  #run-select,
  #order-by,
  #lang-select,
  #tier-filter {
    width: auto;
    min-width: 7rem;
  }

  main {
    flex-direction: row;
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  #list-panel {
    width: 44%;
    max-width: 44%;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-inline-end: 1px solid var(--border);
  }
  html[dir="rtl"] #list-panel {
    direction: rtl;
  }
  .table-scroll #results {
    min-width: 640px;
  }
  #chart-panel {
    width: 56%;
    min-height: 0;
    height: 100%;
    border-top: none;
  }
  .chart-body {
    min-height: 0;
    flex: 1;
  }
  #chart {
    min-height: 0;
  }
  .decision {
    max-height: 34vh;
    overflow-y: auto;
  }
}
