:root {
  color-scheme: light;
  --bg: #f3f6fb;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #dfe6f0;
  --nav: #10223f;
  --nav-2: #0b1930;
  --blue: #276ef1;
  --blue-dark: #1f56bd;
  --green: #13b981;
  --green-soft: #e9fbf4;
  --red: #ef4444;
  --red-soft: #fff1f1;
  --warn: #f59e0b;
  --warn-soft: #fff7ed;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, 0.18);
  --sidebar-w: 200px;
  --header-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 22px;
  letter-spacing: 0;
}

h2 {
  font-size: 15px;
}

.hidden {
  display: none !important;
}

/* ---------- Layout shell ---------- */

.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

.sidebar {
  background: var(--nav);
  color: #fff;
  min-height: 100vh;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
}

.brand {
  height: 74px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: var(--nav-2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.brand-mark {
  width: 18px;
  height: 22px;
  border-radius: 2px;
  background: linear-gradient(135deg, #fff 0 45%, #ef4444 46% 57%, #fff 58% 100%);
  border: 1px solid rgba(255, 255, 255, 0.45);
}

.brand strong {
  display: block;
  font-size: 12px;
  letter-spacing: 1px;
}

.brand span {
  display: block;
  margin-top: 2px;
  color: #93a4bd;
  font-size: 9px;
  letter-spacing: 2px;
}

.drawer-close {
  display: none;
  margin-left: auto;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  font-size: 22px;
  line-height: 1;
}

.side-nav {
  display: grid;
  padding: 12px 0;
  gap: 2px;
}

.side-nav button {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 16px;
  color: #cbd5e1;
  background: transparent;
  text-align: left;
  border-left: 3px solid transparent;
  font-weight: 500;
}

.side-nav button.active {
  color: #fff;
  background: var(--blue);
  border-left-color: #7dd3fc;
}

.nav-ico {
  width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.12);
  font-size: 11px;
  font-weight: 700;
  flex: 0 0 auto;
}

.content {
  min-width: 0;
}

/* ---------- Header ---------- */

.app-header {
  min-height: 72px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 22px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 30;
}

.header-title {
  flex: 1 1 auto;
  min-width: 0;
}

.breadcrumb {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.userbar {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.drawer-toggle {
  display: none;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid var(--line);
  position: relative;
  padding: 0;
  flex: 0 0 auto;
}

.drawer-toggle span {
  display: block;
  position: absolute;
  left: 9px;
  right: 9px;
  height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: transform 0.2s, top 0.2s, opacity 0.2s;
}

.drawer-toggle span:nth-child(1) { top: 13px; }
.drawer-toggle span:nth-child(2) { top: 19px; }
.drawer-toggle span:nth-child(3) { top: 25px; }

.shell.menu-open .drawer-toggle span:nth-child(1) { top: 19px; transform: rotate(45deg); }
.shell.menu-open .drawer-toggle span:nth-child(2) { opacity: 0; }
.shell.menu-open .drawer-toggle span:nth-child(3) { top: 19px; transform: rotate(-45deg); }

/* ---------- Main / pages ---------- */

.main {
  padding: 0 22px 28px;
}

.page {
  display: none;
  gap: 14px;
}

.page.active {
  display: grid;
}

.status-banner,
.panel,
.kpi {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.status-banner {
  min-height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--green-soft);
  border-left: 4px solid var(--green);
}

.status-banner > div {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

.status-banner small {
  color: var(--muted);
  margin-left: 4px;
}

.status-dot,
.health-row i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  display: inline-block;
  flex: 0 0 auto;
}

.health-row {
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  font-size: 12px;
  flex-wrap: wrap;
}

.health-row span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.health-row button {
  margin-left: auto;
}

/* ---------- KPI / grids ---------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.kpi-grid.compact {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.kpi {
  min-height: 72px;
  padding: 14px 16px;
  min-width: 0;
}

.kpi span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.kpi strong {
  font-size: 22px;
  line-height: 1.1;
  word-break: break-all;
}

.blue { color: var(--blue) !important; }
.red { color: var(--red) !important; }
.green { color: var(--green) !important; }
.muted { color: var(--muted); font-size: 12px; }

.panel {
  padding: 16px;
  min-width: 0;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.form-grid {
  display: grid;
  gap: 10px;
  align-items: end;
}

.form-stack {
  display: grid;
  gap: 10px;
}

.form-grid.cols-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.form-grid.cols-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.form-grid.cols-5 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.form-grid > input[type="hidden"] {
  display: none;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  min-width: 0;
}

label small {
  color: var(--muted);
  font-size: 11px;
}

label.check {
  min-height: 40px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}

label.check input[type="checkbox"] {
  width: 20px;
  height: 20px;
  min-height: 20px;
  flex: 0 0 auto;
  accent-color: var(--blue);
}

input,
select,
textarea {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(39, 110, 241, 0.18);
}

button,
.primary-small {
  min-height: 40px;
  border-radius: 8px;
  padding: 0 14px;
  background: var(--blue);
  color: #fff;
  font-weight: 600;
}

button:hover {
  background: var(--blue-dark);
}

button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.wide-action {
  width: min(360px, 100%);
}

.ghost {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
}

.ghost:hover {
  background: #eef3f9;
}

.danger-btn {
  background: var(--red);
}

.danger-btn:hover {
  background: #dc2626;
}

.inline-controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.inline-controls input,
.inline-controls select {
  max-width: 260px;
  min-width: 0;
  flex: 1 1 160px;
}

/* ---------- Tables ---------- */

.table {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 9px 10px;
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-weight: 700;
  background: #fbfdff;
  position: sticky;
  top: 0;
}

tbody tr:hover {
  background: #f8fbff;
}

.list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.subtle-box {
  margin-top: 10px;
  min-height: 36px;
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
  color: var(--muted);
  padding: 10px 12px;
  font-size: 12px;
  flex-wrap: wrap;
}

.subtle-box strong {
  color: var(--text);
}

.simple-defaults {
  min-height: 32px;
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  flex-wrap: wrap;
}

.advanced-settings {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
  padding: 10px;
}

.advanced-settings summary {
  cursor: pointer;
  color: var(--text);
  font-weight: 700;
}

.advanced-settings[open] summary {
  margin-bottom: 10px;
}

.section-subhead {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 14px 0 8px;
  font-size: 12px;
  flex-wrap: wrap;
}

.section-subhead span {
  color: var(--muted);
}

.symbol-picks {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 8px;
}

.symbol-pick {
  min-height: 48px;
  display: grid;
  gap: 3px;
  align-content: center;
  text-align: left;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}

.symbol-pick:hover {
  background: #eef3f9;
}

.symbol-pick strong {
  font-size: 13px;
  word-break: break-all;
}

.symbol-pick span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.item,
.strategy-card {
  border: 1px solid var(--line);
  background: #fbfdff;
  border-radius: 10px;
  padding: 12px;
  min-width: 0;
}

.item strong,
.strategy-card strong {
  display: block;
  word-break: break-all;
}

.strategy-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr));
  gap: 12px;
}

.strategy-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
  line-height: 1.5;
  word-break: break-word;
}

.state-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.orders {
  margin-top: 12px;
  max-height: 220px;
  overflow: auto;
}

.api-output {
  min-height: 150px;
  max-height: 360px;
  overflow: auto;
  margin: 12px 0 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0f172a;
  color: #dbeafe;
  padding: 12px;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-all;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  border-radius: 999px;
  padding: 0 10px;
  font-size: 11px;
  font-weight: 700;
}

.pill.ok { background: var(--green-soft); color: #059669; }
.pill.warn { background: var(--warn-soft); color: #c2410c; }
.pill.stop { background: var(--red-soft); color: var(--red); }

.kv-table {
  display: grid;
}

.kv-row {
  display: grid;
  grid-template-columns: minmax(120px, 200px) 1fr;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 10px 6px;
  font-size: 13px;
  min-width: 0;
}

.kv-row span:first-child {
  color: var(--muted);
}

.kv-row span:last-child {
  word-break: break-word;
}

canvas {
  width: 100% !important;
  height: auto;
  max-height: 420px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  display: block;
}

.notice,
.login-error {
  border: 1px solid #fecaca;
  border-radius: 8px;
  background: #fff5f5;
  color: var(--red);
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
  word-break: keep-all;
}

.danger-box {
  border-color: #fecaca;
  background: #fff1f1;
  color: var(--red);
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: var(--bg);
}

.login-card {
  width: min(390px, 100%);
  display: grid;
  gap: 10px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 22px;
  box-shadow: var(--shadow);
}

.login-card p {
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

/* ---------- Drawer (mobile) ---------- */

.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.2s;
}

.shell.menu-open .drawer-backdrop {
  opacity: 1;
}

/* ---------- Live trading section ---------- */

.live-accounts {
  display: grid;
  gap: 10px;
}

.live-account {
  display: grid;
  gap: 8px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfdff;
}

.live-account-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.live-account-head strong {
  display: block;
  word-break: break-all;
}

.live-account-toggles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.live-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 50px;
}

.live-toggle.danger {
  background: #fff7f7;
  border-color: #fecaca;
}

.live-toggle-text {
  flex: 1 1 auto;
  min-width: 0;
}

.live-toggle-text strong {
  display: block;
  font-size: 13px;
}

.live-toggle-text span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}

/* Toggle switch */
.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex: 0 0 auto;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch .slider {
  position: absolute;
  inset: 0;
  background: #cbd5e1;
  border-radius: 999px;
  transition: background 0.15s;
}

.switch .slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.switch input:checked + .slider {
  background: var(--blue);
}

.switch input:checked + .slider::before {
  transform: translateX(20px);
}

.switch.danger input:checked + .slider {
  background: var(--red);
}

.switch input:disabled + .slider {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Responsive breakpoints ---------- */

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

  .form-grid.cols-3,
  .form-grid.cols-4,
  .form-grid.cols-5 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  :root { --sidebar-w: 180px; }
}

/* ---------- Mobile drawer (≤ 900px) ---------- */
@media (max-width: 900px) {
  :root { --sidebar-w: 0px; }

  .shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 86vw;
    transform: translateX(-100%);
    transition: transform 0.22s ease-out;
    z-index: 50;
    box-shadow: var(--shadow-lg);
    overflow-y: auto;
    padding-bottom: var(--safe-bottom);
  }

  .shell.menu-open .sidebar {
    transform: translateX(0);
  }

  .drawer-backdrop {
    display: block;
  }

  .drawer-close {
    display: grid;
    place-items: center;
  }

  .drawer-toggle {
    display: block;
  }

  .app-header {
    padding: 12px 14px;
    min-height: 60px;
  }

  .userbar #userLabel {
    display: none;
  }

  .main {
    padding: 0 14px 24px;
  }

  h1 {
    font-size: 18px;
  }

  .side-nav button {
    min-height: 48px;
    font-size: 15px;
  }
}

@media (max-width: 680px) {
  body { font-size: 13px; }

  .app-header {
    padding: 10px 12px;
    gap: 8px;
  }

  .userbar {
    gap: 6px;
  }

  .userbar button {
    padding: 0 10px;
    min-height: 36px;
    font-size: 12px;
  }

  h1 { font-size: 17px; }
  h2 { font-size: 14px; }

  .status-banner,
  .health-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .health-row button {
    margin-left: 0;
  }

  .main {
    padding: 0 10px 24px;
  }

  .panel {
    padding: 14px 12px;
    border-radius: 10px;
  }

  .kpi {
    padding: 12px 14px;
    min-height: 64px;
  }

  .kpi strong {
    font-size: 19px;
  }

  .kpi-grid,
  .kpi-grid.compact,
  .form-grid.cols-3,
  .form-grid.cols-4,
  .form-grid.cols-5,
  .state-grid,
  .strategy-cards {
    grid-template-columns: 1fr;
  }

  .live-account-toggles {
    grid-template-columns: 1fr;
  }

  .kv-row {
    grid-template-columns: 1fr;
    gap: 2px;
  }

  .kv-row span:first-child {
    font-size: 11px;
  }

  .symbol-picks {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .api-output {
    font-size: 11px;
    max-height: 280px;
  }

  canvas {
    max-height: 280px;
  }

  th,
  td {
    padding: 8px 8px;
    font-size: 11.5px;
  }

  .wide-action {
    width: 100%;
  }

  button,
  .primary-small {
    min-height: 44px;
  }

  input,
  select,
  textarea {
    min-height: 44px;
    font-size: 16px; /* prevent iOS zoom */
  }

  label.check {
    min-height: 44px;
  }
}

@media (max-width: 380px) {
  .symbol-picks {
    grid-template-columns: 1fr;
  }
}
