:root {
  --bg: #0b0d12;
  --bg-elevated: #12151c;
  --bg-soft: #181c27;
  --border: #2a3142;
  --text: #eef1f8;
  --muted: #9aa3b8;
  --primary: #5b5cff;
  --primary-hover: #7475ff;
  --danger: #ff5b6e;
  --danger-soft: rgba(255, 91, 110, 0.12);
  --success: #3ecf8e;
  --success-soft: rgba(62, 207, 142, 0.12);
  --warning: #f5b942;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --radius-sm: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; min-height: 100%; }
body {
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(91, 92, 255, 0.18), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(62, 207, 142, 0.08), transparent 50%),
    var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--primary-hover); text-decoration: none; }
a:hover { text-decoration: underline; }

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

.boot {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--muted);
}
.boot-disc {
  width: 48px;
  height: 48px;
  display: block;
}

/* Auth layout */
.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.auth-card {
  width: min(440px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  padding: 32px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.brand-auth {
  display: flex;
  justify-content: center;
  margin: 0 0 8px;
}
.brand-logo-text {
  display: block;
  width: min(220px, 72%);
  height: auto;
  margin: 0 auto 4px;
}
.brand-disc {
  width: 40px;
  height: 40px;
  display: block;
  flex-shrink: 0;
  border-radius: 50%;
}
.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, #5b5cff, #8b5cf6);
  display: grid;
  place-items: center;
  font-weight: 800;
  letter-spacing: -0.03em;
}
.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.03em;
}
.auth-card .lead {
  color: var(--muted);
  margin: 0 0 24px;
  font-size: 0.95rem;
  text-align: center;
}
.auth-switch {
  margin-top: 18px;
  color: var(--muted);
  font-size: 0.92rem;
  text-align: center;
}

/* App shell — fixed collapsible sidebar */
:root {
  --sidebar-width: 250px;
  --sidebar-collapsed-width: 72px;
}
.app-shell {
  min-height: 100vh;
}
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  z-index: 30;
  display: flex;
  flex-direction: column;
  background: rgba(18, 21, 28, 0.96);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  backdrop-filter: blur(10px);
  overflow-x: hidden;
  overflow-y: auto;
  transition: width 0.18s ease;
}
.sidebar-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 18px;
}
.sidebar .brand {
  margin: 0;
  padding: 0 4px;
  align-items: center;
  min-width: 0;
  flex: 1;
}
.sidebar .brand-disc {
  width: 40px;
  height: 40px;
}
.brand-text {
  min-width: 0;
}
.brand-sub {
  font-size: 0.8rem;
}
.sidebar-toggle {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}
.sidebar-toggle:hover {
  color: var(--text);
  background: #222738;
}
.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 auto;
}
.nav a, .nav button.linkish {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  border: 0;
  background: transparent;
  cursor: pointer;
  text-align: left;
  width: 100%;
  text-decoration: none;
  white-space: nowrap;
}
.nav-icon {
  flex-shrink: 0;
  opacity: 0.9;
}
.nav-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav a:hover, .nav button.linkish:hover {
  background: var(--bg-soft);
  color: var(--text);
  text-decoration: none;
}
.nav a.active {
  background: rgba(91, 92, 255, 0.14);
  color: #cfd0ff;
  font-weight: 600;
}
.sidebar-footer {
  margin-top: auto;
  padding: 12px 8px 4px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
}
.sidebar-footer strong {
  display: block;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-footer-detail {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main {
  margin-left: var(--sidebar-width);
  padding: 28px clamp(16px, 3vw, 36px) 48px;
  min-width: 0;
  min-height: 100vh;
  transition: margin-left 0.18s ease;
}

/* Collapsed sidebar */
.app-shell.sidebar-collapsed .sidebar {
  width: var(--sidebar-collapsed-width);
  padding-left: 10px;
  padding-right: 10px;
}
.app-shell.sidebar-collapsed .main {
  margin-left: var(--sidebar-collapsed-width);
}
.app-shell.sidebar-collapsed .brand-text,
.app-shell.sidebar-collapsed .nav-label,
.app-shell.sidebar-collapsed .sidebar-footer-detail,
.app-shell.sidebar-collapsed .sidebar-footer-name {
  display: none;
}
.app-shell.sidebar-collapsed .sidebar-top {
  flex-direction: column;
  align-items: center;
}
.app-shell.sidebar-collapsed .brand {
  justify-content: center;
  padding: 0;
}
.app-shell.sidebar-collapsed .nav a,
.app-shell.sidebar-collapsed .nav button.linkish {
  justify-content: center;
  padding: 12px 8px;
}
.app-shell.sidebar-collapsed .sidebar-footer {
  text-align: center;
  padding-left: 4px;
  padding-right: 4px;
}

@media (max-width: 860px) {
  .sidebar {
    width: var(--sidebar-collapsed-width);
    padding-left: 10px;
    padding-right: 10px;
  }
  .main {
    margin-left: var(--sidebar-collapsed-width);
  }
  .app-shell:not(.sidebar-collapsed) .sidebar {
    width: min(250px, 80vw);
    box-shadow: var(--shadow);
  }
  .app-shell:not(.sidebar-collapsed) .main {
    margin-left: var(--sidebar-collapsed-width);
  }
  /* On small screens, expanded menu overlays content */
  .app-shell:not(.sidebar-collapsed) .brand-text,
  .app-shell:not(.sidebar-collapsed) .nav-label,
  .app-shell:not(.sidebar-collapsed) .sidebar-footer-detail,
  .app-shell:not(.sidebar-collapsed) .sidebar-footer-name {
    display: block;
  }
  .app-shell.sidebar-collapsed .brand-text,
  .app-shell.sidebar-collapsed .nav-label,
  .app-shell.sidebar-collapsed .sidebar-footer-detail,
  .app-shell.sidebar-collapsed .sidebar-footer-name {
    display: none;
  }
}
.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}
.page-header h2 {
  margin: 0;
  font-size: 1.55rem;
  letter-spacing: -0.03em;
}
.page-header p {
  margin: 4px 0 0;
  color: var(--muted);
}

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.panel-body { padding: 18px; }
.panel + .panel { margin-top: 18px; }

.form-grid {
  display: grid;
  gap: 14px;
}
.form-grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 700px) {
  .form-grid.two { grid-template-columns: 1fr; }
}

label {
  display: grid;
  gap: 6px;
  font-size: 0.88rem;
  color: var(--muted);
}
label.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  grid-auto-flow: column;
}
label.checkbox input { width: auto; }

input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
select,
textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(91, 92, 255, 0.2);
}
textarea { min-height: 90px; resize: vertical; }

.roles-grid {
  display: grid;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.roles-grid label {
  color: var(--text);
  font-size: 0.92rem;
}
.roles-grid .hint {
  color: var(--muted);
  font-size: 0.8rem;
  margin-left: 22px;
  margin-top: -4px;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  background: var(--bg-soft);
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: #222738; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger {
  background: var(--danger-soft);
  color: #ff8a97;
  border-color: rgba(255, 91, 110, 0.25);
}
.btn-danger:hover { background: rgba(255, 91, 110, 0.22); }
.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}
.btn-sm { padding: 6px 10px; font-size: 0.86rem; }

.list-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 18px;
  border-bottom: 1px solid var(--border);
}
.filter-field {
  flex: 1 1 260px;
  min-width: min(100%, 260px);
  max-width: 480px;
  margin: 0;
}
.filter-field input[type="search"] {
  margin-top: 4px;
}

.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
th, td {
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: rgba(0,0,0,0.15);
  white-space: nowrap;
  user-select: none;
}
th.sortable {
  padding: 0;
}
th.sortable .th-sort-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 12px 14px;
  margin: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  cursor: pointer;
  text-align: left;
}
th.sortable .th-sort-btn:hover {
  color: var(--text);
  background: rgba(91, 92, 255, 0.08);
}
th.sortable.is-sorted .th-sort-btn {
  color: #cfd0ff;
}
th.sortable .sort-indicator {
  font-size: 0.65rem;
  opacity: 0.85;
  min-width: 0.75em;
}
tr:last-child td { border-bottom: 0; }
tr:hover td { background: rgba(255,255,255,0.015); }

.mono { font-family: var(--mono); font-size: 0.88rem; }
.muted { color: var(--muted); }
.truncate {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: rgba(91, 92, 255, 0.14);
  color: #cfd0ff;
  margin: 0 4px 4px 0;
}
.badge.ok { background: var(--success-soft); color: #7be8b4; }
.badge.warn { background: rgba(245, 185, 66, 0.14); color: #f5b942; }
.badge.danger { background: var(--danger-soft); color: #ff8a97; }

.alert {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  font-size: 0.92rem;
}
.alert-error {
  background: var(--danger-soft);
  border: 1px solid rgba(255, 91, 110, 0.3);
  color: #ffb3bc;
}
.alert-success {
  background: var(--success-soft);
  border: 1px solid rgba(62, 207, 142, 0.3);
  color: #9beec8;
}
.alert-info {
  background: rgba(91, 92, 255, 0.12);
  border: 1px solid rgba(91, 92, 255, 0.28);
  color: #cfd0ff;
}

.empty {
  padding: 36px 18px;
  text-align: center;
  color: var(--muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 12, 0.72);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}
.modal {
  width: min(520px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  max-height: min(92vh, 900px);
  overflow: auto;
}
.modal.modal-wide {
  width: min(720px, 100%);
}
.modal h3 { margin: 0 0 8px; }
.modal p { margin: 0 0 16px; color: var(--muted); }
.modal .btn-row { justify-content: flex-end; margin-top: 8px; }

/* QR code UI */
.qr-cell {
  width: 40px;
  vertical-align: top;
  padding-top: 10px;
}
.qr-hover {
  position: relative;
  display: inline-flex;
  align-items: flex-start;
  justify-content: center;
  width: 28px;
  height: 28px;
}
.qr-thumb {
  width: 24px;
  height: 24px;
  image-rendering: pixelated;
  border-radius: 4px;
  background: #fff;
  border: 1px solid var(--border);
  cursor: pointer;
  display: block;
}
.qr-popover {
  display: none;
  position: absolute;
  /* Open to the left of the QR thumb / pointer */
  right: calc(100% + 10px);
  left: auto;
  top: 0;
  transform: none;
  z-index: 40;
  width: max-content;
  max-width: min(420px, calc(100vw - 48px));
  padding: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}
/* Open via JS class so we can linger 100ms after mouseleave */
.qr-hover.is-open .qr-popover {
  display: block;
}

/* Links table helpers */
.short-link-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover {
  color: var(--text);
  background: var(--bg-soft);
}
.icon-btn.is-copied {
  color: var(--success);
  border-color: rgba(62, 207, 142, 0.35);
}
.col-clicks {
  width: 3.5rem;
  max-width: 3.5rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  vertical-align: top;
  padding-left: 6px !important;
  padding-right: 8px !important;
  padding-top: 10px;
}
th.col-clicks {
  width: 3.5rem;
  max-width: 3.5rem;
  text-align: right;
  padding-left: 6px !important;
  padding-right: 8px !important;
}
th.col-clicks .th-sort-btn {
  justify-content: flex-end;
  width: 100%;
  padding-left: 0;
  padding-right: 0;
}
th.col-qr {
  width: 2.5rem;
  max-width: 2.5rem;
  text-align: center;
}
#links-table td {
  vertical-align: top;
}
.col-actions {
  width: 3rem;
  text-align: right;
  vertical-align: top;
  padding-top: 6px;
}
.actions-menu {
  position: relative;
  display: inline-flex;
  justify-content: flex-end;
}
.actions-menu-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.actions-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 35;
  min-width: 150px;
  padding: 6px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.actions-menu-panel[hidden] {
  display: none !important;
}
.actions-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
}
.actions-menu-item:hover {
  background: var(--bg-soft);
}
.actions-menu-item.danger {
  color: #ff8a97;
}
.actions-menu-item.danger:hover {
  background: var(--danger-soft);
}
.qr-popover-layout {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 14px;
}
.qr-popover-preview {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  background: #fff;
  border-radius: 8px;
  padding: 8px;
  border: 1px solid var(--border);
}
.qr-popover-preview img {
  width: 120px;
  height: 120px;
  image-rendering: pixelated;
  display: block;
}
.qr-popover-meta {
  flex: 1 1 160px;
  min-width: 140px;
  max-width: 220px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  justify-content: center;
  align-self: stretch;
}
.qr-popover-url {
  line-height: 1.35;
}
.qr-popover-actions {
  justify-content: flex-start;
  margin-top: auto;
  flex-wrap: wrap;
  gap: 8px;
}
.qr-url-kind-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  align-items: center;
}
.qr-url-kind-compact {
  gap: 8px 12px;
}
.qr-url-kind-compact .checkbox {
  font-size: 0.8rem;
}
.qr-url-fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin: 0;
}
.qr-url-fieldset legend {
  padding: 0 6px;
  color: var(--muted);
  font-size: 0.8rem;
}
.checkbox.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.qr-edit-panel {
  grid-column: 1 / -1;
  margin-top: 4px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}
.qr-edit-layout {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 16px;
  align-items: start;
}
@media (max-width: 640px) {
  .qr-edit-layout { grid-template-columns: 1fr; }
}
.qr-preview-frame {
  display: grid;
  place-items: center;
  background: #fff;
  border-radius: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  min-height: 180px;
}
.qr-preview-frame img {
  max-width: 180px;
  max-height: 180px;
  image-rendering: pixelated;
}
.qr-error-fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin: 0;
  display: grid;
  gap: 10px;
}
.qr-error-fieldset legend {
  padding: 0 6px;
  color: var(--muted);
  font-size: 0.8rem;
}
.qr-settings input[type="color"] {
  width: 100%;
  height: 40px;
  padding: 4px;
  cursor: pointer;
  background: var(--bg-elevated);
}

.custom-slug-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.custom-slug-row input {
  flex: 1 1 160px;
  min-width: 140px;
}
.slug-status.ok { color: #7be8b4; }
.slug-status.bad { color: #ff8a97; }
textarea.is-invalid,
input.is-invalid {
  border-color: #ff8a97;
  box-shadow: 0 0 0 1px rgba(255, 138, 151, 0.35);
}
.sa-nav-link {
  margin-top: 8px;
  color: #f5b942 !important;
}

/* Click tracking report */
.report-root { margin-top: 4px; }
.report-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.report-h3 {
  margin: 0 0 10px;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
}
.bar-track {
  height: 8px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #5b5cff, #3ecf8e);
  border-radius: 999px;
  min-width: 2px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.stat {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.stat .label { color: var(--muted); font-size: 0.8rem; }
.stat .value { font-size: 1.4rem; font-weight: 700; letter-spacing: -0.03em; }

.copy-btn { margin-left: 6px; }

.hidden { display: none !important; }
