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

:root {
  --bg: #0a0d12;
  --surface: #141921;
  --surface2: #181d25;
  --border: #232a35;
  --accent: #06b6d4;
  --accent-hover: #22d3ee;
  --text: #e6e8eb;
  --text-muted: #9ca3af;
  --success: #0d9488;
  --success-hover: #14b8a6;
  --warning: #f59e0b;
  --danger: #f43f5e;
  --radius: 12px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  max-width: 480px;
  margin: 0 auto;
  padding: 16px;
  gap: 16px;
}

header {
  text-align: center;
  padding: 8px 0;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

header p {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* Install button */
.install-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: transparent;
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  font-family: inherit;
}

.install-btn:hover {
  background: var(--accent);
  color: #fff;
}

.install-btn[hidden] { display: none; }

/* Mode switch */
#mode-switch {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.mode-option {
  flex: 1;
  padding: 8px 12px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  font-family: inherit;
}

.mode-option:hover {
  color: var(--text);
}

.mode-option.active {
  background: var(--accent);
  color: #fff;
}

/* Camera area */
#camera-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#preview-container {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  background: var(--surface2);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#preview-container.has-image::after {
  display: none;
}

#preview-container::after {
  content: '';
  display: block;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--surface);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%239ca3af'%3E%3Cpath d='M12 15.2A3.2 3.2 0 1 0 12 8.8a3.2 3.2 0 0 0 0 6.4z'/%3E%3Cpath d='M9 2L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2h-3.17L15 2H9zm3 15c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z'/%3E%3C/svg%3E");
  background-size: 28px;
  background-repeat: no-repeat;
  background-position: center;
}

#image-preview {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: none;
}

#image-preview.visible {
  display: block;
}

#scan-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 15, 26, 0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  display: none;
}

#scan-overlay.visible {
  display: flex;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--surface2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#scan-status {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  -webkit-user-select: none;
  user-select: none;
}

.btn:active { transform: scale(0.97); }

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

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

.btn-primary:disabled {
  background: var(--surface2);
  color: var(--text-muted);
  cursor: not-allowed;
  transform: none;
}

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

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

.btn-ghost {
  background: var(--surface2);
  color: var(--text-muted);
}

.btn-ghost:hover { background: var(--surface); }

#camera-input { display: none; }

/* Result section */
#result-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#result-section.hidden { display: none; }

#result-section label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.textarea-wrap {
  position: relative;
}

#address-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 44px 12px 14px;
  color: var(--text);
  font-size: 1rem;
  line-height: 1.5;
  resize: none;
  min-height: 80px;
  transition: border-color 0.15s;
  font-family: inherit;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  padding: 6px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  font-family: inherit;
}

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

.icon-btn:active { transform: scale(0.95); }

.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
}

.copy-btn.copied {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}

#address-input:focus {
  outline: none;
  border-color: var(--accent);
}

#address-input::placeholder { color: var(--text-muted); }

.action-row {
  display: flex;
  gap: 8px;
}

.action-row .btn { flex: 1; }

/* Toast */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface2);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 999px;
  font-size: 0.9rem;
  transition: transform 0.3s ease;
  white-space: nowrap;
  z-index: 100;
  pointer-events: none;
}

#toast.show { transform: translateX(-50%) translateY(0); }

/* Confidence badge */
#confidence-badge {
  display: none;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}

#confidence-badge.visible { display: inline-flex; }
#confidence-badge.excellent { background: #134e4a; color: #5eead4; }
#confidence-badge.good { background: #164e63; color: #67e8f7; }
#confidence-badge.ok { background: #78350f; color: #fcd34d; }
#confidence-badge.bad { background: #881337; color: #fda4af; }

#mode-badge {
  display: none;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  letter-spacing: 0.03em;
}
#mode-badge.visible { display: inline-flex; }
#mode-badge.mode-online { background: #155e75; color: #a5f3fc; }
#mode-badge.mode-offline { background: #1e293b; color: #94a3b8; }

.label-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* History */
#history-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#history-section.hidden { display: none; }

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.history-header h2 {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.text-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  font-family: inherit;
  transition: background 0.15s, color 0.15s;
}

.text-btn:hover {
  background: var(--surface2);
  color: var(--danger);
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  padding: 4px 0;
}

.toggle-row input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 36px;
  height: 20px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  position: relative;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

.toggle-row input[type="checkbox"]::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.15s, background 0.15s;
}

.toggle-row input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle-row input[type="checkbox"]:checked::before {
  transform: translateX(16px);
  background: #fff;
}

#history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-empty {
  padding: 14px 12px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
}

.text-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.history-item-content {
  flex: 1;
  min-width: 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: transparent;
  border: none;
  text-align: left;
  padding: 0;
  color: inherit;
  font-family: inherit;
}

.history-address {
  font-size: 0.9rem;
  color: var(--text);
  line-height: 1.3;
  word-break: break-word;
}

.history-date {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.history-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.history-item-actions .icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
}

.history-item-actions .icon-btn.calendar:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.history-item-actions .icon-btn.delete:hover {
  color: var(--danger);
  border-color: var(--danger);
}

.history-item-actions .icon-btn.edit:hover {
  color: var(--warning);
  border-color: var(--warning);
}

.history-item-actions .icon-btn.save {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}

.history-item-actions .icon-btn.save:hover {
  background: var(--success-hover);
  border-color: var(--success-hover);
}

.history-item-actions .icon-btn.cancel:hover {
  color: var(--danger);
  border-color: var(--danger);
}

.history-item.editing {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.history-edit-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 0.9rem;
  line-height: 1.4;
  font-family: inherit;
  resize: none;
}

.history-edit-input:focus {
  outline: none;
  border-color: var(--accent-hover);
}

.history-item.editing .history-item-actions {
  justify-content: flex-end;
}

footer {
  text-align: center;
  padding: 8px 0 4px;
  font-size: 0.85rem;
  color: var(--text-muted);
}
