/* ===== CSS Variables ===== */
:root {
  --primary: #1e40af;
  --primary-light: #3b82f6;
  --primary-dark: #1e3a8a;
  --accent: #059669;
  --accent-light: #10b981;
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --danger: #dc2626;
  --warning: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --header-height: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans',
    'Noto Sans JP', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ===== Header ===== */
#app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#header-title {
  font-size: 18px;
  font-weight: 600;
}

.header-btn {
  background: none;
  border: none;
  color: white;
  font-size: 20px;
  padding: 8px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background 0.2s;
}
.header-btn:active {
  background: rgba(255,255,255,0.15);
}

/* ===== Main ===== */
#app-main {
  margin-top: var(--header-height);
  min-height: calc(100dvh - var(--header-height));
}

.view {
  display: none;
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  animation: fadeIn 0.2s ease;
}
.view.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Camera View ===== */
#camera-area {
  position: relative;
  width: 100%;
  aspect-ratio: 3/2;
  max-height: 50vh;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
}

#camera-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
  font-size: 14px;
}
#camera-placeholder p:first-child {
  font-size: 48px;
  margin-bottom: 8px;
}

#camera-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#captured-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

#camera-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 16px 0;
}

#post-capture {
  display: flex;
  gap: 12px;
  padding: 12px 0;
}
#post-capture .btn-primary {
  flex: 2;
}
#post-capture .btn-secondary {
  flex: 1;
}

/* ===== Buttons ===== */
.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
}
.btn-primary:active {
  background: var(--primary-dark);
  transform: scale(0.98);
}
.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-secondary:active {
  background: var(--border-light);
}

.btn-accent {
  background: var(--accent);
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  width: 100%;
}
.btn-accent:active {
  background: #047857;
  transform: scale(0.98);
}

.btn-round {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  padding: 0;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
}

/* ===== Result View ===== */
#result-image-container {
  width: 100%;
  max-height: 200px;
  overflow: hidden;
  border-radius: var(--radius);
  margin-bottom: 16px;
  background: #000;
}
#result-image {
  width: 100%;
  height: 200px;
  object-fit: contain;
}

/* ===== Form Fields ===== */
#ocr-fields {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.field-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.field-group input,
.field-group textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.2s;
  width: 100%;
}
.field-group input:focus,
.field-group textarea:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

.field-row {
  flex-direction: row;
  gap: 12px;
}
.field-row > div {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#result-actions {
  display: flex;
  gap: 12px;
}
#result-actions .btn-primary { flex: 1; }
#result-actions .btn-accent { flex: 1; }

/* ===== Loading / Spinner ===== */
#result-loading,
#research-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 16px;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.sub-text {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 8px;
}

/* ===== List View ===== */
#list-toolbar {
  margin-bottom: 16px;
}

#search-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  background: var(--surface);
}
#search-input:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

#list-stats {
  font-size: 13px;
  color: var(--text-muted);
  padding: 8px 4px;
}

#card-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: all 0.2s;
}
.card-item:active {
  transform: scale(0.99);
  box-shadow: none;
}

.card-thumb {
  width: 56px;
  height: 36px;
  border-radius: 4px;
  object-fit: cover;
  background: var(--border-light);
  flex-shrink: 0;
}

.card-info {
  flex: 1;
  min-width: 0;
}
.card-company {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-name {
  font-size: 13px;
  color: var(--text-secondary);
}
.card-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.card-badge {
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  flex-shrink: 0;
}
.badge-researched {
  background: #d1fae5;
  color: #065f46;
}

#list-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
#list-empty p:first-child {
  font-size: 48px;
  margin-bottom: 8px;
}
#list-empty .btn-primary {
  margin-top: 20px;
  width: auto;
  display: inline-block;
}

/* ===== Detail View ===== */
#detail-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.detail-image-container {
  position: relative;
  width: 100%;
  max-height: 200px;
  overflow: hidden;
  border-radius: var(--radius);
  margin-bottom: 16px;
  background: #000;
  cursor: pointer;
}
.detail-image {
  width: 100%;
  height: 200px;
  object-fit: contain;
}
.detail-image-zoom-hint {
  position: absolute;
  bottom: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  pointer-events: none;
}

/* ===== Image Viewer ===== */
.image-viewer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
  animation: fadeIn 0.2s ease;
}
.image-viewer-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  touch-action: none;
}
.image-viewer-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 201;
}
.image-viewer-close:active {
  background: rgba(255, 255, 255, 0.3);
}

/* ===== Image Editor Toolbar ===== */
.image-editor-toolbar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  padding-bottom: calc(12px + var(--safe-bottom));
  background: linear-gradient(transparent, rgba(0,0,0,0.8));
  z-index: 202;
}
.editor-btn {
  background: rgba(255,255,255,0.2);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.3);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}
.editor-btn:active {
  background: rgba(255,255,255,0.35);
}
.editor-btn-save {
  background: var(--accent);
  border-color: var(--accent);
}
.editor-btn-save:active {
  background: #047857;
}
.editor-btn-cancel {
  background: rgba(255,255,255,0.1);
}

/* ===== Crop UI ===== */
.crop-container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.crop-canvas-wrapper {
  position: relative;
}
.crop-canvas {
  display: block;
}
.crop-selection {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.crop-selection .crop-rect {
  pointer-events: auto;
}
.crop-rect {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.6);
  cursor: move;
}
.crop-handle {
  position: absolute;
  width: 28px;
  height: 28px;
  background: #fff;
  border-radius: 50%;
  border: 2px solid var(--primary-light);
  transform: translate(-50%, -50%);
  pointer-events: auto;
  touch-action: none;
}
.crop-handle-tl { top: 0; left: 0; cursor: nwse-resize; }
.crop-handle-tr { top: 0; right: 0; transform: translate(50%, -50%); cursor: nesw-resize; }
.crop-handle-bl { bottom: 0; left: 0; transform: translate(-50%, 50%); cursor: nesw-resize; }
.crop-handle-br { bottom: 0; right: 0; transform: translate(50%, 50%); cursor: nwse-resize; }
.crop-actions {
  position: relative;
  display: flex;
  gap: 12px;
  margin-top: 16px;
  z-index: 203;
}
.crop-actions button {
  min-width: 120px;
  padding: 12px 24px;
  font-size: 15px;
}

.detail-header {
  text-align: center;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.detail-company {
  font-size: 20px;
  font-weight: 700;
}
.detail-name {
  font-size: 16px;
  color: var(--text-secondary);
  margin-top: 4px;
}
.detail-title-dept {
  font-size: 14px;
  color: var(--text-muted);
  margin-top: 2px;
}

.detail-field {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-light);
}
.detail-field-icon {
  font-size: 18px;
  flex-shrink: 0;
  width: 24px;
  text-align: center;
}
.detail-field-content {
  flex: 1;
}
.detail-field-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.detail-field-value {
  font-size: 15px;
  color: var(--text);
  word-break: break-all;
}
.detail-field-value a {
  color: var(--primary-light);
  text-decoration: none;
}

.detail-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
  flex-wrap: wrap;
}
.detail-actions button {
  flex: 1;
  min-width: 120px;
}

/* ===== Research Report ===== */
#research-report {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  font-size: 14px;
  line-height: 1.8;
}
#research-report h2 {
  font-size: 16px;
  color: var(--primary);
  border-bottom: 2px solid var(--primary);
  padding-bottom: 4px;
  margin: 24px 0 12px;
}
#research-report h2:first-child {
  margin-top: 0;
}
#research-report h3 {
  font-size: 15px;
  margin: 16px 0 8px;
}
#research-report table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
}
#research-report th, #research-report td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: left;
}
#research-report th {
  background: var(--border-light);
  font-weight: 600;
}
#research-report a {
  color: var(--primary-light);
  word-break: break-all;
}

#research-actions {
  display: flex;
  gap: 12px;
}
#research-actions button { flex: 1; }

/* ===== Toast ===== */
#toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: white;
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 200;
  transition: transform 0.3s ease;
  box-shadow: var(--shadow-lg);
  max-width: 90%;
  text-align: center;
}
#toast.show {
  display: block !important;
  transform: translateX(-50%) translateY(0);
}

/* ===== Delete Confirm ===== */
.delete-confirm-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 150;
  padding: 20px;
}
.delete-confirm-dialog {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
}
.delete-confirm-dialog p {
  margin-bottom: 20px;
  font-size: 15px;
}
.delete-confirm-actions {
  display: flex;
  gap: 12px;
}
.delete-confirm-actions button { flex: 1; }
.btn-danger {
  background: var(--danger);
  color: white;
  border: none;
  padding: 12px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  cursor: pointer;
}

/* ===== Responsive ===== */
@media (min-width: 768px) {
  #app-main {
    max-width: 640px;
    margin-left: auto;
    margin-right: auto;
  }
  #camera-area {
    max-height: 400px;
  }
}
