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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #21253a;
  --border: #2a2d3a;
  --text: #e2e4ec;
  --muted: #7a7f96;
  --accent: #4f6ef7;
  --accent-hover: #3d5ce5;
  --success: #3ecf8e;
  --danger: #e05252;
  --danger-hover: #c94141;
  --warn: #f5a623;
  --radius: 8px;
  --mono: 'JetBrains Mono', 'Fira Code', monospace;
  --sidebar-w: 220px;
}

:root[data-theme="light"] {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface2: #eef1f8;
  --border: #d8deea;
  --text: #202432;
  --muted: #667085;
  --accent: #335cff;
  --accent-hover: #2348d6;
  --success: #168a5f;
  --danger: #c83f49;
  --danger-hover: #a8323a;
  --warn: #a86300;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  min-height: 100vh;
  line-height: 1.5;
}

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

/* ── Auth screens ────────────────────────────────────────────────────────── */

.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 44px 40px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.auth-card .logo {
  font-size: 28px;
  margin-bottom: 16px;
}

.auth-card h1 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
}

.auth-card p {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 28px;
  line-height: 1.6;
}

.auth-card .btn-lark {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  padding: 12px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}

.lark-logo {
  width: 22px;
  height: 22px;
  object-fit: contain;
}

.auth-card .btn-lark:hover {
  background: var(--accent-hover);
  text-decoration: none;
}

.auth-card .status-icon {
  font-size: 48px;
  margin-bottom: 16px;
  display: block;
}

.auth-card .pending-info {
  background: rgba(245,166,35,0.1);
  border: 1px solid rgba(245,166,35,0.3);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 13px;
  color: var(--warn);
  margin-top: 20px;
}

.auth-card .rejected-info {
  background: rgba(224,82,82,0.1);
  border: 1px solid rgba(224,82,82,0.3);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 13px;
  color: var(--danger);
  margin-top: 20px;
}

/* ── App shell ───────────────────────────────────────────────────────────── */

.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */

.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 10;
}

.sidebar-logo {
  padding: 20px 18px 16px;
  border-bottom: 1px solid var(--border);
}

.theme-toggle {
  position: absolute;
  top: 14px;
  right: 12px;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

.theme-toggle:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.sidebar-logo .logo-name {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--text);
}

.sidebar-logo .logo-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  text-decoration: none;
}

.nav-item:hover {
  background: rgba(255,255,255,0.05);
  color: var(--text);
}

.nav-item.active {
  background: rgba(79,110,247,0.15);
  color: var(--accent);
}

.nav-item .nav-icon { font-size: 15px; min-width: 18px; text-align: center; }

.sidebar-user {
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  overflow: hidden;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-details { flex: 1; min-width: 0; }
.user-name { font-size: 12px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-email { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn-logout {
  width: 100%;
  padding: 7px 12px;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.12s;
}

.btn-logout:hover { border-color: var(--danger); color: var(--danger); }

/* ── Main content ────────────────────────────────────────────────────────── */

.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  padding: 32px;
}

.page-header {
  margin-bottom: 28px;
}

.page-header h1 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 4px;
}

.page-header p {
  color: var(--muted);
  font-size: 13px;
}

/* ── Cards ───────────────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.card-body { padding: 20px; }

/* ── Tables ──────────────────────────────────────────────────────────────── */

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  text-align: left;
  padding: 10px 16px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: rgba(255,255,255,0.02); }
:root[data-theme="light"] tbody tr:hover td { background: rgba(51,92,255,0.04); }

.td-mono { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.td-muted { font-size: 12px; color: var(--muted); white-space: nowrap; }
.td-actions { display: flex; gap: 6px; justify-content: flex-end; white-space: nowrap; }

.empty-row td {
  text-align: center;
  padding: 48px;
  color: var(--muted);
  font-size: 13px;
}

/* ── Status badges ───────────────────────────────────────────────────────── */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-approved, .badge-signed { background: rgba(62,207,142,0.15); color: var(--success); }
.badge-pending, .badge-processing, .badge-waiting_placement { background: rgba(245,166,35,0.15); color: var(--warn); }
.badge-rejected, .badge-error { background: rgba(224,82,82,0.15); color: var(--danger); }
.badge-waiting_signature { background: rgba(79,110,247,0.15); color: var(--accent); }
.badge-active { background: rgba(62,207,142,0.15); color: var(--success); }

/* ── Status dot (online/offline) ─────────────────────────────────────────── */

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.online { background: var(--success); box-shadow: 0 0 4px var(--success); }
.status-dot.offline { background: var(--muted); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */

.btn {
  padding: 8px 14px;
  border: none;
  border-radius: var(--radius);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.12s;
  white-space: nowrap;
}

.btn i { font-size: 12px; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary { background: var(--surface2); border: 1px solid var(--border); color: var(--text); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-danger { background: transparent; border: 1px solid var(--border); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

.btn-warn { background: transparent; border: 1px solid var(--border); color: var(--warn); }
.btn-warn:hover { background: var(--warn); color: #000; border-color: var(--warn); }

.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--muted); }
.btn-ghost:hover { border-color: var(--success); color: var(--success); }

.btn-sm { padding: 5px 10px; font-size: 11px; }
.btn-full { width: 100%; justify-content: center; }

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

/* ── Forms ───────────────────────────────────────────────────────────────── */

.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.04em; }
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 9px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.12s;
  font-family: inherit;
}

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

.form-select { appearance: none; cursor: pointer; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%237a7f96' d='M6 8L1 3h10z'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; padding-right: 28px; }
.form-textarea { resize: vertical; min-height: 80px; }

.form-row { display: flex; gap: 12px; }
.form-row .form-group { flex: 1; }

.form-hint { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* ── Inline form (create token/connection) ───────────────────────────────── */

.inline-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.inline-form .form-input { flex: 1; }

/* ── Modals ──────────────────────────────────────────────────────────────── */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal h2 { font-size: 18px; font-weight: 700; margin-bottom: 8px; }
.modal .modal-desc { color: var(--muted); font-size: 13px; margin-bottom: 24px; }
.modal-footer { display: flex; gap: 10px; justify-content: flex-end; margin-top: 24px; }

.token-reveal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  font-family: var(--mono);
  font-size: 13px;
  word-break: break-all;
  color: var(--success);
  position: relative;
  line-height: 1.6;
}

/* ── Toast notifications ─────────────────────────────────────────────────── */

#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 13px;
  max-width: 320px;
  pointer-events: all;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
  animation: toast-in 0.2s ease;
}

@keyframes toast-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.toast.toast-success { border-left: 3px solid var(--success); }
.toast.toast-error { border-left: 3px solid var(--danger); }
.toast.toast-warn { border-left: 3px solid var(--warn); }
.toast.toast-info { border-left: 3px solid var(--accent); }

/* ── Spinner ─────────────────────────────────────────────────────────────── */

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  display: inline-block;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Loading state ───────────────────────────────────────────────────────── */

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px;
  color: var(--muted);
}

/* ── Signature placement page ────────────────────────────────────────────── */

.placement-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.placement-header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.placement-header h1 {
  font-size: 18px;
  line-height: 1.2;
  margin-bottom: 3px;
}

.placement-header p {
  color: var(--muted);
  font-size: 12px;
}

.placement-workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 18px;
  overflow: auto;
}

.placement-page-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.placement-viewer {
  position: relative;
  background: #fff;
  border: 1px solid var(--border);
  box-shadow: 0 10px 32px rgba(0,0,0,0.35);
  touch-action: none;
}

.placement-viewer canvas {
  display: block;
}

.placement-signature-box {
  position: absolute;
  border: 2px solid var(--accent);
  background: rgba(79,110,247,0.14);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  cursor: grab;
  user-select: none;
  touch-action: none;
}

.placement-signature-box:active {
  cursor: grabbing;
}

.placement-status {
  min-height: 22px;
  color: var(--muted);
  font-size: 13px;
}

.placement-status[data-type="success"] { color: var(--success); }
.placement-status[data-type="error"] { color: var(--danger); }

/* ── Field mapping grid ──────────────────────────────────────────────────── */

.field-mapping-grid {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px 12px;
  align-items: center;
  margin-bottom: 16px;
}

.field-mapping-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  padding: 8px 0;
}

.field-mapping-arrow {
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

.detected-connection {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0 16px;
}

.detected-connection > div {
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.detected-connection span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 3px;
}

.detected-connection strong {
  display: block;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toggle {
  width: 42px;
  height: 24px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  cursor: pointer;
  transition: all 0.15s;
}

.toggle span {
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--muted);
  transition: all 0.15s;
}

.toggle.on {
  border-color: var(--success);
  background: rgba(62,207,142,0.14);
}

.toggle.on span {
  transform: translateX(18px);
  background: var(--success);
}

/* ── Connection URL input ────────────────────────────────────────────────── */

.url-field-wrap {
  display: flex;
  gap: 8px;
}

.url-field-wrap .form-input { flex: 1; }

/* ── Section spacing ─────────────────────────────────────────────────────── */

.section { margin-bottom: 28px; }

/* ── Responsive ──────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    height: auto;
    min-height: auto;
    position: sticky;
    top: 0;
    flex-direction: row;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0;
    z-index: 20;
  }

  .sidebar-logo {
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 12px 16px;
    flex-shrink: 0;
  }

  .theme-toggle {
    position: static;
    margin-left: 8px;
    flex-shrink: 0;
  }

  .sidebar-nav {
    display: flex;
    flex-direction: row;
    padding: 8px;
    gap: 4px;
    overflow-x: auto;
  }

  .nav-item { padding: 8px 10px; white-space: nowrap; }
  .nav-item .nav-label { display: none; }

  .sidebar-user { display: none; }

  .main-content {
    margin-left: 0;
    padding: 16px;
  }

}

/* ── Utility ─────────────────────────────────────────────────────────────── */

.hidden { display: none !important; }
.text-muted { color: var(--muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warn { color: var(--warn); }
.text-mono { font-family: var(--mono); font-size: 12px; }
.mt-12 { margin-top: 12px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.gap-8 { gap: 8px; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.w-full { width: 100%; }
