* { box-sizing: border-box; }
body {
  margin: 0; color: #222; background: #fafafa;
  font-family: -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", sans-serif;
}
header { padding: 20px 24px 14px; background: #fff; border-bottom: 1px solid #eee; }
header h1 { margin: 0 0 4px; font-size: 20px; }
.sub { margin: 0; color: #888; font-size: 13px; }

main { max-width: 1200px; margin: 18px auto; padding: 0 16px; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; margin-bottom: 10px; }
.pick { font-size: 14px; color: #555; }
.controls select { font-size: 15px; padding: 5px 8px; margin-left: 4px; }
.stat { font-size: 15px; }
.stat b { font-size: 17px; }
.stat .up { color: #d50000; } .stat .down { color: #00897b; }
.ma-toggles { margin-left: auto; display: flex; flex-wrap: wrap; gap: 8px 14px; font-size: 13px; }
.ma-toggles label { cursor: pointer; user-select: none; }
.ma-toggles .ma5 { color: #42a5f5; } .ma-toggles .ma20 { color: #ff9800; } .ma-toggles .ma60 { color: #ab47bc; }
.ma-toggles .ma120 { color: #ec407a; } .ma-toggles .ma240 { color: #8d6e63; }
.ma-toggles .bb { color: #607d8b; }
.ma-toggles .vol { color: #90a4ae; }

#chart-box {
  position: relative;
  width: min(1120px, 90vw); height: 460px;
  min-width: 320px; min-height: 240px;
  max-width: 96vw;
  /* 以整個視窗置中（即使超出 main 寬也沒關係）→ 拖右下角時左右對稱外擴 */
  margin-left: 50%; transform: translateX(-50%);
}
#chart { width: 100%; height: 100%; background: #fff; border: 1px solid #eee; border-radius: 6px; overflow: hidden; }
#legend {
  position: absolute; top: 8px; left: 12px; z-index: 4; pointer-events: none;
  font-size: 12px; line-height: 1.6; color: #333;
  background: rgba(255, 255, 255, .82); padding: 3px 8px; border-radius: 4px;
  white-space: nowrap; max-width: calc(100% - 24px); overflow: hidden;
}
#legend .nm { font-weight: 600; }
#legend .lbl { color: #999; }
#legend .up { color: #d50000; } #legend .down { color: #00897b; }
#legend .c5 { color: #42a5f5; } #legend .c20 { color: #ff9800; } #legend .c60 { color: #ab47bc; }
#grip {
  position: absolute; right: 0; bottom: 0; width: 18px; height: 18px;
  cursor: nwse-resize; z-index: 5; border-bottom-right-radius: 6px;
  background: repeating-linear-gradient(135deg, #b0b0b0 0 2px, transparent 2px 4px);
}
.years { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.years button {
  font-size: 12px; padding: 3px 10px; cursor: pointer;
  border: 1px solid #ccc; border-radius: 4px; background: #fff; color: #555;
}
.years button:hover { border-color: #1976d2; color: #1976d2; }
.years button.active { background: #1976d2; color: #fff; border-color: #1976d2; }

.rebal-ctrl { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; margin-top: 12px; font-size: 13px; color: #555; }
.rebal-ctrl label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; user-select: none; }
.rebal-ctrl input[type="date"] { font-size: 13px; padding: 2px 5px; }
.rebal-ctrl input[type="date"]:disabled { color: #bbb; }

.rebal { margin-top: 14px; border: 1px solid #e3e3e3; border-radius: 6px; padding: 12px 14px; background: #fff; font-size: 13px;
  display: flex; gap: 22px; align-items: center; flex-wrap: wrap; }
.rebal .rb-main { flex: 1 1 340px; min-width: 0; }
.rebal .rb-hd { font-weight: 600; margin-bottom: 2px; }
.rebal .rb-hd input { vertical-align: -1px; cursor: pointer; margin-right: 2px; }
.rebal .rb-sub { color: #888; font-size: 12px; margin-bottom: 10px; }
.rebal .rb-grid { display: flex; flex-wrap: wrap; gap: 14px 30px; }
.rebal .rb-grid > div { display: flex; flex-direction: column; }
.rebal .rb-lbl { color: #999; font-size: 12px; }
.rebal .rb-grid b { font-size: 18px; }
.rebal .up { color: #d50000; } .rebal .down { color: #00897b; }
.rebal .rb-cost { margin-top: 10px; color: #666; font-size: 12px; border-top: 1px dashed #eee; padding-top: 8px; }
.rebal .rb-cost b { color: #444; }
.rebal .rb-note { color: #999; }
.rebal .rb-pie { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.rebal .pie { width: 120px; height: 120px; border-radius: 50%; box-shadow: 0 0 0 1px #eee inset; }
.rebal .pie-legend { font-size: 12px; line-height: 1.7; }
.rebal .pie-legend > div { display: flex; align-items: center; gap: 6px; }
.rebal .pie-legend .dot { width: 10px; height: 10px; border-radius: 2px; }
.rebal .dot.stock { background: #1976d2; } .rebal .dot.cash { background: #b0bec5; }
.rebal .pie-cap { font-size: 11px; color: #999; }

.hint { color: #aaa; font-size: 12px; margin-top: 8px; }

footer { max-width: 1000px; margin: 24px auto; padding: 12px 16px; color: #999; font-size: 12px; border-top: 1px solid #eee; }
footer code { background: #f2f2f2; padding: 1px 5px; border-radius: 3px; }

@media (max-width: 640px) {
  .ma-toggles { margin-left: 0; }
  #chart-box { height: 360px; }
}

/* 策略訊號插槽選擇列 */
.slot-ctrl { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; margin-top: 14px; font-size: 13px; color: #555; }
.slot-ctrl .slot-lbl { font-weight: 600; color: #333; }
.slot-ctrl label { display: inline-flex; align-items: center; gap: 5px; user-select: none; }
.slot-ctrl select { font-size: 13px; padding: 3px 6px; border: 1px solid #ccc; border-radius: 4px; background: #fff; cursor: pointer; }
.slot-ctrl select:focus { outline: none; border-color: #1976d2; }

/* 葛蘭碧八大法則：法則分佈標籤（其餘樣式複用 .rebal 既有定義） */
.gran-rules { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; border-top: 1px dashed #eee; padding-top: 8px; }
.gran-tag { font-size: 11px; padding: 2px 8px; border-radius: 3px; background: #f0f4ff; color: #1976d2; border: 1px solid #d0dff8; }
.gran-tag.best { background: #fff3d6; color: #b26a00; border-color: #f0d089; font-weight: 600; }

/* 最佳參數搜尋：觸發按鈕 */
.btn-run { font-size: 13px; padding: 6px 14px; cursor: pointer; color: #fff; background: #1976d2;
  border: 1px solid #1565c0; border-radius: 5px; }
.btn-run:hover { background: #1565c0; }
.btn-run:active { transform: translateY(1px); }
.opt-hint { font-size: 12px; color: #999; }
