:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface2: #222238;
  --border: #2a2a45;
  --accent: #4fc3f7;
  --accent2: #1f6aa5;
  --up: #ff6b6b;
  --down: #51cf66;
  --text: #e0e0e0;
  --text2: #888;
  --gold: #ffd700;
  --radius: 12px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html { font-size: 15px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Hiragino Sans", "Meiryo", sans-serif;
  min-height: 100svh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ── Header ── */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-title {
  flex: 1;
  font-size: 17px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.3px;
}

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

.icon-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px;
  cursor: pointer;
  color: var(--text);
  transition: background .15s;
}
.icon-btn:active { background: var(--accent2); }
.icon-btn.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Status bar ── */
#status-bar {
  background: var(--surface);
  padding: 8px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--text2);
  border-bottom: 1px solid var(--border);
}

#count-badge {
  font-size: 15px;
  font-weight: 700;
  color: var(--accent);
}

/* ── Filter Panel ── */
#filter-section {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

#filter-toggle {
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
}

#filter-toggle .arrow {
  transition: transform .25s;
  font-size: 12px;
  color: var(--text2);
}
#filter-toggle.open .arrow { transform: rotate(180deg); }

#filter-body {
  overflow: hidden;
  max-height: 0;
  transition: max-height .3s ease;
}
#filter-body.open { max-height: 800px; }

.filter-inner { padding: 0 16px 16px; }

.filter-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text2);
  letter-spacing: .5px;
  text-transform: uppercase;
  margin: 12px 0 6px;
}

/* ── Button Group ── */
.btn-group {
  display: grid;
  gap: 6px;
}
.btn-group.cols-2 { grid-template-columns: 1fr 1fr; }
.btn-group.cols-4 { grid-template-columns: repeat(4, 1fr); }

.tog-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  padding: 9px 4px;
  cursor: pointer;
  text-align: center;
  transition: all .15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tog-btn.active {
  background: var(--accent2);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.tog-btn:active { opacity: .7; }
.pts-btn.pts-active { opacity: 1; box-shadow: 0 0 0 2px #fff3; }
.pts-btn:active { opacity: .7; }

/* ── Sliders ── */
.slider-row {
  margin-bottom: 14px;
}
.slider-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.slider-name { font-size: 13px; color: var(--text2); }
.slider-val  { font-size: 16px; font-weight: 700; color: var(--accent); }

input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 3px solid var(--bg);
  box-shadow: 0 0 0 2px var(--accent2);
}

.run-btn {
  width: 100%;
  margin-top: 14px;
  background: var(--accent2);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 14px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: .3px;
  transition: opacity .15s;
}
.run-btn:active { opacity: .8; }

/* ── Sort bar ── */
#sort-bar {
  display: flex;
  gap: 6px;
  padding: 10px 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
#sort-bar::-webkit-scrollbar { display: none; }

.sort-chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--text2);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.sort-chip.active {
  background: var(--accent2);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* ── Results ── */
#results {
  padding: 8px 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stock-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  transition: border-color .15s;
  position: relative;
  overflow: hidden;
}
.stock-card::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
}
.stock-card.up::before   { background: var(--up); }
.stock-card.down::before { background: var(--down); }
.stock-card:active { border-color: var(--accent); }

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.card-left {}
.card-code {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 2px;
}
.card-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -.3px;
}

.card-right { text-align: right; }
.card-market {
  font-size: 11px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  color: var(--text2);
  margin-bottom: 4px;
  display: inline-block;
}
.card-source {
  font-size: 11px;
  color: var(--text2);
}

.card-metrics {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin-bottom: 8px;
}

.metric {
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 7px 6px;
  text-align: center;
}
.metric-label {
  font-size: 10px;
  color: var(--text2);
  margin-bottom: 2px;
}
.metric-value {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}
.metric-value.up   { color: var(--up); }
.metric-value.down { color: var(--down); }
.metric-value.accent { color: var(--accent); }

/* ── Material ── */
.card-material {
  background: #0d1f0d;
  border: 1px solid #2a4a2a;
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.5;
  display: flex;
  align-items: flex-start;
  gap: 6px;
  cursor: pointer;
  transition: border-color .15s;
}
.card-material:active { border-color: var(--down); }
.card-material.ir     { background: #0d1a2a; border-color: #2a3a5a; }
.card-material.ir:active { border-color: var(--accent); }

.material-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 3px;
  white-space: nowrap;
  flex-shrink: 0;
  margin-top: 1px;
}
.badge-ir      { background: #1f3a5a; color: var(--accent); }
.badge-minkabu { background: #1a2a1a; color: var(--down); }
.badge-kabutan { background: #2a1a1a; color: #ff9999; }

.material-text {
  color: #c8e6c9;
  flex: 1;
}
.material-text.ir-text { color: #b3d4f0; }

.material-link-icon {
  font-size: 12px;
  color: var(--text2);
  flex-shrink: 0;
  margin-top: 1px;
}

/* ── Empty / Loading ── */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,15,26,.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 999;
  gap: 16px;
}
.spinner {
  width: 44px; height: 44px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.loading-text { color: var(--text2); font-size: 14px; }

#empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text2);
  display: none;
}
#empty-state .emoji { font-size: 48px; margin-bottom: 12px; }
#empty-state p { font-size: 15px; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 13px;
  color: var(--text);
  z-index: 200;
  transition: transform .3s;
  white-space: nowrap;
  box-shadow: 0 4px 20px rgba(0,0,0,.5);
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ── Material expand modal ── */
#material-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.7);
  z-index: 300;
  display: flex;
  align-items: flex-end;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
#material-modal.open {
  opacity: 1;
  pointer-events: auto;
}
.modal-sheet {
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  width: 100%;
  padding: 16px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  max-height: 70svh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .3s;
}
#material-modal.open .modal-sheet {
  transform: translateY(0);
}
.modal-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 16px;
}
.modal-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--accent);
}
.modal-item {
  background: var(--surface2);
  border-radius: var(--radius-sm);
  padding: 10px;
  margin-bottom: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}
.modal-item-date {
  font-size: 11px;
  color: var(--text2);
  margin-bottom: 4px;
}
.modal-open-btn {
  width: 100%;
  margin-top: 10px;
  background: var(--accent2);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

/* ── Help Modal ── */
#help-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  font-size: 17px; cursor: pointer; color: var(--text);
  transition: background .15s;
  flex-shrink: 0;
}
#help-btn:hover, #help-btn:active { background: var(--accent2); }

#help-modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.75);
  z-index: 400;
  display: flex; align-items: flex-end;
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
#help-modal.open { opacity: 1; pointer-events: auto; }

.help-sheet {
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-height: 88svh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .3s;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}
#help-modal.open .help-sheet { transform: translateY(0); }

.help-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 12px auto 0;
}

.help-header {
  display: flex; align-items: center;
  padding: 14px 18px 8px;
  border-bottom: 1px solid var(--border);
}
.help-title {
  flex: 1;
  font-size: 17px; font-weight: 700;
  color: var(--accent);
}
.help-close {
  background: none; border: none; color: var(--text2);
  font-size: 22px; cursor: pointer; padding: 0 4px;
}

.help-body { padding: 12px 18px 8px; }

.help-section {
  margin-bottom: 20px;
}
.help-section-title {
  font-size: 13px; font-weight: 700;
  color: var(--accent2);
  border-left: 3px solid var(--accent2);
  padding-left: 8px;
  margin-bottom: 10px;
}

.help-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  margin-bottom: 8px;
}
.help-table th {
  background: var(--surface2);
  color: var(--text2);
  padding: 6px 8px;
  text-align: left;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}
.help-table td {
  padding: 7px 8px;
  border-bottom: 1px solid #1e1e30;
  color: var(--text);
  vertical-align: top;
  line-height: 1.5;
}
.help-table tr:last-child td { border-bottom: none; }

.help-tag {
  display: inline-block;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 11px;
  color: var(--accent);
  margin-right: 4px;
}
.help-tag.up   { color: var(--up);   border-color: var(--up); }
.help-tag.dn   { color: var(--down); border-color: var(--down); }
.help-tag.gold { color: var(--gold); border-color: var(--gold); }
.help-tag.ir   { background: #1f3a5a; color: var(--accent); }

.help-tip {
  background: #0d1a2a;
  border: 1px solid var(--accent2);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text2);
  line-height: 1.6;
  margin-bottom: 8px;
}
.help-tip strong { color: var(--text); }

.help-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 12px;
}
.help-link:hover { text-decoration: underline; }

.help-warning {
  background: #1a1a0a;
  border: 1px solid #555500;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 11px;
  color: #aaa860;
  line-height: 1.6;
  margin-top: 12px;
}
