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

:root {
  --bg-primary: #212121;
  --bg-secondary: #2f2f2f;
  --bg-sidebar: #171717;
  --bg-input: #2f2f2f;
  --border: #3d3d3d;
  --text-primary: #ececec;
  --text-secondary: #8e8ea0;
  --text-muted: #6b6b80;
  --accent: #10a37f;
  --send-btn-bg: #ececec;
  --send-btn-color: #212121;
  --send-btn-disabled: #4a4a4a;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-width: 260px;
  --topbar-height: 52px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

/* ─── Layout ─────────────────────────────────────────────── */

.layout {
  display: flex;
  height: 100vh;
  height: 100dvh; /* dynamic viewport for mobile browsers */
  overflow: hidden;
}

/* ─── Sidebar overlay (mobile tap-to-close) ──────────────── */

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
}

.sidebar-overlay.visible {
  display: block;
}

/* ─── Sidebar ─────────────────────────────────────────────── */

.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  transition: transform 0.25s ease;
  z-index: 100;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-title {
  font-weight: 600;
  font-size: 15px;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.sidebar-close-btn {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  line-height: 0;
}

.sidebar-close-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
  text-align: left;
  flex-shrink: 0;
}

.new-chat-btn:hover {
  background: var(--bg-secondary);
}

/* History list */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 4px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.history-item.active {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.history-item svg {
  flex-shrink: 0;
  opacity: 0.5;
}

.history-item-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-model {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* ─── Top bar (mobile only) ───────────────────────────────── */

.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-height);
  padding: 0 12px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-title {
  font-weight: 600;
  font-size: 15px;
}

.topbar-menu-btn,
.topbar-new-btn {
  width: 38px;
  height: 38px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

.topbar-menu-btn:hover,
.topbar-new-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

/* ─── Main ────────────────────────────────────────────────── */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
  min-width: 0;
}

/* ─── Chat container ──────────────────────────────────────── */

.chat-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-container::-webkit-scrollbar { width: 6px; }
.chat-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ─── Empty state ─────────────────────────────────────────── */

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
  text-align: center;
}

.empty-title {
  font-size: clamp(20px, 5vw, 28px);
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 10px;
  letter-spacing: -0.02em;
}

.empty-subtitle {
  font-size: clamp(13px, 3vw, 15px);
  color: var(--text-secondary);
  max-width: 340px;
}

/* ─── Messages ────────────────────────────────────────────── */

.messages {
  display: flex;
  flex-direction: column;
  padding-bottom: 16px;
}

.message {
  padding: 20px 0;
  border-bottom: 1px solid var(--border);
}

.message:last-child {
  border-bottom: none;
}

.message-inner {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

/* ─── Avatar ──────────────────────────────────────────────── */

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  margin-top: 2px;
}

.avatar-user { background: #5436DA; color: #fff; }
.avatar-assistant { background: var(--accent); color: #fff; }

.message-content {
  flex: 1;
  min-width: 0;
}

/* ─── User message image ──────────────────────────────────── */

.message-image {
  max-width: min(340px, 100%);
  max-height: 260px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  object-fit: cover;
  display: block;
}

/* ─── Result card ─────────────────────────────────────────── */

.result-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.result-entry {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.result-entry:last-child {
  border-bottom: none;
}

.result-work-order {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.result-work-order span {
  color: var(--accent);
  font-weight: 600;
}

.result-dates {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.result-day {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.result-day-header {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.result-chunk-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-primary);
  border-radius: 6px;
  font-size: 13px;
}

.result-chunk-label {
  color: var(--text-secondary);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-chunk-hours {
  font-weight: 500;
  color: var(--text-primary);
  flex-shrink: 0;
}

.result-date-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-primary);
  border-radius: 6px;
  font-size: 13px;
}

.result-date-label { color: var(--text-secondary); }

.result-date-hours {
  font-weight: 600;
  color: var(--text-primary);
  flex-shrink: 0;
}

.result-date-total {
  margin-top: 4px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
  background: transparent;
}

.result-date-total .result-date-label {
  color: var(--text-primary);
  font-weight: 600;
}

.result-date-total .result-date-hours {
  color: var(--accent);
}

/* ─── Error ───────────────────────────────────────────────── */

.error-message {
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.08);
  border: 1px solid rgba(255, 107, 107, 0.2);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
}

/* ─── Loading dots ────────────────────────────────────────── */

.loading-dots {
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 4px 0;
}

.loading-dots span {
  width: 7px;
  height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite ease-in-out;
}

.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* ─── Progress bar ────────────────────────────────────────── */

.progress-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--bg-secondary);
  border-radius: 3px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ─── Input area ──────────────────────────────────────────── */

.input-area {
  padding: 10px 12px 16px;
  background: var(--bg-primary);
  flex-shrink: 0;
}

.input-wrapper {
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color 0.15s;
}

.input-wrapper:focus-within { border-color: #555; }

.input-wrapper.drag-over {
  border-color: var(--accent);
  background: rgba(16, 163, 127, 0.05);
}

.input-wrapper.input-locked {
  opacity: 0.4;
  pointer-events: none;
}

.input-wrapper.input-locked + .input-footer::after {
  content: ' Start a new analysis to upload another image.';
}

.image-preview-wrapper {
  padding: 10px 12px 0;
}

.image-preview-inner {
  position: relative;
  display: inline-block;
}

.image-preview-inner img {
  max-height: 100px;
  max-width: min(200px, 50vw);
  border-radius: 8px;
  border: 1px solid var(--border);
  object-fit: cover;
  display: block;
}

.remove-image-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

.remove-image-btn:hover {
  background: #ff6b6b;
  color: #fff;
  border-color: transparent;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 8px 8px 10px;
}

.upload-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

.upload-btn:hover {
  background: var(--border);
  color: var(--text-primary);
}

.input-text-area {
  flex: 1;
  min-height: 36px;
  display: flex;
  align-items: center;
  padding: 4px 8px;
}

.input-placeholder {
  color: var(--text-muted);
  font-size: 14px;
  pointer-events: none;
  user-select: none;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: var(--send-btn-bg);
  border: none;
  color: var(--send-btn-color);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

.send-btn:disabled {
  background: var(--send-btn-disabled);
  color: var(--text-muted);
  cursor: not-allowed;
}

.send-btn:not(:disabled):hover { background: #d4d4d4; }

.input-footer {
  max-width: 720px;
  margin: 6px auto 0;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
}

/* ─── Mobile breakpoint ───────────────────────────────────── */

@media (max-width: 640px) {
  /* Show topbar, hide desktop sidebar permanently */
  .topbar {
    display: flex;
  }

  .sidebar-close-btn {
    display: flex;
  }

  /* Sidebar slides in from left as a drawer */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    height: 100dvh;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .message-inner {
    padding: 0 12px;
    gap: 10px;
  }

  .result-entry {
    padding: 12px 12px;
  }

  .result-chunk-row,
  .result-date-row {
    font-size: 12px;
    padding: 5px 8px;
  }

  .input-area {
    padding: 8px 10px 12px;
  }

  .empty-state {
    padding: 32px 20px;
  }
}
