/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --border: #e0e0e0;
    --text: #1a1a1a;
    --text-muted: #666;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #d97706;
    --tag-bg: #e8f0fe;
    --tag-text: #1e40af;
    --radius: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
}

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

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

/* ===== Layout ===== */
.app-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    display: flex;
    align-items: center;
    gap: 24px;
    position: sticky;
    top: 0;
    z-index: 100;
}

.app-header h1 {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
}

.app-header nav { display: flex; gap: 16px; }
.app-header nav a { font-size: 13px; color: var(--text-muted); }
.app-header nav a:hover, .app-header nav a.active { color: var(--primary); }

.header-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.container { max-width: 1000px; margin: 0 auto; padding: 24px; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    font-size: 13px;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
}
.btn:hover { border-color: #ccc; background: #fafafa; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #fef2f2; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Tags / Badges ===== */
.tag {
    display: inline-block;
    padding: 2px 8px;
    font-size: 11px;
    font-weight: 600;
    border-radius: 10px;
    background: var(--tag-bg);
    color: var(--tag-text);
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.tag-pending { background: #fef3c7; color: #92400e; }
.tag-proceed { background: #dcfce7; color: #166534; }
.tag-need_followup { background: #fee2e2; color: #991b1b; }
.tag-sensitive { background: #fee2e2; color: #991b1b; }
.tag-discount_request { background: #fef3c7; color: #92400e; }
.tag-unread { background: #dbeafe; color: #1d4ed8; }

/* ===== Unread thread ===== */
.thread-item-subject.unread { font-weight: 700; }

/* ===== Thread List ===== */
.thread-list { display: flex; flex-direction: column; gap: 1px; background: var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }

.thread-item {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 12px;
    align-items: center;
    padding: 12px 16px;
    background: var(--surface);
    text-decoration: none;
    color: var(--text);
    transition: background 0.1s;
}
.thread-item:hover { background: #f8fafc; text-decoration: none; }

.thread-item-main { min-width: 0; }
.thread-item-subject { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.thread-item-date { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

/* ===== Thread Detail ===== */
.thread-header { margin-bottom: 20px; }
.thread-header h2 { font-size: 18px; margin-bottom: 8px; }
.thread-header-meta { display: flex; gap: 12px; align-items: center; font-size: 13px; color: var(--text-muted); }

.message-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 24px; }

.message-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}
.message-card.outgoing { border-left: 3px solid var(--primary); }
.message-card.incoming { border-left: 3px solid var(--success); }
.message-card-header { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 12px; color: var(--text-muted); }
.message-card-body { white-space: pre-wrap; word-break: break-word; font-size: 13px; }

/* ===== AI Panel ===== */
.ai-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}
.ai-panel h3 { font-size: 14px; margin-bottom: 12px; }

.ai-meta { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.ai-meta-item { font-size: 13px; }
.ai-meta-label { font-weight: 600; color: var(--text-muted); }

.reply-area {
    width: 100%;
    min-height: 160px;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 13px;
    line-height: 1.6;
    resize: vertical;
    margin-bottom: 12px;
}
.reply-area:focus { outline: none; border-color: var(--primary); }

.action-bar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }

.revision-input {
    display: flex;
    gap: 8px;
    margin-top: 12px;
}
.revision-input input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
}
.revision-input input:focus { outline: none; border-color: var(--primary); }

/* ===== Knowledge Page ===== */
.section { margin-bottom: 32px; }
.section h2 { font-size: 16px; margin-bottom: 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group.full-width { grid-column: 1 / -1; }
.form-group label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.3px; }
.form-group input, .form-group select, .form-group textarea {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: inherit;
}
.form-group textarea { min-height: 80px; resize: vertical; }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: var(--primary); }

.rule-item {
    display: flex;
    gap: 8px;
    align-items: start;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}
.rule-item:last-child { border-bottom: none; }
.rule-item textarea { flex: 1; min-height: 40px; padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; font-family: inherit; resize: vertical; }

/* ===== Status & Alerts ===== */
.status-bar {
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 13px;
    margin-bottom: 16px;
    display: none;
}
.status-bar.visible { display: block; }
.status-bar.info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
.status-bar.success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.status-bar.error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

.loading { text-align: center; padding: 40px; color: var(--text-muted); }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state p { margin-bottom: 16px; }

/* ===== Copy button ===== */
.copy-feedback { font-size: 12px; color: var(--success); margin-left: 8px; opacity: 0; transition: opacity 0.2s; }
.copy-feedback.visible { opacity: 1; }

/* ===== Responsive ===== */
@media (max-width: 640px) {
    .form-grid { grid-template-columns: 1fr; }
    .container { padding: 16px; }
    .thread-item { grid-template-columns: 1fr auto; }
    .thread-item-date { display: none; }
}
