:root {
  --orange: #FF6B35;
  --orange-dark: #E84525;
  --bg: #f5f5f5;
  --card: #ffffff;
  --text: #1a1a1a;
  --text2: #666;
  --border: #e5e5e5;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111;
    --card: #1e1e1e;
    --text: #eee;
    --text2: #999;
    --border: #333;
  }
}

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

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.screen { min-height: 100vh; }
.hidden { display: none !important; }

/* ===== Connect Screen ===== */
.connect-hero {
  display: flex; flex-direction: column; align-items: center;
  padding: calc(60px + var(--safe-top)) 20px 30px;
  background: linear-gradient(135deg, var(--orange), var(--orange-dark));
  color: #fff; text-align: center;
}
.app-logo { width: 80px; height: 80px; margin-bottom: 12px; }
.connect-hero h1 { font-size: 32px; font-weight: 800; letter-spacing: -1px; }
.connect-hero p { opacity: 0.8; font-size: 15px; margin-top: 4px; }

.connect-form {
  padding: 24px 20px; max-width: 400px; margin: 0 auto;
}
.connect-form label {
  display: block; font-size: 13px; color: var(--text2);
  margin-top: 16px; margin-bottom: 4px;
}
.connect-form input {
  width: 100%; padding: 12px 14px; border: 1px solid var(--border);
  border-radius: 10px; font-size: 15px; background: var(--card); color: var(--text);
}
.connect-form input:focus { outline: none; border-color: var(--orange); }

.btn-primary {
  width: 100%; padding: 14px; margin-top: 20px; border: none; border-radius: 12px;
  background: var(--orange); color: #fff; font-size: 16px; font-weight: 600;
  cursor: pointer;
}
.btn-primary:active { opacity: 0.9; }

.btn-secondary {
  width: 100%; padding: 14px; border: 2px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 16px; font-weight: 600;
  cursor: pointer;
}

.btn-danger {
  width: 100%; padding: 12px; margin-top: 24px; border: none; border-radius: 10px;
  background: var(--danger); color: #fff; font-size: 14px; font-weight: 600;
  cursor: pointer;
}

.divider {
  text-align: center; margin: 20px 0; position: relative;
  color: var(--text2); font-size: 13px;
}
.divider::before, .divider::after {
  content: ""; position: absolute; top: 50%; width: 40%;
  height: 1px; background: var(--border);
}
.divider::before { left: 0; }
.divider::after { right: 0; }

.error-msg { color: var(--danger); text-align: center; padding: 12px; font-size: 14px; }

/* ===== Scan Screen ===== */
.scan-header {
  display: flex; align-items: center; gap: 12px;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: var(--card); border-bottom: 1px solid var(--border);
}
.back-btn {
  background: none; border: none; font-size: 24px; cursor: pointer;
  color: var(--text); padding: 4px 8px;
}
.scan-area {
  position: relative; width: 100%; max-width: 400px; margin: 40px auto;
  aspect-ratio: 1; background: #000; border-radius: 16px; overflow: hidden;
}
.scan-area video { width: 100%; height: 100%; object-fit: cover; }
.scan-overlay {
  position: absolute; inset: 20%; border: 3px solid var(--orange);
  border-radius: 12px; pointer-events: none;
}
.scan-hint { text-align: center; color: var(--text2); font-size: 14px; margin-top: 16px; }

/* ===== Main Screen ===== */
.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + var(--safe-top)) 16px 8px;
  background: var(--card); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.header-left { display: flex; align-items: center; gap: 8px; }
.header-logo { width: 28px; height: 28px; }
.header-title { font-size: 18px; font-weight: 700; }
.header-right { display: flex; align-items: center; gap: 10px; }

.status-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--text2);
}
.status-dot.connected { background: var(--success); }
.status-dot.error { background: var(--danger); }

.icon-btn {
  background: none; border: none; font-size: 22px; cursor: pointer;
  color: var(--text); padding: 4px;
}

.main-content { padding: 12px 16px calc(20px + var(--safe-bottom)); }

.section-bar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.section-bar h2 { font-size: 16px; font-weight: 600; }

/* Instruction cards */
.instr-list { display: flex; flex-direction: column; gap: 8px; }

.instr-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px;
}
.instr-card .type-badge {
  display: inline-block; font-size: 12px; font-weight: 600;
  padding: 2px 8px; border-radius: 6px;
  background: var(--orange); color: #fff;
}
.instr-card .payload {
  margin: 8px 0; color: var(--text2); font-size: 13px; word-break: break-all;
}
.instr-card .card-actions { display: flex; gap: 8px; }
.instr-card .btn-exec {
  flex: 1; padding: 10px; border: none; border-radius: 8px;
  background: var(--orange); color: #fff; font-size: 14px;
  font-weight: 600; cursor: pointer;
}
.instr-card .btn-exec:active { opacity: 0.9; }
.instr-card .btn-skip {
  padding: 10px 16px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--card); color: var(--text2); font-size: 14px; cursor: pointer;
}

/* Workflow */
.workflow-card {
  background: var(--card); border: 2px solid var(--orange);
  border-radius: 12px; padding: 14px;
}
.wf-title { font-size: 14px; font-weight: 600; margin-bottom: 10px; }
.wf-step {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 0; font-size: 13px; color: var(--text2);
}
.wf-step-num {
  width: 24px; height: 24px; border-radius: 50%; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; flex-shrink: 0;
}
.wf-step.running .wf-step-num { background: var(--orange); color: #fff; }
.wf-step.ok .wf-step-num { background: var(--success); color: #fff; }
.wf-step.failed .wf-step-num { background: var(--danger); color: #fff; }
.wf-progress { height: 4px; background: var(--bg); border-radius: 2px; margin-top: 10px; overflow: hidden; }
.wf-progress-bar { height: 100%; background: var(--orange); border-radius: 2px; transition: width 0.3s; width: 0; }
.wf-actions { display: flex; gap: 8px; margin-top: 10px; }

.empty-state {
  text-align: center; padding: 40px 20px; color: var(--text2); font-size: 14px;
}

/* Log */
.log-section {
  margin-top: 16px; background: var(--card); border-radius: 12px;
  border: 1px solid var(--border);
}
.log-section summary {
  padding: 12px 14px; cursor: pointer; font-weight: 600; font-size: 14px;
  display: flex; align-items: center; gap: 8px; user-select: none;
}
.log-list { max-height: 300px; overflow-y: auto; padding: 0 14px 10px; }
.log-entry {
  display: flex; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--border);
  font-size: 12px; align-items: flex-start;
}
.log-entry:last-child { border-bottom: none; }
.log-time { color: var(--text2); white-space: nowrap; flex-shrink: 0; }
.log-type { font-weight: 600; flex-shrink: 0; }
.log-ok { color: var(--success); }
.log-failed { color: var(--danger); }
.log-cancelled { color: var(--text2); }
.log-result { color: var(--text2); word-break: break-all; }

.badge {
  display: inline-block; min-width: 18px; height: 18px; line-height: 18px;
  text-align: center; font-size: 11px; font-weight: 700;
  background: var(--text2); color: #fff; border-radius: 9px; padding: 0 5px;
}

/* Settings Panel */
.settings-panel {
  position: fixed; inset: 0; background: var(--bg); z-index: 100;
  overflow-y: auto;
}
.settings-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  border-bottom: 1px solid var(--border);
}
.settings-body { padding: 16px; max-width: 400px; margin: 0 auto; }
.settings-body label {
  display: block; font-size: 13px; color: var(--text2);
  margin-top: 16px; margin-bottom: 4px;
}
.settings-body input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; background: var(--card); color: var(--text);
}
.toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 16px; font-size: 14px;
}
.switch { position: relative; width: 44px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; inset: 0; background: var(--border);
  border-radius: 24px; cursor: pointer; transition: 0.2s;
}
.slider::before {
  content: ""; position: absolute; width: 20px; height: 20px;
  left: 2px; bottom: 2px; background: #fff;
  border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider { background: var(--orange); }
.switch input:checked + .slider::before { transform: translateX(20px); }

.qr-section { margin-top: 24px; text-align: center; }
.qr-section h3 { font-size: 15px; margin-bottom: 4px; }
.qr-section .hint { font-size: 12px; color: var(--text2); margin-bottom: 12px; }
.qr-display {
  display: inline-block; padding: 16px; background: #fff;
  border-radius: 12px; border: 1px solid var(--border);
}

/* ===== Bottom Tab Bar ===== */
.tab-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 50;
}
.tab-item {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  padding: 8px 0 4px; border: none; background: none;
  color: var(--text2); cursor: pointer; font-size: 11px;
}
.tab-item.active { color: var(--orange); }
.tab-icon { font-size: 22px; line-height: 1; margin-bottom: 2px; }
.tab-label { font-weight: 500; }
.tab-content { padding-bottom: 70px; }

/* ===== Chat Tab ===== */
.quick-actions {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  padding: 0 0 12px;
}
.qa-btn {
  padding: 12px 10px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--card); color: var(--text); font-size: 13px; font-weight: 500;
  cursor: pointer; text-align: center; transition: background 0.15s;
}
.qa-btn:active { background: var(--bg); }

.chat-messages {
  display: flex; flex-direction: column; gap: 8px;
  min-height: 200px; padding-bottom: 70px;
}
.chat-welcome {
  text-align: center; padding: 40px 20px; color: var(--text2);
}
.chat-welcome p { font-size: 15px; }
.chat-welcome .hint { font-size: 12px; margin-top: 4px; }

.chat-bubble {
  max-width: 80%; padding: 10px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.5; word-break: break-word;
}
.chat-bubble.user {
  align-self: flex-end;
  background: var(--orange); color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-bubble.agent {
  align-self: flex-start;
  background: var(--card); border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.chat-bubble .bubble-time {
  font-size: 10px; opacity: 0.6; margin-top: 4px;
  text-align: right;
}
.chat-bubble.agent .bubble-time { text-align: left; }

.chat-input-bar {
  position: fixed; bottom: 56px; left: 0; right: 0;
  display: flex; gap: 8px; padding: 8px 16px;
  padding-bottom: calc(8px + var(--safe-bottom));
  background: var(--bg); border-top: 1px solid var(--border);
  z-index: 40;
}
.chat-input-bar input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 20px; font-size: 14px; background: var(--card);
  color: var(--text); outline: none;
}
.chat-input-bar input:focus { border-color: var(--orange); }
.btn-send {
  padding: 10px 18px; border: none; border-radius: 20px;
  background: var(--orange); color: #fff; font-size: 14px;
  font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn-send:active { opacity: 0.9; }
.btn-send:disabled { opacity: 0.5; }
