:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  font-weight: 400;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-subtle: #f9fafb;
  --surface-strong: #eef1f4;
  --text: #181b20;
  --text-muted: #666d78;
  --border: #dfe3e8;
  --border-strong: #c7cdd5;
  --accent: #2457d6;
  --accent-hover: #1946b5;
  --accent-soft: #edf3ff;
  --danger: #b42318;
  --danger-hover: #8f1c13;
  --danger-soft: #fff0ee;
  --success: #18794e;
  --success-soft: #eaf8f1;
  --warning: #8a5a00;
  --warning-soft: #fff7df;
  --info: #2457d6;
  --info-soft: #edf3ff;
  --focus: rgba(36, 87, 214, 0.22);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-md: 0 12px 32px rgba(16, 24, 40, 0.12);
  --radius-sm: 7px;
  --radius: 10px;
  --radius-lg: 14px;
  --sidebar-width: 238px;
  --content-max: 1180px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

button:disabled,
select:disabled,
input:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

button,
input,
select {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}

button {
  min-height: 38px;
  padding: 0.52rem 0.85rem;
  background: var(--surface);
  color: var(--text);
  font-weight: 650;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease, box-shadow 120ms ease;
}

button:hover:not(:disabled) {
  background: var(--surface-strong);
  border-color: #aeb6c0;
}

button[type="submit"],
button[data-primary="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

button[type="submit"]:hover:not(:disabled),
button[data-primary="true"]:hover:not(:disabled) {
  border-color: var(--accent-hover);
  background: var(--accent-hover);
}

button[data-danger="true"] {
  border-color: #e5b8b2;
  color: var(--danger);
}

button[data-danger="true"]:hover:not(:disabled) {
  border-color: #d79b93;
  background: var(--danger-soft);
  color: var(--danger-hover);
}

input,
select {
  width: 100%;
  min-height: 42px;
  padding: 0.62rem 0.72rem;
  background: var(--surface);
  color: var(--text);
}

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

input:focus-visible,
select:focus-visible,
button:focus-visible,
a:focus-visible,
summary:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.16em;
}

a:hover {
  color: var(--accent-hover);
}

.balance-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

button.balance-link {
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.16em;
}

button.balance-link:hover:not(:disabled) {
  border: 0;
  background: transparent;
  color: var(--accent-hover);
}

h1,
h2,
h3,
p,
dl,
dd {
  margin-top: 0;
}

h1,
h2,
h3 {
  color: var(--text);
  line-height: 1.22;
}

h1 {
  margin-bottom: 0.35rem;
  font-size: clamp(1.65rem, 2vw, 2.05rem);
  letter-spacing: -0.025em;
}

h2 {
  margin-bottom: 0.65rem;
  font-size: 1.16rem;
  letter-spacing: -0.012em;
}

h3 {
  margin-bottom: 0.55rem;
  font-size: 1rem;
}

p {
  margin-bottom: 0.9rem;
}

hr {
  height: 1px;
  margin: 1.35rem 0;
  border: 0;
  background: var(--border);
}

pre,
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

pre {
  max-width: 100%;
  overflow-x: auto;
  padding: 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.brand {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: -0.025em;
  text-decoration: none;
}

.muted-text,
.field-help {
  color: var(--text-muted);
}

.field-help {
  margin: 0.38rem 0 0;
  font-size: 0.86rem;
}

.form-field {
  display: grid;
  gap: 0.38rem;
}

.form-field + .form-field {
  margin-top: 1rem;
}

.form-field label {
  font-size: 0.9rem;
  font-weight: 680;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
  margin-top: 1.1rem;
}

.form-message {
  margin: 0 0 1rem;
  padding: 0.72rem 0.82rem;
  border: 1px solid #efb8b1;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.form-message[data-type="success"] {
  border-color: #acd9c3;
  background: var(--success-soft);
  color: var(--success);
}

.form-message[data-type="warning"] {
  border-color: #ead49a;
  background: var(--warning-soft);
  color: var(--warning);
}

.form-message[data-type="info"] {
  border-color: #bfd0fb;
  background: var(--info-soft);
  color: var(--info);
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  margin: 0.15rem 0.35rem 0 0;
  padding: 0.16rem 0.48rem;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 750;
  line-height: 1.2;
}

/* Authentication and first-setup pages */
.auth-page {
  background:
    radial-gradient(circle at 50% -10%, rgba(36, 87, 214, 0.08), transparent 38rem),
    var(--bg);
}

.auth-shell {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 2rem 1rem;
}

.auth-card {
  width: min(100%, 460px);
  padding: clamp(1.35rem, 4vw, 2rem);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.auth-header {
  margin-bottom: 1.35rem;
}

.auth-header .brand,
.auth-card > .brand {
  display: inline-block;
  margin-bottom: 1.2rem;
}

.auth-header p:last-child {
  margin-bottom: 0;
  color: var(--text-muted);
}

.auth-card form > button[type="submit"] {
  width: 100%;
  margin-top: 1.25rem;
}

/* Dashboard shell */
.dashboard-page {
  overflow-x: hidden;
}

.dashboard-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
}

.dashboard-sidebar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  height: 100vh;
  padding: 1rem;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

.sidebar-header,
.dashboard-topbar,
.page-header,
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.sidebar-header {
  min-height: 42px;
  margin-bottom: 1.25rem;
}

.sidebar-close,
.sidebar-open {
  display: none;
}

.dashboard-nav {
  display: grid;
  gap: 0.22rem;
}

.dashboard-nav a {
  display: block;
  padding: 0.58rem 0.68rem;
  border-radius: var(--radius-sm);
  color: #3c424b;
  font-size: 0.92rem;
  font-weight: 650;
  text-decoration: none;
}

.dashboard-nav a:hover {
  background: var(--surface-strong);
  color: var(--text);
}

.dashboard-nav a[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
}

.sidebar-account {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.sidebar-account p {
  margin-bottom: 0.2rem;
  overflow-wrap: anywhere;
}

.sidebar-account #current-username {
  font-size: 0.92rem;
  font-weight: 720;
}

.sidebar-account #current-role {
  margin-bottom: 0.8rem;
  font-size: 0.8rem;
  text-transform: capitalize;
}

.sidebar-account button {
  width: 100%;
  margin-top: 0.45rem;
  text-align: left;
}

.dashboard-content {
  min-width: 0;
}

.dashboard-topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 64px;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(10px);
}

.topbar-environment {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin-left: auto;
  font-size: 0.88rem;
}

.environment-switch {
  display: inline-flex;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-strong);
}

.environment-switch button {
  min-height: 30px;
  padding: 0.28rem 0.62rem;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.environment-switch button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
  opacity: 1;
}

#environment-test[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

#environment-live[aria-pressed="true"] {
  background: var(--danger-soft);
  color: var(--danger);
}

#dashboard-message {
  width: min(calc(100% - 3rem), var(--content-max));
  margin: 1rem auto 0;
}

.dashboard-main {
  width: min(100%, calc(var(--content-max) + 3rem));
  margin: 0 auto;
  padding: 1.65rem 1.5rem 3rem;
}

.dashboard-view {
  min-width: 0;
}

.page-header {
  align-items: flex-start;
  margin-bottom: 1.25rem;
}

.page-header p {
  margin-bottom: 0;
}

.page-header > :last-child:not(div) {
  flex: 0 0 auto;
}

.panel {
  margin-bottom: 1rem;
  padding: 1.15rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.panel > :last-child {
  margin-bottom: 0;
}

.panel-header {
  align-items: flex-start;
  margin-bottom: 1rem;
}

.panel-header h2,
.panel-header h3,
.panel-header p {
  margin-bottom: 0.2rem;
}

.panel-header p:last-child {
  margin-bottom: 0;
}

.token-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.85rem;
  margin-bottom: 1rem;
}

.summary-card {
  min-width: 0;
  padding: 0.95rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
}

.summary-card h3 {
  margin-bottom: 0.75rem;
}

.summary-card dl,
.detail-list {
  margin-bottom: 0;
}

.summary-card dl > div,
.detail-list > div {
  display: grid;
  grid-template-columns: minmax(130px, 0.8fr) minmax(0, 1.2fr);
  gap: 1rem;
  padding: 0.52rem 0;
  border-top: 1px solid var(--border);
}

.summary-card dl > div:first-child,
.detail-list > div:first-child {
  border-top: 0;
}

.summary-card dt,
.detail-list dt {
  color: var(--text-muted);
  font-size: 0.84rem;
}

.summary-card dd,
.detail-list dd {
  min-width: 0;
  margin: 0;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.secret-panel {
  margin-top: 1rem;
  padding: 1rem;
  border: 1px solid #b9caef;
  border-radius: var(--radius);
  background: #f7f9ff;
}

.panel.secret-panel {
  margin-top: 0;
  padding: 1.15rem;
}

.secret-panel pre {
  background: var(--surface);
}

.stack-list {
  display: grid;
  gap: 1rem;
}

.app-card {
  margin-bottom: 0;
}

.app-card .panel-header > div:first-child {
  min-width: 0;
}

.app-environment {
  margin-top: 0.85rem;
  padding: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
}

.app-environment p {
  margin-bottom: 0.35rem;
  color: #454b54;
  font-size: 0.88rem;
  overflow-wrap: anywhere;
}

.app-environment .form-actions {
  margin-top: 0.75rem;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.7rem;
  margin-bottom: 1rem;
  padding: 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.filter-bar .form-field {
  flex: 1 1 190px;
}

.filter-bar .form-field + .form-field {
  margin-top: 0;
}

.filter-bar button {
  flex: 0 0 auto;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.panel .table-wrap {
  box-shadow: none;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

th,
td {
  padding: 0.72rem 0.8rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}

th {
  background: var(--surface-subtle);
  color: #4f5660;
  font-size: 0.77rem;
  font-weight: 760;
  letter-spacing: 0.025em;
  text-transform: uppercase;
}

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

tbody tr:hover td,
tbody tr:hover th[scope="row"] {
  background: #fbfcfd;
}

td a {
  font-weight: 650;
}

td select {
  min-width: 120px;
  min-height: 34px;
  padding: 0.35rem 0.55rem;
  font-size: 0.84rem;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.7rem;
  margin-top: 0.9rem;
}

.pagination span {
  color: var(--text-muted);
  font-size: 0.86rem;
}

details.panel {
  padding: 0;
}

details.panel > summary {
  padding: 1rem 1.15rem;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
}

details.panel[open] > summary {
  border-bottom: 1px solid var(--border);
}

details.panel > .detail-list {
  padding: 0.45rem 1.15rem 1rem;
}

#general-settings-form,
#capacity-settings-form,
#webhook-security-settings-form,
#rpc-form,
#user-form,
#app-form {
  max-width: 720px;
}

#capacity-settings-form {
  margin-bottom: 0;
}

#rpc-form {
  margin-top: 1rem;
}

/* Native modal */
dialog {
  width: min(calc(100% - 2rem), 520px);
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  padding: 1.2rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-md);
}

dialog::backdrop {
  background: rgba(18, 22, 28, 0.48);
}

dialog form {
  margin: 0;
}

/* Tablet and mobile */
@media (max-width: 900px) {
  .dashboard-shell {
    display: block;
  }

  .dashboard-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    transform: translateX(-102%);
    box-shadow: var(--shadow-md);
    transition: transform 160ms ease;
  }

  .dashboard-sidebar[data-open="true"] {
    transform: translateX(0);
  }

  .sidebar-close,
  .sidebar-open {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .dashboard-topbar {
    padding: 0 1rem;
  }

  .dashboard-main {
    padding: 1.35rem 1rem 2.5rem;
  }

  #dashboard-message {
    width: calc(100% - 2rem);
  }

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

@media (max-width: 620px) {
  .auth-shell {
    align-items: start;
    padding: 1rem;
  }

  .auth-card {
    margin-top: 5vh;
    padding: 1.2rem;
  }

  .dashboard-topbar {
    min-height: 58px;
  }

  .topbar-environment > .muted-text {
    display: none;
  }

  .environment-switch button {
    padding-inline: 0.5rem;
  }

  .dashboard-main {
    padding-top: 1.1rem;
  }

  .page-header,
  .panel-header {
    align-items: stretch;
    flex-direction: column;
  }

  .page-header > :last-child:not(div),
  .panel-header > :last-child:not(div) {
    align-self: flex-start;
  }

  .panel {
    padding: 1rem;
  }

  .summary-card dl > div,
  .detail-list > div {
    grid-template-columns: 1fr;
    gap: 0.12rem;
  }

  .summary-card dd,
  .detail-list dd {
    font-weight: 620;
  }

  .filter-bar {
    align-items: stretch;
  }

  .filter-bar .form-field,
  .filter-bar button {
    flex-basis: 100%;
  }

  .form-actions > button {
    max-width: 100%;
  }

  .pagination {
    justify-content: space-between;
  }

  .pagination span {
    text-align: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
