:root {
  color-scheme: light;
  --bg: #f4f6f5;
  --panel: #ffffff;
  --panel-soft: #f8faf9;
  --ink: #18211f;
  --muted: #6b7471;
  --line: #dbe2df;
  --line-strong: #c3cfca;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --primary-soft: #dff4ef;
  --accent: #b45309;
  --danger: #b42318;
  --shadow: 0 16px 40px rgba(28, 38, 35, 0.08);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--bg);
}

body {
  margin: 0;
  color: var(--ink);
  font-family:
    "Yu Gothic UI", "Hiragino Kaku Gothic ProN", Meiryo, system-ui, sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
label.file-button,
label.file-wide {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(14px, 3vw, 28px);
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 192px;
  gap: 12px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  color: #ffffff;
  background: var(--primary);
  border-radius: var(--radius);
}

.brand-mark svg {
  width: 24px;
  height: 24px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.2;
}

.brand p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.top-actions {
  display: grid;
  grid-template-columns: minmax(220px, 360px) 42px 42px;
  align-items: center;
  gap: 8px;
}

.search-box {
  height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.search-box svg {
  width: 18px;
  height: 18px;
  color: var(--muted);
  flex: 0 0 auto;
}

.search-box input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
}

.workspace {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(320px, 390px) minmax(0, 1fr);
  gap: clamp(14px, 2vw, 22px);
  padding: clamp(14px, 3vw, 28px);
}

.capture-pane,
.database-pane {
  min-width: 0;
}

.capture-pane {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.camera-panel,
.card-form,
.database-pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.camera-panel,
.card-form {
  padding: 14px;
}

.panel-title,
.summary-strip,
.related-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-title h2,
.empty-state h2 {
  font-size: 16px;
  line-height: 1.35;
}

.status-pill {
  max-width: 164px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--primary-dark);
  background: var(--primary-soft);
  border: 1px solid #bce3dc;
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 12px;
}

.status-pill.is-busy {
  color: var(--accent);
  background: #fff5df;
  border-color: #f5d48b;
}

.status-pill.is-error {
  color: var(--danger);
  background: #fff0ef;
  border-color: #ffd0cc;
}

.viewer {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 10;
  margin-top: 12px;
  overflow: hidden;
  background: #e9eeeb;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
}

.viewer video,
.viewer img,
.empty-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.viewer video,
.viewer img {
  display: none;
}

.viewer video {
  object-fit: cover;
}

.viewer img {
  background: #ffffff;
  object-fit: contain;
}

.viewer.has-video video,
.viewer.has-photo img {
  display: block;
}

.viewer.has-photo video,
.viewer.has-video.has-photo video,
.viewer.has-video.has-photo .empty-preview,
.viewer.has-video .empty-preview,
.viewer.has-photo .empty-preview {
  display: none;
}

.empty-preview {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 8px;
  color: var(--muted);
}

.empty-preview svg {
  width: 42px;
  height: 42px;
}

.camera-actions,
.form-actions {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.camera-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.form-actions {
  grid-template-columns: 1fr 1fr;
}

.ocr-panel {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.ocr-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.ocr-panel h3 {
  margin: 0;
  font-size: 14px;
  line-height: 1.3;
}

.ocr-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ocr-actions .button {
  height: 36px;
  padding: 0 10px;
}

.ocr-actions .icon-button {
  width: 36px;
  min-width: 36px;
  height: 36px;
}

.ocr-fields {
  display: grid;
  gap: 7px;
  margin-top: 10px;
}

.ocr-field {
  min-width: 0;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
  color: var(--ink);
  font-size: 13px;
}

.ocr-field dt {
  color: var(--muted);
}

.ocr-field dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.ocr-missing {
  color: var(--muted);
}

.ocr-raw {
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
}

.ocr-raw summary {
  cursor: pointer;
}

.ocr-raw pre {
  max-height: 130px;
  margin: 8px 0 0;
  overflow: auto;
  padding: 8px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  white-space: pre-wrap;
}

.button,
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 42px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition:
    border-color 0.15s ease,
    background 0.15s ease,
    color 0.15s ease,
    transform 0.15s ease;
}

.button svg,
.icon-button svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
}

.button {
  min-width: 0;
  padding: 0 12px;
  font-weight: 700;
}

.button span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.button.primary {
  color: #ffffff;
  background: var(--primary);
  border-color: var(--primary);
}

.button.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
}

.button.secondary,
.icon-button {
  color: var(--ink);
  background: var(--panel-soft);
  border-color: var(--line);
}

.button.secondary:hover,
.icon-button:hover {
  border-color: var(--line-strong);
  background: #eef4f1;
}

.button:active,
.icon-button:active {
  transform: translateY(1px);
}

.button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.icon-button {
  width: 42px;
  min-width: 42px;
  padding: 0;
}

input[type="file"] {
  display: none;
}

body.has-modal {
  overflow: hidden;
}

.photo-editor {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 12px;
}

.photo-editor[hidden] {
  display: none;
}

.photo-editor-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 18, 16, 0.58);
}

.photo-editor-panel {
  position: relative;
  width: min(960px, 100%);
  max-height: calc(100vh - 24px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.photo-editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.photo-editor-head h2 {
  margin: 0;
  font-size: 18px;
}

.photo-edit-stage {
  position: relative;
  height: min(62vh, 560px);
  min-height: 320px;
  overflow: hidden;
  background: #f2f5f4;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  touch-action: none;
}

.photo-edit-stage canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.crop-box {
  position: absolute;
  display: block;
  min-width: 48px;
  min-height: 48px;
  border: 2px solid var(--primary);
  box-shadow: 0 0 0 9999px rgba(12, 18, 16, 0.34);
  cursor: move;
  touch-action: none;
}

.crop-box::before,
.crop-box::after {
  content: "";
  position: absolute;
  inset: 33.333% 0 auto;
  border-top: 1px solid rgba(255, 255, 255, 0.8);
}

.crop-box::after {
  inset: 66.666% 0 auto;
}

.crop-handle {
  position: absolute;
  width: 22px;
  height: 22px;
  background: #ffffff;
  border: 2px solid var(--primary);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.2);
}

.crop-handle.nw {
  top: -12px;
  left: -12px;
  cursor: nwse-resize;
}

.crop-handle.ne {
  top: -12px;
  right: -12px;
  cursor: nesw-resize;
}

.crop-handle.sw {
  bottom: -12px;
  left: -12px;
  cursor: nesw-resize;
}

.crop-handle.se {
  right: -12px;
  bottom: -12px;
  cursor: nwse-resize;
}

.photo-editor-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.card-form {
  position: sticky;
  top: 84px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label {
  min-width: 0;
}

.card-form label,
.related-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.card-form label span,
.metric-label {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.wide {
  grid-column: 1 / -1;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: 0;
}

input,
select {
  height: 40px;
  padding: 0 10px;
}

textarea {
  min-height: 78px;
  resize: vertical;
  padding: 10px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.database-pane {
  padding: 16px;
}

.summary-strip {
  justify-content: flex-start;
  flex-wrap: wrap;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.summary-strip > div {
  min-width: 116px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.summary-strip strong {
  font-size: 24px;
  line-height: 1;
}

.company-switcher {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 14px 0;
}

.group-tabs {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: auto;
  scrollbar-width: thin;
}

.tab-button {
  max-width: 220px;
  height: 36px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 12px;
  color: var(--ink);
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}

.tab-button span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab-button.is-active {
  color: #ffffff;
  background: var(--primary);
  border-color: var(--primary);
}

.tab-button small {
  color: inherit;
  opacity: 0.78;
}

.group-dropdown {
  display: none;
  max-width: 260px;
}

.related-panel {
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 14px;
  padding: 12px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.company-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 7px;
}

.company-tag {
  max-width: 220px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 4px 8px;
  color: var(--primary-dark);
  background: var(--primary-soft);
  border: 1px solid #bce3dc;
  border-radius: var(--radius);
  font-size: 12px;
}

.company-tag span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.related-form {
  flex-direction: row;
  align-items: center;
}

.related-form input {
  width: min(240px, 54vw);
}

.contacts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 12px;
}

.contact-card {
  min-width: 0;
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.thumb {
  width: 82px;
  height: 58px;
  align-self: start;
  display: grid;
  place-items: center;
  overflow: hidden;
  color: var(--muted);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 3px;
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.thumb svg {
  width: 26px;
  height: 26px;
}

.contact-body {
  min-width: 0;
}

.contact-head {
  min-width: 0;
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 8px;
}

.contact-title {
  min-width: 0;
}

.contact-title h3 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 17px;
  line-height: 1.35;
}

.contact-title p {
  margin-top: 2px;
  color: var(--muted);
  overflow-wrap: anywhere;
  font-size: 13px;
}

.contact-actions {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
}

.mini-button {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
}

.mini-button:hover {
  color: var(--ink);
  background: var(--panel-soft);
  border-color: var(--line);
}

.mini-button.is-danger:hover {
  color: var(--danger);
  background: #fff0ef;
  border-color: #ffd0cc;
}

.mini-button svg {
  width: 16px;
  height: 16px;
}

.contact-meta {
  display: grid;
  gap: 5px;
  margin-top: 10px;
  color: var(--ink);
  font-size: 13px;
}

.meta-line {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
}

.meta-line svg {
  width: 15px;
  height: 15px;
  flex: 0 0 auto;
  color: var(--muted);
}

.meta-line span {
  min-width: 0;
  overflow-wrap: anywhere;
}

.memo {
  margin-top: 9px;
  padding: 8px;
  color: #3f4744;
  background: #fbfaf5;
  border: 1px solid #ece4c8;
  border-radius: var(--radius);
  overflow-wrap: anywhere;
  font-size: 13px;
}

.empty-state {
  min-height: 260px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 10px;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
}

.empty-state svg {
  width: 46px;
  height: 46px;
}

.is-hidden {
  display: none !important;
}

[hidden] {
  display: none !important;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .card-form {
    position: static;
  }
}

@media (max-width: 760px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    grid-template-columns: minmax(0, 1fr) 42px 42px;
  }

  .company-switcher {
    display: block;
  }

  .group-tabs {
    display: none;
  }

  .group-dropdown {
    display: block;
    max-width: none;
  }
}

@media (max-width: 520px) {
  .workspace {
    padding: 10px;
  }

  .camera-actions,
  .form-grid,
  .form-actions {
    grid-template-columns: 1fr;
  }

  .contact-card {
    grid-template-columns: 1fr;
  }

  .thumb {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
    padding: 6px;
  }

  .thumb img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
  }

  .thumb.is-portrait {
    height: clamp(400px, 118vw, 560px);
    aspect-ratio: auto;
  }

  .thumb.is-portrait img {
    width: auto;
    max-width: 100%;
    height: 100%;
    max-height: 100%;
  }

  .photo-editor {
    padding: 8px;
  }

  .photo-editor-panel {
    max-height: calc(100vh - 16px);
    padding: 10px;
  }

  .photo-edit-stage {
    height: min(58vh, 520px);
    min-height: 300px;
  }

  .photo-editor-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
