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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 32px 24px;
}

[hidden] {
    display: none !important;
}

/* ---- App shell ---- */
.app {
    position: relative;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    max-width: 880px;
    width: 100%;
    padding: 32px 36px 24px;
}

header {
    text-align: center;
    margin-bottom: 20px;
}

header h1 {
    font-size: 26px;
    font-weight: 700;
    color: #1a1a2e;
}

.subtitle {
    color: #666;
    font-size: 13px;
    margin-top: 4px;
}

/* ---- Presets bar ---- */
.presets-bar {
    margin-bottom: 20px;
}

.presets-label {
    font-size: 12px;
    font-weight: 600;
    color: #98a2b3;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

.presets-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
}

.preset-btn {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 12px 8px;
    border: 2px solid #e4e7ec;
    border-radius: 10px;
    background: #fafbfc;
    cursor: pointer;
    transition: all 0.2s;
}

.preset-btn:hover {
    border-color: #b0baf0;
    background: #f5f6ff;
}

.preset-btn.active {
    border-color: #667eea;
    background: #f0f2ff;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.12);
}

.preset-icon {
    font-size: 22px;
}

.preset-name {
    font-size: 13px;
    font-weight: 600;
    color: #1a1a2e;
}

.preset-desc {
    font-size: 11px;
    color: #98a2b3;
}

/* Save-as-template button */
.preset-save-btn {
    border-style: dashed !important;
    background: transparent !important;
    color: #98a2b3;
}

.preset-save-btn:hover {
    border-color: #667eea !important;
    background: #f5f6ff !important;
    color: #667eea;
}

.preset-save-btn .preset-icon { opacity: .6; }
.preset-save-btn:hover .preset-icon { opacity: 1; }

/* Delete button on custom presets */
.preset-delete {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 20px;
    height: 20px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.06);
    color: #98a2b3;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    cursor: pointer;
    opacity: 0;
    transition: all 0.15s;
    padding: 0;
}

.preset-btn:hover .preset-delete { opacity: 1; }
.preset-delete:hover { background: #fee2e2; color: #ef4444; }

/* ---- Save-template modal ---- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-card {
    background: #fff;
    border-radius: 14px;
    padding: 28px 32px;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.22);
}

.modal-card h3 {
    font-size: 18px;
    color: #1a1a2e;
    margin-bottom: 4px;
}

.modal-hint {
    font-size: 13px;
    color: #98a2b3;
    margin-bottom: 20px;
}

.modal-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 12px;
    color: #667085;
    font-weight: 500;
    margin-bottom: 14px;
}

.modal-field input {
    padding: 9px 12px;
    border: 1px solid #d0d5dd;
    border-radius: 8px;
    font-size: 14px;
    color: #1a1a2e;
    outline: none;
    transition: border-color 0.15s;
}

.modal-field input:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.12);
}

.modal-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 22px;
}

.modal-btn {
    padding: 9px 22px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.modal-btn.cancel { background: #f2f4f7; color: #344054; }
.modal-btn.cancel:hover { background: #e4e7ec; }
.modal-btn.confirm { background: linear-gradient(135deg, #667eea, #764ba2); color: #fff; }
.modal-btn.confirm:hover { box-shadow: 0 4px 12px rgba(102, 126, 234, 0.35); }
.modal-btn.confirm:disabled { opacity: .5; cursor: not-allowed; }

/* ---- Main grid ---- */
.main-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 16px;
}

/* ---- Config panel (left) ---- */
.config-panel {
    background: #f8f9fb;
    border: 1px solid #e4e7ec;
    border-radius: 12px;
    padding: 16px;
    max-height: 560px;
    overflow-y: auto;
}

.panel-title {
    font-size: 14px;
    font-weight: 600;
    color: #344054;
    margin-bottom: 12px;
}

.config-section {
    border: 1px solid #e9ecef;
    border-radius: 8px;
    margin-bottom: 8px;
    background: #fff;
}

.config-section summary {
    padding: 10px 12px;
    font-size: 13px;
    font-weight: 600;
    color: #344054;
    cursor: pointer;
    user-select: none;
    list-style: none;
}

.config-section summary::before {
    content: "▸ ";
    color: #98a2b3;
}

.config-section[open] summary::before {
    content: "▾ ";
}

.config-sub-label {
    padding: 6px 12px 2px;
    font-size: 11px;
    font-weight: 600;
    color: #98a2b3;
    border-top: 1px solid #f0f1f3;
    margin-top: 4px;
}

.config-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 0 12px 12px;
}

.config-grid label.full-width {
    grid-column: 1 / -1;
}

.config-grid label {
    display: flex;
    flex-direction: column;
    gap: 3px;
    font-size: 11px;
    color: #667085;
    font-weight: 500;
}

.config-grid input,
.config-grid select {
    padding: 6px 8px;
    border: 1px solid #d0d5dd;
    border-radius: 6px;
    font-size: 12px;
    color: #1a1a2e;
    background: #fff;
    outline: none;
    transition: border-color 0.15s;
}

.config-grid input:focus,
.config-grid select:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.12);
}

/* Heading level tabs */
.heading-tabs {
    display: flex;
    gap: 4px;
    padding: 0 12px 8px;
}

.htab {
    flex: 1;
    padding: 5px;
    border: 1px solid #d0d5dd;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    background: #fff;
    color: #667085;
    cursor: pointer;
    transition: all 0.15s;
}

.htab:hover {
    background: #f5f6ff;
}

.htab.active {
    background: #667eea;
    color: #fff;
    border-color: #667eea;
}

/* ---- Convert panel (right) ---- */
.convert-panel {
    display: flex;
    flex-direction: column;
}

.upload-area {
    flex: 1;
    border: 2px dashed #d0d5dd;
    border-radius: 12px;
    padding: 48px 24px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;
    background: #fafbfc;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.upload-area:hover,
.upload-area.drag-over {
    border-color: #667eea;
    background: #f0f2ff;
}

.upload-icon {
    font-size: 42px;
    margin-bottom: 10px;
}

.upload-text {
    color: #344054;
    font-size: 14px;
}

.upload-hint {
    color: #98a2b3;
    font-size: 12px;
    margin-top: 4px;
}

.link {
    color: #667eea;
    cursor: pointer;
    text-decoration: underline;
    font-weight: 500;
}

/* File info */
.file-info {
    margin-top: 12px;
}

.file-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: #f8f9fb;
    border: 1px solid #e4e7ec;
    border-radius: 10px;
    padding: 12px 16px;
}

.file-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    color: #fff;
    flex-shrink: 0;
}

.file-badge.md { background: #10b981; }
.file-badge.docx { background: #3b82f6; }
.file-badge.txt { background: #f59e0b; }

.file-name {
    flex: 1;
    font-size: 13px;
    color: #1a1a2e;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-size {
    color: #98a2b3;
    font-size: 12px;
    flex-shrink: 0;
}

.btn-clear {
    background: none;
    border: none;
    color: #98a2b3;
    cursor: pointer;
    font-size: 16px;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.15s;
}

.btn-clear:hover {
    background: #fee2e2;
    color: #ef4444;
}

/* Convert button */
.btn-convert {
    display: block;
    width: 100%;
    margin-top: 12px;
    padding: 13px;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    background: linear-gradient(135deg, #667eea, #764ba2);
    cursor: pointer;
    transition: all 0.2s;
}

.btn-convert:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

.btn-convert:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    background: #b0b8c8;
}

.btn-loading {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

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

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

/* Message */
.message {
    margin-top: 12px;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    text-align: center;
}

.message.success {
    background: #ecfdf5;
    color: #065f46;
    border: 1px solid #a7f3d0;
}

.message.error {
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

footer {
    margin-top: 16px;
    text-align: center;
}

footer p {
    font-size: 11px;
    color: #98a2b3;
}

/* ---- Auth pages (login / register) ---- */
.auth-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    max-width: 400px;
    width: 100%;
    padding: 44px 36px 36px;
    text-align: center;
    margin-top: 10vh;
}

.auth-icon { font-size: 48px; margin-bottom: 10px; }
.auth-card h1 { font-size: 24px; font-weight: 700; color: #1a1a2e; margin-bottom: 4px; }
.auth-hint { color: #98a2b3; font-size: 13px; margin-bottom: 24px; }

.auth-error {
    background: #fef2f2; color: #991b1b; border: 1px solid #fecaca;
    border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 16px;
    text-align: left;
}

.auth-form { text-align: left; }

.auth-field {
    margin-bottom: 16px;
}

.auth-field label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: #667085;
    margin-bottom: 5px;
}

.auth-field input {
    display: block;
    width: 100%;
    padding: 11px 14px;
    border: 2px solid #e4e7ec;
    border-radius: 10px;
    font-size: 14px;
    color: #1a1a2e;
    outline: none;
    transition: border-color 0.2s;
}

.auth-field input:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.12);
}

.auth-btn {
    display: block;
    width: 100%;
    padding: 13px;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    background: linear-gradient(135deg, #667eea, #764ba2);
    cursor: pointer;
    transition: all 0.2s;
    margin-top: 4px;
}

.auth-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.4);
}

.auth-footer {
    margin-top: 20px;
    text-align: center;
    font-size: 13px;
}

.auth-footer a {
    color: #667eea;
    text-decoration: none;
    font-weight: 500;
}

.auth-footer a:hover { text-decoration: underline; }

/* ---- User bar (top of main page) ---- */
.user-bar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    margin-bottom: 4px;
    font-size: 12px;
    color: #98a2b3;
}

.user-bar .user-name { font-weight: 600; color: #344054; }

.user-bar .role-tag {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.role-tag.role-admin { background: #fef3c7; color: #92400e; }
.role-tag.role-pro { background: #dbeafe; color: #1e40af; }
.role-tag.role-free { background: #f3f4f6; color: #6b7280; }

.usage-info {
    font-size: 11px;
    color: #98a2b3;
}

.usage-bar-bg {
    display: inline-block;
    width: 60px;
    height: 5px;
    background: #e4e7ec;
    border-radius: 3px;
    vertical-align: middle;
    margin-left: 4px;
    overflow: hidden;
}

.usage-bar-fill {
    display: block;
    height: 100%;
    border-radius: 3px;
    background: #667eea;
    transition: width 0.3s;
}

.usage-bar-fill.warn { background: #f59e0b; }
.usage-bar-fill.full { background: #ef4444; }

.user-bar a {
    color: #98a2b3;
    text-decoration: none;
    font-size: 12px;
    padding: 3px 10px;
    border: 1px solid #e4e7ec;
    border-radius: 6px;
    transition: all 0.15s;
}

.user-bar a:hover {
    border-color: #667eea;
    color: #667eea;
}

.user-bar a.btn-logout-link:hover {
    background: #fee2e2;
    border-color: #fecaca;
    color: #ef4444;
}

/* ---- Admin page ---- */
.admin-body {
    background: #f0f2f5;
    padding: 0;
    display: block;
}

.admin-shell {
    max-width: 960px;
    margin: 0 auto;
    padding: 24px;
}

.admin-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    text-align: left;
}

.admin-header h1 {
    font-size: 20px;
    font-weight: 700;
    color: #1a1a2e;
}

.admin-nav {
    display: flex;
    gap: 12px;
}

.admin-nav a {
    color: #667eea;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    padding: 6px 14px;
    border-radius: 8px;
    transition: background 0.15s;
}

.admin-nav a:hover { background: rgba(102, 126, 234, 0.1); }

.admin-stats {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 14px;
    margin-bottom: 24px;
}

.stat-card {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    text-align: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.stat-num {
    display: block;
    font-size: 28px;
    font-weight: 700;
    color: #1a1a2e;
    line-height: 1.2;
}

.stat-label {
    font-size: 12px;
    color: #98a2b3;
    margin-top: 4px;
}

.admin-section {
    background: #fff;
    border-radius: 12px;
    padding: 20px 24px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.admin-section h2 {
    font-size: 16px;
    font-weight: 600;
    color: #1a1a2e;
    margin-bottom: 14px;
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
}

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

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

.invite-form select {
    padding: 6px 10px;
    border: 1px solid #d0d5dd;
    border-radius: 6px;
    font-size: 13px;
    color: #344054;
    background: #fff;
}

.tier-info {
    font-size: 12px;
    color: #667085;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.admin-table th {
    text-align: left;
    padding: 10px 12px;
    font-size: 11px;
    font-weight: 600;
    color: #98a2b3;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid #e4e7ec;
}

.admin-table td {
    padding: 10px 12px;
    border-bottom: 1px solid #f0f1f3;
    color: #344054;
}

.admin-table tr:last-child td { border-bottom: none; }

.role-select {
    padding: 4px 8px;
    border: 1px solid #d0d5dd;
    border-radius: 6px;
    font-size: 12px;
    background: #fff;
    color: #344054;
}

.action-cell { white-space: nowrap; }

.btn-sm {
    padding: 5px 12px;
    border: none;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
}

.btn-primary:hover { box-shadow: 0 2px 8px rgba(102,126,234,0.35); }

.btn-ghost {
    background: #f2f4f7;
    color: #344054;
}

.btn-ghost:hover { background: #e4e7ec; }

.btn-danger {
    background: #fee2e2;
    color: #dc2626;
}

.btn-danger:hover { background: #fecaca; }

.status-available { color: #10b981; font-weight: 600; font-size: 12px; }
.status-used { color: #98a2b3; font-size: 12px; }
.empty-hint { text-align: center; color: #98a2b3; padding: 20px; }

.new-codes {
    background: #f0fdf4;
    border: 1px solid #a7f3d0;
    border-radius: 8px;
    padding: 14px 16px;
    margin-bottom: 14px;
    font-size: 13px;
    color: #065f46;
}

.codes-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.copy-code {
    background: #fff;
    border: 1px solid #a7f3d0;
    border-radius: 6px;
    padding: 6px 14px;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 1px;
    cursor: pointer;
    transition: all 0.15s;
}

.copy-code:hover { background: #ecfdf5; }
.copy-code.copied { background: #667eea; color: #fff; border-color: #667eea; }

@media (max-width: 700px) {
    .admin-stats { grid-template-columns: 1fr 1fr; }
    .section-header { flex-direction: column; gap: 10px; align-items: flex-start; }
}

/* ---- Scrollbar ---- */
.config-panel::-webkit-scrollbar {
    width: 5px;
}

.config-panel::-webkit-scrollbar-thumb {
    background: #d0d5dd;
    border-radius: 4px;
}

/* ---- Responsive ---- */
@media (max-width: 700px) {
    .main-grid {
        grid-template-columns: 1fr;
    }
    .presets-list {
        grid-template-columns: 1fr 1fr;
    }
}
