:root {
  color-scheme: dark;
  accent-color: #0ea5e9;
  --bg-base: #090d16;
  --bg-surface: #111827;
  --bg-card: #1e293b;
  --bg-elevated: #263349;
  --bg-input: #0f172a;
  --border-subtle: rgba(148, 163, 184, 0.16);
  --border-strong: rgba(148, 163, 184, 0.28);
  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --brand-primary: #0ea5e9;
  --brand-hover: #38bdf8;
  --status-active: #10b981;
  --status-active-bg: rgba(16, 185, 129, 0.14);
  --status-warn: #f59e0b;
  --status-warn-bg: rgba(245, 158, 11, 0.15);
  --status-danger: #ef4444;
  --status-danger-bg: rgba(239, 68, 68, 0.15);
  --status-neutral: #64748b;
  --status-neutral-bg: rgba(100, 116, 139, 0.18);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.45);
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  min-block-size: 100dvh;
  background-color: var(--bg-base);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Prompt", "Kanit", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Native <dialog> Modal & Scroll Containment (Modern Web Guidance: dialog) */
dialog {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 0;
  max-inline-size: min(560px, 94vw);
  inline-size: 100%;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.75);
}

dialog[open] {
  display: flex;
  flex-direction: column;
}

dialog:modal {
  overscroll-behavior: contain;
  max-block-size: 90dvh;
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(4px);
  overflow: hidden;
  overscroll-behavior: contain;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-block-end: 1px solid var(--border-subtle);
  background: rgba(30, 41, 59, 0.55);
}

.dialog-header h2 {
  margin: 0;
  font-size: clamp(1rem, 0.9rem + 0.4vw, 1.25rem);
  font-weight: 700;
}

.dialog-body {
  padding: 1.25rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.65rem;
  padding: 0.9rem 1.25rem;
  border-block-start: 1px solid var(--border-subtle);
  background: rgba(15, 23, 42, 0.6);
}

/* Login View */
.login-screen {
  min-block-size: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background:
    radial-gradient(circle at 20% 20%, rgba(14, 165, 233, 0.15), transparent 45%),
    radial-gradient(circle at 80% 80%, rgba(16, 185, 129, 0.12), transparent 45%),
    var(--bg-base);
}

.login-card {
  inline-size: 100%;
  max-inline-size: 430px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 1.75rem;
  box-shadow: var(--shadow-card);
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-block-end: 1rem;
}

.login-brand-icon {
  inline-size: 44px;
  block-size: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, #0ea5e9, #10b981);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  font-weight: 800;
  color: #020617;
  flex-shrink: 0;
}

.login-brand h1 {
  margin: 0;
  font-size: clamp(1.1rem, 1rem + 0.5vw, 1.35rem);
  line-height: 1.25;
}

.login-brand p {
  margin: 0.15rem 0 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.security-banner {
  background: rgba(14, 165, 233, 0.1);
  border: 1px solid rgba(14, 165, 233, 0.3);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-block-end: 1.1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-block-end: 0.9rem;
}

.form-group label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.input-row {
  display: flex;
  gap: 0.45rem;
  align-items: stretch;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="number"],
input[type="date"],
input[type="search"],
select,
textarea {
  inline-size: 100%;
  min-block-size: 42px;
  padding: 0.55rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.92rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

textarea {
  min-block-size: 76px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.22);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-block-size: 40px;
  padding: 0.48rem 0.95rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--brand-primary);
  color: #020617;
  font-weight: 700;
}

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

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.btn-secondary:hover {
  background: #334155;
}

.btn-success {
  background: rgba(16, 185, 129, 0.18);
  color: #34d399;
  border-color: rgba(16, 185, 129, 0.4);
}

.btn-warn {
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
  border-color: rgba(245, 158, 11, 0.4);
}

.btn-danger {
  background: rgba(239, 68, 68, 0.16);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.4);
}

.btn-sm {
  min-block-size: 34px;
  padding: 0.3rem 0.65rem;
  font-size: 0.78rem;
}

.btn-block {
  inline-size: 100%;
}

/* Main App Layout (Desktop Sidebar + Mobile Bottom Nav) */
.app-shell {
  min-block-size: 100dvh;
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
}

.sidebar {
  grid-area: sidebar;
  background: var(--bg-surface);
  border-inline-end: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  padding: 1rem 0.75rem;
  position: sticky;
  inset-block-start: 0;
  block-size: 100dvh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.4rem 0.6rem 1rem 0.6rem;
  border-block-end: 1px solid var(--border-subtle);
  margin-block-end: 0.85rem;
}

.sidebar-brand-title {
  font-weight: 700;
  font-size: 0.96rem;
  line-height: 1.25;
}

.sidebar-brand-sub {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.nav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  flex: 1;
}

.nav-btn {
  inline-size: 100%;
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.65rem 0.8rem;
  min-block-size: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: start;
  cursor: pointer;
}

.nav-btn:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
}

.nav-btn.active {
  background: rgba(14, 165, 233, 0.16);
  color: #38bdf8;
  border-color: rgba(14, 165, 233, 0.35);
}

.sidebar-footer {
  padding-block-start: 0.85rem;
  border-block-start: 1px solid var(--border-subtle);
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Topbar */
.topbar {
  grid-area: topbar;
  background: rgba(17, 24, 39, 0.92);
  backdrop-filter: blur(12px);
  border-block-end: 1px solid var(--border-subtle);
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  position: sticky;
  inset-block-start: 0;
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.topbar-title {
  margin: 0;
  font-size: clamp(1rem, 0.92rem + 0.45vw, 1.3rem);
  font-weight: 700;
}

.env-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.22rem 0.6rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  background: rgba(245, 158, 11, 0.16);
  color: #fbbf24;
  border: 1px solid rgba(245, 158, 11, 0.35);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

/* Main Content Area */
.main-content {
  grid-area: main;
  padding: 1.25rem;
  max-inline-size: 1440px;
  inline-size: 100%;
  margin-inline: auto;
}

.view-section {
  display: none;
  flex-direction: column;
  gap: 1.15rem;
}

.view-section.active {
  display: flex;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.9rem;
}

.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.kpi-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
}

.kpi-value {
  font-size: clamp(1.35rem, 1.15rem + 0.8vw, 1.85rem);
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1.15;
}

.kpi-sub {
  font-size: 0.76rem;
  color: var(--text-secondary);
}

/* Panel & Toolbar */
.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.panel-title {
  margin: 0;
  font-size: clamp(0.98rem, 0.9rem + 0.35vw, 1.15rem);
  font-weight: 700;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
  justify-content: space-between;
}

.toolbar-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  flex: 1;
  min-inline-size: min(280px, 100%);
}

.toolbar-filters input[type="search"] {
  max-inline-size: 320px;
}

.toolbar-filters select {
  max-inline-size: 200px;
}

/* Status Badges */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  white-space: nowrap;
}

.status-ACTIVE,
.status-BOUND,
.status-SUCCESS {
  background: var(--status-active-bg);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.35);
}

.status-SUSPENDED,
.status-EXPIRING {
  background: var(--status-warn-bg);
  color: #fbbf24;
  border: 1px solid rgba(245, 158, 11, 0.35);
}

.status-REVOKED,
.status-EXPIRED,
.status-REJECTED {
  background: var(--status-danger-bg);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.35);
}

.status-UNBOUND,
.status-ARCHIVED {
  background: var(--status-neutral-bg);
  color: #cbd5e1;
  border: 1px solid rgba(148, 163, 184, 0.3);
}

.code-pill {
  font-family: "SFMono-Regular", Consolas, Menlo, monospace;
  font-size: 0.8rem;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border-strong);
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  color: #38bdf8;
  white-space: nowrap;
}

/* Responsive Data Table with @container (Modern Web Guidance: responsive-table) */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(580px, 75dvh);
  container-type: inline-size;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
}

table {
  inline-size: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.86rem;
}

caption {
  text-align: start;
  padding: 0.55rem 0.85rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: rgba(15, 23, 42, 0.65);
  border-block-end: 1px solid var(--border-subtle);
}

thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 2;
}

thead th {
  background: #172033;
  color: var(--text-secondary);
  font-weight: 700;
  text-align: start;
  padding: 0.7rem 0.85rem;
  border-block-end: 1px solid var(--border-strong);
  white-space: nowrap;
}

tbody tr {
  background: var(--bg-surface);
}

tbody tr:nth-child(even) {
  background: rgba(30, 41, 59, 0.38);
}

tbody th,
tbody td {
  padding: 0.7rem 0.85rem;
  border-block-end: 1px solid var(--border-subtle);
  vertical-align: middle;
  text-align: start;
}

tbody th {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
  background: #131c2e;
  font-weight: 700;
  color: var(--text-primary);
}

.action-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

/* Stacked Mobile Table via @container query */
@container (width < 680px) {
  table,
  caption,
  thead,
  tbody,
  tr,
  th,
  td {
    display: block;
  }

  thead {
    display: none;
  }

  tbody tr {
    margin: 0.65rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--bg-card);
  }

  tbody th {
    position: static;
    background: #1e293b;
    border-block-end: 1px solid var(--border-strong);
    padding: 0.6rem 0.8rem;
  }

  tbody td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.52rem 0.8rem;
  }

  tbody td::before {
    font-weight: 600;
    color: var(--text-muted);
    flex-shrink: 0;
  }

  td:nth-child(2)::before { content: var(--label-2) ": "; }
  td:nth-child(3)::before { content: var(--label-3) ": "; }
  td:nth-child(4)::before { content: var(--label-4) ": "; }
  td:nth-child(5)::before { content: var(--label-5) ": "; }
  td:nth-child(6)::before { content: var(--label-6) ": "; }
  td:nth-child(7)::before { content: var(--label-7) ": "; }
  td:nth-child(8)::before { content: var(--label-8) ": "; }
}

/* System Monitoring Grid */
.monitor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
  gap: 0.95rem;
}

.monitor-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.monitor-card h3 {
  margin: 0;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.kv-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.83rem;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding-block: 0.25rem;
  border-block-end: 1px dashed rgba(148, 163, 184, 0.14);
}

.kv-row span:first-child {
  color: var(--text-muted);
}

.kv-row strong {
  color: var(--text-primary);
  text-align: end;
}

/* One-time license code reveal box */
.one-time-code-box {
  background: #020617;
  border: 2px solid var(--status-active);
  border-radius: var(--radius-md);
  padding: 1rem;
  text-align: center;
}

.one-time-code-value {
  font-family: "SFMono-Regular", Consolas, Menlo, monospace;
  font-size: clamp(1.05rem, 0.95rem + 0.6vw, 1.35rem);
  font-weight: 800;
  color: #34d399;
  letter-spacing: 0.06em;
  word-break: break-all;
  margin-block: 0.4rem;
}

/* Alert Box */
.alert-box {
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius-sm);
  font-size: 0.84rem;
  display: none;
}

.alert-box.show {
  display: block;
}

.alert-error {
  background: var(--status-danger-bg);
  border: 1px solid rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}

.alert-success {
  background: var(--status-active-bg);
  border: 1px solid rgba(16, 185, 129, 0.45);
  color: #6ee7b7;
}

/* Mobile Bottom Nav (Hidden on Desktop, Sticky on Mobile) */
.mobile-bottom-nav {
  display: none;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
    padding-block-end: 78px;
  }

  .sidebar {
    display: none;
  }

  .topbar {
    flex-wrap: wrap;
    padding: 0.65rem 0.85rem;
    gap: 0.5rem;
  }

  .topbar-actions {
    flex-wrap: wrap;
    inline-size: 100%;
    justify-content: flex-start;
  }

  .main-content {
    padding: 0.85rem;
    overflow-x: hidden;
  }

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

  .kpi-card {
    padding: 0.8rem 0.85rem;
  }

  .table-wrapper {
    max-block-size: none;
    overflow-x: hidden;
  }

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

  .mobile-bottom-nav {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    position: fixed;
    inset-block-end: 0;
    inset-inline: 0;
    background: rgba(15, 23, 42, 0.96);
    backdrop-filter: blur(14px);
    border-block-start: 1px solid var(--border-strong);
    z-index: 40;
    padding: 0.25rem 0.2rem;
  }

  .mobile-nav-btn {
    min-block-size: 52px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.15rem;
    font-family: inherit;
    font-size: 0.68rem;
    font-weight: 600;
    border-radius: 8px;
    cursor: pointer;
    padding: 0.2rem;
  }

  .mobile-nav-btn.active {
    color: #38bdf8;
    background: rgba(14, 165, 233, 0.15);
  }
}
