:root {
  --bg: #0b0e14;
  --surface: #111623;
  --muted: #7a8599;
  --text: #e6eaf2;
  --green: #39d353;
  --red: #ff5d62;
  --yellow: #ffd866;
  --blue: #59a9ff;
  --border: #20293a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text);
  background: var(--bg);
}

header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

h1 { margin: 0 0 8px; font-size: 20px; }
h2 { margin: 24px 0 12px; font-size: 18px; }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }

/* Row viewer (collapsible) */
.rv-wrap { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; background: #000; overflow: hidden; display: none; }
.rv-toolbar { display: flex; gap: 10px; align-items: center; padding: 8px; background: #0b0e14; border-bottom: 1px solid var(--border); }
.rv-toolbar .spacer { flex: 1; }
.rv-toolbar .slider { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.rv-stage { position: relative; height: 58vh; overflow: hidden; }
.rv-stage, .rv-overlay, .rv-content { user-select: none; }
.rv-center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
.rv-content { position: absolute; top: 0; left: 0; transform: scale(1); transform-origin: 0 0; }
.rv-stage img { position: absolute; top: 0; left: 0; max-width: none; max-height: none; display: block; }
.rv-overlay { position: absolute; top: 0; left: 0; pointer-events: none; }
.rv .obox { position: absolute; border: 2px solid rgba(0,200,255,0.85); background: rgba(0,200,255,0.08); border-radius: 2px; cursor: pointer; pointer-events: auto; }
.rv .obox .olabel { position: absolute; right: 0; top: 0; background: rgba(0,0,0,0.6); color: #fff; padding: 2px 4px; font-size: 12px; border-bottom-left-radius: 4px; }
.rv .obox.sel { border-color: #ffd866; background: rgba(255, 216, 102, 0.12); }

.ai { margin-top: 24px; }
.ai details { border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.ai details summary { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 16px; cursor: pointer; list-style: none; }
.ai details summary::-webkit-details-marker { display: none; }
.ai details[open] summary { border-bottom: 1px solid var(--border); }
.ai-summary { color: var(--muted); font-size: 14px; }
.ai .ai-content { padding: 12px 16px 16px; }
.ai .ai-row { display: flex; align-items: center; gap: 12px; }
.ai .ai-config { display: grid; grid-template-columns: repeat(3, minmax(200px, 1fr)); gap: 12px; margin-top: 8px; }
.switch { display: inline-flex; align-items: center; gap: 8px; }

.spinner { width: 16px; height: 16px; border: 2px solid #3a4a6a; border-top-color: var(--blue); border-radius: 50%; animation: spin 0.8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

.endpoint-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.endpoint-row .proxy-toggle { margin-left: auto; display: inline-flex; gap: 6px; align-items: center; color: var(--muted); }

.ocr-engine-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  min-width: 200px;
}

select:hover {
  background: #1a2336;
}

label { font-weight: 600; }
input[type="text"], input[type="file"], textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px;
  border-radius: 8px;
}

input[type="text"] { width: 360px; }

button {
  background: #1a2336;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
}

button:hover { background: #222c43; }
.danger { border-color: #522; color: #fbb; }

.status { padding: 4px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); }
.status.online { background: rgba(57, 211, 83, 0.12); color: var(--green); }
.status.offline { background: rgba(255, 93, 98, 0.12); color: var(--red); }
.status.unknown { background: #1a2336; color: var(--muted); }

.muted { color: var(--muted); font-size: 12px; }
.help { margin-top: 6px; color: var(--muted); }

.uploader .actions { margin-top: 8px; display: flex; gap: 8px; }
.drop-zone { margin-top: 8px; border: 2px dashed var(--border); border-radius: 8px; padding: 24px; text-align: center; color: var(--muted); background: #0b0e14; }
.drop-zone.dragover { background: #111b2e; border-color: var(--blue); color: var(--text); }

.results .db-actions { display: flex; gap: 10px; align-items: center; }
.results .import-label { display: inline-flex; align-items: center; gap: 8px; }
.results .import-label input { display: none; }

.results-list { display: grid; gap: 12px; margin-top: 12px; }

.item {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.thumb {
  width: 160px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  transition: transform 0.3s ease;
}

/* Rotation controls overlay */
.rotation-controls {
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
  background: rgba(0, 0, 0, 0.8);
  padding: 4px;
  border-radius: 6px;
}

.item:hover .rotation-controls {
  opacity: 1;
}

/* Auto-rotation indicator badge */
.auto-rotation-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(89, 169, 255, 0.9);
  color: white;
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 14px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  z-index: 10;
  pointer-events: none;
  animation: badgePulse 2s ease-in-out;
}

@keyframes badgePulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.rotate-btn {
  background: rgba(89, 169, 255, 0.2);
  border: 1px solid var(--blue);
  color: var(--blue);
  padding: 4px 8px;
  font-size: 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 32px;
}

.rotate-btn:hover {
  background: var(--blue);
  color: #fff;
  transform: scale(1.1);
}

/* Photo Management Section (Sync Page) */
.photo-management {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 2px solid var(--border);
}

.ocr-settings {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.batch-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.batch-btn {
  background: #1a2a40;
  border-color: #2a4a6a;
  color: var(--text);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.2s;
}

.batch-btn:hover:not(:disabled) {
  background: #2a4a6a;
  border-color: var(--blue);
}

.batch-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pending-count {
  margin-left: auto;
  color: var(--muted);
  font-size: 14px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.photos-list {
  display: grid;
  gap: 12px;
}

.photo-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  transition: all 0.2s;
}

.photo-item:hover {
  background: #1a2336;
  border-color: #2a4a6a;
}

.photo-thumb {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.photo-info {
  flex: 1;
  min-width: 0;
}

.photo-name {
  font-weight: 600;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.photo-meta {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.status-badge {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid;
  margin-top: 4px;
}

.status-uploaded { background: #1a2336; color: var(--muted); border-color: var(--border); }
.status-pending { background: rgba(255, 216, 102, 0.12); color: var(--yellow); border-color: var(--yellow); }
.status-processing { 
  background: rgba(89, 169, 255, 0.12); 
  color: var(--blue); 
  border-color: var(--blue);
  animation: pulse 2s ease-in-out infinite;
}
.status-ai_extracting {
  background: rgba(138, 99, 210, 0.12);
  color: #b794f6;
  border-color: #b794f6;
  animation: pulse 2s ease-in-out infinite;
}
.status-completed { background: rgba(57, 211, 83, 0.12); color: var(--green); border-color: var(--green); }
.status-failed { background: rgba(255, 93, 98, 0.12); color: var(--red); border-color: var(--red); }

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

.ocr-preview {
  margin-top: 8px;
  padding: 8px;
  background: #0e1422;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  color: var(--muted);
  max-height: 60px;
  overflow: hidden;
  line-height: 1.4;
}

.photo-controls {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.photo-btn {
  padding: 8px 12px;
  font-size: 13px;
  background: #1a2336;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 48px;
  min-height: 48px;
}

.photo-btn:hover:not(:disabled) {
  background: #222c43;
  transform: translateY(-1px);
}

.photo-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Mobile optimizations */
@media (max-width: 640px) {
  .photo-item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .photo-thumb {
    width: 100%;
    height: auto;
    max-height: 200px;
  }
  
  .photo-controls {
    width: 100%;
    justify-content: stretch;
  }
  
  .photo-btn {
    flex: 1;
  }
  
  .batch-controls {
    flex-direction: column;
    align-items: stretch;
  }
  
  .batch-btn {
    width: 100%;
  }
  
  .pending-count {
    margin-left: 0;
    text-align: center;
  }
}

.meta { display: flex; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 12px; }
.meta .pill { padding: 2px 8px; background: #1a2336; border: 1px solid var(--border); border-radius: 999px; }

.text-editor { width: 100%; min-height: 96px; resize: vertical; }

.row-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.progress { height: 6px; background: #1a2336; border: 1px solid var(--border); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--blue); width: 0%; transition: width .2s; }

/* Retry buttons styling */
.retry-btn {
  background: #1a2a40;
  border-color: #2a4a6a;
  color: var(--blue);
  font-size: 13px;
  transition: all 0.2s;
}

.retry-btn:hover:not(:disabled) {
  background: #2a4a6a;
  border-color: var(--blue);
  transform: translateY(-1px);
}

.retry-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.json-box {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 12px;
  background: #0e1422;
  color: #cdd7e6;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  white-space: pre-wrap;
  max-height: 160px;
  overflow: auto;
}

.parts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.part-pill { background: #1a2336; border: 1px solid var(--border); color: var(--text); padding: 2px 8px; border-radius: 999px; font-size: 12px; }

.hidden { display: none; }

.zoom-modal { position: fixed; inset: 0; background: rgba(0,0,0,0.8); z-index: 1000; display: grid; grid-template-rows: auto 1fr; }
.zoom-toolbar { display: flex; gap: 8px; align-items: center; padding: 8px; background: #0b0e14; border-bottom: 1px solid var(--border); }
.zoom-stage { position: relative; overflow: hidden; background: #000; }
.zoom-inner { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(1); transform-origin: 0 0; }
.zoom-stage img { position: absolute; top: 0; left: 0; max-width: none; max-height: none; display: block; }
.zoom-overlay { position: absolute; top: 0; left: 0; pointer-events: none; }
.obox { position: absolute; border: 2px solid rgba(255,0,0,0.8); background: rgba(255,0,0,0.08); border-radius: 2px; cursor: pointer; pointer-events: auto; }
.obox .olabel { position: absolute; right: 0; top: 0; background: rgba(0,0,0,0.6); color: #fff; padding: 2px 4px; font-size: 11px; border-bottom-left-radius: 4px; }

/* Ensure hidden state wins over base display */
.zoom-modal.hidden { display: none !important; }
.toast { margin-left: auto; color: #fff; background: rgba(0,0,0,0.6); border: 1px solid var(--border); padding: 4px 8px; border-radius: 6px; font-size: 12px; }
.rv-stage { cursor: grab; }
.rv-stage.grabbing { cursor: grabbing; }
.rv-split { display: grid; grid-template-columns: 1fr 300px; gap: 10px; }
.rv-side { background: var(--surface); border-left: 1px solid var(--border); padding: 8px; overflow: auto; }
.rv-lines { display: grid; gap: 6px; }
.rv-line { padding: 6px 8px; border: 1px solid transparent; border-radius: 6px; cursor: pointer; background: #0e1422; color: var(--text); }
.rv-line:hover { border-color: var(--border); }
.rv-line.sel { border-color: #ffd866; background: #1a2330; }

/* Auth Modal Styles */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Ensure modal hides when hidden class is applied */
.modal.hidden {
  display: none !important;
}

.modal-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  min-width: 400px;
  max-width: 90vw;
}

.modal-content h2 {
  margin: 0 0 16px;
}

.modal-content form {
  display: grid;
  gap: 16px;
}

.modal-content label {
  display: grid;
  gap: 6px;
  font-weight: 600;
}

.modal-content input[type="text"],
.modal-content input[type="password"] {
  width: 100%;
}

.auth-actions {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

.auth-actions button {
  flex: 1;
}

.error {
  background: rgba(255, 93, 98, 0.12);
  color: var(--red);
  padding: 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 93, 98, 0.3);
  margin-bottom: 16px;
}

/* User Info Styles */
.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.user-info strong {
  color: var(--blue);
}

button.small {
  padding: 4px 8px;
  font-size: 12px;
}

.sync-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--blue);
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.2s;
}

.sync-link:hover {
  background: #4a8fd8;
  transform: translateY(-1px);
}

/* Photo Sync Section Styles */
.photo-sync {
  margin-top: 24px;
}

.sync-status {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.status-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.status-label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.status-item span:last-child {
  font-size: 24px;
  font-weight: bold;
  color: var(--text);
}

.ws-status {
  font-size: 14px !important;
  font-weight: normal !important;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.ws-status.connected {
  background: rgba(57, 211, 83, 0.12);
  color: var(--green);
  border-color: var(--green);
}

.ws-status.disconnected {
  background: rgba(255, 93, 98, 0.12);
  color: var(--red);
  border-color: var(--red);
}

.ws-status.connecting {
  background: rgba(255, 216, 102, 0.12);
  color: var(--yellow);
  border-color: var(--yellow);
}

.sync-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.synced-photos {
  display: grid;
  gap: 12px;
  margin-top: 16px;
}

.synced-photo {
  display: grid;
  grid-template-columns: 120px 1fr auto;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.synced-photo img {
  width: 120px;
  height: 90px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
}

.synced-photo-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.synced-photo-filename {
  font-weight: 600;
  color: var(--text);
}

.synced-photo-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--muted);
}

.synced-photo-status {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  border: 1px solid var(--border);
}

.synced-photo-status.pending {
  background: rgba(255, 216, 102, 0.12);
  color: var(--yellow);
}

.synced-photo-status.processing {
  background: rgba(89, 169, 255, 0.12);
  color: var(--blue);
}

.synced-photo-status.completed {
  background: rgba(57, 211, 83, 0.12);
  color: var(--green);
}

.synced-photo-status.failed {
  background: rgba(255, 93, 98, 0.12);
  color: var(--red);
}

.synced-photo-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.synced-photo-actions button {
  white-space: nowrap;
}

/* History Section Styles */
.history { margin-top: 32px; }
.history details { border: 1px solid var(--border); border-radius: 12px; background: var(--surface); overflow: hidden; }
.history summary { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 16px 20px; cursor: pointer; list-style: none; }
.history summary::-webkit-details-marker { display: none; }
.history-summary-row { background: rgba(17, 22, 35, 0.75); border-bottom: 1px solid var(--border); }
.history-summary-main { display: flex; flex-direction: column; gap: 4px; }
.history-title { font-size: 18px; font-weight: 600; color: var(--text); }
.history-summary-text { font-size: 13px; color: var(--muted); }
.history-summary-stats { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.history-summary-stats .stat { display: inline-flex; align-items: center; gap: 6px; background: rgba(17, 22, 35, 0.6); border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; }
.history-summary-stats .stat[data-count="0"] { opacity: 0.45; }
.history-summary-stats .stat strong { color: var(--text); font-weight: 600; }
.history-body { padding: 16px 20px 20px; display: flex; flex-direction: column; gap: 16px; }
.history-controls { display: flex; gap: 16px; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; }
.history-control-left { display: flex; flex: 1 1 auto; gap: 16px; flex-wrap: wrap; align-items: flex-start; }
.history-search { display: flex; flex-direction: column; gap: 6px; min-width: 220px; }
.history-search input { width: 240px; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.history-status-filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.history-chip { border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px; background: #0d121f; color: var(--muted); font-size: 12px; cursor: pointer; transition: all 0.15s ease; }
.history-chip:hover { background: #172031; color: var(--text); }
.history-chip.active { background: rgba(89, 169, 255, 0.16); border-color: var(--blue); color: var(--text); }
.history-control-right { display: flex; gap: 8px; align-items: center; }
.history-control-right .secondary { background: transparent; border-style: dashed; color: var(--muted); }
.history-empty { display: none; padding: 12px; border: 1px dashed var(--border); border-radius: 8px; text-align: center; }
.history-empty.visible { display: block; }
.history-list { display: grid; gap: 10px; }
.history-item { display: grid; grid-template-columns: 1fr auto; gap: 12px; padding: 14px; border: 1px solid var(--border); border-radius: 10px; background: #101623; }
.history-content { display: flex; flex-direction: column; gap: 8px; }
.history-info { display: flex; flex-direction: column; gap: 4px; }
.history-name { font-weight: 600; color: var(--text); }
.history-meta { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.history-ocr { margin: 0; font-size: 13px; line-height: 1.4; color: var(--text); background: rgba(15, 22, 36, 0.8); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; white-space: pre-line; }
.history-ai { display: flex; flex-direction: column; gap: 6px; }
.history-ai-label { font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; }
.history-ai-summary { font-size: 12px; }
.history-ai-parts { display: flex; gap: 6px; flex-wrap: wrap; }
.history-ai-parts .part-pill { background: rgba(89, 169, 255, 0.16); border: 1px solid rgba(89, 169, 255, 0.35); color: var(--text); }
.history-status { padding: 2px 6px; border-radius: 4px; font-size: 11px; }
.history-status.pending { background: rgba(255, 216, 102, 0.12); color: var(--yellow); }
.history-status.uploaded { background: rgba(255, 216, 102, 0.08); color: var(--yellow); }
.history-status.processing { background: rgba(89, 169, 255, 0.12); color: var(--blue); }
.history-status.ai_extracting { background: rgba(89, 169, 255, 0.12); color: var(--blue); }
.history-status.completed { background: rgba(57, 211, 83, 0.12); color: var(--green); }
.history-status.failed { background: rgba(255, 93, 98, 0.12); color: var(--red); }
.history-status.unknown { background: rgba(122, 133, 153, 0.16); color: var(--muted); }
.history-actions { display: flex; gap: 8px; align-items: center; }
.history-actions .small { font-size: 12px; padding: 6px 10px; }
