:root {
  --bg: #f3f6f6;
  --surface: #ffffff;
  --surface-soft: #f8fbfa;
  --text: #0f2a2c;
  --muted: #607476;
  --line: #d6e0e1;
  --brand: #0f766e;
  --brand-dark: #115e59;
  --danger: #b42318;
  --warning: #b54708;
  --input-bg: #ffffff;
  --overlay-bg: rgba(243, 246, 246, 0.72);
  --shadow: rgba(18, 35, 38, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Plus Jakarta Sans', sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 12% 0%, #d9efee 0%, transparent 35%),
    radial-gradient(circle at 90% 20%, #d5e7f4 0%, transparent 40%),
    var(--bg);
  min-height: 100vh;
}

body.theme-dark {
  --bg: #0e181c;
  --surface: #142329;
  --surface-soft: #1b2d34;
  --text: #e4f0f2;
  --muted: #a6bec2;
  --line: #2e464f;
  --brand: #1ca591;
  --brand-dark: #158876;
  --danger: #ff9d95;
  --warning: #f7c173;
  --input-bg: #102026;
  --overlay-bg: rgba(6, 11, 13, 0.76);
  --shadow: rgba(0, 0, 0, 0.25);
  background: radial-gradient(circle at 12% 0%, #173036 0%, transparent 35%),
    radial-gradient(circle at 90% 20%, #1c3140 0%, transparent 40%),
    var(--bg);
}

.page-shell {
  width: min(1120px, calc(100% - 2rem));
  margin: 2rem auto 3rem;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  margin-bottom: 1rem;
}

.topbar-title h1 {
  margin: 0;
  font-size: clamp(1.3rem, 2vw, 1.8rem);
}

.eyebrow {
  color: var(--brand-dark);
  text-transform: uppercase;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  font-weight: 700;
}

.user-chip {
  display: inline-flex;
  align-items: center;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.4rem 0.8rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.user-menu {
  position: relative;
}

.user-chip-button {
  cursor: pointer;
  user-select: none;
  gap: 0.35rem;
}

.user-chip-button::-webkit-details-marker {
  display: none;
}

.user-chip-button::marker {
  content: '';
}

.user-chip-caret {
  font-size: 0.75rem;
  line-height: 1;
}

.user-menu[open] .user-chip-button {
  border-color: var(--brand);
  color: var(--text);
}

.user-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 0.35rem);
  min-width: 170px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 24px var(--shadow);
  padding: 0.3rem;
  z-index: 30;
}

.user-menu-item {
  display: block;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  padding: 0.5rem 0.6rem;
}

.user-menu-item:hover {
  background: var(--surface-soft);
}

.topbar-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-actions .button {
  white-space: nowrap;
}

.icon-button {
  width: 2.5rem;
  min-width: 2.5rem;
  padding: 0.5rem;
  line-height: 0;
}

.theme-icon {
  width: 1.2rem;
  height: 1.2rem;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  display: block;
  margin: 0 auto;
}

body.theme-dark .theme-icon {
  stroke: #ffd166;
}

.inline-form {
  margin: 0;
}

.content {
  display: grid;
  gap: 1rem;
}

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

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1rem;
  box-shadow: 0 10px 28px var(--shadow);
}

.card h2 {
  margin: 0 0 0.8rem;
  font-size: 1.03rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.8rem;
}

.stats-grid article {
  padding: 0.75rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-soft);
}

.stats-grid strong {
  display: block;
  font-size: 1.25rem;
}

.stats-grid span {
  color: var(--muted);
  font-size: 0.85rem;
}

.form-grid {
  display: grid;
  gap: 0.75rem;
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

label {
  display: grid;
  gap: 0.35rem;
}

label span {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  font: inherit;
  background: var(--input-bg);
  color: var(--text);
}

input:focus,
select:focus {
  outline: 2px solid rgba(15, 118, 110, 0.22);
  border-color: var(--brand);
}

.inline-check {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.inline-check input {
  width: auto;
}

.button {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.58rem 0.9rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.button-primary {
  background: var(--brand);
  color: #fff;
}

.button-primary:hover {
  background: var(--brand-dark);
}

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

.button-ghost:hover {
  background: var(--surface-soft);
}

.button-danger {
  background: #fff5f4;
  border-color: #f7c8c3;
  color: var(--danger);
}

.button-danger:hover {
  background: #ffe8e6;
}

body.theme-dark .button-danger {
  background: #3a2428;
  border-color: #6a4248;
  color: #ffb4ae;
}

body.theme-dark .button-danger:hover {
  background: #4a2b31;
}

.instances-table {
  display: grid;
  gap: 0.65rem;
}

.instance-row {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.75rem;
  display: flex;
  justify-content: space-between;
  gap: 0.7rem;
  align-items: center;
  background: var(--surface-soft);
}

.instance-row-selectable {
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
}

.instance-row-selectable:hover {
  border-color: #93b8b5;
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.06);
}

.instance-row-selectable:focus-visible {
  outline: 3px solid rgba(15, 118, 110, 0.28);
  outline-offset: 2px;
}

.instance-row-active {
  border-color: #5aa8a1;
  background: #e8f7f5;
  box-shadow: inset 0 0 0 1px rgba(15, 118, 110, 0.18);
}

body.theme-dark .instance-row-active {
  border-color: #2bd2bc;
  background: #1d3c45;
  box-shadow: inset 0 0 0 1px rgba(43, 210, 188, 0.35);
}

body.theme-dark .instance-row-active h3 {
  color: #f2fcff;
}

.instance-main {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 180px;
}

.instance-title-group {
  display: inline-flex;
  align-items: baseline;
  gap: 0.45rem;
}

.instance-main h3 {
  margin: 0;
  font-size: 1rem;
}

.instance-phone {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

.instance-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;
}

.badge {
  font-size: 0.73rem;
  border-radius: 999px;
  padding: 0.24rem 0.55rem;
  font-weight: 700;
}

.badge-success {
  background: #dcfae6;
  color: #166534;
}

.badge-muted {
  background: #eceff1;
  color: #52616a;
}

.badge-warning {
  background: #fff4dd;
  color: var(--warning);
}

.badge-danger {
  background: #fee4e2;
  color: #b42318;
}

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

.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 0.8rem;
  align-items: end;
  margin-bottom: 0.8rem;
}

.toolbar-actions {
  display: flex;
  align-items: end;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.toolbar-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
}

.toolbar-inline label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.toolbar-inline select {
  width: auto;
  min-width: 90px;
  padding: 0.5rem 0.6rem;
}

.messages {
  display: grid;
  gap: 0.45rem;
  margin-bottom: 1rem;
}

.message {
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  font-size: 0.9rem;
  border: 1px solid;
}

.message-success {
  background: #ecfdf3;
  border-color: #abefc6;
  color: #067647;
}

.message-error {
  background: #fef3f2;
  border-color: #fecdca;
  color: #b42318;
}

.message-warning {
  background: #fffaeb;
  border-color: #fedf89;
  color: #b54708;
}

.message-info {
  background: #eff8ff;
  border-color: #b2ddff;
  color: #175cd3;
}

.auth-wrap {
  min-height: 75vh;
  display: grid;
  place-items: center;
}

.auth-card {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.2rem;
  box-shadow: 0 18px 34px var(--shadow);
}

.auth-header h2 {
  margin: 0.25rem 0 0.4rem;
}

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

.field-error {
  color: #b42318;
  font-size: 0.78rem;
}

.qr-wrap {
  margin-top: 0.8rem;
  border: 1px dashed var(--line);
  border-radius: 14px;
  padding: 0.9rem;
  display: flex;
  justify-content: center;
  background: var(--input-bg);
}

.qr-wrap img {
  width: min(100%, 280px);
  height: auto;
}

.action-loader-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: grid;
  place-items: center;
  z-index: 9999;
}

.action-loader-overlay[hidden] {
  display: none;
}

body.is-loading {
  cursor: progress;
}

.loader {
  width: 50px;
  padding: 8px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: #25b09b;
  --_m: conic-gradient(#0000 10%, #000), linear-gradient(#000 0 0) content-box;
  -webkit-mask: var(--_m);
  mask: var(--_m);
  -webkit-mask-composite: source-out;
  mask-composite: subtract;
  animation: l3 1s infinite linear;
}

@keyframes l3 {
  to {
    transform: rotate(1turn);
  }
}

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

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

  .instance-row {
    flex-direction: column;
    align-items: stretch;
  }

  .instance-actions {
    justify-content: flex-start;
  }

  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar-actions {
    width: 100%;
  }

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