/* 電子小黒板 v2 — スタイル */
:root {
  --green-board: #1f5d3a;
  --green-board-dark: #173f29;
  --green-deep: #0e2c1c;
  --chalk: #f4f4ee;
  --bg: #f5f5f0;
  --card: #ffffff;
  --text: #222;
  --text-soft: #555;
  --border: #d8d6cf;
  --primary: #2b6cb0;
  --primary-dark: #1f4f86;
  --danger: #c0392b;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.6;
  overscroll-behavior-y: contain;
}
body { min-height: 100vh; min-height: 100dvh; }
body.fullscreen-mode { background: #000; overflow: hidden; height: 100vh; height: 100dvh; }
body.fullscreen-mode .app-header { display: none; }

/* ヘッダー */
.app-header {
  background: var(--green-board);
  color: var(--chalk);
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}
.app-header h1 {
  font-size: 18px;
  margin: 0;
  flex: 1;
  font-weight: 700;
  letter-spacing: 0.05em;
}
.header-back, .header-icon {
  background: rgba(255,255,255,0.15);
  color: var(--chalk);
  border: none;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  font-size: 22px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.header-back:active, .header-icon:active { background: rgba(255,255,255,0.3); }
.header-back[hidden], .header-icon[hidden] { display: none; }
.header-logo {
  height: 26px;
  width: auto;
  opacity: 0.95;
  filter: brightness(0) invert(1);
}

/* 画面共通 */
.screen { display: none; padding: 16px; max-width: 800px; margin: 0 auto; padding-bottom: calc(80px + env(safe-area-inset-bottom)); }
.screen.active { display: block; }
.screen h2 { font-size: 20px; margin: 8px 0 16px; color: var(--green-deep); }
.screen .subtitle { color: var(--text-soft); margin-bottom: 16px; font-size: 14px; }
.subtitle-inline { color: var(--text-soft); font-size: 14px; }

/* ボタン */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 14px 20px;
  font-size: 17px;
  font-weight: 600;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  background: var(--primary);
  color: white;
  min-height: 52px;
  width: 100%;
  margin-bottom: 8px;
  transition: background 0.15s;
  -webkit-appearance: none;
  text-decoration: none;
}
.btn:active { background: var(--primary-dark); }
.btn.secondary { background: white; color: var(--text); border: 2px solid var(--border); }
.btn.secondary:active { background: #f0f0eb; }
.btn.danger { background: var(--danger); }
.btn.small { padding: 8px 14px; font-size: 14px; min-height: 36px; width: auto; }
.btn:disabled { opacity: 0.5; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; }

/* 案件タイル */
.project-list { display: grid; gap: 10px; margin-bottom: 16px; }
.project-card {
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.project-card:active { background: #f8f8f3; }
.project-card .icon {
  width: 38px; height: 38px;
  background: var(--green-board);
  color: var(--chalk);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: bold;
  flex-shrink: 0;
}
.project-card .info { flex: 1; min-width: 0; }
.project-card .name { font-size: 17px; font-weight: 600; }
.project-card .addr { font-size: 12px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-card .photo-count { font-size: 12px; color: var(--text-soft); flex-shrink: 0; }
.project-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 40px; height: 40px;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: 8px;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: rgba(0,0,0,0.05); }
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-soft);
  background: var(--card);
  border: 2px dashed var(--border);
  border-radius: 10px;
  margin-bottom: 16px;
}

/* モーダル */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: none;
  align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-overlay.active { display: flex; }
.modal {
  background: white;
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.modal h3 { margin: 0 0 12px; font-size: 18px; }
.modal label { display: block; font-size: 14px; color: var(--text-soft); margin-bottom: 6px; }
.modal input[type="text"] {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border: 2px solid var(--border);
  border-radius: 8px;
  -webkit-appearance: none;
  margin-bottom: 12px;
}
.modal input[type="text"]:focus { border-color: var(--primary); outline: none; }
.modal textarea {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border: 2px solid var(--border);
  border-radius: 8px;
  -webkit-appearance: none;
  margin-bottom: 12px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
}
.modal textarea:focus { border-color: var(--primary); outline: none; }
.modal .btn.small { margin-top: 4px; }

/* 内容履歴チップ */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip-btn {
  border: 2px solid var(--border);
  background: #f8f8f3;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
}
.chip-btn:active { background: var(--green-board); color: var(--chalk); }

/* カメラ画面 — 完全固定（スクロール不可・全操作が常に画面内） */
#screen-camera.active {
  display: block;
  position: fixed;
  inset: 0;
  padding: 0;
  max-width: 100%;
  background: #000;
  overflow: hidden;
  touch-action: none;
}
.camera-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
}
/* 純正カメラと同じく4:3を等倍表示（はみ出しクロップせず黒帯レターボックス） */
#video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

/* 黒板（プレビュー）— JSがleft/topを制御。ドラッグ可 */
.kokuban-preview {
  position: absolute;
  left: 12px;
  top: 50%;
  width: min(72vw, 380px);
  background: var(--green-board);
  border: 3px solid #5a3819;
  border-radius: 6px;
  padding: 10px 12px;
  color: var(--chalk);
  font-family: "Hiragino Maru Gothic ProN", "YuGothic", "Yu Gothic", sans-serif;
  box-shadow: 0 4px 14px rgba(0,0,0,0.6);
  line-height: 1.4;
  touch-action: none;
  cursor: grab;
  transform-origin: top left;
  user-select: none;
  -webkit-user-select: none;
  z-index: 4;
}
.kokuban-preview .row-line {
  display: flex;
  border-bottom: 1px dashed rgba(255,255,255,0.4);
  padding: 4px 2px;
  font-size: 13px;
  word-break: break-all;
  pointer-events: none;
}
.kokuban-preview .row-line:last-of-type { border-bottom: none; }
.kokuban-preview .lbl {
  flex: 0 0 56px;
  font-weight: bold;
  color: #ffe9a8;
  font-size: 12px;
}
.kokuban-preview .val { flex: 1; font-weight: 600; white-space: pre-wrap; }
.kokuban-preview .row-line.placeholder .val { color: rgba(244,244,238,0.55); font-weight: 400; }
.kokuban-preview .company {
  text-align: center;
  margin-top: 6px;
  font-size: 12px;
  letter-spacing: 0.05em;
  color: #ffe9a8;
  pointer-events: none;
}

/* --- iOS純正カメラ風UI --- */
.cam-grid {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
}
/* 9マス（3×3・純正と同じ） */
.cam-grid.g9 {
  background:
    linear-gradient(to right, transparent calc(33.33% - 0.5px), rgba(255,255,255,0.28) 33.33%, transparent calc(33.33% + 0.5px)),
    linear-gradient(to right, transparent calc(66.66% - 0.5px), rgba(255,255,255,0.28) 66.66%, transparent calc(66.66% + 0.5px)),
    linear-gradient(to bottom, transparent calc(33.33% - 0.5px), rgba(255,255,255,0.28) 33.33%, transparent calc(33.33% + 0.5px)),
    linear-gradient(to bottom, transparent calc(66.66% - 0.5px), rgba(255,255,255,0.28) 66.66%, transparent calc(66.66% + 0.5px));
}
/* 4マス（2×2・中心十字） */
.cam-grid.g4 {
  background:
    linear-gradient(to right, transparent calc(50% - 0.5px), rgba(255,255,255,0.28) 50%, transparent calc(50% + 0.5px)),
    linear-gradient(to bottom, transparent calc(50% - 0.5px), rgba(255,255,255,0.28) 50%, transparent calc(50% + 0.5px));
}
.cam-grid.goff { display: none; }

.cam-top {
  position: absolute;
  top: env(safe-area-inset-top, 0);
  left: 0; right: 0;
  padding: 10px 12px;
  padding-left: calc(12px + env(safe-area-inset-left));
  padding-right: calc(12px + env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  align-items: center;
  z-index: 5;
}
.cam-top-spacer { flex: 1; }
.cam-pill {
  background: rgba(30,30,30,0.6);
  color: white;
  padding: 7px 13px;
  border-radius: 999px;
  font-size: 13px;
  border: none;
  backdrop-filter: blur(8px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cam-pill.exit { cursor: pointer; font-size: 15px; flex-shrink: 0; }
.cam-pill.ratio { cursor: pointer; font-size: 12px; font-weight: 600; flex-shrink: 0; min-width: 48px; text-align: center; }
.cam-mask {
  position: absolute;
  background: rgba(0,0,0,0.6);
  z-index: 2;
  pointer-events: none;
  display: none;
}
.cam-pill.name { max-width: 40vw; }
.cam-round {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: rgba(30,30,30,0.6);
  backdrop-filter: blur(8px);
  border: none;
  color: white;
  cursor: pointer;
  font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.cam-round:active { background: rgba(255,255,255,0.25); }
.cam-round.sm { width: 38px; height: 38px; font-size: 18px; }
.cam-round.orient { font-size: 13px; font-weight: 600; }

.cam-zoom {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(118px + env(safe-area-inset-bottom));
  display: flex;
  gap: 4px;
  background: rgba(30,30,30,0.5);
  border-radius: 999px;
  padding: 4px;
  z-index: 5;
  backdrop-filter: blur(8px);
}
.cam-zoom button {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.85);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.cam-zoom button.on {
  background: rgba(0,0,0,0.55);
  color: #ffd60a;
  font-size: 14px;
}

.cam-bottom {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  padding: 14px 28px;
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(to top, rgba(0,0,0,0.55), transparent);
  z-index: 5;
}
.cam-thumb {
  width: 48px; height: 48px;
  border-radius: 10px;
  border: 1.5px solid rgba(255,255,255,0.7);
  background: rgba(30,30,30,0.6);
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  flex-shrink: 0;
}
.cam-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-thumb img[src=""] { display: none; }

.shutter-btn {
  width: 70px; height: 70px;
  border-radius: 50%;
  border: 4px solid white;
  background: transparent;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
  padding: 0;
}
.shutter-btn::after {
  content: '';
  position: absolute;
  inset: 4px;
  background: white;
  border-radius: 50%;
  transition: transform 0.08s;
}
.shutter-btn:active::after { transform: scale(0.85); }

/* 横向き：操作を右端の縦バーに（純正カメラの横持ちと同じ） */
@media (orientation: landscape) {
  .cam-top {
    padding-right: calc(104px + env(safe-area-inset-right));
  }
  .cam-bottom {
    left: auto;
    top: 0; right: 0; bottom: 0;
    width: auto;
    flex-direction: column-reverse;
    justify-content: space-between;
    padding: 20px 14px;
    padding-right: calc(14px + env(safe-area-inset-right));
    background: linear-gradient(to left, rgba(0,0,0,0.55), transparent);
  }
  .cam-zoom {
    left: auto;
    transform: none;
    right: calc(108px + env(safe-area-inset-right));
    bottom: 50%;
    flex-direction: column;
    transform: translateY(50%);
  }
}

/* プレビュー画面 — 完全固定・1画面完結（縦=上下、横=左右レイアウト） */
#screen-preview.active {
  display: block;
  position: fixed;
  inset: 0;
  background: #111;
  color: white;
  padding: 0;
  max-width: 100%;
  overflow: hidden;
}
#screen-preview .preview-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  padding-left: calc(12px + env(safe-area-inset-left));
  padding-right: calc(12px + env(safe-area-inset-right));
}
.preview-status { min-height: 22px; }
#preview-img {
  flex: 1;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  border-radius: 8px;
  background: #222;
  display: block;
}
.preview-actions { display: flex; flex-direction: column; flex-shrink: 0; }
.preview-actions .btn { margin-bottom: 6px; min-height: 48px; }
.preview-actions .btn:last-child { margin-bottom: 0; }
#screen-preview .btn.secondary {
  background: rgba(255,255,255,0.1);
  color: white;
  border-color: rgba(255,255,255,0.3);
}
@media (orientation: landscape) {
  #screen-preview .preview-wrap { flex-direction: row; align-items: stretch; }
  #preview-img { width: auto; height: 100%; flex: 1; min-width: 0; }
  .preview-actions { width: 260px; justify-content: center; }
}

/* 状態チップ */
.chip {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.chip.done { background: #d7efdc; color: #1d6b35; }
.chip.pending { background: #fdeccc; color: #92600a; }
.chip.local { background: #e3e3de; color: #555; }

/* ギャラリー */
.gallery-date {
  font-weight: bold;
  color: var(--green-deep);
  border-left: 4px solid var(--green-board);
  padding-left: 10px;
  margin: 18px 0 10px;
  font-size: 15px;
}
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.gallery-cell {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.gallery-cell img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  display: block;
  background: #ddd;
}
.gallery-cell .meta { padding: 8px 10px; }
.gallery-cell .content-line {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gallery-cell .sub { font-size: 11px; color: var(--text-soft); display: flex; gap: 6px; align-items: center; }

/* 写真詳細モーダル */
#modal-photo img { width: 100%; border-radius: 8px; margin-bottom: 10px; background: #eee; }
.ph-content { font-weight: 600; margin-bottom: 4px; }
.ph-meta { font-size: 12px; color: var(--text-soft); word-break: break-all; margin-bottom: 12px; }
#ph-drive-link { margin-bottom: 10px; }

/* 設定 */
.home-section {
  background: var(--card);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.home-section h3 { margin: 0 0 12px; font-size: 15px; color: var(--text-soft); font-weight: 600; }
.home-section input[type="text"] {
  width: 100%;
  padding: 12px;
  font-size: 15px;
  border: 2px solid var(--border);
  border-radius: 8px;
  -webkit-appearance: none;
  margin-bottom: 12px;
}
.field-label { display: block; font-size: 13px; color: var(--text-soft); margin-bottom: 4px; }
.check-label { display: flex; align-items: center; gap: 8px; font-size: 15px; margin-bottom: 12px; }
.check-label input { width: 20px; height: 20px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

/* エラー・トースト */
.error-box {
  background: #fff3cd;
  border: 1px solid #ffe69a;
  color: #664d03;
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-size: 14px;
  line-height: 1.6;
}
.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.85);
  color: white;
  padding: 12px 22px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

/* 横向き: 黒板2列グリッド・操作を右端縦並び */
@media (orientation: landscape) {
  .kokuban-preview {
    display: grid;
    grid-template-columns: 0.85fr 1.15fr;
    column-gap: 10px;
    row-gap: 0;
    width: min(42vw, 430px);
    padding: 6px 10px;
  }
  /* 左列=工事名/日付/場所、右列=内容（左列2段ぶんの高さを使う）/天候 */
  .kokuban-preview .row-line[data-lbl="工事名"] { grid-column: 1; grid-row: 1; }
  .kokuban-preview .row-line[data-lbl="日付"]   { grid-column: 1; grid-row: 2; }
  .kokuban-preview .row-line[data-lbl="場所"]   { grid-column: 1; grid-row: 3; }
  .kokuban-preview .row-line[data-lbl="内容"]   { grid-column: 2; grid-row: 1 / span 2; }
  .kokuban-preview .row-line[data-lbl="天候"]   { grid-column: 2; grid-row: 3; }
  .kokuban-preview .row-line {
    font-size: 12px;
    padding: 3px 2px;
  }
  .kokuban-preview .lbl { flex-basis: 40px; font-size: 11px; }
  .kokuban-preview .company { grid-column: 1 / -1; margin-top: 2px; font-size: 11px; }
  .camera-controls {
    flex-direction: column;
    left: auto;
    right: 0;
    top: 0;
    bottom: 0;
    width: 96px;
    padding: 16px;
    padding-right: calc(16px + env(safe-area-inset-right));
    background: linear-gradient(to left, rgba(0,0,0,0.6), transparent);
    justify-content: center;
  }
  .hint-pill { display: none; }
  .board-size-btns { right: calc(108px + env(safe-area-inset-right)); }
}

@media (max-width: 380px) {
  .kokuban-preview { width: 80vw; }
  .kokuban-preview .row-line { font-size: 12px; }
  .kokuban-preview .lbl { flex-basis: 52px; font-size: 11px; }
}
