:root {
  --bg: #fbfaf7;
  --paper: #fffefc;
  --ink: #1c1915;
  --muted: #6f685f;
  --line: #e4dfd6;
  --head: #fcfaf6;
  --up: #e7f5ec;
  --up-ink: #14663a;
  --down: #fdeceb;
  --down-ink: #9d261f;
  --low: #8a4b12;
  --focus: #1c1915;
  --block-line: #d6ccbd;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background: var(--bg);
  font: 13px/1.35 "Segoe UI", system-ui, sans-serif;
}

.top {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: flex-start;
  padding: 18px 22px 8px;
}

h1 {
  margin: 0 0 6px;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.rule { display: none; }

.save {
  margin: 2px 0 0;
  color: var(--muted);
  white-space: nowrap;
}

.settings {
  display: none;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 8px 22px 12px;
}

.set {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 92px;
}

.set span {
  color: var(--muted);
  font-size: 11px;
}

.set input {
  width: 88px;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 4px;
  padding: 4px 6px;
  font: 13px/1.2 ui-monospace, "SF Mono", Menlo, monospace;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  padding: 0 22px 10px;
}

.stats {
  display: flex;
  gap: 14px;
  color: var(--muted);
}

.stats b { color: var(--ink); font-weight: 650; }
.stats .up { color: var(--up-ink); }
.stats .down { color: var(--down-ink); }

.tools {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

#q {
  width: 180px;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 4px;
  padding: 6px 8px;
  font: inherit;
}

.filters { display: flex; gap: 4px; }

.filters button,
.check {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 4px;
  padding: 5px 8px;
  font: inherit;
  cursor: pointer;
}

.filters button.on {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.check { display: flex; gap: 6px; align-items: center; }

.table-wrap {
  margin: 0 12px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  overflow: auto;
  max-height: calc(100vh - 96px);
}

table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  table-layout: fixed;
}

th, td {
  padding: 4px 5px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  text-align: right;
  vertical-align: middle;
}

th {
  position: sticky;
  top: 0;
  z-index: 2;
  height: 1px;
  background: var(--head);
  font-size: 11px;
  font-weight: 650;
  color: var(--muted);
  text-align: right;
  cursor: pointer;
  user-select: none;
  white-space: normal;
  line-height: 1.2;
  vertical-align: bottom;
  padding: 0;
}

.th-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  gap: 3px;
  box-sizing: border-box;
  height: 100%;
  padding: 6px 3px 5px;
}

.th-label {
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  width: 100%;
  min-height: 2.4em;
  text-align: right;
  text-wrap: balance;
}

.th-label { font-size: var(--label-size, 11px); }

.th-label > span {
  min-width: 0;
  max-width: 100%;
  overflow-wrap: normal;
  hyphens: manual;
}

.th-top {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  width: 100%;
  min-height: 15px;
}
th.geo .th-top { justify-content: flex-start; }

.th-tag { color: var(--muted); font-size: 10px; font-weight: 500; }

.th-total {
  width: 100%;
  color: var(--ink);
  font-size: 10.5px;
  font-weight: 650;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  text-align: right;
}
.th-total.neg { color: var(--down-ink); }
#grid.tight .th-box { padding-left: 2px; padding-right: 2px; }
#grid.tight .th-total { font-size: 9px; white-space: normal; }
#grid.tight .th-top { flex-wrap: wrap; row-gap: 2px; }
th.geo .th-total { text-align: left; font-size: 11px; }

th.geo .th-box { align-items: flex-start; }
th.geo .th-label { text-align: left; justify-content: flex-start; }

th.geo, td.geo {
  text-align: left;
  position: sticky;
  left: 0;
  z-index: 1;
  width: 148px;
  background: var(--paper);
  box-shadow: 1px 0 0 var(--line);
}

td.geo {
  overflow: hidden;
  text-overflow: ellipsis;
}

th.geo { z-index: 3; background: var(--head); cursor: pointer; max-width: none; }

.geo .name { font-weight: 600; }
.geo .code { color: var(--muted); margin-left: 6px; font-size: 11px; }
.flag {
  width: 20px;
  height: 14px;
  margin-right: 8px;
  vertical-align: -2px;
  object-fit: cover;
}

td.num, th.num { font-variant-numeric: tabular-nums; }
td.num {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
}
td.num.gap { white-space: normal; line-height: 1.2; }

td input {
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
  border: 0;
  background: transparent;
  color: inherit;
  font: 11px/1.2 ui-monospace, "SF Mono", Menlo, monospace;
  text-align: right;
  padding: 2px 0;
}

td input:focus {
  outline: 1px solid var(--focus);
  background: #fff;
  border-radius: 2px;
}

td input.wide { width: 64px; }
td.band-green { color: var(--up-ink); font-weight: 650; }
td.band-orange { color: #d97706; font-weight: 650; }
td.band-red { color: #dc2626; font-weight: 650; }
td.band-blue { color: #1d4ed8; font-weight: 650; }
td.time-idle { color: #b3aca2; }
td.low { color: var(--low); }
td.neg { color: var(--down-ink); }

.price-up {
  color: var(--up-ink);
  font-weight: 650;
}

.price-down {
  color: var(--down-ink);
  font-weight: 650;
}

td.above-yes { color: var(--up-ink); font-weight: 650; }
td.above-no { color: var(--muted); }

.cols { position: relative; }

.cols-btn {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 4px;
  padding: 5px 8px;
  font: inherit;
  cursor: pointer;
}
.cols-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 30;
  width: 240px;
  padding: 6px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 12px;
  box-shadow: 0 8px 24px rgba(28, 25, 21, 0.12);
}

.cols-list {
  max-height: min(60vh, 460px);
  overflow: auto;
}

.cols-item {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 4px 12px;
  cursor: pointer;
}
.cols-item:hover { background: var(--bg); }
.cols-item.detail { color: var(--muted); }
.cols-item.locked { color: var(--muted); cursor: default; }
.cols-item.locked:hover { background: none; }

.cols-actions {
  display: flex;
  gap: 6px;
  padding: 8px 12px 2px;
  margin-top: 4px;
  border-top: 1px solid var(--line);
}

.cols-actions button {
  flex: 1;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 4px;
  padding: 4px 6px;
  font: inherit;
  cursor: pointer;
}
.cols-actions button:hover { border-color: var(--ink); }

th .info {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  margin: 0;
  padding: 0;
  border: 1px solid #c9c0b4;
  border-radius: 50%;
  background: #fff;
  color: #8a8176;
  font: 600 9px/1 system-ui, sans-serif;
  cursor: pointer;
}
th .info:hover { color: var(--ink); border-color: var(--ink); }

.info-pop {
  position: fixed;
  z-index: 30;
  width: 280px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 12px;
  font-weight: 450;
  line-height: 1.4;
  text-align: left;
  box-shadow: 0 8px 24px rgba(28, 25, 21, 0.12);
}
th {
  border-right: 1px solid #e0d8cc;
  border-bottom: 1px solid #d9cfc1;
}
th:last-child { border-right: 0; }

td.blk-buy, td.blk-comp { background: #fffefc; }
td.blk-shop, td.blk-profit { background: #fbf8f3; }
td.col-detail { background: #f5efe5; }
td.blk-rec { background: #fdf7ec; padding-right: 10px; }
#grid tr > th.blk-rec,
#grid tr > td.blk-rec { box-shadow: inset 2px 0 0 #c9baa4; }
td .qty { color: var(--muted); font-weight: 300; }

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

.login-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  margin: 0;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  padding: 28px 26px 24px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--paper);
  box-shadow: 0 12px 32px rgba(28, 25, 21, 0.06);
}

.login-card h1 { margin: 0; font-size: 20px; }
.login-sub { margin: 4px 0 20px; color: var(--muted); font-size: 13px; }

.login-field { display: block; margin-bottom: 14px; }
.login-field span { display: block; margin-bottom: 5px; color: var(--muted); font-size: 12px; font-weight: 600; }
.login-field input {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
}
.login-field input:focus { outline: none; border-color: var(--ink); }

.login-error { margin: 0 0 12px; color: var(--down-ink); font-size: 13px; }

.login-btn {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--ink);
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.login-btn:disabled { opacity: 0.6; cursor: default; }

.logout-btn {
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--muted);
  border-radius: 4px;
  padding: 5px 8px;
  font: inherit;
  cursor: pointer;
}
.logout-btn:hover { color: var(--ink); border-color: var(--ink); }

@media (max-width: 800px) {
  .top, .settings, .toolbar, .table-wrap { margin-left: 10px; margin-right: 10px; }
  .top, .settings, .toolbar { padding-left: 10px; padding-right: 10px; }
  .table-wrap { margin-left: 10px; margin-right: 10px; max-height: none; }
}
