/* Tesla 팔레트 (tesla.com 기준):
   red #E82127, blue #3E6AE1, dark #171A20, surface #F4F4F4, gray #5C5E62.
   등락 색: 국내 관례 — 상승 빨강, 하락 파랑. 라이트/다크 두 테마. */
:root, [data-theme="dark"] {
  --bg: #141414;
  --card: #1b1b1f;
  --card-border: #2b2b2e;
  --text: #f4f4f4;
  --text-dim: #8e8e93;
  --up: #e82127;
  --down: #3e6ae1;
  --accent: #3e6ae1;
  --overlay: rgba(0, 0, 0, 0.78);
  --badge-open: #4ade80;
  --badge-open-border: #14532d;
  --btn-hover: #2b2b2e;
}
[data-theme="light"] {
  --bg: #ffffff;
  --card: #f4f4f4;
  --card-border: #d0d1d2;
  --text: #171a20;
  --text-dim: #5c5e62;
  --up: #e82127;
  --down: #3457d5;
  --accent: #3457d5;
  --overlay: rgba(255, 255, 255, 0.82);
  --badge-open: #0b8a3d;
  --badge-open-border: #9ad9b4;
  --btn-hover: #e4e4e6;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Gotham SSm", -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  transition: background 0.25s, color 0.25s;
}

body {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: clamp(8px, 1.6vh, 22px) clamp(10px, 2vw, 30px);
  gap: clamp(8px, 1.6vh, 18px);
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex: none;
}

.brand {
  font-size: clamp(0.9rem, 1.7vw, 1.4rem);
  font-weight: 600;
  letter-spacing: 0.35em;
  color: var(--text-dim);
}

.status {
  display: flex;
  align-items: center;
  gap: clamp(0.5rem, 1vw, 1rem);
  font-size: clamp(0.95rem, 1.7vw, 1.3rem);
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.badge {
  font-size: clamp(0.7rem, 1.2vw, 0.95rem);
  font-weight: 600;
  padding: 0.25em 0.9em;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  color: var(--text-dim);
}
.badge.open { color: var(--badge-open); border-color: var(--badge-open-border); }

.ex-btn {
  font-family: inherit;
  cursor: pointer;
  background: transparent;
  color: var(--text);
  border-color: var(--accent);
}
.ex-btn:hover { background: var(--btn-hover); }

.status-badge {
  font-size: clamp(0.65rem, 1.2vw, 0.8rem);
  font-weight: 700;
  padding: 0.2em 0.5em;
  border-radius: 4px;
  letter-spacing: 0.02em;
  line-height: 1;
  display: inline-block;
  vertical-align: middle;
}
.status-badge.realtime {
  background: rgba(16, 185, 129, 0.15);
  color: #10b981;
}
.status-badge.delayed {
  background: rgba(245, 158, 11, 0.15);
  color: #f59e0b;
}
.status-badge.offline {
  background: rgba(239, 68, 68, 0.15);
  color: #ef4444;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: clamp(1.1rem, 2vw, 1.5rem);
  line-height: 1;
  cursor: pointer;
  padding: 0.2em 0.35em;
  border-radius: 8px;
}
.icon-btn:hover { background: var(--btn-hover); color: var(--text); }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.6vh, 18px);
  min-height: 0;
}

.index-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: clamp(8px, 1.5vw, 20px);
  flex: none;
}

.card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: clamp(10px, 2vh, 26px) clamp(12px, 1.8vw, 28px);
  transition: background 0.25s, border-color 0.25s;
}

.index-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: clamp(10px, 2vw, 30px);
}
.index-left {
  display: flex;
  flex-direction: column;
  flex: 1;
}
.index-right {
  display: flex;
  flex-direction: column;
  gap: clamp(4px, 0.6vh, 10px);
  flex: none;
  border-left: 1px solid var(--card-border);
  padding-left: clamp(12px, 2vw, 30px);
  justify-content: center;
}
.investor-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: clamp(8px, 1.2vw, 20px);
  font-size: clamp(0.85rem, 1.5vw, 1.15rem);
}
.investor-row .lbl {
  color: var(--text-dim);
  font-weight: 500;
}
.investor-row .val {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
  min-width: clamp(4rem, 6.5vw, 6rem);
}
.index-card .name {
  font-size: clamp(0.9rem, 1.5vw, 1.2rem);
  color: var(--text-dim);
  letter-spacing: 0.15em;
  margin-bottom: 0.4vh;
}
.index-card .price {
  font-size: clamp(1.8rem, 6.5vmin, 3.6rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.12;
}
.index-card .delta {
  font-size: clamp(1rem, 2.6vmin, 1.5rem);
  font-variant-numeric: tabular-nums;
}

.stock-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(clamp(130px, 22vw, 240px), 1fr));
  grid-auto-rows: 1fr;
  gap: clamp(8px, 1.5vw, 20px);
  min-height: 0;
}

.stock-card {
  position: relative; /* 순서변경 바 오버레이 기준 */
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.5vh;
  touch-action: none; /* 롱프레스가 스크롤/제스처로 안 새게 */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none; /* 롱프레스 콜아웃/선택 → pointercancel 방지 (Tesla/iOS) */
}
.stock-card.reordering {
  outline: 2px solid var(--accent);
  z-index: 5;
}
.reorder-bar {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 1.2vh, 14px);
  background: rgba(0, 0, 0, 0.72);
  border-radius: 14px;
  padding: clamp(8px, 1.5vh, 20px);
}
.reorder-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: clamp(6px, 1.5vw, 16px);
  width: 100%;
}
.ro-btn {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 10px;
  font-size: clamp(0.95rem, 1.8vw, 1.25rem);
  height: clamp(2.2rem, 4.5vh, 3rem);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0 0.8em;
  transition: background 0.15s, color 0.15s;
}
.ro-btn:active { background: var(--accent); color: #fff; }
.ro-prev, .ro-next {
  flex: 1;
  max-width: clamp(2.4rem, 5vw, 3.2rem);
}
.ro-stop {
  flex: 2;
  font-weight: 600;
}
.ro-holding {
  width: 100%;
  font-weight: 600;
  font-size: clamp(0.88rem, 1.6vw, 1.15rem);
}
.stock-card .name {
  font-size: clamp(0.95rem, 1.8vw, 1.35rem);
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stock-card .price {
  font-size: clamp(1.3rem, 4.5vmin, 2.6rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.12;
}
.stock-card .delta {
  font-size: clamp(0.85rem, 2vmin, 1.25rem);
  font-variant-numeric: tabular-nums;
}

.up   { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--text-dim); }

/* --- 오버레이 / 패널 공통 --- */
#error-overlay, #settings-panel {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
/* id 선택자가 [hidden]의 display:none을 이기므로 명시적으로 숨김 규칙 추가 */
#error-overlay[hidden], #settings-panel[hidden] { display: none; }

.error-box {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 3vh 4vw;
  text-align: center;
}
.error-title { font-size: 2rem; font-weight: 600; margin-bottom: 1vh; }
.error-msg { color: var(--text-dim); font-size: 1.1rem; }

/* --- 설정 패널 --- */
.settings-box {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: clamp(16px, 3vh, 30px);
  width: min(92vw, 520px);
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.settings-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 1.3rem;
  font-weight: 600;
}
.add-form { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.add-form input {
  flex: 1 1 120px;
  background: var(--bg);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  color: var(--text);
  padding: 0.7em 0.9em;
  font-size: 1rem;
}
.add-form input:focus { outline: none; border-color: var(--accent); }
#add-btn {
  flex: none;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0.7em 1.4em;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
#add-btn:disabled { opacity: 0.5; cursor: default; }
.add-msg { font-size: 0.9rem; min-height: 1.2em; color: var(--up); }
.add-msg.ok { color: var(--badge-open); }

.wl-list { list-style: none; overflow-y: auto; display: flex; flex-direction: column; gap: 0.5rem; }
.wl-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  padding: 0.7em 1em;
}
.wl-list .wl-name { font-weight: 600; }
.wl-list .wl-code { color: var(--text-dim); font-size: 0.9rem; margin-left: 0.6em; }
.wl-actions { display: flex; align-items: center; gap: 0.2em; }
.wl-up, .wl-down {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0 0.3em;
}
.wl-up:active, .wl-down:active { color: var(--accent); }
.wl-del {
  background: none;
  border: none;
  color: var(--up);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0 0.3em;
}

/* --- 보유 종목 카드 하단 스타일 --- */
.stock-card .holding-info {
  margin-top: 0.8vh;
  padding-top: 0.8vh;
  border-top: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  gap: 0.4vh;
  font-size: clamp(0.7rem, 1.3vw, 0.88rem);
}
.holding-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.holding-row .lbl {
  color: var(--text-dim);
  flex-shrink: 0;
  white-space: nowrap;
}
.holding-row .val {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: right;
}
.holding-row .val.profit {
  font-weight: 600;
}

/* --- 보유 입력 패널 --- */
#holding-panel {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 11; /* 설정창(z-index: 10)보다 한 단계 위에 띄움 */
}
#holding-panel[hidden] { display: none; }

.holding-box {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: clamp(16px, 3vh, 30px);
  width: min(92vw, 420px);
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}
.holding-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 1.25rem;
  font-weight: 600;
}
.holding-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.holding-form .form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.holding-form label {
  font-size: 0.9rem;
  color: var(--text-dim);
  font-weight: 500;
}
.holding-form input {
  background: var(--bg);
  border: 1px solid var(--card-border);
  border-radius: 10px;
  color: var(--text);
  padding: 0.7em 0.9em;
  font-size: 1rem;
}
.holding-form input:focus {
  outline: none;
  border-color: var(--accent);
}
.holding-buttons {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.5rem;
}
.holding-buttons button {
  flex: 1;
  border: none;
  border-radius: 10px;
  padding: 0.8em;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.holding-buttons .clear-btn {
  background: var(--btn-hover);
  color: var(--text);
  border: 1px solid var(--card-border);
}
.holding-buttons .clear-btn:hover {
  background: rgba(232, 33, 39, 0.12);
  color: var(--up);
  border-color: var(--up);
}
.holding-buttons .save-btn {
  background: var(--accent);
  color: #fff;
}
.holding-buttons .save-btn:hover {
  filter: brightness(1.15);
}
