/* ============================================================
   AutoResearchClaw — Frontend Management Dashboard
   Pure CSS, no build step. Inherits design system from website/
   ============================================================ */

/* ---------- CSS Variables (inherited from website/) ---------- */
:root {
  --color-bg:        #0A0A0A;
  --color-bg-alt:    #141418;
  --color-surface:   #1C1C22;
  --color-border:    #27272A;
  --color-text:      #FFFFFF;
  --color-text-muted:#A1A1AA;
  --color-primary:   #A855F7;
  --color-primary-d: #9333EA;
  --color-accent:    #A855F7;
  --color-accent-d:  #9333EA;
  --color-success:   #34D399;
  --color-warning:   #fbbf24;
  --color-danger:    #f87171;
  --color-white:     #f8fafc;

  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'IBM Plex Mono', 'JetBrains Mono', 'Consolas', monospace;

  --radius:  0.75rem;
  --radius-lg: 1rem;
  --shadow:  0 4px 6px -1px rgba(0,0,0,.3), 0 2px 4px -2px rgba(0,0,0,.2);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.4), 0 4px 6px -4px rgba(0,0,0,.3);
  --transition: 0.2s ease;
}

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

html { scroll-behavior: smooth; font-size: 16px; }

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100vh;
}

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

input, textarea, select {
  font-family: inherit;
  background: var(--color-bg-alt);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 0.6rem 0.9rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.1);
}

/* ---------- Navbar ---------- */
#navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: rgba(15, 23, 42, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--color-border);
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
}

.navbar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--color-white);
}

.navbar-nav {
  display: flex;
  gap: 1.5rem;
  list-style: none;
}

.navbar-nav a {
  color: var(--color-text-muted);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.4rem 0;
  cursor: pointer;
}

.navbar-nav a:hover,
.navbar-nav a.active {
  color: var(--color-primary);
}

.connection-badge {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--color-text-muted);
  transition: color var(--transition);
}

.connection-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-danger);
  animation: pulse-slow 2s infinite;
}

.connection-dot.connected {
  background: var(--color-success);
  animation: none;
}

@keyframes pulse-slow {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ---------- Main Layout ---------- */
main {
  margin-top: 56px;
  padding: 2rem 1.5rem;
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
}

section {
  display: none;
}

section.active {
  display: block;
}

/* ---------- Dashboard View Layout ---------- */
#view-dashboard {
  display: grid;
  grid-template-columns: 350px 1fr;
  gap: 2rem;
}

@media (max-width: 900px) {
  #view-dashboard {
    grid-template-columns: 1fr;
  }
}

/* ---------- Card Component ---------- */
.card {
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.card h2,
.card h3 {
  font-size: 1.15rem;
  margin-bottom: 1rem;
  color: var(--color-white);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

.card-header h3 {
  margin: 0;
}

/* ---------- Button Styles ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.7rem 1.5rem;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
}

.btn-primary {
  background: var(--color-accent);
  color: var(--color-bg);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-danger {
  background: var(--color-danger);
  color: var(--color-white);
}

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

.btn-success {
  background: var(--color-success);
  color: var(--color-bg);
}

.btn-success:hover {
  background: #31c244;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--color-text-muted);
  padding: 0.5rem;
  cursor: pointer;
  font-size: 1rem;
}

.btn-icon:hover {
  color: var(--color-text);
}

/* ---------- Control Panel ---------- */
#control-panel {
  grid-column: 1;
}

#control-panel .status-badge {
  display: inline-block;
  padding: 0.4rem 0.8rem;
  border-radius: 9999px;
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 1rem;
  background: var(--color-surface);
  color: var(--color-text);
}

#control-panel .status-badge.running {
  background: rgba(56, 189, 248, 0.2);
  color: var(--color-primary);
}

#control-panel .status-badge.completed {
  background: rgba(74, 222, 128, 0.2);
  color: var(--color-success);
}

#control-panel .status-badge.failed {
  background: rgba(248, 113, 113, 0.2);
  color: var(--color-danger);
}

#start-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#start-form input {
  width: 100%;
  padding: 0.7rem;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  cursor: pointer;
}

.toggle-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

#running-panel {
  margin-top: 1.5rem;
  padding: 1rem;
  background: rgba(56, 189, 248, 0.05);
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: var(--radius);
}

#running-panel p {
  margin-bottom: 1rem;
  font-size: 0.95rem;
  color: var(--color-text-muted);
}

/* Overall Progress Bar */
.progress-bar-overall {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1rem 0;
  background: var(--color-surface);
  height: 8px;
  border-radius: 9999px;
  overflow: hidden;
}

.progress-bar-overall > div {
  height: 100%;
  background: var(--color-accent);
  border-radius: 9999px;
  transition: width 0.4s ease;
  width: 0%;
}

#progress-text {
  display: inline-block;
  min-width: 50px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
}

#stop-btn {
  width: 100%;
  margin-top: 1rem;
}

/* ---------- Stages Panel ---------- */
#stages-panel {
  grid-column: 2;
}

@media (max-width: 900px) {
  #stages-panel {
    grid-column: 1;
    order: 2;
  }
}

.phase-group {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  overflow: hidden;
}

.phase-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  background: rgba(168, 85, 247, 0.12);
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
  user-select: none;
}

.phase-letter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--color-primary);
  color: var(--color-bg);
  font-weight: 700;
  font-size: 0.85rem;
}

.phase-name {
  font-weight: 600;
  color: var(--color-white);
  flex: 1;
}

.phase-toggle {
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.stage-list {
  padding: 0;
}

.stage-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--color-border);
  transition: background-color var(--transition);
}

.stage-row:last-child {
  border-bottom: none;
}

.stage-row:hover {
  background: rgba(56, 189, 248, 0.05);
}

.stage-status-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--color-text-muted);
  flex-shrink: 0;
}

.stage-status-dot[data-status="pending"] {
  background: var(--color-text-muted);
}

.stage-status-dot[data-status="running"] {
  background: var(--color-primary);
  animation: pulse-dot 1.5s ease-in-out infinite;
}

.stage-status-dot[data-status="done"] {
  background: var(--color-success);
}

.stage-status-dot[data-status="failed"] {
  background: var(--color-danger);
}

.stage-status-dot[data-status="blocked_approval"] {
  background: var(--color-warning);
  animation: pulse-dot 1s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.3); }
}

.stage-num-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  flex-shrink: 0;
}

.stage-name {
  flex: 1;
  font-size: 0.9rem;
  color: var(--color-text);
  font-weight: 500;
}

.stage-gate-badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 700;
  background: rgba(251, 191, 36, 0.2);
  color: var(--color-warning);
}

/* ---------- Log Panel ---------- */
#log-panel {
  margin-top: 2rem;
  grid-column: 1 / -1;
}

.log-card {
  display: flex;
  flex-direction: column;
}

#log-output {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  max-height: 300px;
  overflow-y: auto;
  color: var(--color-text-muted);
}

.log-line {
  margin: 0.25rem 0;
  padding: 0.25rem 0;
  word-break: break-word;
  white-space: pre-wrap;
  animation: fadeIn 0.2s ease;
}

.log-line.log-error {
  color: var(--color-danger);
}

.log-line.log-warning {
  color: var(--color-warning);
}

.log-line.log-success {
  color: var(--color-success);
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ---------- Metrics Panel ---------- */
#metrics-panel {
  margin-top: 2rem;
  grid-column: 1 / -1;
}

.metrics-chart-container {
  position: relative;
  height: 300px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
}

/* ---------- Gate Modal ---------- */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2000;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
}

.modal-overlay.active {
  display: flex;
}

.modal-card {
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  max-width: 500px;
  width: 90%;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-header {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-border);
}

.gate-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(251, 191, 36, 0.2);
  color: var(--color-warning);
  font-size: 1.5rem;
  flex-shrink: 0;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.1rem;
  color: var(--color-white);
}

#gate-summary {
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
  color: var(--color-text-muted);
  line-height: 1.6;
}

.modal-actions {
  display: flex;
  gap: 1rem;
}

.modal-actions .btn {
  flex: 1;
  justify-content: center;
}

/* ---------- History View ---------- */
#view-history {
  display: grid;
  grid-template-columns: 1fr;
  gap: 2rem;
}

.runs-list {
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.runs-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
  transition: background-color var(--transition);
}

.runs-list-item:hover {
  background: rgba(56, 189, 248, 0.05);
}

.runs-list-item:last-child {
  border-bottom: none;
}

.run-info h4 {
  font-size: 0.95rem;
  margin-bottom: 0.25rem;
  color: var(--color-white);
}

.run-info p {
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

.run-status-badge {
  padding: 0.3rem 0.7rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--color-surface);
  color: var(--color-text);
}

.run-status-badge.completed {
  background: rgba(74, 222, 128, 0.2);
  color: var(--color-success);
}

.run-status-badge.failed {
  background: rgba(248, 113, 113, 0.2);
  color: var(--color-danger);
}

.run-detail {
  background: var(--color-bg-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
}

.run-detail h3 {
  margin-bottom: 1.5rem;
}

.run-detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

.run-detail-item dt {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.25rem;
}

.run-detail-item dd {
  font-size: 0.95rem;
  color: var(--color-white);
  font-weight: 500;
}

/* ---------- Responsive Design ---------- */
@media (max-width: 768px) {
  #navbar {
    padding: 0.5rem 1rem;
  }

  .navbar-nav {
    gap: 1rem;
  }

  main {
    padding: 1rem;
  }

  #view-dashboard {
    gap: 1rem;
  }

  .card {
    padding: 1rem;
  }

  .stage-row {
    padding: 0.7rem;
  }

  #log-output {
    max-height: 200px;
  }

  .modal-card {
    padding: 1.5rem;
  }
}

/* Pencil UI: 標題採 Geist */
h1, h2, h3, .navbar-brand { font-family: 'Geist', 'Inter', sans-serif; }
.status-badge, #progress-text, #elapsed-time, .stage-num-badge { font-family: var(--font-mono); }
