:root {
  color-scheme: light dark;
  --ink: #14324a;
  --accent: #c8452f;
  --bg: #f4f5f3;
  --surface: #ffffff;
  --surface-2: #eceeea;
  --text: #1c2529;
  --muted: #5e6b70;
  --line: #d8dcd6;
  --link: #0f5e9c;
  --primary-bg: #14324a;
  --primary-text: #ffffff;
  --warn-bg: #fdf1dc;
  --warn-text: #7a4d00;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgb(20 50 74 / 0.08), 0 2px 8px rgb(20 50 74 / 0.06);
  --radius: 10px;
  font-family: -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11181c;
    --surface: #1a2328;
    --surface-2: #243036;
    --text: #e8ecec;
    --muted: #9eabb0;
    --line: #33424a;
    --link: #79b8ee;
    --primary-bg: #e8ecec;
    --primary-text: #14324a;
    --warn-bg: #3d2f12;
    --warn-text: #f3cf8b;
    --danger: #f2877f;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--link); overflow-wrap: anywhere; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
}

/* ---- ボタン ---- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  min-height: 42px;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary-bg); color: var(--primary-text); border-color: var(--primary-bg); }
.btn-primary:hover { background: var(--primary-bg); opacity: 0.9; }
.btn-danger { color: var(--danger); }
.btn-small { padding: 5px 12px; font-size: 14px; min-height: 36px; }
.btn-block { display: block; width: 100%; margin-top: 8px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn { border: 0; background: none; color: inherit; font-size: 20px; line-height: 1; cursor: pointer; padding: 4px 8px; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

/* ---- ヘッダー ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 10px calc(16px + env(safe-area-inset-left));
  background: var(--ink);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; flex: none; }
.brand img { border-radius: 7px; display: block; }
.brand h1 { font-size: 17px; font-weight: 700; letter-spacing: 0.04em; white-space: nowrap; }
.search { flex: 1; min-width: 0; }
.search input {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 8px 14px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  color: #1c2529;
}
.top-actions { display: flex; gap: 8px; flex: none; }
.top-actions select {
  font: inherit;
  padding: 8px;
  border-radius: 8px;
  border: 0;
  background: #fff;
  color: #1c2529;
  min-height: 42px;
}
.top-actions .btn { background: transparent; color: #fff; border-color: rgb(255 255 255 / 0.45); }
.top-actions .btn:hover { background: rgb(255 255 255 / 0.12); }

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
}
.notice span { flex: 1; }
.notice .btn { color: inherit; background: transparent; border-color: currentColor; }

/* ---- 本体 ---- */
main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 16px calc(16px + env(safe-area-inset-right)) calc(96px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}
.dropzone {
  display: block;
  width: 100%;
  padding: 18px;
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  text-align: center;
  cursor: pointer;
}
.dropzone:hover { border-color: var(--ink); }
.dropzone strong { display: block; color: var(--text); font-size: 16px; }
.dropzone span { font-size: 13px; }

.add-buttons { display: flex; gap: 8px; margin-top: 10px; justify-content: flex-end; }
#btn-camera, #btn-photos { display: none; }

.list-head { display: flex; align-items: center; gap: 10px; margin: 18px 0 8px; min-height: 30px; }
.count { color: var(--muted); font-size: 14px; }
.chip {
  border: 1px solid var(--warn-text);
  background: transparent;
  color: var(--warn-text);
  border-radius: 99px;
  font: inherit;
  font-size: 13px;
  padding: 2px 12px;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--warn-bg); font-weight: 700; }

.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 12px;
}
.card {
  position: relative;
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  min-width: 0;
}
.card:hover { border-color: var(--muted); }
.thumb {
  flex: none;
  width: 96px;
  height: 60px;
  border-radius: 5px;
  background: var(--surface-2);
  overflow: hidden;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 22px;
  font-weight: 700;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-main { min-width: 0; flex: 1; }
.card-open {
  all: unset;
  display: block;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  overflow-wrap: anywhere;
}
.card-open::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.card-open:focus-visible::after { outline: 2px solid var(--link); outline-offset: 2px; }
.card-name-empty { color: var(--muted); font-weight: 500; }
.card-org { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.card-links { display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 13px; margin-top: 3px; }
.card-links a { position: relative; z-index: 1; padding: 2px 0; }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 0 7px;
  border-radius: 4px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 11px;
  font-weight: 700;
  vertical-align: 2px;
}

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty img { border-radius: 16px; margin-bottom: 12px; }
.empty h2 { color: var(--text); font-size: 18px; margin-bottom: 4px; }
.empty .btn { margin-top: 14px; }

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgb(20 50 74 / 0.86);
  color: #fff;
  font-size: 26px;
  font-weight: 700;
  pointer-events: none;
}

/* ---- ダイアログ ---- */
dialog.sheet {
  width: min(680px, 100vw);
  max-width: 100vw;
  max-height: 92dvh;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; }
dialog.sheet form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.sheet-narrow { width: min(480px, 100vw) !important; }
dialog::backdrop { background: rgb(10 20 30 / 0.55); }
.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.sheet-title { flex: 1; text-align: center; font-size: 16px; }
.head-gap { width: 62px; }
.spacer { flex: 1; }
.sheet-body { padding: 16px; overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; min-height: 0; }

.photo { margin: 0 0 12px; }
.photo-btn { all: unset; display: block; cursor: zoom-in; width: 100%; text-align: center; }
.photo-btn:focus-visible { outline: 2px solid var(--link); }
.photo img { max-width: 100%; max-height: 240px; border-radius: 6px; display: inline-block; vertical-align: top; }
.photo.zoomed img { max-height: none; }
.photo.zoomed .photo-btn { cursor: zoom-out; }
.photo-actions { display: flex; gap: 8px; justify-content: center; margin-top: 8px; }

.status { background: var(--warn-bg); color: var(--warn-text); padding: 8px 12px; border-radius: 8px; font-size: 14px; margin-bottom: 12px; }
.kana { color: var(--muted); font-size: 13px; }
.detail-name { font-size: 22px; line-height: 1.3; overflow-wrap: anywhere; }
.detail-org { color: var(--muted); margin-bottom: 12px; overflow-wrap: anywhere; }
.rows { display: grid; grid-template-columns: 7em 1fr; gap: 6px 10px; margin: 0 0 16px; }
.rows dt { color: var(--muted); font-size: 13px; padding-top: 2px; }
.rows dd { margin: 0; overflow-wrap: anywhere; min-width: 0; }
.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

input:not([type="radio"], [type="file"], [type="checkbox"]), textarea, .map-table select {
  width: 100%;
  font: inherit;
  font-size: 16px; /* iPhone の入力時ズームを防ぐ */
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.topbar input { background: #fff; color: #1c2529; border: 0; }
textarea { resize: vertical; display: block; }

.raw { margin-top: 12px; font-size: 13px; color: var(--muted); }
.raw summary { cursor: pointer; }
.raw pre { white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; margin: 6px 0 0; }
.danger-row { display: flex; align-items: center; gap: 8px; margin-top: 18px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; align-content: start; }
.form-grid .wide { grid-column: 1 / -1; }
.form-grid label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 2px; }
.form-grid .status { margin: 0; }
.form-error { color: var(--danger); font-size: 14px; }

.summary { font-weight: 700; margin-bottom: 4px; }
.hint { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
.map-table { border-collapse: collapse; width: 100%; font-size: 14px; }
.map-table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.map-table th, .map-table td { padding: 6px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.map-table tr:last-child td { border-bottom: 0; }
.map-table .sample { color: var(--muted); max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-table select { min-width: 130px; padding: 5px 6px; }
.dup { border: 0; padding: 0; margin: 14px 0 0; }
.dup legend { font-size: 13px; color: var(--muted); padding: 0; margin-bottom: 4px; }
.dup label { display: block; padding: 3px 0; }

.stack section + section { margin-top: 20px; }
.stack h3 { font-size: 14px; margin-bottom: 6px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 100;
  max-width: min(92vw, 520px);
  width: max-content;
  padding: 10px 16px;
  border-radius: 10px;
  background: #1c2529;
  color: #fff;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.3);
}
.toast button { all: unset; cursor: pointer; font-weight: 700; color: #ffd9a8; white-space: nowrap; }

/* ---- スマートフォン・タッチ端末 ---- */
@media (pointer: coarse) {
  .dropzone { display: none; }
  #btn-camera, #btn-photos { display: block; }
  .add-buttons {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 4;
    margin: 0;
    padding: 8px calc(12px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
    background: var(--surface);
    border-top: 1px solid var(--line);
  }
  .add-buttons .btn { flex: 1; padding: 9px 4px; white-space: nowrap; }
  .add-buttons .btn-primary { flex: 1.4; }
}

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: 8px 10px; padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); }
  .brand { order: 1; flex: 1; }
  .top-actions { order: 2; }
  .search { order: 3; flex-basis: 100%; }
  .top-actions select { max-width: 132px; }
  .top-actions .btn { padding: 5px 10px; font-size: 14px; }
  main { padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); }
  .grid { grid-template-columns: 1fr; }
  dialog.sheet {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    margin: 0;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sheet-narrow { width: 100vw !important; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ---- ログイン ---- */
.login-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--ink);
}
.login-box {
  width: min(380px, 100%);
  padding: 28px 24px;
  background: var(--surface);
  border-radius: 14px;
  text-align: center;
}
.login-box img { border-radius: 14px; }
.login-box h1 { font-size: 22px; margin: 8px 0 16px; }
.login-box label { display: block; text-align: left; font-size: 13px; color: var(--muted); margin: 12px 0 2px; }
.login-box .form-error { text-align: left; margin-top: 10px; }
.login-box .btn { margin-top: 16px; }
.who { margin: 12px 0 0; }

/* ---- 利用者の管理 ---- */
.user-list { list-style: none; margin: 0 0 12px; padding: 0; }
.user-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.user-row span { flex: 1 1 100%; overflow-wrap: anywhere; }
.user-off span { color: var(--muted); }
#user-form .field, #password-form .field { margin-top: 10px; }
.check { display: block; margin-top: 10px; font-size: 14px; }