:root {
  --bg: #0f1420;
  --panel: #171e2e;
  --panel-2: #1d2536;
  --border: #2a3348;
  --text: #e6e9f0;
  --text-dim: #9aa4bb;
  --accent: #4f8cff;
  --accent-2: #7c5cff;
  --good: #35c78a;
  --bad: #ef5a6f;
  --warn: #e0a530;
  --radius: 12px;
  font-size: 15px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --panel: #ffffff;
    --panel-2: #f0f2f8;
    --border: #dfe4ee;
    --text: #1a2033;
    --text-dim: #5b6478;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", "Pretendard", "Malgun Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
  gap: 10px;
}

.topbar h1 { font-size: 1.25rem; margin: 0; }
.header-left { display: flex; align-items: center; gap: 14px; }

.home-link {
  font-size: 0.85rem;
  color: var(--text-dim);
  text-decoration: none;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  white-space: nowrap;
}
.home-link:hover { color: var(--text); border-color: var(--accent); }

.layout {
  max-width: 980px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 20px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.card h2 {
  font-size: 1rem;
  margin: 0 0 12px;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.hidden { display: none !important; }

/* ── 모드 배지 ── */
.mode-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.mode-banner.mode-safe { background: rgba(53, 199, 138, 0.08); border-color: var(--good); }
.mode-banner.mode-live { background: rgba(239, 90, 111, 0.10); border-color: var(--bad); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.badge.safe { background: rgba(53, 199, 138, 0.18); color: var(--good); }
.badge.live { background: rgba(239, 90, 111, 0.20); color: var(--bad); }
.badge.neutral { background: var(--panel-2); color: var(--text-dim); }

.mode-banner .detail { color: var(--text-dim); font-size: 0.85rem; }

.mode-quick-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  flex-wrap: wrap;
}
.mode-quick-controls label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-dim);
}
.mode-quick-controls select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 0.82rem;
}
.mode-quick-controls button {
  padding: 7px 14px;
  font-size: 0.82rem;
}

/* ── 히스토리 필터 ── */
.history-filter { display: flex; gap: 8px; margin-bottom: 12px; }
.filter-btn {
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text-dim);
  cursor: pointer;
}
.filter-btn.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  border-color: transparent;
}

/* ── 요약 그리드 ── */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.summary-item { background: var(--panel-2); border-radius: 10px; padding: 12px 14px; }
.summary-item .label { color: var(--text-dim); font-size: 0.78rem; }
.summary-item .value { font-size: 1.05rem; font-weight: 700; margin-top: 4px; }

/* ── 설정 폼 ── */
.config-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px 16px; }
.config-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 0.82rem; color: var(--text-dim);
}
.config-form input, .config-form select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.88rem;
}
.config-form input:disabled, .config-form select:disabled {
  opacity: 0.65; cursor: not-allowed;
}
.config-form input:focus, .config-form select:focus { outline: none; border-color: var(--accent); }

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

button {
  padding: 11px 18px;
  border-radius: 10px;
  border: none;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
}
button.secondary { background: var(--panel-2); color: var(--text-dim); border: 1px solid var(--border); }
button.danger { background: linear-gradient(135deg, #e0546a, #c53d52); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ── 확인 모달 ── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 22px; max-width: 420px; width: 90%;
}
.modal h3 { margin: 0 0 10px; }
.modal p { color: var(--text-dim); font-size: 0.88rem; line-height: 1.5; }
.modal input { width: 100%; margin-top: 10px; padding: 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); }
.modal .actions { justify-content: flex-end; }

/* ── 로딩 오버레이 (진행 중엔 다른 조작 막기) ── */
.loading-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; /* 확인 모달(50)보다 위 - 확인 후 곧바로 이 오버레이가 덮음 */
  cursor: wait;
}
.loading-box {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 32px 40px; text-align: center; max-width: 320px;
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading-message { font-size: 0.88rem; color: var(--text-dim); line-height: 1.5; }

/* ── 보유종목 vs 목표 테이블 ── */
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
table th, table td { padding: 7px 8px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
table th:first-child, table td:first-child { text-align: left; }
table th { color: var(--text-dim); font-weight: 600; }
.matrix-wrap { overflow-x: auto; }

.diff-ok { color: var(--good); }
.diff-warn { color: var(--warn); }

/* ── 히스토리 타임라인 ── */
.timeline { display: flex; flex-direction: column; gap: 10px; }
.timeline-item {
  background: var(--panel-2); border-radius: 10px; padding: 12px 14px;
  border-left: 3px solid var(--border);
}
.timeline-item.trigger-manual { border-left-color: var(--accent); }
.timeline-item.trigger-cron { border-left-color: var(--good); }
.timeline-item.action-error { border-left-color: var(--bad); }
.timeline-item .tl-head { display: flex; justify-content: space-between; gap: 10px; font-size: 0.85rem; }
.timeline-item .tl-head .ts { color: var(--text-dim); }
.timeline-item .tl-trades { margin-top: 8px; font-size: 0.8rem; color: var(--text-dim); }
.timeline-item .tl-trades div { padding: 2px 0; }

.status-box {
  padding: 14px 18px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text-dim);
}
.status-box.error { border-color: var(--bad); color: var(--bad); }
