/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #131722;
  --panel:     #1e222d;
  --panel2:    #252b3b;
  --border:    #2a2e3a;
  --text:      #d1d4dc;
  --text-dim:  #787b86;
  --text-sub:  #535760;
  --green:     #26a69a;
  --red:       #ef5350;
  --accent:    #2962ff;
  --accent-h:  #1e4fd8;
  --hover:     #2a2e3b;
  --active:    #2962ff22;
  --radius:    6px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Kaku Gothic ProN', sans-serif;
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ===== Header ===== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 48px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.back-btn {
  display: none;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
}
.back-btn:hover { background: var(--hover); }

.logo {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.02em;
}

.add-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 6px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.add-btn:hover { background: var(--accent-h); }

/* ===== Main Layout ===== */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== Watchlist Panel ===== */
.watchlist-panel {
  width: 300px;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.watchlist-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.wl-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.last-updated {
  font-size: 11px;
  color: var(--text-sub);
}

.watchlist-items {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}
.watchlist-items::-webkit-scrollbar { width: 4px; }
.watchlist-items::-webkit-scrollbar-track { background: transparent; }
.watchlist-items::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.loading-msg {
  padding: 24px;
  color: var(--text-dim);
  text-align: center;
  font-size: 13px;
}

/* ===== Category Group ===== */
.cat-section { border-bottom: 1px solid var(--border); }

.cat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  cursor: pointer;
  user-select: none;
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  transition: background .1s;
}
.cat-header:hover { background: var(--hover); }

.cat-chevron {
  display: inline-block;
  width: 10px;
  transition: transform .2s;
  color: var(--text-sub);
  font-size: 10px;
}
.cat-chevron.open { transform: rotate(0deg); }
.cat-chevron.closed { transform: rotate(-90deg); }

.cat-body { overflow: hidden; }
.cat-body.collapsed { display: none; }

/* ===== Watchlist Item ===== */
.wl-item {
  display: flex;
  align-items: center;
  padding: 10px 14px 10px 8px;
  cursor: pointer;
  transition: background .1s;
  position: relative;
  gap: 6px;
}
.wl-item:hover { background: var(--hover); }
.wl-item.active {
  background: var(--active);
  border-left: 2px solid var(--accent);
  padding-left: 6px;
}

/* Drag handle */
.drag-handle {
  color: var(--text-sub);
  font-size: 14px;
  cursor: grab;
  padding: 2px 3px;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity .1s;
  line-height: 1;
  user-select: none;
}
.wl-item:hover .drag-handle { opacity: 1; }
.wl-item.dragging {
  opacity: 0.4;
}
.wl-item.drag-over-top {
  border-top: 2px solid var(--accent);
}
.wl-item.drag-over-bottom {
  border-bottom: 2px solid var(--accent);
}

.wl-item-info {
  flex: 1;
  min-width: 0;
}

.wl-item-symbol {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.wl-item-name {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.wl-item-quote {
  text-align: right;
  flex-shrink: 0;
}

.wl-item-price {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.wl-item-change {
  font-size: 11px;
  white-space: nowrap;
  margin-top: 1px;
}
.wl-item-change.up   { color: var(--green); }
.wl-item-change.down { color: var(--red); }
.wl-item-change.flat { color: var(--text-dim); }

.wl-item-del {
  display: none;
  background: none;
  border: none;
  color: var(--text-sub);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  line-height: 1;
  font-size: 16px;
  flex-shrink: 0;
}
.wl-item:hover .wl-item-del { display: block; }
.wl-item-del:hover { color: var(--red); background: #ef535022; }

/* ===== Chart Panel ===== */
.chart-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.no-selection {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-sub);
  font-size: 14px;
}
.no-selection-icon { font-size: 48px; opacity: 0.5; }

/* ===== Chart Header ===== */
.chart-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.chart-header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 100px;
}

.chart-symbol {
  font-size: 18px;
  font-weight: 800;
  color: var(--text);
}

.chart-name {
  font-size: 12px;
  color: var(--text-dim);
}

.chart-header-center {
  flex: 1;
  min-width: 0;
}

/* P&L row */
.pnl-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  font-size: 12px;
}
.pnl-label { color: var(--text-sub); }
.pnl-entry { color: var(--text-dim); }
.pnl-sep   { color: var(--text-sub); font-size: 10px; }
.pnl-value { font-weight: 700; font-size: 13px; }
.pnl-value.up   { color: var(--green); }
.pnl-value.down { color: var(--red); }

.chart-header-right {
  text-align: right;
  flex-shrink: 0;
}

.price-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.chart-price {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

.pos-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
  font-size: 13px;
  cursor: pointer;
  padding: 3px 6px;
  line-height: 1;
  transition: background .1s, border-color .1s;
}
.pos-btn:hover { background: var(--hover); border-color: var(--accent); }
.pos-btn.has-pos { border-color: #f59e0b; color: #f59e0b; }

.chart-change {
  display: block;
  font-size: 13px;
  margin-top: 4px;
}
.chart-change.up   { color: var(--green); }
.chart-change.down { color: var(--red); }

/* Position symbol label */
.pos-symbol-label {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 14px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

/* ===== Period Bar ===== */
.period-bar {
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.period-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background .1s, color .1s;
}
.period-btn:hover { background: var(--hover); color: var(--text); }
.period-btn.active { background: var(--accent); color: #fff; }

/* ===== Chart Wrap ===== */
.chart-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

#chart-container {
  width: 100%;
  height: 100%;
  min-height: 300px;
}

.chart-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 5;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.chart-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--red);
  font-size: 14px;
  background: var(--bg);
  z-index: 5;
}

/* ===== Modal ===== */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.overlay.open { display: flex; }

.modal {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 440px;
  overflow: hidden;
}
.modal-sm { max-width: 340px; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 16px; font-weight: 700; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 4px;
}
.modal-close:hover { background: var(--hover); color: var(--text); }

.modal-body {
  padding: 20px;
}
.modal-body p { color: var(--text-dim); font-size: 14px; }

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.form-label .required { color: var(--red); margin-left: 2px; }

.form-input, .form-select {
  display: block;
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 12px;
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
}
.form-input:focus, .form-select:focus { border-color: var(--accent); }
.form-input::placeholder { color: var(--text-sub); }
.form-select option { background: var(--panel2); }

.search-wrap {
  position: relative;
}

.search-dropdown {
  display: none;
  position: absolute;
  top: calc(100% - 1px);
  left: 0;
  right: 0;
  background: var(--panel2);
  border: 1px solid var(--accent);
  border-top: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 260px;
  overflow-y: auto;
  z-index: 300;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}
.search-dropdown.open { display: block; }
.search-dropdown::-webkit-scrollbar { width: 4px; }
.search-dropdown::-webkit-scrollbar-thumb { background: var(--border); }

.search-result {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover, .search-result.focused { background: var(--hover); }

.result-left { flex: 1; min-width: 0; }
.result-symbol { font-size: 13px; font-weight: 700; color: var(--text); }
.result-name {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 1px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-right { text-align: right; flex-shrink: 0; margin-left: 8px; }
.result-exchange { font-size: 11px; color: var(--text-sub); }
.result-type {
  font-size: 10px;
  color: var(--text-sub);
  background: var(--panel);
  border-radius: 3px;
  padding: 1px 5px;
  margin-top: 2px;
  display: inline-block;
}

.search-loading {
  padding: 12px;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
}

.input-hints {
  margin-top: 8px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1.8;
}

.modal-foot {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
}

.modal-error {
  color: var(--red);
  font-size: 12px;
  margin-bottom: 10px;
  min-height: 16px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn-cancel {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  transition: background .1s;
}
.btn-cancel:hover { background: var(--hover); color: var(--text); }

.btn-confirm {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 8px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .1s;
}
.btn-confirm:hover { background: var(--accent-h); }
.btn-confirm:disabled { opacity: .5; cursor: not-allowed; }

.btn-delete {
  background: var(--red);
  border: none;
  color: #fff;
  padding: 8px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .1s;
}
.btn-delete:hover { background: #d32f2f; }

/* ===== Mobile ===== */
@media (max-width: 768px) {
  .watchlist-panel {
    width: 100%;
    min-width: 0;
    position: absolute;
    inset: 48px 0 0 0;
    z-index: 5;
    border-right: none;
  }

  .chart-panel {
    position: absolute;
    inset: 48px 0 0 0;
    z-index: 5;
    transform: translateX(100%);
    transition: transform .25s ease;
  }

  .main.chart-active .chart-panel {
    transform: translateX(0);
  }

  .main.chart-active .watchlist-panel {
    pointer-events: none;
  }

  .back-btn { display: block; }
  .logo { font-size: 14px; }

  .chart-header { padding: 10px 14px 8px; }
  .chart-symbol { font-size: 16px; }
  .chart-price { font-size: 20px; }
  .chart-name { font-size: 11px; }
}
