:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --panel: #ffffff;
  --ink: #172033;
  --muted: #627084;
  --line: #d8e0ea;
  --blue: #2563eb;
  --green: #0f9f6e;
  --red: #d14343;
  --amber: #b76b00;
  --shadow: 0 8px 24px rgba(23, 32, 51, 0.08);
}

* {
  box-sizing: border-box;
}

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 22px 28px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.1;
}

h2 {
  font-size: 17px;
}

.topbar p,
.section-title span,
label span {
  color: var(--muted);
  font-size: 13px;
}

.state {
  min-width: 88px;
  padding: 8px 12px;
  border-radius: 8px;
  text-align: center;
  font-weight: 700;
  border: 1px solid var(--line);
}

.state.idle {
  color: var(--green);
  background: #ecfdf5;
}

.state.running {
  color: var(--blue);
  background: #eff6ff;
}

.state.busy,
.state.error {
  color: var(--red);
  background: #fff1f1;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 0.9fr) minmax(520px, 1.4fr);
  gap: 18px;
  padding: 18px;
}

.panel,
.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.control-panel,
.chart-panel,
.records-panel {
  padding: 18px;
}

.control-panel {
  min-width: 0;
}

.form {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

label {
  display: grid;
  gap: 6px;
}

fieldset {
  margin: 0;
  padding: 0;
  border: 0;
}

input {
  width: 100%;
  min-height: 40px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fbfdff;
  font: inherit;
}

input:focus {
  outline: 2px solid rgba(37, 99, 235, 0.2);
  border-color: var(--blue);
}

.choice-field {
  display: grid;
  gap: 6px;
}

.choice-field > span {
  color: var(--muted);
  font-size: 13px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  min-height: 40px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
}

.segmented-control label {
  position: relative;
  display: block;
  min-width: 0;
}

.segmented-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented-control span {
  display: grid;
  min-height: 32px;
  place-items: center;
  border-radius: 6px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.segmented-control input:checked + span {
  color: #ffffff;
  background: var(--blue);
}

.segmented-control input:focus-visible + span {
  outline: 2px solid rgba(37, 99, 235, 0.3);
  outline-offset: 1px;
}

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

button {
  min-height: 40px;
  padding: 9px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: #ffffff;
  background: var(--blue);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.action-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

.stop-button {
  color: var(--red);
  background: #fff1f1;
  border-color: #f5bcbc;
}

[hidden] {
  display: none !important;
}

#refreshButton {
  min-height: 34px;
  color: var(--blue);
  background: #eff6ff;
  border-color: #bfdbfe;
}

.secondary-button {
  min-height: 34px;
  padding: 7px 12px;
  color: var(--blue);
  background: #eff6ff;
  border-color: #bfdbfe;
}

.command-panel {
  display: grid;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
}

.command-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.command-title span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.command-text {
  display: block;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 38px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  word-break: break-all;
}

.notice {
  min-height: 22px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.notice.error {
  color: var(--red);
}

.metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-self: stretch;
  gap: 12px;
}

.metric {
  padding: 14px;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 24px;
}

.limit-metric {
  grid-column: 1 / -1;
}

.chart-panel {
  min-width: 0;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

canvas {
  display: block;
  width: 100%;
  height: 280px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdff;
}

.event-log {
  margin-top: 12px;
  max-height: 120px;
  overflow: auto;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.records-panel {
  grid-column: 1 / -1;
}

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

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 980px;
  background: #ffffff;
}

th,
td {
  padding: 11px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
  font-size: 13px;
}

th {
  color: var(--muted);
  background: #f8fafc;
  font-weight: 700;
}

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

.score-good {
  color: var(--green);
  font-weight: 700;
}

.score-mid {
  color: var(--amber);
  font-weight: 700;
}

.score-bad {
  color: var(--red);
  font-weight: 700;
}

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

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

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

@media (max-width: 560px) {
  body {
    background: #eef3f8;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
    padding: 14px;
  }

  h1 {
    font-size: 24px;
  }

  h2 {
    font-size: 16px;
  }

  .state {
    width: 100%;
    min-width: 0;
  }

  .layout {
    gap: 12px;
    padding: 10px;
  }

  .control-panel,
  .chart-panel,
  .records-panel {
    padding: 14px;
  }

  input,
  button {
    min-height: 44px;
  }

  .segmented-control {
    min-height: 44px;
  }

  .segmented-control span {
    min-height: 36px;
  }

  .command-title {
    display: grid;
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .command-title .secondary-button {
    width: 100%;
  }

  .metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .metric {
    padding: 12px;
  }

  .metric strong {
    font-size: 21px;
    overflow-wrap: anywhere;
  }

  .section-title {
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
  }

  canvas {
    height: 220px;
  }

  .event-log {
    max-height: 96px;
    overflow-wrap: anywhere;
  }

  .table-wrap {
    overflow: visible;
    border: 0;
  }

  table,
  thead,
  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  table {
    min-width: 0;
    background: transparent;
  }

  thead {
    display: none;
  }

  tr {
    margin-bottom: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #ffffff;
    overflow: hidden;
  }

  td {
    display: grid;
    grid-template-columns: minmax(92px, 38%) minmax(0, 1fr);
    gap: 10px;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    overflow-wrap: anywhere;
  }

  td::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 700;
  }

  .empty-row td {
    display: block;
    text-align: center;
  }

  .empty-row td::before {
    content: "";
  }
}

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

  td {
    grid-template-columns: minmax(82px, 34%) minmax(0, 1fr);
  }
}
