:root {
  color-scheme: dark;
  --bg: #090d0c;
  --surface: #111816;
  --surface-2: #17211e;
  --border: rgba(255, 255, 255, 0.1);
  --border-emerald: rgba(16, 185, 129, 0.35);
  --text: #f4f7f5;
  --muted: #9ca9a3;
  --emerald: #10b981;
  --emerald-soft: rgba(16, 185, 129, 0.14);
  --gold: #facc15;
  --gold-soft: rgba(250, 204, 21, 0.14);
  --danger: #f43f5e;
  --danger-soft: rgba(244, 63, 94, 0.14);
  --warn: #f59e0b;
  --warn-soft: rgba(245, 158, 11, 0.14);
  --radius: 16px;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  min-block-size: 100dvb;
  background: radial-gradient(circle at top center, #0e241d 0%, var(--bg) 52%);
  color: var(--text);
  font-family: 'Noto Sans Thai', 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

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

button {
  cursor: pointer;
}

/* TOAST */
.toast {
  position: fixed;
  inset-block-start: calc(16px + env(safe-area-inset-top));
  inset-inline-start: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  padding: 10px 18px;
  border-radius: 999px;
  background: #064e3b;
  border: 1px solid #34d399;
  color: #ecfdf5;
  font-size: 0.85rem;
  font-weight: 700;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
  max-inline-size: min(92vw, 440px);
  text-align: center;
}

/* LOGIN */
.login-shell {
  min-block-size: 100dvb;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  inline-size: 100%;
  max-inline-size: 420px;
  padding: 28px 24px;
  border-radius: 24px;
  background: rgba(17, 24, 22, 0.92);
  border: 1px solid var(--border-emerald);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-logo {
  inline-size: 52px;
  block-size: 52px;
  border-radius: 14px;
  border: 1px solid var(--border-emerald);
  object-fit: cover;
  flex-shrink: 0;
}

.brand-title {
  margin: 0;
  font-family: 'Kanit', 'Noto Sans Thai', sans-serif;
  font-size: 1.25rem;
  font-weight: 800;
  color: #6ee7b7;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

/* FORMS & INPUTS */
.form-stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #d1dad5;
}

input[type='text'],
input[type='password'],
input[type='number'],
input[type='search'],
select,
textarea {
  inline-size: 100%;
  min-block-size: 44px;
  padding: 10px 13px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.45);
  color: var(--text);
  font-size: 0.92rem;
  outline: none;
  transition: border-color 0.15s ease;
}

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

textarea {
  min-block-size: 110px;
  resize: vertical;
  line-height: 1.55;
}

.password-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrap input {
  padding-inline-end: 74px;
}

.btn-inline {
  position: absolute;
  inset-inline-end: 8px;
  min-block-size: 32px;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.08);
  font-size: 0.75rem;
  font-weight: 600;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

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

.alert-error {
  padding: 10px 13px;
  border-radius: 12px;
  background: var(--danger-soft);
  border: 1px solid rgba(244, 63, 94, 0.45);
  color: #fecdd3;
  font-size: 0.82rem;
  font-weight: 600;
}

/* BUTTONS */
.btn-primary,
.btn-ghost,
.btn-danger,
.btn-warn-sm,
.btn-danger-sm,
.btn-chip,
.dur-chip,
.pill,
.var-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 12px;
  font-weight: 700;
  transition: transform 0.1s ease, filter 0.15s ease, background 0.15s ease;
}

.btn-primary {
  min-block-size: 44px;
  padding: 10px 16px;
  border: none;
  background: linear-gradient(135deg, #10b981, #059669);
  color: #032219;
  font-size: 0.88rem;
  white-space: nowrap;
}

.btn-primary.compact {
  min-block-size: 38px;
  padding: 7px 12px;
  font-size: 0.82rem;
}

.btn-primary.full {
  inline-size: 100%;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-ghost {
  min-block-size: 42px;
  padding: 9px 14px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 0.84rem;
  white-space: nowrap;
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.1);
}

.btn-danger {
  min-block-size: 42px;
  padding: 9px 14px;
  border: 1px solid rgba(244, 63, 94, 0.4);
  background: var(--danger-soft);
  color: #fda4af;
  font-size: 0.84rem;
}

/* APP SHELL */
.app-shell {
  min-block-size: 100dvb;
  display: flex;
  flex-direction: column;
}

.top-header {
  position: sticky;
  inset-block-start: 0;
  z-index: 40;
  padding-block-start: env(safe-area-inset-top);
  background: rgba(11, 18, 16, 0.9);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-inline-size: 1180px;
  margin-inline: auto;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-inline-size: 0;
}

.header-logo {
  inline-size: 36px;
  block-size: 36px;
  border-radius: 10px;
  border: 1px solid var(--border-emerald);
  flex-shrink: 0;
}

.header-title {
  font-family: 'Kanit', 'Noto Sans Thai', sans-serif;
  font-size: 0.95rem;
  font-weight: 800;
  color: #ecfdf5;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-sub {
  font-size: 0.72rem;
  color: #6ee7b7;
}

.desktop-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.nav-tab {
  min-block-size: 38px;
  padding: 7px 13px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

.nav-tab.active {
  background: var(--emerald-soft);
  border-color: var(--border-emerald);
  color: #a7f3d0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

@media (max-width: 820px) {
  .desktop-tabs {
    display: none;
  }
}

.main-content {
  flex: 1;
  inline-size: 100%;
  max-inline-size: 1180px;
  margin-inline: auto;
  padding: 14px 14px calc(92px + env(safe-area-inset-bottom));
}

@media (min-width: 821px) {
  .main-content {
    padding: 20px 20px 40px;
  }
}

/* KPI GRID */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-block-end: 14px;
}

@media (max-width: 680px) {
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.kpi-card {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}

.kpi-card.kpi-emerald {
  border-color: var(--border-emerald);
}

.kpi-card.kpi-gold {
  border-color: rgba(250, 204, 21, 0.35);
}

.kpi-label {
  font-size: 0.74rem;
  color: var(--muted);
}

.kpi-value {
  margin-block-start: 2px;
  font-family: 'Kanit', 'Noto Sans Thai', sans-serif;
  font-size: 1.4rem;
  font-weight: 800;
}

/* NEWLY CREATED BANNER */
.new-key-banner {
  margin-block-end: 14px;
  padding: 16px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(6, 78, 59, 0.55), rgba(17, 24, 22, 0.95));
  border: 1px solid rgba(52, 211, 153, 0.55);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.new-key-code {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(250, 204, 21, 0.35);
  color: #fde047;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95rem;
  font-weight: 700;
  word-break: break-all;
}

/* TOOLBAR & FILTER PILLS */
.toolbar-card {
  padding: 12px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  margin-block-end: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

@media (max-width: 520px) {
  .search-row {
    flex-direction: column;
    align-items: stretch;
  }
}

.filter-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.pill {
  min-block-size: 34px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 0.78rem;
}

.pill.active {
  background: var(--emerald-soft);
  border-color: var(--border-emerald);
  color: #a7f3d0;
}

/* CARDS */
.card-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.license-card,
.script-card,
.panel-card {
  padding: 15px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.license-card.is-dead {
  border-color: rgba(244, 63, 94, 0.35);
  background: rgba(35, 14, 20, 0.45);
}

.card-top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.cust-name {
  font-size: 1rem;
  font-weight: 800;
  color: #ffffff;
}

.cust-meta {
  font-size: 0.78rem;
  color: var(--muted);
  margin-block-start: 2px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
}

.badge-active {
  background: var(--emerald-soft);
  border: 1px solid var(--border-emerald);
  color: #6ee7b7;
}

.badge-vip {
  background: var(--gold-soft);
  border: 1px solid rgba(250, 204, 21, 0.45);
  color: #fde047;
}

.badge-dead {
  background: var(--danger-soft);
  border: 1px solid rgba(244, 63, 94, 0.45);
  color: #fda4af;
}

.badge-cat {
  background: rgba(255, 255, 255, 0.08);
  color: #e4e4e7;
}

.code-box {
  padding: 9px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88rem;
  font-weight: 700;
  color: #fde047;
  word-break: break-all;
  user-select: all;
}

.devices-box {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.07);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.devices-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.78rem;
}

.device-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.03);
  font-size: 0.76rem;
}

.btn-warn-sm {
  min-block-size: 32px;
  padding: 4px 10px;
  border: 1px solid rgba(245, 158, 11, 0.45);
  background: var(--warn-soft);
  color: #fcd34d;
  font-size: 0.74rem;
}

.btn-danger-sm {
  min-block-size: 32px;
  padding: 4px 10px;
  border: 1px solid rgba(244, 63, 94, 0.4);
  background: var(--danger-soft);
  color: #fda4af;
  font-size: 0.74rem;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}

.card-actions .btn-act {
  min-block-size: 38px;
  padding: 7px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 700;
  flex: 1 1 auto;
  white-space: nowrap;
}

.card-actions .btn-act.primary {
  background: linear-gradient(135deg, #10b981, #059669);
  border-color: transparent;
  color: #032219;
}

.card-actions .btn-act.gold {
  background: rgba(250, 204, 21, 0.16);
  border-color: rgba(250, 204, 21, 0.45);
  color: #fde047;
}

/* SCRIPTS & TEMPLATES */
.script-body {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.85rem;
  color: #e4eae6;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 12px;
  padding: 12px;
  line-height: 1.55;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.panel-title {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 800;
  color: #a7f3d0;
}

.panel-hint {
  margin: 4px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.var-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.var-chip {
  min-block-size: 30px;
  padding: 4px 9px;
  border: 1px solid var(--border-emerald);
  background: var(--emerald-soft);
  color: #6ee7b7;
  font-size: 0.74rem;
}

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

.code-textarea {
  font-size: 0.85rem;
}

/* MOBILE BOTTOM NAV */
.mobile-bottom-nav {
  position: fixed;
  inset-block-end: 0;
  inset-inline: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  background: rgba(9, 13, 12, 0.96);
  backdrop-filter: blur(18px);
  border-top: 1px solid var(--border);
}

@media (min-width: 821px) {
  .mobile-bottom-nav {
    display: none;
  }
}

.mnav-btn {
  min-block-size: 48px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.7rem;
  font-weight: 700;
}

.mnav-btn.active {
  background: var(--emerald-soft);
  color: #6ee7b7;
}

.mnav-ico {
  font-size: 1.05rem;
  line-height: 1;
}

/* NATIVE <dialog> MODALS */
dialog.modal-dialog {
  padding: 0;
  margin: auto;
  border: none;
  background: transparent;
  color: var(--text);
  max-inline-size: min(95vw, 520px);
  inline-size: 100%;
  max-block-size: 90dvb;
  overflow: visible;
}

dialog.modal-dialog::backdrop {
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(6px);
}

.modal-card {
  background: #121a18;
  border: 1px solid var(--border-emerald);
  border-radius: 20px;
  padding: 18px;
  max-block-size: 90dvb;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-head h2 {
  margin: 0;
  font-size: 1.02rem;
  font-weight: 800;
}

.btn-close {
  min-block-size: 36px;
  min-inline-size: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
}

.duration-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.dur-chip,
.btn-chip {
  min-block-size: 36px;
  padding: 6px 11px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.78rem;
}

.dur-chip.active {
  background: var(--emerald-soft);
  border-color: var(--border-emerald);
  color: #6ee7b7;
}

.dur-chip.dur-vip,
.btn-chip.dur-vip {
  border-color: rgba(250, 204, 21, 0.35);
  color: #fde047;
}

.dur-chip.dur-vip.active {
  background: var(--gold-soft);
  border-color: var(--gold);
}

.device-manage-box {
  padding: 12px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.device-box-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
}

.device-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-block-start: 4px;
}

.modal-footer-split {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding-block-start: 4px;
}

.left-btns {
  display: flex;
  gap: 6px;
}

.empty-state {
  padding: 36px 20px;
  text-align: center;
  border-radius: var(--radius);
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 0.88rem;
}
