:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --surface: var(--tg-theme-section-bg-color, #ffffff);
  --surface-soft: var(--tg-theme-secondary-bg-color, #f4f6f9);
  --text: var(--tg-theme-text-color, #2f3034);
  --muted: var(--tg-theme-hint-color, #777b82);
  --line: #e2e4e8;
  --line-strong: #d3d6dc;
  --accent: var(--tg-theme-button-color, #0d438c);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --gain: #d84b56;
  --loss: #176bc4;
  --neutral: #5b616a;
  --radius: 4px;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans KR",
    system-ui, sans-serif;
  letter-spacing: 0;
}

button,
select {
  font: inherit;
}

.app-shell {
  width: min(100%, 880px);
  margin: 0 auto;
  padding-bottom: calc(28px + env(safe-area-inset-bottom));
  background: var(--surface);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 82px;
  padding: calc(20px + env(safe-area-inset-top)) 18px 10px;
}

h1,
h2 {
  margin: 0;
  line-height: 1.15;
  font-weight: 750;
}

h1 {
  color: var(--accent);
  font-size: 28px;
}

h2 {
  color: #36383d;
  font-size: 21px;
}

.status-pill {
  min-height: 30px;
  padding: 7px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.status-pill.ok {
  border-color: rgba(13, 67, 140, 0.22);
  color: var(--accent);
}

.status-pill.error {
  border-color: rgba(216, 75, 86, 0.28);
  color: var(--gain);
}

.tabbar {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  min-height: 54px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.tabbar a {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  color: #383a3f;
  text-decoration: none;
  font-size: 18px;
  font-weight: 650;
  white-space: nowrap;
}

.tabbar a.active {
  color: var(--accent);
}

.tabbar a.active::after {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: -1px;
  height: 3px;
  background: var(--accent);
  content: "";
}

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  padding: 7px 18px;
  border-top: 1px solid var(--line);
  background: #f2f2f4;
  overflow: hidden;
}

.notice strong {
  color: #0670cf;
  font-size: 16px;
  white-space: nowrap;
}

.notice span {
  min-width: 0;
  overflow: hidden;
  color: #3f4248;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profit-selector {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 10px;
  padding: 24px 18px 18px;
  border-bottom: 1px solid var(--line);
}

.profit-selector label,
.scope-filters label {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.profit-selector span,
.scope-filters span,
.filters span {
  color: var(--muted);
}

.profit-selector label > span,
.scope-filters label > span {
  font-size: 11px;
  font-weight: 750;
}

.profit-selector select {
  height: 34px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: #303237;
  outline: none;
}

.return-type-control select {
  max-width: 100%;
  font-size: 21px;
  font-weight: 800;
}

.profit-selector label:not(.return-type-control) select {
  width: 126px;
  color: #45474d;
  font-size: 18px;
  text-align: right;
}

.scope-filters {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding: 12px 18px 14px;
  background: var(--surface-soft);
}

.scope-filters select {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: #fff;
  color: #303237;
  padding: 0 9px;
  outline: none;
}

.page[hidden],
.notice[hidden],
.profit-selector[hidden],
.scope-filters[hidden],
.empty-state[hidden] {
  display: none;
}

.focus-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-height: 92px;
  padding: 19px 18px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.focus-metrics div {
  min-width: 0;
  display: grid;
  align-content: center;
  gap: 6px;
}

.focus-metrics div + div {
  padding-left: 26px;
  border-left: 1px solid var(--line);
}

.focus-metrics span {
  color: #33363b;
  font-size: 17px;
}

.focus-metrics strong {
  min-width: 0;
  overflow-wrap: anywhere;
  color: #44474d;
  font-size: 25px;
  font-weight: 500;
  line-height: 1.1;
}

.focus-metrics strong.gain {
  color: var(--gain);
}

.focus-metrics strong.loss {
  color: var(--loss);
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0;
  border-bottom: 8px solid var(--surface-soft);
}

.metric-card {
  min-height: 74px;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
}

.metric-card:nth-child(odd) {
  border-right: 1px solid var(--line);
}

.metric-card span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.metric-card strong {
  min-width: 0;
  overflow-wrap: anywhere;
  color: #3f4248;
  font-size: 19px;
  font-weight: 550;
  line-height: 1.1;
}

.gain {
  color: var(--gain) !important;
}

.loss {
  color: var(--loss) !important;
}

.chart-panel,
.list-panel,
.table-panel,
.empty-state {
  background: #fff;
}

.chart-panel,
.list-panel,
.table-panel {
  padding: 24px 18px 18px;
  border-bottom: 8px solid var(--surface-soft);
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 18px;
}

.panel-title span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 22px;
  border: 1px solid var(--line-strong);
}

.segmented button {
  min-width: 0;
  min-height: 43px;
  border: 0;
  border-right: 1px solid var(--line-strong);
  background: #fff;
  color: #70747b;
  font-size: 16px;
  font-weight: 750;
}

.segmented button:last-child {
  border-right: 0;
}

.segmented button.active {
  box-shadow: inset 0 0 0 2px var(--accent);
  color: var(--accent);
}

.chart-unit {
  margin: -6px 0 5px;
  color: #9a9da3;
  font-size: 13px;
  font-weight: 650;
  text-align: right;
}

.chart {
  width: 100%;
  height: 260px;
  overflow: hidden;
}

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

.axis-label {
  fill: #50535a;
  font-size: 11px;
  font-weight: 600;
}

.line-return,
.line-profit {
  fill: none;
  stroke: var(--gain);
  stroke-width: 2.5;
}

.area-return {
  fill: url(#returnGradient);
}

.grid-line {
  stroke: #dedfe3;
  stroke-dasharray: 4 5;
  stroke-width: 1;
}

.axis-line {
  stroke: #d3d5db;
  stroke-width: 1;
}

.bar-gain {
  fill: var(--gain);
}

.bar-loss {
  fill: var(--loss);
}

.row-list {
  display: grid;
  gap: 0;
  border-top: 1px solid var(--line);
}

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

.list-row strong {
  display: block;
  margin-bottom: 4px;
  font-size: 16px;
  font-weight: 700;
}

.list-row span,
.settings-grid span {
  color: var(--muted);
  font-size: 13px;
}

.badge {
  align-self: start;
  padding: 4px 8px;
  border: 1px solid rgba(13, 67, 140, 0.22);
  border-radius: 999px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
}

.table-wrap {
  overflow-x: auto;
  border-top: 1px solid var(--line);
}

table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
}

th,
td {
  padding: 13px 8px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  font-size: 14px;
  white-space: nowrap;
}

th:first-child,
td:first-child,
th:nth-child(2),
td:nth-child(2) {
  text-align: left;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.settings-grid {
  display: grid;
  border-top: 1px solid var(--line);
}

.settings-grid div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  border-bottom: 1px solid var(--line);
}

.empty-state {
  margin: 18px;
  padding: 22px 12px;
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  text-align: center;
}

.empty-state span {
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 390px) {
  h1 {
    font-size: 25px;
  }

  .tabbar a {
    font-size: 15px;
  }

  .return-type-control select {
    font-size: 18px;
  }

  .profit-selector label:not(.return-type-control) select {
    width: 110px;
    font-size: 16px;
  }

  .segmented button {
    font-size: 14px;
  }
}

@media (min-width: 680px) {
  .app-shell {
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }

  .summary-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .metric-card {
    border-right: 1px solid var(--line);
  }

  .metric-card:nth-child(4n) {
    border-right: 0;
  }

  .chart {
    height: 300px;
  }
}
