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

:root {
  --bg:         #F9F9F8;
  --surface:    #FFFFFF;
  --surface2:   #F4F4F2;
  --sidebar:    #1C1C1C;
  --sidebar2:   #2A2A2A;
  --border:     #E5E5E3;
  --border2:    #D0D0CE;
  --ink:        #1A1A19;
  --ink2:       #3D3D3A;
  --muted:      #6B6B68;
  --accent:     #D97757;
  --accent2:    #C4623E;
  --blue:       #3B82F6;
  --green:      #22C55E;
  --yellow:     #EAB308;
  --red:        #EF4444;
  --shadow-sm:  0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow:     0 4px 12px rgba(0,0,0,.08), 0 2px 4px rgba(0,0,0,.04);
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang TC', 'Microsoft JhengHei', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

a { text-decoration: none; color: inherit; }

/* ── Top nav ─────────────────────────────────────────── */
.topnav {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topnav-brand {
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}
.topnav-brand .logo-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
}
.topnav-right { display: flex; align-items: center; gap: 10px; }
.topnav-user {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.user-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #E8956A);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Buttons ─────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  border: none;
  transition: all .15s;
  font-family: inherit;
  white-space: nowrap;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent2); }
.btn-secondary {
  background: var(--surface);
  border: 1px solid var(--border2);
  color: var(--ink2);
}
.btn-secondary:hover { background: var(--surface2); border-color: var(--border2); }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
}
.btn-ghost:hover { background: var(--surface2); color: var(--ink); }
.btn-danger { background: #FEE2E2; color: #DC2626; border: 1px solid #FECACA; }
.btn-danger:hover { background: #FECACA; }
.btn-sm { padding: 5px 11px; font-size: 12px; border-radius: 6px; }
.btn-lg { padding: 11px 24px; font-size: 15px; border-radius: 10px; }
.btn-full { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Card ─────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
}

/* ── Form ─────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink2);
  margin-bottom: 6px;
}
.form-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 8px;
  color: var(--ink);
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(217,119,87,.12);
}
.form-input::placeholder { color: #B0B0AE; }
select.form-input option { background: #fff; }
textarea.form-input { resize: vertical; min-height: 88px; line-height: 1.6; }

/* ── Badges ─────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 500;
  border-radius: 20px;
  border: 1px solid transparent;
}
.badge-open     { background: #FEF9EE; border-color: #FDE68A; color: #92400E; }
.badge-progress { background: #FFF1EE; border-color: #FECDBF; color: #C2410C; }
.badge-wait     { background: #F8F8F6; border-color: var(--border2); color: var(--muted); }
.badge-resolved { background: #F0FDF4; border-color: #BBF7D0; color: #166534; }
.badge-closed   { background: #F8F8F6; border-color: var(--border); color: var(--muted); }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ── Alert ─────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 14px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.alert-error   { background: #FEF2F2; border: 1px solid #FECACA; color: #991B1B; }
.alert-success { background: #F0FDF4; border: 1px solid #BBF7D0; color: #166534; }
.alert-info    { background: #EFF6FF; border: 1px solid #BFDBFE; color: #1E40AF; }

/* ── Admin sidebar layout ────────────────────────────── */
.app-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: calc(100vh - 56px);
  overflow: hidden;
}
.app-layout.panel-open { grid-template-columns: 240px 1fr 460px; }

.sidebar {
  background: var(--sidebar);
  color: #E8E8E6;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sidebar-section { margin-bottom: 20px; }
.sidebar-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #6B6B68;
  padding: 0 10px;
  margin-bottom: 4px;
}
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 14px;
  color: #B0B0AE;
  cursor: pointer;
  transition: .15s;
  user-select: none;
}
.sidebar-item:hover { background: var(--sidebar2); color: #E8E8E6; }
.sidebar-item.active { background: rgba(217,119,87,.15); color: #F0A080; }
.sidebar-badge {
  margin-left: auto;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 20px;
  min-width: 20px;
  text-align: center;
}

/* ── Main content area ──────────────────────────────── */
.main-content {
  background: var(--bg);
  overflow-y: auto;
  padding: 24px;
}

/* ── Toolbar ────────────────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.search-box {
  flex: 1;
  min-width: 200px;
  position: relative;
}
.search-box input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 8px 12px 8px 36px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  color: var(--ink);
  transition: .15s;
}
.search-box input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(217,119,87,.1); }
.search-box input::placeholder { color: #B0B0AE; }
.search-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; }
.filter-tabs { display: flex; gap: 4px; }
.ftab {
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  cursor: pointer;
  transition: .15s;
  font-family: inherit;
}
.ftab:hover { border-color: var(--border2); color: var(--ink); }
.ftab.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── Table ─────────────────────────────────────────── */
.data-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 10px; overflow: hidden; border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.data-table th { text-align: left; padding: 11px 16px; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface2); border-bottom: 1px solid var(--border); letter-spacing: .03em; }
.data-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--ink); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--bg); cursor: pointer; }
.data-table tr.selected td { background: #FFF5F2; }
.ticket-no-text { font-family: 'SF Mono', monospace; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.empty-row td { text-align: center; padding: 60px; color: var(--muted); }

/* ── Detail Panel ───────────────────────────────────── */
.detail-panel {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.detail-panel.open { display: flex; }
.panel-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.panel-header-top { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px; }
.panel-ticket-no { font-family: monospace; font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.panel-subject { font-size: 15px; font-weight: 600; color: var(--ink); }
.panel-meta { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 8px; }
.panel-close { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; padding: 4px; border-radius: 6px; }
.panel-close:hover { background: var(--surface2); color: var(--ink); }
.panel-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg-bubble { padding: 12px 14px; border-radius: 10px; }
.msg-bubble-player { background: var(--surface2); border: 1px solid var(--border); }
.msg-bubble-agent  { background: #FFF5F2; border: 1px solid #FECDBF; }
.msg-bubble-ai     { background: #FFFBEB; border: 1px solid #FDE68A; }
.msg-bubble-internal { background: #F0FDF4; border: 1px solid #BBF7D0; opacity: .9; }
.msg-bubble-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.msg-bubble-sender { font-size: 12px; font-weight: 600; }
.msg-bubble-sender.agent { color: var(--accent2); }
.msg-bubble-sender.player { color: var(--blue); }
.msg-bubble-sender.ai { color: #D97706; }
.msg-bubble-time { font-size: 11px; color: var(--muted); }
.msg-bubble-content { font-size: 13px; line-height: 1.7; white-space: pre-wrap; color: var(--ink); }
.msg-bubble-tag { font-size: 11px; color: var(--muted); margin-top: 6px; display: flex; align-items: center; gap: 4px; }

/* Reply box in panel */
.panel-reply { padding: 14px 16px; border-top: 1px solid var(--border); background: var(--bg); }
.reply-tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.reply-tab { padding: 5px 12px; font-size: 12px; font-weight: 500; border-radius: 6px; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-family: inherit; transition: .15s; }
.reply-tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.reply-tab.active.internal { background: #16A34A; border-color: #16A34A; }
.status-tabs { display: flex; gap: 4px; margin-bottom: 10px; flex-wrap: wrap; }
.status-tab { padding: 4px 10px; font-size: 11px; border-radius: 6px; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-family: inherit; }
.status-tab.active { border-color: var(--accent); color: var(--accent); background: #FFF1EE; }
.panel-reply textarea { width: 100%; border: 1px solid var(--border2); border-radius: 8px; padding: 9px 12px; font-size: 13px; font-family: inherit; resize: vertical; min-height: 80px; outline: none; background: var(--surface); color: var(--ink); transition: .15s; }
.panel-reply textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(217,119,87,.1); }
.reply-footer { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; }
.mail-check { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); cursor: pointer; }
.mail-check input { accent-color: var(--accent); }

/* ── Player pages ────────────────────────────────────── */
.page-container { max-width: 860px; margin: 0 auto; padding: 28px 20px; }
.page-header { margin-bottom: 24px; }
.page-title { font-size: 20px; font-weight: 600; color: var(--ink); }
.stats-row { display: grid; grid-template-columns: repeat(4,1fr); gap: 12px; margin-bottom: 24px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px; text-align: center; box-shadow: var(--shadow-sm); }
.stat-val { font-size: 30px; font-weight: 700; color: var(--ink); line-height: 1.2; }
.stat-val.accent { color: var(--accent); }
.stat-val.green  { color: #16A34A; }
.stat-val.blue   { color: var(--blue); }
.stat-lbl { font-size: 12px; color: var(--muted); margin-top: 4px; }

.ticket-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  transition: .15s;
  box-shadow: var(--shadow-sm);
}
.ticket-card:hover { border-color: var(--accent); box-shadow: var(--shadow); transform: translateY(-1px); }
.ticket-card-left { flex: 1; min-width: 0; }
.ticket-card-no { font-family: monospace; font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.ticket-card-title { font-size: 14px; font-weight: 500; color: var(--ink); margin-bottom: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ticket-card-meta { font-size: 12px; color: var(--muted); display: flex; gap: 14px; }

.ticket-list { display: flex; flex-direction: column; gap: 8px; }

/* Individual ticket page */
.ticket-page-header { margin-bottom: 24px; }
.ticket-page-no { font-family: monospace; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.ticket-page-title { font-size: 22px; font-weight: 700; color: var(--ink); margin-bottom: 10px; }
.ticket-page-meta { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.messages-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 24px; }
.msg-card { border-radius: 10px; padding: 14px 16px; }
.msg-card-player   { background: var(--surface2); border: 1px solid var(--border); }
.msg-card-agent    { background: #FFF5F2; border: 1px solid #FECDBF; }
.msg-card-header   { display: flex; justify-content: space-between; margin-bottom: 8px; }
.msg-card-sender   { font-size: 13px; font-weight: 600; }
.msg-card-sender.player { color: var(--blue); }
.msg-card-sender.agent  { color: var(--accent2); }
.msg-card-time     { font-size: 12px; color: var(--muted); }
.msg-card-content  { font-size: 14px; line-height: 1.75; white-space: pre-wrap; color: var(--ink); }
.reply-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px; box-shadow: var(--shadow-sm); }
.reply-card-title { font-size: 14px; font-weight: 500; color: var(--ink); margin-bottom: 14px; }
.reply-card-actions { display: flex; justify-content: flex-end; margin-top: 12px; }
.resolved-notice { text-align: center; padding: 24px; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; color: var(--muted); font-size: 13px; }

/* ── Auth page ───────────────────────────────────────── */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); padding: 20px; }
.auth-box { width: 380px; }
.auth-logo { text-align: center; margin-bottom: 32px; }
.auth-logo-icon { width: 48px; height: 48px; background: linear-gradient(135deg, var(--accent), #E8956A); border-radius: 12px; display: flex; align-items: center; justify-content: center; margin: 0 auto 12px; font-size: 22px; }
.auth-logo-title { font-size: 20px; font-weight: 600; color: var(--ink); margin-bottom: 4px; }
.auth-logo-sub { font-size: 13px; color: var(--muted); }
.auth-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth-footer { text-align: center; margin-top: 18px; font-size: 12px; color: var(--muted); }
.auth-footer a { color: var(--accent); }

/* ── Agent cards ────────────────────────────────────── */
.agent-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px,1fr)); gap: 12px; }
.agent-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; box-shadow: var(--shadow-sm); }
.agent-card-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.agent-avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, #6366F1, #8B5CF6); color: #fff; font-size: 13px; font-weight: 600; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.agent-name { font-size: 14px; font-weight: 600; color: var(--ink); }
.agent-role { font-size: 11px; color: var(--muted); }
.agent-stat { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.agent-card.inactive { opacity: .5; }
.role-chip { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 500; }
.role-chip.admin { background: #FFF5F2; color: var(--accent2); border: 1px solid #FECDBF; }
.role-chip.agent { background: var(--surface2); color: var(--muted); border: 1px solid var(--border); }

/* ── Modal ─────────────────────────────────────────── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); backdrop-filter: blur(4px); z-index: 200; align-items: center; justify-content: center; }
.modal-overlay.open { display: flex; }
.modal-box { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: 500px; max-width: 95vw; max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.15); position: relative; }
.modal-title { font-size: 16px; font-weight: 600; color: var(--ink); margin-bottom: 20px; }
.modal-close { position: absolute; top: 14px; right: 16px; background: none; border: none; font-size: 18px; cursor: pointer; color: var(--muted); border-radius: 6px; padding: 4px; }
.modal-close:hover { background: var(--surface2); color: var(--ink); }

/* ── Misc ───────────────────────────────────────────── */
.back-link { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); margin-bottom: 20px; transition: color .15s; }
.back-link:hover { color: var(--ink); }
.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-state-icon { font-size: 40px; margin-bottom: 12px; opacity: .4; }
.empty-state p { font-size: 14px; }
.ai-indicator { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--yellow); margin-left: 6px; animation: pulse 2s infinite; vertical-align: middle; }
.info-hint { font-size: 12px; color: var(--muted); background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 13px; line-height: 1.7; margin-top: 14px; }

::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 20px; }
::-webkit-scrollbar-thumb:hover { background: #BBBBB8; }

@keyframes fadeUp { from { opacity:0; transform:translateY(10px); } to { opacity:1; transform:none; } }
@keyframes pulse  { 0%,100% { opacity:.4; } 50% { opacity:1; } }
.fade-up { animation: fadeUp .3s ease forwards; }
