:root {
  --bg: var(--tg-theme-bg-color, #f6f8fa);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-soft: var(--tg-theme-secondary-bg-color, #f9fbfc);
  --border: #d9e2e8;
  --text: var(--tg-theme-text-color, #14212b);
  --muted: var(--tg-theme-hint-color, #657480);
  --teal: #087f8c;
  --teal-soft: #e4f4f5;
  --coral: #f15b4f;
  --amber: #d79200;
  --green: #2f8f67;
  --shadow: 0 12px 32px rgba(20, 33, 43, 0.07);
  --radius: 8px;
}

body {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.access-denied {
  min-height: 60vh;
  display: grid;
  place-items: center;
  text-align: center;
  gap: 10px;
  padding: 24px;
}

.access-denied h1 {
  font-size: 22px;
}

.access-denied p {
  color: var(--muted);
  max-width: 32ch;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
select {
  font: inherit;
}

.page {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 42px;
  display: grid;
  gap: 16px;
}

.header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  padding: 4px 0 8px;
}

.eyeline {
  margin: 0 0 8px;
  color: var(--teal);
  font-size: 13px;
  font-weight: 760;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  max-width: 620px;
  font-size: clamp(30px, 5vw, 52px);
  line-height: 1.02;
}

h2 {
  font-size: 18px;
  line-height: 1.2;
}

p {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 760;
}

select {
  min-height: 38px;
  min-width: 150px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}

.summary-grid {
  display: grid;
  grid-template-columns: 1.35fr 1fr 1fr;
  gap: 12px;
}

.phase-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 14px;
}

.phase-card {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.phase-card span,
.phase-card small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 760;
}

.phase-card strong {
  display: block;
  margin-top: 8px;
  font-size: 28px;
  line-height: 1;
}

.phase-card p {
  margin-top: 9px;
}

.phase-card small {
  display: block;
  margin-top: 9px;
  color: var(--teal);
}

.tape-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  padding: 14px;
}

.tape-card {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.tape-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 760;
}

.tape-card strong {
  display: block;
  margin-top: 8px;
  font-size: 27px;
  line-height: 1;
}

.tape-card p {
  margin-top: 9px;
}

.answer-card,
.panel,
.kpi-grid {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.answer-card {
  padding: 18px;
  min-height: 148px;
  display: grid;
  align-content: space-between;
  gap: 12px;
}

.answer-card.primary {
  background: var(--teal);
  border-color: var(--teal);
  color: white;
}

.answer-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 760;
}

.answer-card.primary span,
.answer-card.primary p {
  color: rgba(255, 255, 255, 0.82);
}

.answer-card strong {
  font-size: clamp(22px, 3vw, 34px);
  line-height: 1.05;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  overflow: hidden;
}

.kpi-card {
  min-height: 126px;
  padding: 16px;
  border-right: 1px solid var(--border);
}

.kpi-card:last-child {
  border-right: 0;
}

.kpi-label {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 760;
}

.metric-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.kpi-value {
  margin-top: 12px;
  font-size: 30px;
  line-height: 1;
  font-weight: 780;
}

.kpi-unit {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.kpi-change {
  margin-top: 10px;
  font-size: 13px;
  font-weight: 760;
}

.good {
  color: var(--green);
}

.bad {
  color: var(--coral);
}

.neutral {
  color: var(--muted);
}

.panel {
  overflow: hidden;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.panel-head p {
  margin-top: 5px;
}

.segmented {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.segmented button {
  min-height: 32px;
  padding: 0 11px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 760;
  cursor: pointer;
}

.segmented button.active {
  background: var(--teal);
  color: white;
}

.chart-wrap {
  padding: 12px 14px 16px;
  position: relative;
}

#trendChart {
  width: 100%;
  height: 360px;
  display: block;
  cursor: pointer;
  touch-action: manipulation;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: 0 8px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.legend i {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.legend em {
  font-style: normal;
  font-weight: 600;
  opacity: 0.72;
}

.legend .axis-tag {
  font-size: 13px;
  line-height: 1;
  opacity: 0.55;
}

.grid-line {
  stroke: #dce4ea;
  stroke-dasharray: 4 4;
}

.chart-label,
.chart-warning,
.chart-year,
.axis-tick,
.event-label {
  fill: var(--muted);
  font-size: 11px;
}

.chart-year {
  font-size: 10px;
  font-weight: 700;
  opacity: 0.7;
}

.axis-tick {
  font-size: 10px;
  font-weight: 700;
}

.chart-warning {
  fill: var(--amber);
  font-weight: 760;
}

.event-line {
  stroke: #6b5bd2;
  stroke-width: 1.5;
  stroke-dasharray: 2 4;
  opacity: 0.75;
}

.event-label {
  fill: #6b5bd2;
  font-size: 10px;
  font-weight: 760;
}

.select-line {
  stroke: var(--text);
  stroke-width: 1;
  opacity: 0.28;
}

.chart-tooltip {
  position: absolute;
  z-index: 4;
  transform: translate(-50%, -100%);
  min-width: 148px;
  max-width: 240px;
  padding: 9px 11px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  pointer-events: none;
  font-size: 12px;
}

.chart-tooltip.below {
  transform: translate(-50%, 0);
}

.chart-tooltip[hidden] {
  display: none;
}

.chart-tooltip strong {
  display: block;
  font-size: 13px;
  margin-bottom: 6px;
}

.chart-tooltip ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
}

.chart-tooltip li {
  display: flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
}

.chart-tooltip li i {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  flex: 0 0 auto;
}

.chart-tooltip li span {
  color: var(--muted);
}

.chart-tooltip li b {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

.chart-tooltip small {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
  font-size: 12px;
}

th,
td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  text-align: right;
  white-space: nowrap;
}

th:first-child,
td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  text-align: left;
}

th {
  color: var(--muted);
  font-weight: 760;
  background: var(--surface-soft);
}

.delta-good {
  color: var(--green);
  font-weight: 760;
}

.delta-bad {
  color: var(--coral);
  font-weight: 760;
}

.delta-neutral {
  color: var(--amber);
  font-weight: 760;
}

@media (max-width: 800px) {
  body {
    background: var(--surface);
  }

  .page {
    width: 100%;
    padding: 16px 14px 32px;
  }

  .header,
  .panel-head {
    display: grid;
  }

  h1 {
    font-size: 34px;
  }

  .controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    justify-content: stretch;
  }

  select {
    width: 100%;
    min-width: 0;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .phase-grid {
    grid-template-columns: 1fr;
    padding: 14px;
  }

  .tape-summary {
    grid-template-columns: 1fr 1fr;
    padding: 14px;
  }

  .answer-card {
    min-height: 126px;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin: 0;
    border-radius: var(--radius);
  }

  .kpi-card {
    min-height: 112px;
    padding: 14px;
    border-bottom: 0;
    border-right: 1px solid var(--border);
  }

  .kpi-card:nth-child(2n) {
    border-right: 0;
  }

  .kpi-card:nth-child(-n + 4) {
    border-bottom: 1px solid var(--border);
  }

  .kpi-value {
    font-size: 27px;
  }

  .panel {
    margin: 0 -14px;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .panel-head {
    padding: 15px 14px;
  }

  .segmented {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
  }

  #trendChart {
    height: 320px;
  }

  table {
    min-width: 650px;
  }
}

@media (max-width: 420px) {
  .controls {
    grid-template-columns: 1fr;
  }

  .tape-summary {
    grid-template-columns: 1fr;
  }
}
