/* ── Reset & Base ─────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: #f5f7fa;
    color: #1a1a2e;
    min-height: 100vh;
}

/* ── Header ──────────────────────────────────────── */
header {
    background: linear-gradient(135deg, #1e1b4b, #312e81, #4338ca);
    color: #fff;
    padding: 22px 32px;
    display: flex;
    align-items: baseline;
    gap: 16px;
    box-shadow: 0 2px 12px rgba(67, 56, 202, .3);
}
header h1 { font-size: 22px; font-weight: 700; letter-spacing: .5px; }
header .subtitle { font-size: 13px; opacity: .7; font-weight: 400; flex: 1; }
.btn-login {
    background: rgba(255,255,255,.15);
    border: 1px solid rgba(255,255,255,.3);
    color: #fff;
    padding: 6px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}
.btn-login:hover { background: rgba(255,255,255,.25); }

/* ── Tabs ────────────────────────────────────────── */
.tabs {
    display: flex;
    gap: 0;
    background: #fff;
    border-bottom: 2px solid #e8e8e8;
    padding: 0 32px;
    box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.tab {
    padding: 12px 20px;
    border: none;
    background: transparent;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    color: #666;
    border-bottom: 3px solid transparent;
    transition: all .2s;
    white-space: nowrap;
}
.tab:hover { color: #4338ca; }
.tab.active {
    color: #4338ca;
    border-bottom-color: #4338ca;
}
.tab.disabled {
    color: #aaa;
    cursor: not-allowed;
    opacity: 0.5;
}
.tab.disabled:hover { color: #aaa; }

/* ── Layout ──────────────────────────────────────── */
.layout {
    display: grid;
    grid-template-columns: 1fr 400px;
    gap: 24px;
    padding: 24px 32px;
    max-width: 1400px;
    margin: 0 auto;
}
@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; padding: 12px; gap: 16px; }
    header { flex-wrap: wrap; padding: 14px 16px; gap: 8px; }
    header h1 { font-size: 18px; }
    header .subtitle { display: none; }
    .btn-login { margin-left: auto; font-size: 12px; padding: 5px 12px; }
    .tabs { padding: 0 8px; overflow-x: auto; white-space: nowrap; }
    .tab { padding: 10px 12px; font-size: 13px; }
    .form-panel { padding: 16px; border-radius: 10px; }
    .task-panel { padding: 16px; border-radius: 10px; position: static; max-height: none; }
    .row { grid-template-columns: 1fr; gap: 8px; }
    .apikey-modal { width: 90vw; padding: 24px 20px; }
    .quota-badge { font-size: 12px; padding: 4px 10px; }
    .history-filters { flex-direction: column; gap: 8px; }
    .tc-media video { max-width: 100%; }
}

/* ── Form Panel ──────────────────────────────────── */
.form-panel {
    background: #fff;
    border-radius: 14px;
    padding: 28px;
    box-shadow: 0 2px 12px rgba(0,0,0,.06);
    border: 1px solid #f0f0f0;
}
.tab-content { display: none; }
.tab-content.active { display: block; }

.field { margin-bottom: 16px; }
.field label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: #333;
}
.field-hint {
    font-size: 12px;
    color: #d97706;
    margin: 6px 0 0;
}
.field textarea,
.field select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    font-size: 14px;
    background: #fafbfc;
    transition: border-color .2s, box-shadow .2s;
}
.field textarea:focus,
.field select:focus {
    outline: none;
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, .1);
    background: #fff;
}

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ── Upload Area ─────────────────────────────────── */
.upload-area {
    border: 2px dashed #d4d4d8;
    border-radius: 10px;
    padding: 24px;
    text-align: center;
    cursor: pointer;
    transition: border-color .2s, background .2s;
    position: relative;
}
.upload-area:hover { border-color: #6366f1; background: #f5f3ff; }
.upload-area input { opacity: 0; position: absolute; inset: 0; cursor: pointer; }
.upload-area p { color: #999; font-size: 13px; }
.upload-area.uploading { opacity: .5; pointer-events: none; }

.preview-row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    flex-wrap: wrap;
}
.preview-item {
    position: relative;
    display: inline-block;
}
.preview-item img,
.preview-item video {
    width: 80px;
    height: 80px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid #e8e8e8;
}
.preview-del {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: none;
    background: #ef4444;
    color: #fff;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    cursor: pointer;
    padding: 0;
}
.preview-del:hover { background: #dc2626; }
.file-tag {
    display: inline-block;
    padding: 4px 10px;
    background: #f0f0f0;
    border-radius: 4px;
    font-size: 12px;
}

/* ── Button ──────────────────────────────────────── */
.btn-primary {
    display: inline-block;
    padding: 12px 32px;
    background: linear-gradient(135deg, #4338ca, #6366f1);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity .2s, transform .1s;
    margin-top: 8px;
    box-shadow: 0 2px 8px rgba(99, 102, 241, .3);
}
.btn-primary:hover { opacity: .9; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: .4; cursor: not-allowed; transform: none; }

.btn-reset {
    display: inline-block;
    padding: 12px 24px;
    background: #fff;
    color: #666;
    border: 1px solid #d4d4d8;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all .2s;
    margin-top: 8px;
    margin-left: 12px;
}
.btn-reset:hover { background: #fee2e2; color: #dc2626; border-color: #fca5a5; }

.panel-disabled .upload-area,
.panel-disabled .ref-editor-wrapper,
.panel-disabled .ref-editor,
.panel-disabled select,
.panel-disabled input[type="checkbox"] {
    pointer-events: none;
    opacity: .5;
}

/* ── Task Panel ──────────────────────────────────── */
.task-panel {
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 100px);
    position: sticky;
    top: 16px;
}
.task-panel h2 {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 8px;
    color: #333;
    flex-shrink: 0;
}
.task-section {
    overflow-y: auto;
    padding-right: 4px;
}
#task-monitor {
    max-height: 40vh;
    margin-bottom: 12px;
}
#task-history {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    padding-right: 4px;
}
.history-filters {
    display: flex;
    gap: 8px;
    margin-bottom: 10px;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.history-filters input[type="text"] {
    flex: 1;
    min-width: 0;
    padding: 6px 10px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    font-size: 13px;
    outline: none;
}
.history-filters input[type="text"]:focus { border-color: #6366f1; }
.history-filters select,
.history-filters input[type="date"] {
    padding: 6px 8px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    font-size: 13px;
    background: #fff;
    cursor: pointer;
}
.load-more-btn {
    width: 100%;
    padding: 8px;
    background: #f5f7fa;
    border: 1px dashed #ccc;
    border-radius: 8px;
    cursor: pointer;
    color: #666;
    font-size: 13px;
    margin-top: 4px;
}
.load-more-btn:hover { background: #eef2f7; }

/* ── Task Card ───────────────────────────────────── */
.task-card {
    background: #fff;
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 1px 6px rgba(0,0,0,.06);
    border: 1px solid #f0f0f0;
}
.task-card .tc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.task-card .tc-type {
    font-size: 12px;
    font-weight: 600;
    padding: 2px 10px;
    border-radius: 6px;
    background: linear-gradient(135deg, #eef2ff, #e0e7ff);
    color: #4338ca;
}
.task-card .tc-id {
    font-size: 11px;
    color: #999;
    font-family: monospace;
}
.task-card .tc-prompt {
    font-size: 13px;
    color: #555;
    margin-bottom: 4px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.task-card .tc-copy-btn {
    font-size: 11px;
    color: #888;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0;
    margin-bottom: 8px;
    text-decoration: underline;
}
.task-card .tc-copy-btn:hover { color: #333; }
.task-card .tc-time {
    font-size: 12px;
    color: #999;
    float: right;
    line-height: 24px;
}
.task-card .tc-status {
    font-size: 12px;
    font-weight: 600;
    padding: 2px 10px;
    border-radius: 10px;
    display: inline-block;
}
.task-card .tc-timer {
    font-size: 12px;
    color: #888;
    margin-left: 8px;
    font-variant-numeric: tabular-nums;
}
.task-card .tc-timer-done {
    color: #155724;
    font-weight: 600;
}
.task-card .tc-cost {
    font-size: 12px;
    color: #d97706;
    margin-left: 8px;
    font-weight: 600;
}
.status-submitted,
.status-processing,
.status-unknown { background: #fef3c7; color: #92400e; }
.status-succeed,
.status-succeeded,
.status-success,
.status-completed,
.status-done { background: #d1fae5; color: #065f46; }
.status-failed,
.status-failure,
.status-error { background: #fee2e2; color: #991b1b; }

.task-card .tc-error-msg {
    font-size: 12px;
    color: #991b1b;
    background: #fef2f2;
    border-left: 3px solid #ef4444;
    padding: 6px 10px;
    margin-top: 6px;
    border-radius: 4px;
    word-break: break-all;
    line-height: 1.5;
}

.field-hint {
    font-size: 12px;
    color: #92400e;
    background: #fffbeb;
    border: 1px solid #fcd34d;
    border-radius: 6px;
    padding: 6px 10px;
    margin-bottom: 8px;
}

.task-card .tc-actions {
    margin-top: 10px;
    display: flex;
    gap: 8px;
}
.task-card .tc-actions button,
.task-card .tc-actions a {
    font-size: 12px;
    padding: 4px 12px;
    border-radius: 6px;
    border: 1px solid #e0e0e0;
    background: #fff;
    cursor: pointer;
    text-decoration: none;
    color: #333;
    transition: all .15s;
}
.task-card .tc-actions button:hover,
.task-card .tc-actions a:hover {
    background: #4338ca;
    color: #fff;
    border-color: #4338ca;
}

/* ── Media Preview ───────────────────────────────── */
.task-card video,
.task-card img.result-img {
    width: 100%;
    border-radius: 10px;
    margin-top: 10px;
    background: #000;
}
.tc-img-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
    margin-top: 10px;
}
.tc-img-grid img {
    width: 100%;
    border-radius: 8px;
    cursor: pointer;
    transition: transform .2s;
    border: 1px solid #e8e8e8;
}
.tc-img-grid img:hover {
    transform: scale(1.03);
}

/* ── Spinner ─────────────────────────────────────── */
.spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid #e0e0e0;
    border-top-color: #4338ca;
    border-radius: 50%;
    animation: spin .6s linear infinite;
    vertical-align: middle;
    margin-right: 4px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Activation Code Modal ──────────────────────── */
.apikey-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}
.apikey-modal {
    background: #fff;
    border-radius: 16px;
    padding: 32px 36px;
    width: 380px;
    max-width: 90vw;
    box-shadow: 0 8px 32px rgba(0, 0, 0, .2);
    text-align: center;
    position: relative;
}
.apikey-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    font-size: 24px;
    color: #999;
    cursor: pointer;
    line-height: 1;
}
.apikey-close:hover {
    color: #333;
}
.apikey-modal h3 {
    font-size: 18px;
    margin-bottom: 8px;
    color: #1a1a2e;
}
.apikey-hint {
    font-size: 13px;
    color: #888;
    margin-bottom: 16px;
}
.apikey-modal input[type="text"] {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    font-size: 15px;
    text-align: center;
    letter-spacing: 1px;
    outline: none;
    transition: border-color .2s;
}
.apikey-modal input[type="text"]:focus {
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, .1);
}
.apikey-error {
    font-size: 13px;
    color: #dc2626;
    margin-top: 8px;
}
.apikey-actions {
    margin-top: 16px;
}
.apikey-actions .btn-primary {
    width: 100%;
}
.apikey-pricing {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid #eee;
}
.apikey-pricing h4 {
    font-size: 13px;
    color: #555;
    margin-bottom: 8px;
}
.apikey-pricing table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}
.apikey-pricing th, .apikey-pricing td {
    padding: 5px 8px;
    text-align: left;
    border-bottom: 1px solid #f0f0f0;
}
.apikey-pricing th {
    background: #f9fafb;
    font-weight: 600;
    color: #666;
}
.apikey-refund-note {
    margin-top: 10px;
    font-size: 12px;
    color: #e67e22;
}

/* ── Quota Badge ────────────────────────────────── */
.quota-badge {
    background: rgba(255, 255, 255, .18);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 8px;
}
.quota-logout {
    background: none;
    border: none;
    font-size: 16px;
    cursor: pointer;
    color: rgba(255,255,255,.6);
    line-height: 1;
    padding: 0 2px;
}
.quota-logout:hover { color: #fca5a5; }

/* ── Image Generation Result ─────────────────────── */
.igen-mode-switch {
    display: flex;
    gap: 0;
    margin-bottom: 16px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    overflow: hidden;
}
.igen-mode {
    flex: 1;
    padding: 8px 0;
    border: none;
    background: #f9f9f9;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    color: #666;
    transition: all .2s;
}
.igen-mode.active {
    background: #4338ca;
    color: #fff;
}
.igen-mode:hover:not(.active) {
    background: #eef2ff;
}
.igen-result {
    margin-top: 16px;
}
.igen-result .igen-loading {
    text-align: center;
    color: #666;
    font-size: 14px;
    padding: 20px 0;
}
.igen-result .igen-error {
    color: #dc2626;
    font-size: 13px;
    background: #fef2f2;
    padding: 10px 14px;
    border-radius: 8px;
    border-left: 3px solid #ef4444;
}
.igen-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 12px;
}
.igen-item {
    position: relative;
    border-radius: 10px;
    overflow: hidden;
    background: #f5f5f5;
    border: 1px solid #e8e8e8;
}
.igen-item img {
    width: 100%;
    display: block;
    cursor: pointer;
    transition: transform .2s;
}
.igen-item img:hover {
    transform: scale(1.02);
}
.igen-item .igen-download {
    display: block;
    text-align: center;
    padding: 6px 0;
    font-size: 12px;
    color: #4338ca;
    text-decoration: none;
    background: #fff;
    border-top: 1px solid #e8e8e8;
}
.igen-item .igen-download:hover {
    background: #4338ca;
    color: #fff;
}

/* ── Mention Editor (@ reference) ─────────────────── */
.ref-editor-wrapper {
    position: relative;
    display: flex;
    align-items: flex-start;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    background: #fafbfc;
    transition: border-color .2s, box-shadow .2s;
}
.ref-editor-wrapper:focus-within {
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, .1);
    background: #fff;
}
.ref-editor {
    flex: 1;
    min-height: 140px;
    max-height: 300px;
    overflow-y: auto;
    padding: 10px 12px;
    padding-right: 40px;
    font-size: 14px;
    line-height: 1.6;
    outline: none;
    word-break: break-word;
}
.ref-editor:empty::before {
    content: attr(data-placeholder);
    color: #999;
    pointer-events: none;
}
.ref-at-btn {
    position: absolute;
    right: 6px;
    top: 6px;
    width: 28px;
    height: 28px;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    background: #fff;
    color: #4338ca;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background .15s, border-color .15s;
}
.ref-at-btn:hover {
    background: #eef2ff;
    border-color: #6366f1;
}

/* Mention Pill */
.mention-pill {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 1px 8px;
    margin: 0 1px;
    background: linear-gradient(135deg, #eef2ff, #e0e7ff);
    border: 1px solid #c7d2fe;
    border-radius: 12px;
    font-size: 13px;
    font-weight: 500;
    color: #4338ca;
    user-select: all;
    -webkit-user-select: all;
    white-space: nowrap;
    vertical-align: baseline;
    line-height: 1.6;
}
.mention-pill .pill-icon {
    font-size: 12px;
}

/* Mention Popover */
.mention-popover {
    display: none;
    position: absolute;
    z-index: 100;
    left: 0;
    right: 0;
    top: 100%;
    margin-top: 4px;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, .1);
    max-height: 240px;
    overflow-y: auto;
    padding: 6px;
}
.mention-popover.visible {
    display: block;
}
.mention-popover .mention-group-label {
    font-size: 11px;
    font-weight: 600;
    color: #888;
    padding: 6px 8px 2px;
    text-transform: uppercase;
}
.mention-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: #333;
    transition: background .1s;
}
.mention-item:hover,
.mention-item.active {
    background: #eef2ff;
}
.mention-item .mi-icon {
    font-size: 16px;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}
.mention-item .mi-thumb {
    width: 28px;
    height: 28px;
    border-radius: 4px;
    object-fit: cover;
    border: 1px solid #e8e8e8;
    flex-shrink: 0;
}
.mention-item .mi-label {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.mention-popover .mention-empty {
    padding: 12px;
    text-align: center;
    color: #999;
    font-size: 13px;
}
