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

:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #3b82f6;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* Login Page */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  width: 100%;
  max-width: 400px;
}

.login-card h1 { text-align: center; margin-bottom: 0.25rem; }
.login-card .subtitle { text-align: center; color: var(--text-muted); margin-bottom: 1.5rem; }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 500; margin-bottom: 0.25rem; font-size: 0.875rem; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus, .form-group select:focus { border-color: var(--primary); }

/* Password toggle */
.password-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.password-wrapper input {
  padding-right: 2.5rem;
}
.toggle-password {
  position: absolute;
  right: 0.5rem;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s;
}
.toggle-password:hover {
  color: var(--text);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-sm { padding: 0.25rem 0.5rem; font-size: 0.75rem; }
/* Icon action buttons */
.action-btns {
  display: inline-flex;
  gap: 0.35rem;
  align-items: center;
}
.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
  color: white;
}
.action-btn svg { width: 15px; height: 15px; }
.action-btn.btn-view { background: var(--info); }
.action-btn.btn-view:hover { background: #2563eb; }
.action-btn.btn-dl { background: var(--success); }
.action-btn.btn-dl:hover { background: #059669; }
.action-btn.btn-retry { background: var(--warning); }
.action-btn.btn-retry:hover { background: #d97706; }
.action-btn.btn-del { background: #fee2e2; color: var(--danger); }
.action-btn.btn-del:hover { background: var(--danger); color: white; }
.btn-full { width: 100%; }
.btn + .btn { margin-left: 0.5rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.error-text { color: var(--danger); font-size: 0.875rem; margin-top: 0.5rem; }

/* Layout */
.app-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  background: #1e293b;
  color: white;
  padding: 1rem 0;
  flex-shrink: 0;
}

.sidebar .logo {
  padding: 0 1rem 1rem;
  border-bottom: 1px solid #334155;
  margin-bottom: 0.5rem;
}

.sidebar .logo h2 { font-size: 1rem; }
.sidebar .logo small { color: #94a3b8; font-size: 0.75rem; }

.sidebar nav a {
  display: flex;
  align-items: center;
  padding: 0.5rem 1rem;
  color: #cbd5e1;
  text-decoration: none;
  font-size: 0.875rem;
  transition: all 0.2s;
}
.sidebar nav a:hover, .sidebar nav a.active {
  background: #334155;
  color: white;
}
.sidebar nav a .icon { margin-right: 0.5rem; width: 1.25rem; text-align: center; }

.sidebar .user-info {
  position: absolute;
  bottom: 0;
  width: 240px;
  padding: 0.75rem 1rem;
  border-top: 1px solid #334155;
  font-size: 0.8rem;
  color: #94a3b8;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logout-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  color: #94a3b8;
  transition: all 0.2s;
  text-decoration: none;
}
.logout-btn:hover {
  background: #334155;
  color: #ef4444;
}

.main-content {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
}

/* Header */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}
.page-header h1 { font-size: 1.5rem; }

/* Cards */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}
.card-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}
.card-body { padding: 1rem; }

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  text-align: center;
}
.stat-card .stat-value { font-size: 2rem; font-weight: 700; }
.stat-card .stat-label { color: var(--text-muted); font-size: 0.8rem; }

/* Table */
.table-container { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th, td { padding: 0.5rem 0.75rem; text-align: center; vertical-align: middle; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--text-muted); font-size: 0.75rem; text-transform: uppercase; }
tr:hover { background: #f1f5f9; }

/* Badge */
.badge {
  display: inline-block;
  padding: 0.125rem 0.5rem;
  border-radius: 9999px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
}
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-downloading { background: #dbeafe; color: #1e40af; }
.badge-downloaded { background: #e0e7ff; color: #3730a3; }
.badge-rewriting { background: #ede9fe; color: #5b21b6; }
.badge-testing { background: #fce7f3; color: #9d174d; }
.badge-passed { background: #d1fae5; color: #065f46; }
.badge-failed { background: #fee2e2; color: #991b1b; }
.badge-packaging { background: #cffafe; color: #155e75; }
.badge-ready { background: #d1fae5; color: #065f46; }
.badge-error { background: #fee2e2; color: #991b1b; }
.badge-running { background: #dbeafe; color: #1e40af; }
.badge-completed { background: #d1fae5; color: #065f46; }
.badge-cancelled { background: #f1f5f9; color: #475569; }

/* Progress bar */
.progress-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}
.progress-bar {
  width: 120px;
  min-width: 120px;
  max-width: 120px;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.progress-bar .progress {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.3s;
}
.progress-msg {
  white-space: nowrap;
  font-size: 0.75rem;
  color: var(--text-muted);
  min-width: 2.5rem;
}

/* Confirm dialog */
.confirm-overlay {
  display: flex;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.45);
  z-index: 2000;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.15s ease;
}
.confirm-box {
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  width: 90%;
  max-width: 380px;
  padding: 1.5rem;
  text-align: center;
  animation: scaleIn 0.2s ease;
}
.confirm-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.75rem;
}
.confirm-icon.warn { background: #fef3c7; color: #d97706; }
.confirm-icon.danger { background: #fee2e2; color: var(--danger); }
.confirm-box h3 { font-size: 1rem; margin-bottom: 0.4rem; }
.confirm-box p { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.25rem; line-height: 1.5; }
.confirm-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}
.confirm-actions .btn { min-width: 90px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes scaleIn { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal-overlay.show { display: flex; }
.modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-header {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-body { padding: 1rem; }
/* View detail - left-aligned two-column layout */
.view-detail-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.4rem 1rem;
  align-items: baseline;
  margin-bottom: 1rem;
}
.view-detail-grid .label {
  font-weight: 600;
  color: var(--text-muted);
  font-size: 0.85rem;
  white-space: nowrap;
  text-align: right;
}
.view-detail-grid .value {
  font-size: 0.85rem;
  text-align: left;
  word-break: break-all;
}
.modal-footer { padding: 0.75rem 1rem; border-top: 1px solid var(--border); text-align: right; }
.modal-close { background: none; border: none; font-size: 1.25rem; cursor: pointer; color: var(--text-muted); }

/* Toast notifications */
.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 2000;
}
.toast {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  padding: 0.75rem 1rem;
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  animation: slideIn 0.3s ease;
  max-width: 400px;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.info { border-left: 3px solid var(--info); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Filters */
.filters {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.filters input, .filters select {
  padding: 0.375rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8rem;
}

/* Pagination */
.pagination {
  display: flex;
  justify-content: center;
  gap: 0.25rem;
  margin-top: 1rem;
}
.pagination button {
  padding: 0.375rem 0.75rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.8rem;
}
.pagination button.active { background: var(--primary); color: white; border-color: var(--primary); }
.pagination button:disabled { opacity: 0.5; cursor: not-allowed; }

/* Portal */
.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}
.game-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform 0.2s;
}
.game-card:hover { transform: translateY(-2px); }
.game-card .thumb {
  width: 100%;
  aspect-ratio: 16/9;
  object-fit: cover;
  background: var(--border);
}
.game-card .info { padding: 0.75rem; }
.game-card .info h3 { font-size: 1rem; margin-bottom: 0.25rem; }
.game-card .info .meta { color: var(--text-muted); font-size: 0.8rem; }
.game-card .actions { padding: 0 0.75rem 0.75rem; }

/* Test level tooltip */
.test-level {
  position: relative;
  cursor: help;
  font-weight: 600;
  padding: 0.125rem 0.4rem;
  border-radius: 4px;
  background: #f0fdf4;
  color: #166534;
  font-size: 0.8rem;
}
.test-level[data-level="0"] { background: #f1f5f9; color: #64748b; }
.test-level[data-level="1"] { background: #fef3c7; color: #92400e; }
.test-level[data-level="2"] { background: #dbeafe; color: #1e40af; }
.test-level[data-level="3"] { background: #d1fae5; color: #065f46; }
.test-level[data-level="4"] { background: #d1fae5; color: #065f46; }
.test-level .tooltip {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #f1f5f9;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  font-size: 0.72rem;
  font-weight: 400;
  white-space: nowrap;
  z-index: 100;
  line-height: 1.5;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.test-level .tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #1e293b;
}
.test-level:hover .tooltip { display: block; }

/* Responsive */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .main-content { padding: 1rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .filters { flex-direction: column; }
}
