:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1f2933;
  --muted: #607080;
  --line: #d8dee6;
  --accent: #1769aa;
  --accent-dark: #0f4f83;
  --danger: #b42318;
  --success: #137333;
  --warning: #a15c00;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, 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: 24px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
}

h2 {
  font-size: 18px;
}

p {
  margin-top: 6px;
  color: var(--muted);
}

.auth-panel {
  display: grid;
  place-items: start center;
  padding: 48px 16px;
}

.login-card {
  width: min(420px, 100%);
}

.hint {
  font-size: 13px;
  line-height: 1.45;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 480px) minmax(0, 1fr);
  gap: 20px;
  padding: 20px clamp(16px, 4vw, 40px) 40px;
}

.app {
  padding: 20px clamp(16px, 4vw, 40px) 40px;
}

.view {
  display: grid;
  gap: 18px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

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

.source-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #34495e;
  font-size: 13px;
  font-weight: 750;
}

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

label,
legend {
  color: #34495e;
  font-size: 13px;
  font-weight: 650;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: #fff;
  font: inherit;
  padding: 10px 11px;
}

textarea {
  min-height: 150px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.45;
}

fieldset {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 500;
}

.check input {
  width: auto;
}

button,
.link-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-weight: 650;
  padding: 9px 12px;
  text-decoration: none;
  text-align: center;
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

.segmented button {
  border: 0;
  border-radius: 0;
  padding: 7px 10px;
}

.segmented button + button {
  border-left: 1px solid var(--line);
}

.segmented .active {
  background: var(--accent);
  color: #fff;
}

button:hover,
.link-button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

button.loading {
  position: relative;
  color: transparent;
  pointer-events: none;
}

button.loading::after {
  position: absolute;
  inset: 0;
  width: 16px;
  height: 16px;
  margin: auto;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 999px;
  content: "";
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.primary:hover {
  border-color: var(--accent-dark);
  background: var(--accent-dark);
  color: #fff;
}

.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

#configCount {
  min-width: 28px;
  border-radius: 999px;
  background: #e8eef4;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  padding: 3px 8px;
  text-align: center;
}

.message {
  min-height: 24px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 14px;
}

.message.error {
  color: var(--danger);
}

.message.success {
  color: var(--success);
}

.config-list {
  display: grid;
  gap: 12px;
  margin-top: 8px;
}

.client-slots {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.config-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.config-title {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.token {
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
}

.badge {
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.badge.success {
  background: #e7f4ea;
  color: var(--success);
}

.badge.failure {
  background: #fce8e6;
  color: var(--danger);
}

.badge.never {
  background: #eef1f4;
  color: var(--muted);
}

.badge.merged {
  background: #eef1f4;
  color: #55616d;
}

.meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 12px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.inline-field {
  display: grid;
  grid-template-columns: auto 120px auto;
  align-items: center;
  gap: 8px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.empty {
  padding: 28px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.public-url {
  margin-top: 12px;
  padding: 9px 10px;
  border-radius: 6px;
  background: #f3f6f9;
  color: #34495e;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.token-panel {
  display: grid;
  gap: 12px;
}

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

.source-editor-list {
  display: grid;
  gap: 12px;
}

.source-editor {
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.source-editor textarea {
  min-height: 100px;
}

.source-editor .source-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
}

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

.settings-card,
.setup-panel {
  display: grid;
  gap: 12px;
}

.settings-card {
  align-content: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.settings-card h3 {
  margin: 0;
}

.setup-panel {
  margin-top: 12px;
}

.otp-qr {
  display: grid;
  place-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.otp-qr svg {
  width: min(220px, 100%);
  height: auto;
}

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

.passkey-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.compact {
  padding: 12px;
}

.diagnostics {
  display: grid;
  gap: 6px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.warn {
  color: var(--warning);
}

.error {
  color: var(--danger);
}

.info {
  color: var(--accent);
}

.log-filters {
  display: grid;
  grid-template-columns: repeat(4, auto);
  justify-content: start;
  gap: 10px;
  margin-top: 14px;
}

.log-settings {
  display: grid;
  grid-template-columns: auto 160px auto;
  align-items: center;
  justify-content: start;
  gap: 10px;
  margin-top: 14px;
}

.logs-list {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.log-row {
  display: grid;
  grid-template-columns: 170px 60px 150px 100px 120px 1fr 1.5fr;
  gap: 8px;
  align-items: start;
  text-align: left;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.hidden {
  display: none;
}

dialog {
  width: min(960px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
}

.dialog-body {
  margin: 0;
  padding: 0 14px 14px;
  max-height: 85vh;
  overflow: auto;
}

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

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#previewOutput {
  max-height: 70vh;
  margin: 0;
  overflow: auto;
  padding: 14px;
  background: #111827;
  color: #e5e7eb;
  font-size: 13px;
  line-height: 1.45;
}

@media (max-width: 900px) {
  .topbar,
  .layout {
    padding-left: 14px;
    padding-right: 14px;
  }

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

  .client-slots,
  .security-grid,
  .token-edit,
  .source-editor .source-header,
  .log-filters,
  .log-settings,
  .log-row {
    grid-template-columns: 1fr;
  }

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

  .source-header,
  .inline-field {
    align-items: stretch;
    grid-template-columns: 1fr;
    flex-direction: column;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }
}

@media (max-width: 640px) {
  body {
    font-size: 14px;
  }

  .app,
  .auth-panel {
    padding: 14px 10px 28px;
  }

  .topbar {
    gap: 14px;
    padding: 16px 10px;
  }

  h1 {
    font-size: 22px;
  }

  .top-actions,
  .actions,
  .dialog-actions {
    width: 100%;
  }

  .top-actions button,
  .actions button,
  .dialog-actions button,
  .primary {
    flex: 1 1 auto;
  }

  .panel,
  .config-card,
  .source-editor {
    padding: 12px;
  }

  .section-heading,
  .dialog-header,
  .source-header {
    align-items: stretch;
    flex-direction: column;
  }

  .segmented {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .segmented button + button {
    border-left: 1px solid var(--line);
  }

  .token-edit,
  .inline-field {
    grid-template-columns: 1fr;
  }

  dialog {
    width: calc(100vw - 16px);
    max-height: calc(100vh - 20px);
  }

  .dialog-body {
    max-height: calc(100vh - 80px);
  }

  #previewOutput,
  #logDetail {
    max-height: 62vh;
    font-size: 12px;
  }

  .log-row {
    gap: 4px;
    padding: 10px;
  }
}
