:root {
  --paper: #f3efe7;
  --ink: #161916;
  --muted: #6d6c65;
  --line: #d8d1c5;
  --panel: #fbf8f2;
  --red: #c53d2d;
  --green: #137352;
  --yellow: #e2b13c;
  --blue: #2f67a3;
  --shadow: 0 18px 60px rgba(52, 41, 28, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(rgba(22, 25, 22, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(22, 25, 22, 0.035) 1px, transparent 1px),
    var(--paper);
  background-size: 28px 28px;
  font-family: "Yu Gothic UI", "Hiragino Kaku Gothic ProN", sans-serif;
}

body.detail-open { overflow: hidden; }

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-box {
  width: min(430px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 34px;
}

.brand-mark {
  width: 62px;
  height: 62px;
  display: grid;
  place-items: center;
  color: white;
  background: var(--red);
  font-family: Georgia, serif;
  font-weight: 800;
  font-size: 1.5rem;
  margin-bottom: 24px;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--red);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.login-box h1, .topbar h1 {
  margin: 0;
  font-family: Georgia, "Yu Mincho", serif;
  letter-spacing: 0;
}

.login-box h1 {
  font-size: clamp(2.4rem, 8vw, 4.6rem);
  line-height: 0.88;
  margin-bottom: 32px;
}

label {
  display: grid;
  gap: 7px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
}

input, select, textarea {
  width: 100%;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 2px;
  padding: 12px 13px;
  outline: none;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--ink);
}

.login-box button, .settings button {
  border: 0;
  background: var(--ink);
  color: white;
  padding: 13px 17px;
  font-weight: 800;
}

.login-box button {
  width: 100%;
  margin-top: 16px;
}

.error { color: var(--red); min-height: 1.3em; }

.app {
  width: min(1440px, calc(100vw - 32px));
  margin: 0 auto 50px;
}

.topbar {
  min-height: 116px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid var(--ink);
}

.topbar h1 { font-size: clamp(1.5rem, 3vw, 2.9rem); }

.topbar-meta, .settings-actions, .settings-head, .chart-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mode-badge {
  background: var(--yellow);
  color: var(--ink);
  padding: 7px 10px;
  font-size: 0.7rem;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 1px solid var(--ink);
  background: transparent;
  color: var(--ink);
  font-size: 1.35rem;
}

.main-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 180px));
  width: fit-content;
  border: 1px solid var(--ink);
  margin-top: 22px;
}

.main-tab {
  min-height: 46px;
  padding: 0 18px;
  border: 0;
  border-right: 1px solid var(--ink);
  background: transparent;
  color: var(--ink);
  font-weight: 900;
}

.main-tab:last-child { border-right: 0; }
.main-tab.active { color: white; background: var(--ink); }

.view-panel {
  margin-top: 18px;
}

.ranking-switch {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 180px));
  width: fit-content;
  border: 1px solid var(--ink);
  margin-top: 0;
}

.ranking-button {
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-right: 1px solid var(--ink);
  background: transparent;
  color: var(--ink);
  font-weight: 900;
}

.ranking-button:last-child { border-right: 0; }
.ranking-button.active { color: white; background: var(--ink); }
.ranking-up:not(.active) { color: var(--green); }
.ranking-down:not(.active) { color: var(--red); }

.controls {
  display: grid;
  grid-template-columns: auto minmax(260px, 1fr) 170px 150px 170px auto;
  align-items: end;
  gap: 14px;
  padding: 22px 0;
}

.period-switch {
  display: grid;
  grid-template-columns: repeat(3, 68px);
  border: 1px solid var(--ink);
}

.period {
  border: 0;
  border-right: 1px solid var(--ink);
  background: transparent;
  min-height: 43px;
  font-weight: 900;
}

.period:last-child { border-right: 0; }
.period.active { background: var(--ink); color: white; }

.toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 43px;
  white-space: nowrap;
}

.toggle-label input { width: 18px; height: 18px; }

.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  margin-bottom: 18px;
}

.summary-strip > div {
  padding: 15px 17px;
  background: rgba(251, 248, 242, 0.72);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.summary-strip span { color: var(--muted); font-size: 0.75rem; display: block; }
.summary-strip strong { font-family: Georgia, serif; font-size: 1.5rem; }

.portfolio-shell {
  background: rgba(251, 248, 242, 0.78);
  border: 1px solid var(--line);
  margin-bottom: 18px;
  padding: 18px;
}

.portfolio-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 14px;
}

.portfolio-head h2 {
  margin: 0;
  font-family: Georgia, "Yu Mincho", serif;
  font-size: clamp(1.5rem, 3vw, 2.3rem);
}

.portfolio-value {
  min-width: 190px;
  text-align: right;
}

.portfolio-value span,
.portfolio-meta,
.holding-row small {
  color: var(--muted);
  font-size: .75rem;
  font-weight: 700;
}

.portfolio-value strong {
  display: block;
  font-family: Georgia, serif;
  font-size: clamp(1.3rem, 3vw, 2rem);
}

.portfolio-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(300px, .9fr);
  gap: 16px;
}

.portfolio-chart {
  min-height: 230px;
  background: #fffdfa;
  border: 1px solid var(--line);
  overflow: hidden;
}

.portfolio-chart svg { width: 100%; height: 100%; display: block; }

.portfolio-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.portfolio-list {
  display: grid;
  gap: 8px;
  max-height: 255px;
  overflow: auto;
}

.holding-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 36px;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: #fffdfa;
  border: 1px solid var(--line);
}

.holding-row strong,
.holding-row small {
  display: block;
}

.holding-row strong {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.holding-row span {
  display: block;
  text-align: right;
  font-family: Georgia, serif;
  font-weight: 800;
}

.add-owned,
.remove-owned {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: white;
  font-weight: 900;
}

.add-owned {
  min-height: 38px;
  padding: 0 12px;
}

.add-owned:disabled {
  cursor: not-allowed;
  opacity: .35;
}

.remove-owned {
  width: 34px;
  height: 34px;
  font-size: 1.2rem;
  line-height: 1;
}

.empty-state.compact {
  padding: 22px;
}

.table-shell {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.table-header, .card-row {
  display: grid;
  grid-template-columns: minmax(280px, 2.2fr) minmax(120px, .8fr) minmax(170px, 1fr) minmax(120px, .8fr) minmax(150px, .8fr) 72px 48px;
  align-items: center;
  gap: 12px;
}

.table-header {
  min-height: 44px;
  padding: 0 16px;
  background: var(--ink);
  color: white;
  font-size: 0.72rem;
  font-weight: 800;
}

.card-row {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  max-width: none;
  min-height: 105px;
  padding: 12px 16px;
  color: inherit;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 120ms ease;
}

.card-row:hover, .card-row:focus-visible, .card-row.selected { background: #ede5d9; }

.product-cell {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 13px;
  align-items: center;
  min-width: 0;
}

.product-cell img, .detail-identity img {
  object-fit: contain;
  background: #e7e0d5;
  border: 1px solid var(--line);
}

.product-cell img { width: 58px; height: 78px; }
.product-cell strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.product-cell small { color: var(--muted); }

.price, .delta {
  font-family: Georgia, "Yu Mincho", serif;
  font-weight: 700;
  font-size: 1.1rem;
}

.price-values {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.price .minimum-price {
  color: var(--red);
  font-family: "Yu Gothic UI", sans-serif;
  font-size: 0.72rem;
  font-weight: 800;
  white-space: nowrap;
}

.price small {
  display: block;
  color: var(--muted);
  font-family: "Yu Gothic UI", sans-serif;
  font-size: 0.7rem;
  font-weight: 700;
}

.verify-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 7px;
  font-family: "Yu Gothic UI", sans-serif;
  font-size: 0.72rem;
}

.verify-links a {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 8px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #fffdfa;
  text-decoration: none;
  font-weight: 800;
}

.verify-links a:hover,
.verify-links a:focus-visible {
  border-color: var(--ink);
}

.external-refs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 5px;
  font-size: .74rem;
  color: var(--red);
}

.external-refs a {
  color: var(--red);
  text-decoration: none;
}

.detail-verify {
  justify-content: center;
  margin-top: 12px;
}

.up { color: var(--green); }
.down { color: var(--red); }

.delta small {
  display: block;
  color: inherit;
  font-family: "Yu Gothic UI", sans-serif;
  font-size: 0.74rem;
}

.status {
  display: inline-flex;
  width: fit-content;
  padding: 5px 8px;
  background: #e8e3da;
  font-size: 0.72rem;
  font-weight: 800;
}

.status.low { color: #6a4b00; background: #f5df9f; }
.status.pending { color: #555; background: #e7e4dd; }
.row-open {
  font-size: 1.8rem;
  line-height: 1;
  text-align: center;
}

.empty-state { padding: 50px; text-align: center; color: var(--muted); }

.load-more {
  width: 100%;
  min-height: 48px;
  border: 0;
  border-top: 1px solid var(--line);
  background: #e8e3da;
  color: var(--ink);
  font-weight: 900;
}

.load-more:hover, .load-more:focus-visible { background: #ddd4c7; }

.detail {
  margin-top: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 24px;
}

.detail-head {
  display: flex;
  justify-content: space-between;
  align-items: start;
  border-bottom: 1px solid var(--line);
  padding-bottom: 20px;
}

.detail-identity {
  display: flex;
  gap: 18px;
  align-items: center;
}

.detail-identity img { width: 82px; height: 112px; }
.detail h2 { margin: 0; font-family: Georgia, "Yu Mincho", serif; font-size: clamp(1.7rem, 4vw, 3rem); }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-left: 1px solid var(--line);
  margin: 22px 0;
}

.metric { border: 1px solid var(--line); border-left: 0; padding: 16px; }
.metric span { color: var(--muted); font-size: .75rem; display: block; }
.metric strong { font-family: Georgia, serif; font-size: clamp(1.2rem, 2vw, 2rem); }
.metric .metric-minimum {
  display: block;
  margin-top: 5px;
  color: var(--red);
  font-size: 0.78rem;
  font-weight: 800;
}

.quality-badge {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  padding: 5px 8px;
  background: #e7e4dd;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 800;
}

.quality-badge.high { color: var(--green); background: #dfece5; }
.quality-badge.medium { color: #6a4b00; background: #f5df9f; }
.quality-badge.low { color: var(--red); background: #f1d7d1; }

.chart-section {
  border: 1px solid var(--line);
  padding: 18px;
}

.chart-toolbar { justify-content: space-between; flex-wrap: wrap; align-items: flex-start; }
.chart-toolbar h3, .detail-lower h3 { margin: 0; }
.chart-caption { margin: 5px 0 0; color: var(--muted); font-size: .75rem; }
.chart-mode {
  display: grid;
  grid-template-columns: repeat(2, 76px);
  border: 1px solid var(--ink);
}
.chart-mode-button {
  min-height: 35px;
  border: 0;
  border-right: 1px solid var(--ink);
  color: var(--ink);
  background: transparent;
  font-size: .76rem;
  font-weight: 800;
}
.chart-mode-button:last-child { border-right: 0; }
.chart-mode-button.active { color: white; background: var(--ink); }
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 16px;
  font-size: .72rem;
}
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #fff;
  padding: 6px 9px;
  font-weight: 700;
}
.legend-item.inactive { opacity: .35; }
.legend-dot { width: 9px; height: 9px; border-radius: 50%; }
.chart {
  height: 390px;
  margin-top: 10px;
  overflow: hidden;
  background: #fffdfa;
  border-top: 1px solid var(--line);
}
.chart svg { width: 100%; height: 100%; display: block; }

.detail-lower {
  display: grid;
  grid-template-columns: 1.2fr .8fr;
  gap: 20px;
  margin-top: 20px;
}

.condition-table-wrap, .settings {
  border: 1px solid var(--line);
  padding: 18px;
}

.condition-row {
  display: grid;
  grid-template-columns: 1fr 1fr .8fr;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
}

.condition-row:last-child { border-bottom: 0; }
.evidence-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.listing-evidence {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.evidence-row {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  padding: 9px;
  background: #fffdfa;
  border: 1px solid var(--line);
}

.evidence-row span {
  font-family: Georgia, serif;
  font-weight: 800;
}

.evidence-row small {
  color: var(--muted);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.evidence-row:hover,
.evidence-row:focus-visible {
  border-color: var(--ink);
}
.settings { display: grid; gap: 14px; }
.settings-head { justify-content: space-between; }
.settings-actions { align-items: stretch; }
.settings-actions button { flex: 1; }
.settings .secondary { color: var(--ink); background: #ddd4c7; }

.auto-query-box {
  display: grid;
  gap: 8px;
}

.auto-query-box > span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.auto-query-box div {
  display: grid;
  gap: 6px;
}

.auto-query-box code {
  display: block;
  padding: 8px 9px;
  overflow-wrap: anywhere;
  color: var(--ink);
  background: #fffdfa;
  border: 1px solid var(--line);
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.72rem;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  color: white;
  background: var(--ink);
  padding: 13px 17px;
  box-shadow: var(--shadow);
}

@media (max-width: 900px) {
  .app { width: min(100% - 20px, 720px); }
  .topbar { align-items: flex-end; padding: 24px 0 18px; }
  .controls { grid-template-columns: 1fr 1fr; }
  .search { grid-column: 1 / -1; }
  .summary-strip { grid-template-columns: repeat(2, 1fr); }
  .portfolio-grid { grid-template-columns: 1fr; }
  .table-header { display: none; }
  .card-row {
    grid-template-columns: 1fr 78px auto;
    gap: 8px 14px;
    padding: 14px;
  }
  .product-cell { grid-column: 1 / -1; }
  .card-row > :nth-child(5) { grid-column: 1 / 2; }
  .add-owned { grid-column: 2; grid-row: 2 / 5; align-self: center; }
  .row-open { grid-column: 3; grid-row: 2 / 5; align-self: center; }
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .detail-lower { grid-template-columns: 1fr; }
  .chart { height: 300px; }
  .chart-mode { grid-template-columns: repeat(2, 68px); }
}

@media (max-width: 520px) {
  .topbar h1 { font-size: 1.35rem; }
  .mode-badge { display: none; }
  .controls { grid-template-columns: 1fr; }
  .search { grid-column: auto; }
  .period-switch { grid-template-columns: repeat(3, 1fr); }
  .summary-strip { grid-template-columns: 1fr 1fr; }
  .portfolio-head { display: grid; }
  .portfolio-value { text-align: left; }
  .holding-row { grid-template-columns: minmax(0, 1fr) auto 34px; }
  .portfolio-chart { min-height: 210px; }
  .main-tabs {
    grid-template-columns: repeat(2, 1fr);
    width: 100%;
    margin-top: 16px;
  }
  .main-tab {
    padding: 0 8px;
    font-size: 0.82rem;
  }
  .ranking-switch {
    grid-template-columns: repeat(3, 1fr);
    width: 100%;
    margin-top: 16px;
  }
  .ranking-button {
    padding: 0 6px;
    font-size: 0.78rem;
  }
  .table-shell, .card-list, .card-row { width: 100%; }
  .detail {
    position: fixed;
    inset: 8px;
    z-index: 50;
    max-height: calc(100dvh - 16px);
    margin: 0;
    padding: 14px;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: 0 12px 40px rgba(26, 23, 19, 0.35);
  }
  .detail-head {
    position: sticky;
    top: -14px;
    z-index: 2;
    background: var(--panel);
    padding-top: 14px;
  }
  .detail-identity img { width: 64px; height: 88px; }
  .metric-grid { grid-template-columns: 1fr 1fr; }
  .metric { padding: 12px; }
  .chart-section { padding: 10px; }
  .chart { height: 250px; }
  .legend { gap: 5px; }
  .legend-item { padding: 5px 7px; }
  .settings-actions { flex-direction: column; }
}
