:root {
  --primary: #4361ee;
  --primary-hover: #3651d4;
  --success: #06d6a0;
  --error: #ef476f;
  --warning: #ffd166;
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface-light: #1f2b47;
  --text: #edf2f4;
  --text-muted: #8d99ae;
  --radius: 8px;
  --spacing: 16px;
}

/* Animations */
@keyframes flash-fade {
  0% {
    opacity: 0.8;
  }
  100% {
    opacity: 0;
  }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

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

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app-container {
  max-width: 480px;
  margin: 0 auto;
  padding: var(--spacing);
}

/* Header */
header {
  text-align: center;
  margin-bottom: var(--spacing);
}

header h1 {
  font-size: 1.5rem;
  font-weight: 600;
}

/* Scanner container */
#scanner-section {
  margin-bottom: var(--spacing);
}

#reader {
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  min-height: 200px;
}

/* html5-qrcode overrides */
#reader video {
  border-radius: var(--radius);
}

#reader__scan_region {
  background: var(--surface) !important;
}

#reader__dashboard {
  padding: 8px !important;
}

#reader__dashboard_section_csr button {
  background: var(--primary) !important;
  border: none !important;
  padding: 10px 16px !important;
  border-radius: var(--radius) !important;
  color: white !important;
  font-size: 0.875rem !important;
}

/* Controls section */
#controls {
  margin-bottom: var(--spacing);
}

.form-group {
  margin-bottom: var(--spacing);
}

.form-row {
  display: flex;
  gap: var(--spacing);
}

.form-row .form-group {
  flex: 1;
}

label {
  display: block;
  margin-bottom: 4px;
  font-size: 0.875rem;
  color: var(--text-muted);
}

input, select {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border: 1px solid var(--surface-light);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  min-height: 48px;
  -webkit-appearance: none;
  appearance: none;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%238d99ae' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
}

input[type="number"] {
  text-align: center;
}

/* Buttons */
.button-group {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

button {
  flex: 1;
  min-height: 48px;
  padding: 12px 16px;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color 0.2s, opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
}

button:active {
  transform: scale(0.98);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

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

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

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--surface-light);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-light);
}

.btn-secondary.active {
  background: var(--warning);
  color: #000;
  border-color: var(--warning);
}

/* Status line */
#status-section {
  margin-bottom: var(--spacing);
}

.status {
  padding: 12px;
  border-radius: var(--radius);
  text-align: center;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 500;
  background: var(--surface);
  color: var(--text-muted);
}

.status:empty {
  display: none;
}

.status.success {
  background: var(--success);
  color: #000;
}

.status.error {
  background: var(--error);
  color: #fff;
}

.status.warning {
  background: var(--warning);
  color: #000;
}

/* Scan log */
#log-section h2 {
  font-size: 1rem;
  margin-bottom: 8px;
  color: var(--text-muted);
}

#log-count {
  font-weight: normal;
}

#scan-log {
  list-style: none;
  max-height: 300px;
  overflow-y: auto;
}

#scan-log:empty::after {
  content: "No scans yet";
  display: block;
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-style: italic;
}

#scan-log li {
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  margin-bottom: 8px;
  border-left: 3px solid var(--success);
}

#scan-log li.error {
  border-left-color: var(--error);
}

#scan-log li .barcode {
  font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
  font-size: 1.1rem;
  font-weight: 600;
  word-break: break-all;
}

#scan-log li .meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

#scan-log li .error-text {
  color: var(--error);
  font-size: 0.8rem;
  margin-top: 4px;
}

/* Footer */
footer {
  margin-top: calc(var(--spacing) * 2);
  padding-top: var(--spacing);
  border-top: 1px solid var(--surface-light);
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.version-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 4px;
}

.version-info #app-version {
  font-weight: 600;
  font-family: monospace;
}

.update-badge {
  display: inline-block;
  padding: 2px 8px;
  background: var(--warning);
  color: #000;
  border-radius: 12px;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  animation: pulse 2s ease-in-out infinite;
}

.build-date {
  font-size: 0.7rem;
  opacity: 0.7;
}

/* Screen Navigation */
.hidden {
  display: none !important;
}

/* Screen sections - ensure only one visible at a time */
#start-screen,
#scan-screen,
#summary-screen {
  width: 100%;
  min-height: 60vh;
}

#start-screen {
  display: block;
}

#scan-screen {
  display: none;
}

#summary-screen {
  display: none;
}

#start-screen.hidden,
#scan-screen.hidden,
#summary-screen.hidden {
  display: none !important;
}

#scan-screen:not(.hidden) {
  display: block;
}

#summary-screen:not(.hidden) {
  display: block;
}

/* Start Screen */
.start-content {
  text-align: center;
}

.start-content h2 {
  margin-bottom: calc(var(--spacing) * 1.5);
  font-size: 1.3rem;
}

.btn-large {
  min-height: 56px;
  font-size: 1.1rem;
}

.sessions-section {
  margin-top: calc(var(--spacing) * 2);
}

.sessions-section h3 {
  font-size: 1rem;
  color: var(--text-muted);
  margin-bottom: var(--spacing);
  text-align: left;
}

#sessions-list {
  list-style: none;
  margin-bottom: var(--spacing);
}

.session-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 8px;
  border-left: 3px solid var(--primary);
}

.session-item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.session-name {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 4px;
}

.session-batch {
  font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.session-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.session-actions {
  display: flex;
  gap: 8px;
}

.session-actions button {
  flex: 1;
  min-height: 36px;
  padding: 8px 12px;
  font-size: 0.875rem;
}

.empty-state {
  text-align: center;
  padding: calc(var(--spacing) * 2);
  color: var(--text-muted);
  font-style: italic;
}

/* Scan Screen - Session Header */
#session-header {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: var(--spacing);
  text-align: center;
  border: 2px solid var(--primary);
}

#session-header h2 {
  font-size: 1.1rem;
  margin-bottom: 8px;
}

.book-count {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--success);
  margin: 0;
}

/* Recent Scans (compact version) */
#log-section h3 {
  font-size: 1rem;
  margin-bottom: 8px;
  color: var(--text-muted);
}

#recent-scans {
  list-style: none;
  max-height: 200px;
  overflow-y: auto;
}

#recent-scans:empty::after {
  content: "No scans yet";
  display: block;
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-style: italic;
}

#recent-scans li {
  padding: 8px 12px;
  background: var(--surface);
  border-radius: var(--radius);
  margin-bottom: 6px;
  border-left: 3px solid var(--success);
  font-size: 0.9rem;
}

#recent-scans li.error {
  border-left-color: var(--error);
}

#recent-scans li .barcode {
  font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
  font-weight: 600;
}

#recent-scans li .meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Finish Button */
.finish-section {
  margin-top: calc(var(--spacing) * 1.5);
}

.btn-finish {
  width: 100%;
  min-height: 56px;
  background: var(--warning);
  color: #000;
  font-size: 1.1rem;
}

.btn-finish:hover:not(:disabled) {
  background: #ffc233;
}

/* Summary Screen */
.summary-content {
  text-align: center;
  padding: calc(var(--spacing) * 2) 0;
}

.summary-content h2 {
  font-size: 1.8rem;
  margin-bottom: calc(var(--spacing) * 2);
  color: var(--success);
}

.summary-stats {
  margin-bottom: calc(var(--spacing) * 2);
}

.summary-count-large {
  font-size: 3rem;
  font-weight: 700;
  color: var(--success);
  margin: 0;
}

.summary-details {
  background: var(--surface);
  border-radius: var(--radius);
  padding: calc(var(--spacing) * 1.5);
  margin-bottom: calc(var(--spacing) * 2);
  text-align: left;
}

.summary-details p {
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.summary-details p:last-child {
  margin-bottom: 0;
}

.summary-details strong {
  color: var(--text-muted);
  font-weight: 600;
}

.summary-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Light mode */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f8f9fa;
    --surface: #ffffff;
    --surface-light: #e9ecef;
    --text: #212529;
    --text-muted: #6c757d;
  }

  input, select {
    border-color: #dee2e6;
  }

  .btn-secondary {
    border-color: #dee2e6;
  }
}

/* Landscape orientation - two column layout */
@media (orientation: landscape) and (min-width: 600px) {
  .app-container {
    max-width: 800px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto 1fr;
    gap: var(--spacing);
  }

  header {
    grid-column: 1 / -1;
  }

  #scanner-section {
    grid-column: 1;
    grid-row: 2 / 4;
  }

  #controls {
    grid-column: 2;
    grid-row: 2;
  }

  #status-section {
    grid-column: 2;
    grid-row: 3;
    align-self: start;
  }

  #log-section {
    grid-column: 1 / -1;
  }
}
