:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f0f3f8;
  --ink: #111c2d;
  --muted: #5d6878;
  --line: #d8deea;
  --brand: #6c2cf5;
  --brand-dark: #4b18c6;
  --green: #087f4e;
  --green-soft: #e1f7ec;
  --red: #a92f35;
  --red-soft: #fdebed;
  --amber: #855700;
  --amber-soft: #fff2ce;
  --navy: #111c2d;
  --focus: #1769e0;
  --shadow: 0 12px 36px rgba(17, 28, 45, .08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { min-width: 0; min-height: 100%; overflow-x: clip; background: var(--bg); }
body {
  min-width: 0;
  min-height: 100vh;
  margin: 0;
  overflow-x: clip;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body.has-bottom-nav { padding-bottom: calc(88px + env(safe-area-inset-bottom)); }
button, input, select, textarea { font: inherit; }
button, a, input, select, textarea { -webkit-tap-highlight-color: transparent; }
button, .button, input, select, textarea, .tap-target { min-height: 44px; }
a { color: var(--brand-dark); text-underline-offset: 3px; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
  border-radius: 8px;
}

.skip-link {
  position: fixed;
  z-index: 300;
  left: 12px;
  top: 12px;
  transform: translateY(-160%);
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--navy);
  color: white;
}
.skip-link:focus { transform: translateY(0); }

.page-shell { width: min(100%, 1160px); margin: 0 auto; padding: 0 16px; }
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  border-bottom: 1px solid rgba(216, 222, 234, .9);
  background: rgba(255, 255, 255, .96);
}
.site-header .page-shell {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand {
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
}
.brand img { width: 44px; height: 44px; object-fit: contain; }
.brand-copy { display: grid; line-height: 1.1; }
.brand-copy strong { font-size: 20px; letter-spacing: -.03em; }
.brand-copy span { margin-top: 4px; color: var(--muted); font-size: 11px; }
.header-status { color: var(--muted); font-size: 13px; text-align: right; }

main.page-shell { padding-top: 20px; padding-bottom: 32px; }
.screen { animation: reveal .18s ease-out; }
.screen-head { margin: 0 0 18px; }
.screen-head h1, .screen-head h2 { margin: 0; font-size: clamp(25px, 7vw, 38px); line-height: 1.15; letter-spacing: -.035em; }
.screen-head p { max-width: 68ch; margin: 7px 0 0; color: var(--muted); }
.kicker { margin: 0 0 5px; color: var(--brand-dark); font-size: 12px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }

.grid, .card-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.card {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 2px 7px rgba(17, 28, 45, .035);
}
.card h2, .card h3 { margin: 0 0 8px; line-height: 1.25; }
.card p { margin: 6px 0; }
.card-accent { border-color: #d9cafd; background: linear-gradient(145deg, #fff 45%, #f4efff); }
.metric { display: grid; gap: 2px; }
.metric strong { font-size: 25px; line-height: 1.2; }
.metric span { color: var(--muted); font-size: 13px; }
.muted { color: var(--muted); }
.fine { color: var(--muted); font-size: 12px; }
.stack { display: grid; gap: 12px; }
.cluster { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.split { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.divider { height: 1px; margin: 14px 0; background: var(--line); }

.button, button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-width: 0;
  padding: 10px 15px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--brand);
  color: white;
  font-weight: 750;
  line-height: 1.15;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}
button:hover, .button:hover { background: var(--brand-dark); }
button.secondary, .button.secondary { border-color: var(--line); background: white; color: var(--ink); }
button.secondary:hover, .button.secondary:hover { background: var(--surface-soft); }
button.danger { background: var(--red); }
button.ghost { background: transparent; color: var(--brand-dark); }
button:disabled { cursor: not-allowed; opacity: .55; }
.button-row { display: flex; flex-wrap: wrap; gap: 9px; }
.button-row > * { flex: 1 1 140px; }

label { display: grid; gap: 6px; color: var(--ink); font-weight: 680; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid #bfc8d7;
  border-radius: 11px;
  background: white;
  color: var(--ink);
}
input:invalid:not(:placeholder-shown) { border-color: var(--red); }
textarea { min-height: 100px; resize: vertical; }
fieldset { min-width: 0; margin: 0; padding: 14px; border: 1px solid var(--line); border-radius: 14px; }
legend { padding: 0 6px; font-weight: 800; }
.field-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.check {
  min-height: 44px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 0;
  font-weight: 500;
}
.check input { flex: 0 0 22px; width: 22px; min-height: 22px; margin: 2px 0 0; }

.tabs, .segments, .timeframes {
  display: flex;
  gap: 6px;
  padding: 4px;
  overflow-x: hidden;
  border-radius: 14px;
  background: var(--surface-soft);
}
.tabs button, .segments button, .timeframes button {
  flex: 1 0 auto;
  border: 0;
  background: transparent;
  color: var(--muted);
}
.tabs button[aria-selected="true"], .segments button[aria-pressed="true"], .timeframes button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 2px 8px rgba(17, 28, 45, .08);
}

.segments {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.segments > * {
  min-width: 0;
  white-space: normal;
}

.badge {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
.badge.good { background: var(--green-soft); color: var(--green); }
.badge.bad { background: var(--red-soft); color: var(--red); }
.badge.warn { background: var(--amber-soft); color: var(--amber); }
.direction-long { color: var(--green); }
.direction-short { color: var(--red); }

.state-panel {
  min-height: 96px;
  display: grid;
  place-items: center;
  gap: 8px;
  padding: 22px;
  border: 1px dashed #bdc7d7;
  border-radius: 16px;
  color: var(--muted);
  text-align: center;
  background: rgba(255, 255, 255, .7);
}
.state-panel.error { border-style: solid; border-color: #e8aeb2; background: var(--red-soft); color: var(--red); }
.state-panel.offline { border-style: solid; border-color: #e6c775; background: var(--amber-soft); color: var(--amber); }
.loading-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--brand); animation: pulse .9s infinite alternate; }

.status-banner {
  position: sticky;
  top: 68px;
  z-index: 40;
  padding: 10px 16px;
  background: var(--amber-soft);
  color: var(--amber);
  text-align: center;
  font-weight: 700;
}
.status-banner.error { background: var(--red-soft); color: var(--red); }
.status-banner.good { background: var(--green-soft); color: var(--green); }

.idea-card .ticker { font-size: 24px; font-weight: 850; letter-spacing: -.025em; }
.idea-meta, .data-pairs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
.data-pairs div { min-width: 0; padding: 9px; border-radius: 10px; background: var(--surface-soft); }
.data-pairs dt { color: var(--muted); font-size: 11px; }
.data-pairs dd { margin: 3px 0 0; overflow-wrap: anywhere; font-weight: 750; }
.reason-list, .timeline { margin: 8px 0 0; padding-left: 20px; }
.reason-list li, .timeline li { margin: 5px 0; }
.timeline { list-style: none; padding-left: 12px; border-left: 2px solid #d8cafa; }

.bottom-nav {
  position: fixed;
  z-index: 80;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 2px;
  padding: 7px max(7px, env(safe-area-inset-right)) calc(7px + env(safe-area-inset-bottom)) max(7px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, .98);
}
.bottom-nav a {
  min-width: 0;
  min-height: 52px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  padding: 4px 2px;
  border-radius: 11px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 720;
  line-height: 1;
  text-align: center;
  text-decoration: none;
}
.bottom-nav a::before { font-size: 19px; line-height: 1; }
.bottom-nav a[data-section-link="home"]::before { content: "⌂"; }
.bottom-nav a[data-section-link="ideas"]::before { content: "◇"; }
.bottom-nav a[data-section-link="strategies"]::before { content: "☷"; }
.bottom-nav a[data-section-link="positions"]::before { content: "↗"; }
.bottom-nav a[data-section-link="profile"]::before { content: "○"; }
.bottom-nav a[aria-current="page"] { background: #eee7ff; color: var(--brand-dark); }

.auth-layout { min-height: 100vh; display: grid; align-items: center; padding: 20px 0; }
.auth-panel { width: min(100%, 470px); margin: 0 auto; }
.auth-intro { margin-bottom: 20px; text-align: center; }
.auth-intro .brand { justify-content: center; }
.auth-intro h1 { margin: 18px 0 5px; font-size: clamp(28px, 9vw, 42px); line-height: 1.08; }
.auth-intro p { margin: 0; color: var(--muted); }
.auth-card { padding: 20px; border-radius: 22px; background: white; box-shadow: var(--shadow); }
.auth-links { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-top: 18px; font-size: 13px; }
.auth-links a, .card a:not(.button), .cluster a, .check a {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}
.inline-message { margin: 12px 0 0; padding: 11px; border-radius: 10px; background: var(--surface-soft); }
.inline-message.error { background: var(--red-soft); color: var(--red); }
.inline-message.success { background: var(--green-soft); color: var(--green); }

dialog {
  width: min(calc(100% - 24px), 560px);
  max-height: calc(100dvh - 24px);
  padding: 0;
  overflow: auto;
  border: 0;
  border-radius: 20px;
  box-shadow: 0 24px 80px rgba(17, 28, 45, .35);
}
dialog::backdrop { background: rgba(17, 28, 45, .65); }
.dialog-body { padding: 20px; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.dialog-head h2 { margin: 0; }
.icon-button { flex: 0 0 44px; width: 44px; padding: 0; border-radius: 50%; background: var(--surface-soft); color: var(--ink); }

.chart-wrap { min-height: 330px; position: relative; overflow: hidden; border: 1px solid var(--line); border-radius: 16px; background: white; }
#klinecharts-container { width: 100%; height: 360px; }
.chart-empty { position: absolute; z-index: 5; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(255,255,255,.9); text-align: center; }

.legal-page { width: min(100%, 820px); margin: 0 auto; }
.legal-page article { padding: 22px; border-radius: 18px; background: white; }
.legal-page h1 { line-height: 1.15; }
.draft-banner { padding: 12px; border: 2px solid var(--red); border-radius: 12px; color: var(--red); font-weight: 850; text-align: center; }
.legal-page section { margin-top: 24px; }
.legal-page footer { margin: 24px 0; color: var(--muted); }

.admin-tabs { margin: 0 0 24px; }
.admin-tabs a { min-width: 112px; }
.admin-tabs a[aria-current="page"] { border-color: var(--brand); background: white; color: var(--brand-dark); }
.admin-section > .screen-head { margin-top: 4px; }
.admin-list, .admin-stats { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; margin-top: 14px; }
.admin-stat { display: grid; align-content: start; gap: 4px; }
.admin-stat > span, .admin-stat > small { color: var(--muted); }
.admin-stat > strong { overflow-wrap: anywhere; font-size: 26px; }
.admin-card h3 { margin: 0; overflow-wrap: anywhere; }
.admin-card time { color: var(--muted); font-size: 12px; text-align: right; }
.admin-identity { overflow-wrap: anywhere; }
.admin-user-detail { margin-top: 14px; border-color: #cbb8fc; box-shadow: var(--shadow); }
.admin-user-detail h3 { margin: 18px 0 6px; }
.owner-boundary { border-color: #e6c775; background: var(--amber-soft); }

@keyframes reveal { from { opacity: 0; transform: translateY(4px); } }
@keyframes pulse { to { opacity: .28; transform: scale(.75); } }

@media (min-width: 640px) {
  .page-shell { padding-left: 24px; padding-right: 24px; }
  .segments { grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); }
  .field-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .card { padding: 21px; }
  .admin-stats, .admin-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .admin-user-detail, .owner-boundary, .admin-list > .state-panel { grid-column: 1 / -1; }
}

@media (min-width: 900px) {
  body.has-bottom-nav { padding-bottom: 0; }
  .site-header { position: static; }
  .status-banner { top: 0; }
  main.page-shell { padding-top: 30px; padding-bottom: 50px; padding-left: 220px; }
  .grid, .card-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .span-all { grid-column: 1 / -1; }
  .bottom-nav {
    top: 96px;
    right: auto;
    bottom: auto;
    left: max(24px, calc((100vw - 1160px) / 2));
    width: 176px;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 5px;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: var(--shadow);
  }
  .bottom-nav a { grid-template-columns: 26px 1fr; place-items: center start; padding: 8px 10px; font-size: 14px; text-align: left; }
  .auth-layout { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 350px) {
  .page-shell { padding-left: 10px; padding-right: 10px; }
  .brand-copy span { display: none; }
  .header-status { max-width: 95px; font-size: 11px; }
  .card { padding: 14px; border-radius: 14px; }
  .idea-meta, .data-pairs { grid-template-columns: minmax(0, 1fr); }
  .bottom-nav a { font-size: 9px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
