:root {
    --bg: #f7f8fa;
    --surface: #ffffff;
    --border: #e6e8ec;
    --border-strong: #d4d7dd;
    --text: #1f2433;
    --text-muted: #6b7280;
    --accent: #7c5ce6;
    --accent-strong: #6748d4;
    --accent-soft: #efeaff;
    --danger: #d64545;
    --danger-bg: #fdecec;
    --success: #2f8f5f;
    --success-bg: #e7f6ee;
    --shadow-card: 0 1px 2px rgba(15, 6, 53, 0.04), 0 1px 8px rgba(15, 6, 53, 0.04);
}

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

html, body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ---------- Top bar ---------- */

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 32px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 10;
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.topbar-logo {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: block;
}

.topbar-title {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text);
    letter-spacing: -0.01em;
}

.topbar-user {
    display: flex;
    align-items: center;
    gap: 12px;
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--accent-soft);
    border: 1px solid var(--border);
}

.user-email {
    color: var(--text);
    font-size: 0.92rem;
    font-weight: 500;
}

/* ---------- Page layout ---------- */

.page {
    flex: 1;
    width: 100%;
    max-width: 880px;
    margin: 0 auto;
    padding: 32px 24px 48px;
}

.signin-section {
    display: flex;
    justify-content: center;
    padding-top: 48px;
}

.section + .section,
.card + .card {
    margin-top: 16px;
}

/* ---------- Card ---------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 28px 32px;
    box-shadow: var(--shadow-card);
}

.card-narrow {
    max-width: 420px;
    width: 100%;
}

.card h2 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
    letter-spacing: -0.01em;
}

.form-description {
    color: var(--text-muted);
    font-size: 0.92rem;
    margin-bottom: 22px;
}

/* ---------- Form ---------- */

.form-group {
    margin-bottom: 18px;
}

.form-group label {
    display: block;
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--text);
    margin-bottom: 6px;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    background: var(--surface);
    font-size: 0.95rem;
    color: var(--text);
    font-family: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.form-group select {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%236b7280' d='M2.5 4.5L6 8l3.5-3.5z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.hint {
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-top: 6px;
}

/* ---------- Buttons ---------- */

.btn {
    border: 1px solid transparent;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 500;
    padding: 10px 18px;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, border-color 0.15s, box-shadow 0.15s, transform 0.05s;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    width: 100%;
    margin-top: 4px;
}

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

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

.btn-primary:disabled {
    background: var(--accent-soft);
    color: var(--accent-strong);
    cursor: not-allowed;
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border-color: var(--border-strong);
}

.btn-ghost:hover {
    color: var(--text);
    background: var(--bg);
}

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

.btn-secondary:hover { background: var(--border); }

.btn-small {
    padding: 6px 12px;
    font-size: 0.82rem;
}

/* ---------- Result card ---------- */

#result-content {
    color: var(--text);
}

.description {
    color: var(--text-muted);
    font-size: 0.92rem;
    margin-bottom: 18px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.token-section,
.repos-section,
.instructions-section {
    margin-top: 22px;
    padding-top: 18px;
    border-top: 1px solid var(--border);
}

.token-section h3,
.repos-section h3,
.instructions-section h3 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 10px;
}

.token-display {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    align-items: center;
}

.token-display code {
    flex: 1;
    min-width: 240px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px 12px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
    font-size: 0.82rem;
    color: var(--text);
    word-break: break-all;
}

.repos-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.repos-list li {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px 12px;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.repos-list a {
    color: var(--accent);
    text-decoration: none;
    word-break: break-all;
}

.repos-list a:hover { text-decoration: underline; }

.code-block {
    position: relative;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 12px;
}

.code-block pre {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
    font-size: 0.82rem;
    color: var(--text);
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    margin-bottom: 10px;
}

.instructions-section p {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 8px;
}

.instructions-section code {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 6px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
    font-size: 0.85rem;
}

/* ---------- Status badges ---------- */

.status-badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    border: 1px solid transparent;
    letter-spacing: 0.01em;
}

.status-new {
    background: var(--success-bg);
    color: var(--success);
    border-color: rgba(47, 143, 95, 0.2);
}

.status-existing {
    background: var(--accent-soft);
    color: var(--accent-strong);
    border-color: rgba(124, 92, 230, 0.2);
}

.status-failed {
    background: var(--danger-bg);
    color: var(--danger);
    border-color: rgba(214, 69, 69, 0.2);
}

/* ---------- Error card ---------- */

.error-card {
    border-color: rgba(214, 69, 69, 0.3);
    background: #fff;
}

.error-card h2 { color: var(--danger); }

#error-content {
    background: var(--danger-bg);
    color: var(--danger);
    padding: 12px 14px;
    border-radius: 6px;
    font-size: 0.9rem;
}

/* ---------- Loading spinner ---------- */

.loading {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-left: 8px;
    vertical-align: -2px;
}

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

/* ---------- Registered Customers ---------- */

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 6px;
}

.card-header h2 { margin-bottom: 0; }

.tenant-node,
.customer-node {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0;
    margin-bottom: 8px;
}

.tenant-node > summary,
.customer-node > summary {
    list-style: none;
    cursor: pointer;
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    user-select: none;
    border-radius: 8px;
}

.tenant-node > summary::-webkit-details-marker,
.customer-node > summary::-webkit-details-marker {
    display: none;
}

.tenant-node > summary:hover,
.customer-node > summary:hover {
    background: var(--bg);
}

.caret {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid var(--text-muted);
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    transition: transform 0.15s;
}

details[open] > summary > .caret {
    transform: rotate(90deg);
}

.tenant-name {
    font-weight: 600;
    color: var(--text);
}

.customer-name {
    color: var(--text);
    font-weight: 500;
}

.muted-count,
.muted-meta {
    color: var(--text-muted);
    font-size: 0.82rem;
    margin-left: auto;
}

.customer-list {
    list-style: none;
    padding: 4px 14px 12px 28px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.customer-list > li {
    margin: 0;
}

.customer-node {
    margin-bottom: 0;
    border-color: var(--border);
    background: var(--bg);
}

.customer-repos {
    list-style: none;
    padding: 0 14px 12px 32px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 0.88rem;
}

.customer-repos a {
    color: var(--accent);
    text-decoration: none;
    word-break: break-all;
}

.customer-repos a:hover { text-decoration: underline; }

.repo-row {
    flex-direction: column;
    align-items: stretch !important;
    gap: 6px;
}

.repo-line {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.repo-line .invite-toggle {
    margin-left: auto;
    padding: 4px 10px;
    font-size: 0.78rem;
}

.invite-form {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding-left: 8px;
}

.invite-form input[name="username"] {
    flex: 1;
    min-width: 180px;
    padding: 6px 10px;
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    font-size: 0.85rem;
    font-family: inherit;
    color: var(--text);
    background: var(--surface);
}

.invite-form input[name="username"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.invite-status {
    font-size: 0.82rem;
    color: var(--text-muted);
}

.invite-status.invite-ok { color: var(--success); }
.invite-status.invite-error { color: var(--danger); }

/* ---------- Responsive ---------- */

@media (max-width: 640px) {
    .topbar { padding: 10px 16px; }
    .topbar-title { display: none; }
    .user-email { display: none; }
    .page { padding: 20px 16px 32px; }
    .card { padding: 22px 18px; }
}
