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

body {
  font-family: 'Hiragino Kaku Gothic Pro', 'Meiryo', 'Yu Gothic', sans-serif;
  background: #0f1117;
  color: #e1e4e8;
  font-size: 14px;
  min-height: 100vh;
}

.container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.header-left h1 {
  font-size: 1.4rem;
  font-weight: 700;
  color: #58a6ff;
  letter-spacing: 0.02em;
}

.last-updated {
  font-size: 0.75rem;
  color: #8b949e;
  margin-top: 4px;
  display: block;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-select {
  background: #21262d;
  color: #c9d1d9;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}

.btn-add {
  background: #1f4e79;
  color: #79c0ff;
  border: 1px solid #388bfd;
  border-radius: 6px;
  padding: 7px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-add:hover { background: #2a6099; }

.btn-policy-all {
  background: #3d2b00;
  color: #e3b341;
  border: 1px solid #bb8009;
  border-radius: 6px;
  padding: 7px 16px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-policy-all:hover { background: #5c4200; }
.btn-policy-all:disabled { background: #3c4048; color: #8b949e; cursor: not-allowed; }

.btn-refresh {
  background: #238636;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 16px;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.2s;
}

.btn-refresh:hover { background: #2ea043; }
.btn-refresh:disabled { background: #3c4048; color: #8b949e; cursor: not-allowed; }

.refresh-icon { font-size: 1rem; }
.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Table */
.table-wrapper {
  overflow-x: auto;
  overflow-y: auto;
  max-height: calc(100vh - 130px);
  border-radius: 8px;
  border: 1px solid #30363d;
}

table {
  width: 100%;
  border-collapse: collapse;
  white-space: nowrap;
}

thead tr {
  background: #161b22;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #161b22;
}

th {
  padding: 10px 12px;
  text-align: center;
  font-size: 12px;
  color: #8b949e;
  border-bottom: 1px solid #30363d;
  user-select: none;
}

th.sortable { cursor: pointer; }
th.sortable:hover { color: #58a6ff; background: #1c2128; }
th.sort-asc::after { content: " ▲"; color: #58a6ff; }
th.sort-desc::after { content: " ▼"; color: #58a6ff; }

td {
  padding: 10px 12px;
  border-bottom: 1px solid #21262d;
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }

tr:hover td { background: #1c2128; }

tr.error-row td { opacity: 0.5; }

.num { text-align: right; }

/* Cell styles */
.cell-name {
  font-weight: 600;
  color: #c9d1d9;
}
.cell-ticker {
  font-size: 11px;
  color: #8b949e;
}

.cell-price { font-weight: 600; color: #e6edf3; }

/* PER coloring */
.per-low { color: #3fb950; }   /* < 15 */
.per-mid { color: #e6edf3; }   /* 15-25 */
.per-high { color: #f85149; }  /* > 25 */

/* Yield coloring */
.yield-high { color: #3fb950; }  /* >= 4% */
.yield-mid  { color: #d29922; }  /* 2-4% */
.yield-low  { color: #8b949e; }  /* < 2% */

/* Payout ratio coloring */
.payout-low    { color: #3fb950; }   /* < 30% */
.payout-normal { color: #e6edf3; }   /* 30-50% */
.payout-mid    { color: #d29922; }   /* 50-80% */
.payout-high   { color: #f85149; }   /* >= 80% */

/* Payout target from 決算短信 */
.payout-target { color: #a371f7; }

/* RSI coloring */
.rsi-oversold  { color: #58a6ff; font-weight: 700; }  /* <= 30 */
.rsi-normal    { color: #e6edf3; }
.rsi-overbought { color: #f85149; font-weight: 700; } /* >= 70 */

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-doi     { background: #1f4e79; color: #79c0ff; border: 1px solid #388bfd; }
.badge-ruishin { background: #1a3d2b; color: #7ee787; border: 1px solid #3fb950; }
.badge-both    { background: #3d2b00; color: #e3b341; border: 1px solid #bb8009; }

/* Consecutive years */
.years-gold { color: #e3b341; font-weight: 700; }
.years-normal { color: #c9d1d9; }

/* 利回り相当株価 comparison */
.price-above { color: #3fb950; }  /* current price >= target → below target yield (need to go down) */
.price-below { color: #f85149; }  /* current price < target → above target yield (attractive) */

/* Refresh button per row */
.btn-row-refresh {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  border-radius: 4px;
  padding: 3px 8px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
}
.btn-row-refresh:hover { border-color: #58a6ff; color: #58a6ff; }

/* Loading */
.loading-msg {
  text-align: center;
  padding: 40px;
  color: #8b949e;
  font-size: 1rem;
}

/* Legend */
.legend {
  margin-top: 16px;
  font-size: 12px;
  color: #8b949e;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}
.legend .badge { margin-right: 2px; }
.legend .rsi-low  { color: #58a6ff; }
.legend .rsi-high { color: #f85149; }

/* Error badge */
.cell-error { color: #f85149; font-size: 11px; }

/* n/a */
.na { color: #484f58; }

/* ── Chart Popup ─────────────────────────────────────── */
.chart-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
}

.chart-popup {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: min(900px, 95vw);
  max-height: 85vh;
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 10px;
  z-index: 101;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.chart-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #21262d;
  border-bottom: 1px solid #30363d;
  font-weight: 700;
  color: #58a6ff;
}

.chart-popup-close {
  background: none;
  border: none;
  color: #8b949e;
  font-size: 1rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
.chart-popup-close:hover { background: #30363d; color: #e6edf3; }

.chart-popup-body {
  padding: 16px;
  overflow-y: auto;
}

.chart-loading {
  text-align: center;
  padding: 40px;
  color: #8b949e;
}

.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.chart-item {
  background: #0d1117;
  border: 1px solid #21262d;
  border-radius: 8px;
  padding: 12px;
}

.chart-label {
  font-size: 11px;
  color: #8b949e;
  margin-bottom: 8px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Price chart icon button */
.btn-price-chart {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
  opacity: 0.6;
  vertical-align: middle;
  line-height: 1;
}
.btn-price-chart:hover { opacity: 1; }

/* Price chart popup (wider than financial chart) */
.price-chart-popup {
  width: min(1500px, 98vw);
  max-height: 95vh;
}

.price-chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
}

.price-chart-item {
  background: #0d1117;
  border: 1px solid #21262d;
  border-radius: 8px;
  padding: 8px 8px 4px;
  min-width: 0;
}

@media (max-width: 900px) {
  .price-chart-grid { grid-template-columns: 1fr; }
}

/* Clickable stock name */
.name-link {
  cursor: pointer;
  color: #c9d1d9;
  text-decoration: none;
  border-bottom: 1px dashed #444;
}
.name-link:hover { color: #58a6ff; border-color: #58a6ff; }

/* ── Add Stock Modal ─────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 101;
  pointer-events: none;
}

.modal-box {
  background: #161b22;
  border: 1px solid #30363d;
  border-radius: 10px;
  width: min(480px, 95vw);
  pointer-events: all;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: #21262d;
  border-bottom: 1px solid #30363d;
  font-weight: 700;
  color: #e6edf3;
  border-radius: 10px 10px 0 0;
}

.modal-close {
  background: none;
  border: none;
  color: #8b949e;
  font-size: 1rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
.modal-close:hover { background: #30363d; color: #e6edf3; }

.modal-body { padding: 18px; display: flex; flex-direction: column; gap: 14px; }

.form-row { display: flex; flex-direction: column; gap: 5px; }
.form-row label { font-size: 12px; color: #8b949e; }
.form-row input, .form-row select {
  background: #0d1117;
  color: #e6edf3;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
}
.form-row input:focus, .form-row select:focus {
  outline: none;
  border-color: #58a6ff;
}

.form-row-half { flex-direction: row; gap: 12px; }
.form-row-half > div { flex: 1; display: flex; flex-direction: column; gap: 5px; }
.form-row-half label { font-size: 12px; color: #8b949e; }

.required { color: #f85149; }

.modal-error {
  color: #f85149;
  font-size: 12px;
  background: rgba(248,81,73,0.1);
  border: 1px solid rgba(248,81,73,0.3);
  border-radius: 6px;
  padding: 8px 10px;
}

.modal-footer {
  padding: 14px 18px;
  border-top: 1px solid #30363d;
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.btn-cancel {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  border-radius: 6px;
  padding: 7px 18px;
  cursor: pointer;
}
.btn-cancel:hover { border-color: #58a6ff; color: #58a6ff; }

.btn-submit {
  background: #238636;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 18px;
  cursor: pointer;
  font-weight: 600;
}
.btn-submit:hover { background: #2ea043; }
.btn-submit:disabled { background: #3c4048; color: #8b949e; cursor: not-allowed; }

/* Policy detail text */
.policy-detail {
  font-size: 10px;
  color: #8b949e;
  max-width: 180px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.policy-updated {
  font-size: 10px;
  color: #3d8a4f;
  margin-top: 1px;
}

/* Policy update button per row */
.btn-policy {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  border-radius: 4px;
  padding: 3px 7px;
  cursor: pointer;
  font-size: 11px;
  margin-left: 4px;
}
.btn-policy:hover { border-color: #e3b341; color: #e3b341; }
.btn-policy:disabled { opacity: 0.5; cursor: not-allowed; }

/* WEB policy button per row */
.btn-policy-web {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  border-radius: 4px;
  padding: 3px 7px;
  cursor: pointer;
  font-size: 11px;
  margin-left: 2px;
}
.btn-policy-web:hover { border-color: #58a6ff; color: #58a6ff; }
.btn-policy-web:disabled { opacity: 0.5; cursor: not-allowed; }

/* Delete button in row */
.btn-row-delete {
  background: none;
  border: 1px solid #30363d;
  color: #8b949e;
  border-radius: 4px;
  padding: 3px 7px;
  cursor: pointer;
  font-size: 12px;
  margin-left: 4px;
}
.btn-row-delete:hover { border-color: #f85149; color: #f85149; }

/* Responsive */
@media (max-width: 768px) {
  header { flex-direction: column; align-items: flex-start; }
  .header-right {
    width: 100%;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: flex-start;
  }
  .header-left h1 { font-size: 1.1rem; }
  .container { padding: 10px 6px 30px; }
  .chart-grid { grid-template-columns: 1fr; }

  /* ボタンのタップ領域を確保 */
  .btn-add, .btn-refresh, .btn-policy-all, .btn-impact-all, .btn-keyword-mgr {
    padding: 8px 12px;
    font-size: 12px;
    min-height: 36px;
  }

  /* テーブルのフォントサイズを縮小 */
  th { padding: 8px 6px; font-size: 11px; }
  td { padding: 8px 6px; font-size: 12px; }

  /* テーブルラッパーの高さ調整 */
  .table-wrapper { max-height: calc(100vh - 180px); }

  /* 行ボタン */
  .btn-row-refresh, .btn-row-delete, .btn-policy, .btn-row-ai {
    padding: 5px 8px;
    font-size: 12px;
    min-height: 30px;
  }

  /* チャートポップアップ */
  .chart-popup { width: 100vw; max-height: 100vh; border-radius: 0; top: 0; left: 0; transform: none; }
  .price-chart-popup { width: 100vw; max-height: 100vh; }

  /* フィルターセレクト */
  .filter-select { font-size: 12px; padding: 6px 8px; }

  /* モーダル */
  .modal-box { width: 95vw; }

  /* タブ */
  .tab-btn { padding: 8px 14px; font-size: 0.85rem; }
}

/* Keyword manager button */
.btn-keyword-mgr {
  background: #2d1f4e;
  color: #a371f7;
  border: 1px solid #6e40c9;
  border-radius: 6px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-keyword-mgr:hover { background: #3d2b6e; }

/* AI calc button in impact modal */
/* Header bulk impact button */
.btn-impact-all {
  background: #1a3a1a;
  color: #3fb950;
  border: 1px solid #238636;
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-impact-all:hover { background: #1f4f1f; }
.btn-impact-all:disabled { background: #2a2a2a; color: #8b949e; cursor: not-allowed; }

/* Per-row AI button */
.btn-row-ai {
  background: transparent;
  color: #3fb950;
  border: 1px solid #238636;
  border-radius: 4px;
  padding: 2px 5px;
  font-size: 0.78rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-row-ai:hover { background: #1a3a1a; }
.btn-row-ai:disabled { color: #8b949e; border-color: #444; cursor: not-allowed; }

/* AI calc button in impact modal */
.btn-ai-calc {
  background: #1a3a1a;
  color: #3fb950;
  border: 1px solid #238636;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-ai-calc:hover { background: #1f4f1f; }
.btn-ai-calc:disabled { background: #2a2a2a; color: #8b949e; cursor: not-allowed; }

/* Per-keyword impact score cells */
.impact-score-cell { font-weight: 600; font-size: 0.85rem; }
.impact-score-cell.impact-pos-high { color: #3fb950; }
.impact-score-cell.impact-pos-mid  { color: #7ee787; }
.impact-score-cell.impact-neg-high { color: #f85149; }
.impact-score-cell.impact-neg-mid  { color: #ffa198; }
.impact-score-cell.impact-neutral  { color: #8b949e; }

/* Impact badges in table */
.impact-badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  margin: 1px;
}
.impact-pos-high  { background: #1a3d2b; color: #7ee787; border: 1px solid #3fb950; }
.impact-pos-mid   { background: #1a3020; color: #56d364; border: 1px solid #2ea043; }
.impact-neg-high  { background: #3d1a1a; color: #ff7b72; border: 1px solid #f85149; }
.impact-neg-mid   { background: #2d1a1a; color: #ffa198; border: 1px solid #da3633; }
.impact-neutral   { background: #21262d; color: #8b949e; border: 1px solid #30363d; }

/* ── Tabs ────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  border-bottom: 2px solid #21262d;
  padding-bottom: 0;
}

.tab-btn {
  background: transparent;
  color: #8b949e;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 8px 20px;
  font-size: 0.9rem;
  cursor: pointer;
  margin-bottom: -2px;
  transition: color 0.2s, border-color 0.2s;
}

.tab-btn:hover { color: #c9d1d9; }
.tab-btn.active { color: #58a6ff; border-bottom-color: #58a6ff; font-weight: 600; }

/* Yutai header */
.yutai-header {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  justify-content: flex-end;
}

/* Yutai checkboxes */
.yutai-check {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: #3fb950;
}

/* 優待テーブルソート */
#yutaiTable th.yutai-sortable { cursor: pointer; user-select: none; }
#yutaiTable th.yutai-sortable:hover { color: #58a6ff; }

/* 長期優待行 */
#yutaiTable tr.yutai-chouki-row td {
  background: rgba(56, 139, 253, 0.05);
}

/* 優待テーブル 銘柄名セル */
#yutaiTable td:first-child {
  max-width: 8em;
}
#yutaiTable td:first-child .name-link {
  display: block;
  overflow-x: auto;
  white-space: nowrap;
  max-width: 8em;
  scrollbar-width: thin;
  scrollbar-color: #444 transparent;
}
#yutaiTable td:first-child .name-link::-webkit-scrollbar { height: 3px; }
#yutaiTable td:first-child .name-link::-webkit-scrollbar-thumb { background: #444; border-radius: 2px; }

/* Yutai text cells */
.yutai-text-cell {
  max-width: 400px;
  min-width: 200px;
  font-size: 12px;
  line-height: 1.5;
}
.yutai-text-cell .yutai-scroll {
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: #444 transparent;
}
.yutai-text-cell .yutai-scroll::-webkit-scrollbar {
  height: 4px;
}
.yutai-text-cell .yutai-scroll::-webkit-scrollbar-thumb {
  background: #444;
  border-radius: 2px;
}
.yutai-detail-text {
  color: #c9d1d9;
}
.yutai-kaken {
  display: block;
  color: #8b949e;
  font-size: 11px;
  margin-top: 2px;
}

/* Mobile button label toggle */
.btn-label-short { display: none; }
.btn-label-full  { display: inline; }

@media (max-width: 480px) {
  .btn-label-short { display: inline; }
  .btn-label-full  { display: none; }
}

/* Impact range input */
.impact-range-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid #21262d;
}
.impact-range-row:last-child { border-bottom: none; }
.impact-kw-name { flex: 1; font-size: 13px; color: #c9d1d9; }
.impact-value-display {
  width: 52px;
  text-align: right;
  font-size: 13px;
  font-weight: 600;
}
.impact-range-row input[type=range] { flex: 2; accent-color: #58a6ff; }
