:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #000000;
  --muted: #374151;
  --line: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --plus: #059669;
  --minus: #dc2626;
  --buy: #2563eb;   /* MT5 に合わせて 買い=青 */
  --sell: #dc2626;  /* 売り=赤 */
  --chip: #eef0f3;
  --tab-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17;
    --surface: #151b26;
    --text: #ffffff;
    --muted: #e5e7eb;
    --line: #334155;
    --accent: #3b82f6;
    --plus: #4ade80;
    --minus: #ff7b7b;
    --buy: #3b82f6;
    --sell: #ef4444;
    --chip: #2a3446;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2 { margin: 0; }

.plus { color: var(--plus); }
.minus { color: var(--minus); }
.dim { color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; }
.loading, .empty { color: var(--muted); text-align: center; padding: 48px 16px; }
.error-text { color: var(--minus); }

/* ---- 上部の見出し ---- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 20px; }
.top .sub { color: var(--muted); font-size: 14px; }
.top input[type="date"], .top select {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 6px 10px; font-size: 16px; min-height: 40px;
}

.page { padding: 12px 16px calc(var(--tab-h) + var(--safe-b) + 24px); max-width: 720px; margin: 0 auto; }
.page.center { display: flex; flex-direction: column; align-items: stretch; gap: 16px; padding-top: 18vh; text-align: center; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack input, .add input, .note {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-size: 16px;
}

/* ---- ボタン ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 600; text-decoration: none; color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--minus); }
.btn:disabled { opacity: .5; }

/* ---- 記録画面 ---- */
.form { padding-bottom: calc(var(--tab-h) + var(--safe-b) + 150px); }
.field { margin-top: 14px; }
.field h2 { font-size: 13px; color: var(--muted); font-weight: 600; margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 42px; padding: 0 14px; border-radius: 999px;
  border: 1.5px solid transparent; background: var(--chip); font-size: 15px;
}
.chip.on { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.note { margin-top: 10px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg button {
  min-height: 52px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--surface); font-size: 18px; font-weight: 700; color: var(--muted);
}
.seg .buy.on { background: var(--buy); border-color: var(--buy); color: #fff; }
.seg .sell.on { background: var(--sell); border-color: var(--sell); color: #fff; }
.seg .plus.on { background: var(--plus); border-color: var(--plus); color: #fff; }
.seg .minus.on { background: var(--minus); border-color: var(--minus); color: #fff; }
.seg.small button { min-height: 48px; font-size: 15px; padding: 0 12px; white-space: nowrap; }

.savebar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b)); z-index: 6;
  padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 8px;
}
.savebar > * { max-width: 688px; width: 100%; margin: 0 auto; }
.amount-row { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px; }
.amount-row .seg { grid-template-columns: auto auto; gap: 4px; }
.amount {
  display: flex; align-items: center; gap: 4px; padding: 0 12px; min-width: 0;
  border: 1.5px solid currentColor; border-radius: 12px; background: var(--surface);
}
.amount input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  font-size: 22px; font-weight: 700; text-align: right; color: inherit; min-height: 46px;
}
.amount .sign { font-size: 22px; font-weight: 700; }
.amount span:last-child { color: var(--muted); }
.actions { display: flex; gap: 8px; }
.actions .primary { flex: 1; font-size: 17px; min-height: 52px; }

/* ---- 一覧画面 ---- */
.day { margin-top: 14px; }
.day h2 {
  display: flex; justify-content: space-between; font-size: 13px; color: var(--muted);
  font-weight: 600; padding: 0 4px 6px;
}
.trade {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 12px; margin-bottom: 6px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
}
.trade .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.trade small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trade .pnl { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 6px; color: #fff; flex-shrink: 0; }
.badge.buy { background: var(--buy); }
.badge.sell { background: var(--sell); }

/* ---- 集計画面 ---- */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.card small { display: block; color: var(--muted); font-size: 12px; }
.card strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.section-title { font-size: 14px; color: var(--muted); margin: 20px 0 8px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.matrix { border-collapse: collapse; width: 100%; font-size: 14px; font-variant-numeric: tabular-nums; }
.matrix th, .matrix td { padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; text-align: right; }
.matrix thead th { font-size: 12px; color: var(--muted); font-weight: 600; }
.matrix tbody th, .matrix tfoot th, .matrix thead th:first-child {
  text-align: left; position: sticky; left: 0; background: var(--surface); font-weight: 600;
}
.matrix td small { display: block; color: var(--muted); font-size: 11px; }
.matrix tfoot th, .matrix tfoot td, .matrix .total { font-weight: 700; }
.matrix tfoot tr { border-top: 2px solid var(--line); }
.matrix tr:last-child th, .matrix tr:last-child td { border-bottom: 0; }

/* ---- 設定画面 ---- */
.opt-group { margin-top: 18px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.opt-group h2 { font-size: 15px; margin-bottom: 6px; }
.opt-group ul { list-style: none; margin: 0; padding: 0; }
.opt-group li { display: flex; align-items: center; gap: 4px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.opt-group li .label { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.icon { width: 40px; height: 40px; border-radius: 10px; border: 0; background: transparent; font-size: 17px; color: var(--muted); }
.icon:disabled { opacity: .25; }
.icon.danger { color: var(--minus); }
.add { display: flex; gap: 8px; margin-top: 10px; }
.add input { flex: 1; min-width: 0; }

/* ---- 下のタブ ---- */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7;
  height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
}
#tabs[hidden] { display: none; }
#tabs button {
  border: 0; background: transparent; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px;
}
#tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#tabs button.on { color: var(--accent); font-weight: 700; }

/* ---- お知らせ ---- */
#toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 12px); z-index: 20;
  transform: translate(-50%, -150%); transition: transform .25s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: 14px; font-weight: 600; max-width: calc(100% - 32px); pointer-events: none;
}
#toast.show { transform: translate(-50%, 0); }
#toast.error { background: var(--minus); color: #fff; }

/* ---- 金額待ち ---- */
:root { --wait: #b45309; --wait-bg: #fef3c7; }
@media (prefers-color-scheme: dark) { :root { --wait: #fbbf24; --wait-bg: #3a2a0a; } }
.pending-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 8px; width: 100%;
  margin-bottom: 12px; padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--wait);
  background: var(--wait-bg); color: var(--wait); font-size: 15px; text-align: left;
}
.pending-bar span:last-child { font-size: 13px; font-weight: 600; }
.trade.pending { border-color: var(--wait); }
.trade .pnl.wait { color: var(--wait); font-size: 13px; }
.chip.add-chip { background: transparent; border: 1.5px dashed var(--line); color: var(--muted); }

/* ---- 入力・確認の小窓 ---- */
.modal-back {
  position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / .45);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  width: 100%; max-width: 360px; background: var(--surface); border-radius: 16px; padding: 18px;
  display: flex; flex-direction: column; gap: 14px; box-shadow: 0 10px 40px rgb(0 0 0 / .3);
}
.modal p { margin: 0; font-weight: 600; }
.modal input { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); font-size: 16px; }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn.danger-fill { background: var(--minus); border-color: var(--minus); color: #fff; }

/* ---- 集計のマス(押せる) ---- */
.matrix td .cell {
  display: block; width: 100%; border: 0; background: transparent; padding: 0;
  text-align: right; font: inherit; font-variant-numeric: tabular-nums;
}
.matrix td .cell small { display: block; color: var(--muted); font-size: 11px; text-decoration: underline; }
.section-title small { display: block; font-weight: 400; font-size: 12px; margin-top: 2px; }

/* ---- 下から出る一覧 ---- */
.sheet-back { position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / .45); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 720px; max-height: 80vh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 16px 16px 0 0; padding-bottom: var(--safe-b);
}
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { font-size: 17px; }
.sheet-head small { color: var(--muted); }
.sheet-body { overflow-y: auto; padding: 10px 16px 16px; }

.matrix td .cell small.rate { text-decoration: none; color: var(--text); font-size: 12px; }
@media (max-width: 380px) { .card strong { font-size: 17px; } }
.cards.four { grid-template-columns: repeat(2, 1fr); margin-top: 8px; }
.cards.four strong { font-size: 18px; }
.sheet-list { margin-top: 12px; }

/* ---- メモ ---- */
.matrix .head {
  border: 0; background: transparent; padding: 0; font: inherit; font-weight: 600; color: inherit;
  text-decoration: underline; text-decoration-color: var(--line); text-underline-offset: 3px;
}
.memo-mark { font-size: 11px; }
.memo { margin-bottom: 12px; }
.memo label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.memo label small { color: var(--plus); font-weight: 400; margin-left: 6px; }
.memo textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font: inherit; font-size: 16px; resize: vertical; color: var(--text);
}

/* ---- 文字をくっきり太めに ---- */
body { font-weight: 500; }
.field h2, .day h2, .card small, .section-title, .hint, .top .sub, .trade small,
.matrix th, .matrix td small, .sheet-head small, .opt-group li .label, .chip,
#tabs button, .memo label, .amount span:last-child { font-weight: 600; }
.trade strong, .card strong, .matrix td span, .pnl { font-weight: 700; }
.field h2, .day h2, .card small { font-size: 14px; }
.trade small { font-size: 14px; }
#tabs button { font-size: 12px; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .75; }

/* ---- 日付は黄色、ルール名は水色 ---- */
:root { --date: #a16207; --rule: #0369a1; }
@media (prefers-color-scheme: dark) { :root { --date: #fde047; --rule: #67e8f9; } }
.date, .day h2 .date, .trade small .date { color: var(--date); }
.rule, .trade small .rule, .matrix tbody .head { color: var(--rule); }
.top input[type="date"] { color: var(--date); font-weight: 700; }
