:root {
  color-scheme: light;
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-soft: #f7f9fc;
  --surface-tint: #f1f5f9;
  --text: #172033;
  --text-strong: #0b1220;
  --muted: #627084;
  --line: #d8e0ea;
  --line-strong: #b8c5d6;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #eef4ff;
  --success: #087f5b;
  --success-soft: #e6f6ef;
  --danger: #b42318;
  --danger-soft: #fff0ee;
  --warning-soft: #fff7df;
  --shadow: 0 2px 8px rgba(15, 23, 42, .06);
  --focus: 0 0 0 3px rgba(37, 99, 235, .16);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

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

button,
input,
select {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 8px 14px;
  background: var(--brand);
  color: #fff;
  font-weight: 650;
  cursor: pointer;
  transition: background-color .16s ease, border-color .16s ease, box-shadow .16s ease, transform .16s ease;
}

button:hover {
  background: var(--brand-dark);
  box-shadow: 0 2px 8px rgba(37, 99, 235, .18);
}

button:active { transform: translateY(1px); }
button:focus-visible { outline: none; box-shadow: var(--focus); }

button.ghost {
  background: #fff;
  color: var(--text);
  border-color: var(--line);
}

button.ghost:hover {
  background: var(--surface-soft);
  border-color: var(--line-strong);
  box-shadow: none;
}

button.danger {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #f7c9c6;
}

button.danger:hover {
  background: #fbd9d6;
  box-shadow: none;
}

.compact-button { min-width: 76px; }
.primary-action { min-width: 112px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 12px 28px;
  background: rgba(247, 249, 252, .92);
  border-bottom: 1px solid rgba(184, 197, 214, .82);
  backdrop-filter: blur(10px);
}

.topbar form { width: auto; }

.brand-block {
  display: flex;
  align-items: baseline;
  gap: 14px;
  min-width: 0;
}

.brand-block strong {
  color: var(--text-strong);
  font-size: 15px;
  font-weight: 780;
}

.brand-block span {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  width: min(1320px, calc(100% - 36px));
  margin: 18px auto 38px;
}

.panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.span-2 { grid-column: span 2; }

.panel-head,
.section-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.section-title {
  display: block;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.eyebrow {
  margin: 0 0 2px;
  color: #475569;
  font-size: 11px;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: none;
}

h1,
h2,
h3 {
  margin: 0;
  color: var(--text-strong);
  letter-spacing: 0;
}

h1 { font-size: 23px; }
h2 { font-size: 17px; line-height: 1.28; }
h3 { font-size: 15px; }

.subhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 16px 0 9px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-tint);
}

.stats div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-left: 1px solid var(--line);
}

.stats div:first-child { border-left: 0; }
.stats span { color: var(--muted); font-weight: 650; }
.stats b { color: var(--text-strong); font-size: 24px; line-height: 1; }

.ratio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 7px 14px;
}

.ratio-grid div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  min-height: 32px;
  padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
}

.ratio-grid span {
  min-width: 0;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.ratio-grid b {
  flex: 0 0 auto;
  color: var(--text-strong);
  font-size: 14px;
}

.note {
  margin: 0;
  color: var(--muted);
}

.alert.error {
  margin: 0 0 12px;
  padding: 10px 12px;
  border: 1px solid #f4b7b1;
  border-radius: 8px;
  background: var(--danger-soft);
  color: #8f2119;
}

.alert.warning {
  margin: 0 0 10px;
  padding: 10px 12px;
  border: 1px solid #f0d58a;
  border-radius: 8px;
  background: var(--warning-soft);
  color: #76520a;
}

.mapping-warning {
  grid-column: 1 / -1;
}

form { margin: 0; }

.grid-form,
.mapping-form {
  display: grid;
  gap: 10px 12px;
}

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

.mapping-form {
  grid-template-columns: minmax(140px, 1.1fr) minmax(150px, 1fr) minmax(150px, 1fr) minmax(116px, .75fr) auto auto;
  align-items: end;
}

label {
  display: grid;
  gap: 5px;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color .16s ease, box-shadow .16s ease, background-color .16s ease;
}

input::placeholder { color: #9aa7b5; }

input:focus,
select:focus {
  border-color: var(--brand);
  box-shadow: var(--focus);
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  color: var(--text);
  font-weight: 650;
}

.check input {
  width: 16px;
  min-height: 16px;
  accent-color: var(--brand);
}

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

table {
  width: 100%;
  min-width: 1324px;
  table-layout: fixed;
  border-collapse: collapse;
  background: #fff;
}

th:nth-child(1),
td:nth-child(1) { width: 72px; }

th:nth-child(2),
td:nth-child(2) { width: 200px; }

th:nth-child(3),
td:nth-child(3) { width: 120px; }

th:nth-child(4),
td:nth-child(4) { width: 160px; }

th:nth-child(5),
td:nth-child(5) { width: 220px; }

th:nth-child(6),
td:nth-child(6) { width: 72px; white-space: nowrap; }

th:nth-child(7),
td:nth-child(7) { width: 132px; }

th:nth-child(8),
td:nth-child(8) {
  width: 64px;
  text-align: center;
  white-space: nowrap;
}

th:nth-child(9),
td:nth-child(9) { width: 284px; }

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

th {
  background: #f4f7fb;
  color: #475569;
  font-size: 12px;
  font-weight: 750;
  white-space: nowrap;
}

tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: #f9fbfe; }

.cell-title {
  display: block;
  margin-bottom: 6px;
  color: var(--text-strong);
  overflow-wrap: anywhere;
}

.cell-meta {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.mono {
  max-width: 380px;
  color: #334155;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  overflow-wrap: anywhere;
}

.actions {
  min-width: 0;
  white-space: nowrap;
}

.actions details,
.actions form {
  display: inline-block;
  width: auto;
  margin: 0 6px 7px 0;
  vertical-align: top;
}

.actions details:last-child,
.actions form:last-child {
  margin-right: 0;
}

.actions button {
  min-height: 34px;
  padding: 6px 9px;
  white-space: nowrap;
}

.inline-edit { min-width: 0; }

.inline-edit summary {
  min-height: 34px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  white-space: nowrap;
}

.inline-edit summary::-webkit-details-marker { display: none; }

.inline-edit[open] summary {
  border-color: #bad6ff;
  background: #f2f7ff;
}

.compact-form {
  width: min(640px, 78vw);
  margin-top: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-tint);
}

.compact-form button { align-self: end; }

.status-pill,
.soft-pill,
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 750;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.status-pill.success {
  background: var(--success-soft);
  color: var(--success);
}

.status-pill.muted {
  background: #edf1f5;
  color: var(--muted);
}

.status-pill.danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.soft-pill {
  background: #edf3ff;
  color: #1e4fa8;
  width: fit-content;
  white-space: normal;
  text-align: left;
  justify-content: flex-start;
}

.soft-pill.neutral {
  background: #f0f3f7;
  color: #3f4c5f;
}

.mapping-list {
  display: grid;
  gap: 8px;
}

.mapping-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  overflow: hidden;
}

.mapping-item summary {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 13px;
  cursor: pointer;
  list-style: none;
}

.mapping-item summary::-webkit-details-marker { display: none; }

.mapping-item[open] summary {
  border-bottom: 1px solid var(--line);
  background: var(--surface-tint);
}

.mapping-main {
  display: grid;
  gap: 2px;
  min-width: 220px;
  margin-right: auto;
}

.mapping-main strong {
  color: var(--text-strong);
  overflow-wrap: anywhere;
}

.mapping-main span {
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.mapping-item .mapping-form {
  margin: 13px;
}

.delete-line {
  margin: 0 13px 13px;
}

.log-list {
  max-height: 430px;
  overflow-y: auto;
  padding: 7px;
  border: 1px solid #263244;
  border-radius: 8px;
  background: #111827;
}

.log-list details {
  border: 1px solid rgba(148, 163, 184, .22);
  border-radius: 6px;
  margin: 7px 0;
  background: rgba(15, 23, 42, .78);
  color: #dbeafe;
}

.log-list details:first-child { margin-top: 0; }
.log-list details:last-child { margin-bottom: 0; }

.log-list summary {
  padding: 9px 10px;
  cursor: pointer;
  overflow-wrap: anywhere;
}

.log-list pre {
  max-height: 280px;
  margin: 0;
  padding: 12px;
  overflow: auto;
  border-top: 1px solid rgba(148, 163, 184, .22);
  color: #d1e7ff;
  white-space: pre-wrap;
}

.badge {
  min-width: 44px;
  margin-right: 7px;
  background: #2d3a4f;
  color: #e2e8f0;
}

.badge.error {
  background: #5d1d1a;
  color: #fecaca;
}

.badge.info {
  background: #173b6f;
  color: #bfdbfe;
}

.badge.debug {
  background: #283345;
  color: #cbd5e1;
}

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 18px;
}

.auth-card {
  width: min(420px, 100%);
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.auth-card h1 { margin-bottom: 8px; }
.auth-card p { margin: 0 0 18px; color: var(--muted); }
.auth-card form { display: grid; gap: 14px; }

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    width: min(760px, calc(100% - 28px));
    gap: 14px;
  }

  .span-2 { grid-column: span 1; }

  .grid-form,
  .mapping-form {
    grid-template-columns: 1fr 1fr;
  }

  .mapping-form button { align-self: end; }

  .panel-head {
    align-items: stretch;
  }
}

@media (max-width: 720px) {
  body { font-size: 13px; }

  .topbar {
    position: static;
    flex-direction: column;
    align-items: stretch;
    padding: 12px 14px;
  }

  .brand-block {
    display: grid;
    gap: 3px;
  }

  .topbar form,
  .topbar button {
    width: 100%;
  }

  .layout {
    width: calc(100% - 20px);
    margin: 14px auto 28px;
    gap: 12px;
  }

  .panel {
    padding: 14px;
  }

  .panel-head {
    flex-direction: column;
  }

  .panel-head form,
  .panel-head button,
  .grid-form button,
  .mapping-form button,
  .auth-card button,
  .actions form,
  .actions button {
    width: 100%;
  }

  .grid-form,
  .mapping-form,
  .stats {
    grid-template-columns: 1fr;
  }

  .stats div {
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .stats div:first-child { border-top: 0; }

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

  .table-wrap {
    margin-right: -14px;
    margin-left: -14px;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  table { min-width: 940px; }

  .actions {
    white-space: normal;
  }

  .compact-form {
    width: min(540px, 78vw);
  }

  .mapping-item summary {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }

  .mapping-main {
    min-width: 0;
    width: 100%;
  }

  .mapping-item .mapping-form {
    margin: 12px;
  }

  .delete-line {
    margin: 0 12px 12px;
  }

  .log-list {
    max-height: 340px;
  }
}

@media (max-width: 420px) {
  .layout {
    width: calc(100% - 16px);
  }

  .panel {
    padding: 12px;
    border-radius: 7px;
  }

  .section-title {
    margin-bottom: 12px;
  }

  h1 { font-size: 22px; }
  h2 { font-size: 16px; }

  input,
  select,
  button {
    min-height: 40px;
  }

  .table-wrap {
    margin-right: -12px;
    margin-left: -12px;
  }

  .compact-form {
    width: min(480px, 80vw);
  }

  .auth-card {
    padding: 20px;
  }
}
