:root {
  --bg: #ffffff;
  --panel: #f6f7f9;
  --line: #e3e5e8;
  --text: #1d2125;
  --muted: #6b7280;
  --buy: #1565c0;
  --buy-bg: #e8f1fc;
  --sell: #d32f2f;
  --sell-bg: #fdecec;
  --plus: #1565c0;
  --minus: #d32f2f;
  --accent: #111827;
  --green: #22a34a;
  --yellow: #e0a100;
  --red: #dc2626;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; touch-action: manipulation; }
input, select {
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fff; min-width: 0;
}
input:focus, select:focus { outline: 2px solid #93c5fd; outline-offset: 0; border-color: #93c5fd; }
[hidden] { display: none !important; }
.num, b, #lot, input[inputmode="decimal"] { font-variant-numeric: tabular-nums; }

/* ---------- ログイン ---------- */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; background: var(--panel); }
#loginForm { width: 100%; max-width: 340px; background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 24px 20px; display: grid; gap: 14px; }
#loginForm label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
#loginForm input { font-size: 16px; padding: 10px 12px; color: var(--text); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; margin-bottom: 4px; }
.brand img { width: 36px; height: 36px; border-radius: 9px; }
.err { color: var(--red); margin: 0; min-height: 1.2em; font-size: 13px; }

.primary { background: var(--accent); color: #fff; border-radius: 10px; padding: 11px 16px; font-weight: 600; }
.primary:disabled { opacity: .5; }
.ghost { border: 1px solid var(--line); border-radius: 9px; padding: 8px 12px; background: #fff; }
.danger { background: var(--sell); color: #fff; border-radius: 9px; padding: 8px 12px; font-weight: 600; }

/* ---------- レイアウト ---------- */
#app { height: 100%; display: flex; flex-direction: column; }
#top {
  display: flex; align-items: center; gap: 10px; padding: calc(8px + var(--safe-t)) 12px 8px;
  border-bottom: 1px solid var(--line); background: #fff; flex: none;
}
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot.green { background: var(--green); }
.dot.yellow { background: var(--yellow); }
.dot.red { background: var(--red); }
#sym { font-weight: 700; font-size: 15px; padding: 5px 8px; max-width: 34vw; }
#quote { display: flex; flex-direction: column; line-height: 1.1; }
#qBid { font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; }
#qSpread { color: var(--muted); font-size: 11px; }
#eqBox { margin-left: auto; text-align: right; display: flex; flex-direction: column; line-height: 1.15; }
#eqBox small { color: var(--muted); font-size: 10px; }
#eqBox b { font-size: 14px; }

#main { flex: 1; min-height: 0; position: relative; }
.view { display: none; height: 100%; overflow: auto; }
.view.active { display: flex; flex-direction: column; }
#view-chart { overflow: hidden; }

#tabs {
  display: flex; border-top: 1px solid var(--line); background: #fff; flex: none;
  padding-bottom: var(--safe-b);
}
#tabs button { flex: 1; padding: 11px 4px 12px; font-size: 13px; color: var(--muted); position: relative; }
#tabs button.on { color: var(--text); font-weight: 700; }
#tabs button.on::after { content: ""; position: absolute; left: 30%; right: 30%; top: 0; height: 2px; background: var(--accent); }
#posBadge { display: inline-block; margin-left: 4px; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; line-height: 18px; }

/* ---------- チャート ---------- */
#tfbar { display: flex; gap: 2px; padding: 6px 8px; flex: none; overflow-x: auto; }
#tfbar button { padding: 5px 10px; border-radius: 7px; font-size: 13px; color: var(--muted); flex: none; }
#tfbar button.on { background: var(--panel); color: var(--text); font-weight: 700; }
#tfbar .sp { flex: 1; }
#tfbar .icon { color: var(--muted); }
#chartWrap { flex: 1; min-height: 160px; position: relative; }
#chart { position: absolute; inset: 0; }
#legend { position: absolute; left: 6px; top: 4px; right: 64px; font-size: 11px; color: var(--text); pointer-events: none; z-index: 3; font-variant-numeric: tabular-nums; display: grid; gap: 3px; justify-items: start; }
#legend .chips.lv { max-width: 100%; }
#legend .r1 { display: flex; align-items: center; gap: 6px; font-size: 12px; background: rgba(255,255,255,.85); border-radius: 6px; padding: 1px 4px; }
#legend .tfl { color: var(--muted); font-weight: 600; }
#legend .tr { font-weight: 700; font-size: 11px; padding: 0 5px; border-radius: 4px; }
#legend .tr.up { color: var(--buy); background: var(--buy-bg); }
#legend .tr.dn { color: var(--sell); background: var(--sell-bg); }
#legend #lgToggle { pointer-events: auto; font-size: 11px; color: var(--muted); border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 1px 8px; }
#legend .ohlc { color: var(--muted); background: rgba(255,255,255,.85); border-radius: 6px; padding: 1px 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 3px 4px; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: rgba(255,255,255,.88); border: 1px solid var(--line); border-radius: 10px; padding: 1px 7px 1px 5px; font-size: 11px; white-space: nowrap; }
.chip i { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.chip i.dash { height: 0; border-top: 2px dashed; border-radius: 0; }
.chip i.hist { width: 9px; height: 9px; border-radius: 1px; background: linear-gradient(90deg, #3cb371 50%, #ff6347 50%); }
.chip b { font-weight: 700; }
#legend2 { position: absolute; left: 6px; right: 64px; top: 60%; z-index: 3; pointer-events: none; display: flex; flex-wrap: wrap; gap: 3px 4px; align-items: center; font-variant-numeric: tabular-nums; }
#legend2 .ttl { font-size: 11px; font-weight: 700; background: rgba(255,255,255,.88); border-radius: 6px; padding: 1px 4px; }
#hint { position: absolute; left: 8px; right: 70px; top: 22px; z-index: 3; text-align: left; font-size: 12px; background: #fff7e6; color: #8a5300; border: 1px solid #f5d38f; border-radius: 8px; padding: 6px 9px; }
#loading { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(255,255,255,.7); color: var(--muted); z-index: 4; }

/* ---------- 注文 ---------- */
#orderbar { flex: none; border-top: 1px solid var(--line); padding: 8px 10px 10px; display: grid; gap: 8px; background: #fff; }
.orow { display: flex; gap: 8px; align-items: center; }
.orow.small label { flex: 1; display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.orow.small input { width: 100%; padding: 6px 8px; font-size: 15px; color: var(--text); }
.seg { display: flex; background: var(--panel); border-radius: 9px; padding: 3px; flex: none; }
.seg button { padding: 6px 10px; border-radius: 7px; font-size: 13px; color: var(--muted); }
.seg button.on { background: #fff; color: var(--text); font-weight: 700; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.lot { display: flex; align-items: center; margin-left: auto; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.lot button { width: 38px; height: 36px; font-size: 18px; background: var(--panel); }
.lot input { width: 64px; text-align: center; border: 0; border-radius: 0; font-weight: 700; font-size: 16px; padding: 6px 2px; }
.buy, .sell { flex: 1; border-radius: 10px; padding: 8px 6px; display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.buy { background: var(--buy); color: #fff; }
.sell { background: var(--sell); color: #fff; }
.buy small, .sell small { font-size: 11px; opacity: .9; font-weight: 600; letter-spacing: .05em; }
.buy b, .sell b { font-size: 18px; }
.buy:active, .sell:active { filter: brightness(.9); }
.buy:disabled, .sell:disabled { opacity: .45; }

/* ---------- ポジション ---------- */
.phead { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: #fff; z-index: 1; }
.phead small { display: block; color: var(--muted); font-size: 11px; }
.phead b { font-size: 18px; }
.pbtns { display: flex; gap: 6px; }
.pbtns button { font-size: 12px; }
h3.sub, .view h3 { margin: 16px 12px 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.pcard { padding: 10px 12px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.pcard .r1 { display: flex; align-items: center; gap: 8px; }
.pcard .side { font-size: 11px; font-weight: 700; padding: 2px 6px; border-radius: 5px; }
.side.buy-t { background: var(--buy-bg); color: var(--buy); }
.side.sell-t { background: var(--sell-bg); color: var(--sell); }
.pcard .pl { margin-left: auto; font-weight: 700; font-size: 16px; }
.pcard .r2 { color: var(--muted); font-size: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
.pcard .r3 { display: flex; gap: 6px; flex-wrap: wrap; }
.pcard .r3 button { font-size: 12px; padding: 6px 10px; }
.plus { color: var(--plus); }
.minus { color: var(--minus); }
.empty { padding: 30px 12px; text-align: center; color: var(--muted); }

/* ---------- 口座・設定 ---------- */
#acctList { margin: 0; padding: 6px 0; }
#acctList div { display: flex; justify-content: space-between; padding: 11px 14px; border-bottom: 1px solid var(--line); }
#acctList dt { color: var(--muted); }
#acctList dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; max-width: 68%; }
#acctList div { gap: 12px; }
.form { display: grid; gap: 10px; padding: 0 14px 6px; }
.form label { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.form label input[type="number"], .form label input[type="text"] { width: 110px; text-align: right; }
.form label.chk { justify-content: flex-start; }
.form label.chk input { width: 20px; height: 20px; }
.note { color: var(--muted); font-size: 12px; margin: 0; }

/* ---------- モーダル・通知 ---------- */
#modal { position: fixed; inset: 0; background: rgba(17,24,39,.4); display: grid; place-items: end center; z-index: 20; padding: 0 0 var(--safe-b); }
.mbox { width: 100%; max-width: 440px; background: #fff; border-radius: 16px 16px 0 0; padding: 18px 16px 16px; display: grid; gap: 12px; }
.mbox h3 { margin: 0; font-size: 17px; }
#mBody { display: grid; gap: 8px; }
#mBody .kv { display: flex; justify-content: space-between; border-bottom: 1px solid var(--line); padding: 6px 0; }
#mBody .kv span { color: var(--muted); }
#mBody label { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
#mBody input { width: 140px; text-align: right; font-size: 16px; }
.mbtns { display: flex; gap: 8px; }
.mbtns button { flex: 1; padding: 12px; }
#mOk.buyc { background: var(--buy); }
#mOk.sellc { background: var(--sell); }

#toasts { position: fixed; left: 0; right: 0; top: calc(62px + var(--safe-t)); display: grid; justify-items: center; gap: 6px; pointer-events: none; z-index: 30; padding: 0 12px; }
.toast { background: #111827; color: #fff; padding: 9px 14px; border-radius: 10px; font-size: 13px; max-width: 420px; box-shadow: 0 4px 16px rgba(0,0,0,.15); }
.toast.ng { background: var(--red); }
.toast.ok { background: #166534; }

/* ================= 画面サイズで自動レイアウト =================
   スマホ縦（〜639px）：基本のレイアウト
   スマホ横・タブレット（640px〜）：注文パネルを1行に
   横向きで高さが低い（〜520px）：上下の帯を細く
   PC（1100px〜）：右側にポジションと経済指標を常に表示 */
@media (min-width: 640px) {
  #orderbar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; }
  #orderbar .orow { flex: none; }
  #orderbar .orow.small { flex: 1; min-width: 200px; }
  #orderbar .orow:last-child { width: min(360px, 34vw); }
  .buy, .sell { padding: 6px; }
  .buy b, .sell b { font-size: 16px; }
  #top { gap: 14px; }
  #sym { max-width: 220px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  #top { padding-top: calc(4px + var(--safe-t)); padding-bottom: 4px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  #tfbar { padding: 2px 8px; }
  #tfbar button { padding: 3px 9px; }
  #nextEv { margin-bottom: 2px; padding: 3px 10px; }
  #orderbar { padding: 4px 10px; }
  .lot button { height: 30px; }
  .seg button { padding: 4px 9px; }
  .orow.small input { padding: 4px 8px; }
  #tabs button { padding: 6px 4px 7px; }
  #legend .ohlc { display: none; }
  #nextEv { display: none !important; }
  #legend .chips.lv { max-height: 46px; overflow: hidden; }
  #toasts { top: 8px; }
}
@media (min-width: 900px) {
  #modal { place-items: center; }
  .mbox { border-radius: 16px; }
}
@media (min-width: 1100px) {
  body { font-size: 15px; }
  #main { display: grid; grid-template-columns: minmax(0, 1fr) 380px; grid-template-rows: minmax(0, 1.1fr) minmax(0, 1fr); }
  #view-chart { display: flex; grid-column: 1; grid-row: 1 / 3; }
  #view-pos { display: flex; flex-direction: column; grid-column: 2; grid-row: 1; border-left: 1px solid var(--line); }
  #view-cal { display: flex; flex-direction: column; grid-column: 2; grid-row: 2; border-left: 1px solid var(--line); border-top: 2px solid var(--line); }
  #view-acct.active, #view-set.active, #view-str.active { grid-column: 1; grid-row: 1 / 3; z-index: 10; background: #fff; }
  #view-set.active .form, #view-set.active h3 { max-width: 720px; }
  #tabs .tab-pos, #tabs button[data-view=cal] { display: none; }
  #tabs { justify-content: center; }
  #tabs button { flex: 0 1 200px; }
  #legend { font-size: 12px; }
  .chip { font-size: 12px; }
  #qBid { font-size: 17px; }
  #eqBox b { font-size: 16px; }
}

.form label.chk .sw { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.form label.chk input.per { width: 64px; text-align: right; margin-left: auto; }
#tfbar { scrollbar-width: none; -webkit-overflow-scrolling: touch; }
#tfbar::-webkit-scrollbar { display: none; }
#tfbar button { min-width: 40px; }

/* ---------- Face ID ---------- */
.primary.pk { width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px; }
.primary.pk::before { content: ""; width: 20px; height: 20px; border: 2px solid #fff; border-radius: 6px; box-sizing: border-box;
  background: radial-gradient(circle at 35% 40%, #fff 1.5px, transparent 2px), radial-gradient(circle at 65% 40%, #fff 1.5px, transparent 2px); }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; margin: 12px 0 2px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
#pkList .pkrow { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
#pkList .pkrow small { color: var(--muted); display: block; font-size: 11px; }
#pkList .pkrow button { font-size: 12px; padding: 5px 10px; }

/* ---------- 経済指標 ---------- */
#nextEv { flex: none; margin: 0 8px 4px; padding: 6px 10px; border-radius: 8px; background: var(--panel); font-size: 12px; text-align: left;
  display: flex; gap: 8px; align-items: center; white-space: nowrap; overflow: hidden; }
#nextEv .nm { overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
#nextEv .cd { font-weight: 700; flex: none; }
#nextEv.soon { background: #fdecec; color: #b71c1c; }
#nextEv.now { background: #d32f2f; color: #fff; }
.calhead { display: flex; gap: 8px; padding: 10px 10px 6px; flex-wrap: wrap; position: sticky; top: 0; background: #fff; z-index: 1; border-bottom: 1px solid var(--line); }
.calday { margin: 0; padding: 8px 12px 4px; font-size: 12px; color: var(--muted); font-weight: 700; background: #fafafa; border-bottom: 1px solid var(--line); }
.calrow { display: grid; grid-template-columns: 46px 1fr; gap: 2px 10px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.calrow.past { opacity: .55; }
.calrow.next { background: #fff7e6; }
.calrow .tm { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 13px; }
.calrow .ttl { display: flex; align-items: flex-start; gap: 6px; min-width: 0; flex-wrap: wrap; }
.calrow .cur { font-size: 11px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--panel); flex: none; }
.calrow .im { color: #e0a100; font-size: 11px; letter-spacing: -1px; flex: none; }
.calrow .im.h { color: #d32f2f; }
.calrow .nm { font-size: 13px; line-height: 1.35; }
.calrow .vals { grid-column: 2; font-size: 12px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; font-variant-numeric: tabular-nums; }
.calrow .vals b { color: var(--text); }
.calrow .vals b.up { color: var(--plus); }
.calrow .vals b.dn { color: var(--minus); }
.calrow .left { grid-column: 2; font-size: 11px; color: #b26a00; font-weight: 700; }
.calnote { padding: 10px 12px 20px; }
#calBadge { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--sell); margin-left: 3px; vertical-align: top; }

.form label.chk .sw.dash { height: 0; background: none; border-top: 2px dashed; }
#tfbar #penBtn.on, #tfbar #zzBtn.on { background: var(--accent); color: #fff; border-radius: 7px; }

#chart { touch-action: none; }

/* 通貨強弱 */
#strWrap { position: relative; height: min(52vh, 460px); min-height: 240px; flex: none; }
#strChart { position: absolute; inset: 0; }
#strMsg { position: absolute; inset: 0; display: flex; white-space: pre-line; z-index: 5; align-items: center; justify-content: center; color: var(--muted); font-size: 13px; background: #fff; text-align: center; padding: 0 20px; }
#strMsg[hidden] { display: none; }
#strRank { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; padding: 8px 10px 0; }
.srow { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 9px; font-size: 13px; }
.srow .no { color: var(--muted); font-size: 11px; width: 14px; }
.srow i { width: 12px; height: 4px; border-radius: 2px; flex: none; }
.srow b { font-size: 14px; }
.srow .v { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 700; }
.srow .v.up { color: var(--plus); } .srow .v.dn { color: var(--minus); }
.srow.mine { background: #f6f7f9; border-color: #c9ccd1; }
.srow.gold { border-style: dashed; }

@media (max-width: 480px) { #tabs button { white-space: nowrap; font-size: 12px; padding-left: 1px; padding-right: 1px; } }

/* トレンド通知 */
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; }
.btnrow button { flex: 1 1 auto; }
#alList { display: grid; gap: 6px; }
.alrow { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; font-size: 13px; }
.alrow b { font-size: 14px; }
.alrow .k { color: var(--muted); font-size: 12px; }
.alrow button { margin-left: auto; color: var(--red); font-size: 12px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 7px; }
