:root {
  --border: #c9ccd1;
  --border-strong: #8a8f98;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #22242a;
  --accent: #2f6fed;
  --accent-dark: #1e4fb8;
  --danger: #d64545;
  --ok: #1f9d55;
  --sat: #cfe8ff;
  --sun: #ffdcb0;
  --blank: #ececec;
  --cell-w: 20px;
  --row-h: 26px;
  --label-w: 250px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Yu Gothic", "Meiryo", "Hiragino Sans", "Segoe UI", sans-serif;
  font-size: 13px;
}

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

/* ---------- Toolbar ---------- */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: #1f2430;
  color: #fff;
  flex-wrap: wrap;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.toolbar-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.1em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 46vw;
}

.toolbar-sub {
  font-size: 12px;
  color: #aab0bd;
  white-space: nowrap;
}

.btn-back {
  background: #3a4256;
  text-decoration: none;
  display: inline-block;
}

.btn-excel {
  background: #1d6f42;
  border-color: #14522f;
}
.btn-excel:hover { background: #14522f; }

.toolbar-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.btn {
  border: 1px solid #444a58;
  background: #2b3140;
  color: #fff;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: #3a4256; }

.btn:disabled, .btn:disabled:hover {
  opacity: 0.4;
  cursor: default;
  background: #2b3140;
}

.btn-accent {
  background: var(--accent);
  border-color: var(--accent-dark);
}
.btn-accent:hover { background: var(--accent-dark); }

.btn-danger {
  background: #4a2323;
  border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger); }

.palette-bar {
  position: sticky;
  top: 42px;
  z-index: 49;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: #eef0f4;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.palette-label { font-size: 12.5px; color: #555; }

.palette {
  display: flex;
  gap: 4px;
}

.swatch {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.swatch.selected {
  border-color: #222;
  box-shadow: 0 0 0 2px #fff inset;
}

.tool-btn.selected {
  outline: 2px solid #222;
}

.custom-color {
  width: 30px;
  height: 26px;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0;
  cursor: pointer;
}

/* ---------- 編集モード ---------- */
.btn-edit-mode {
  background: var(--accent);
  border-color: var(--accent-dark);
  font-weight: 700;
}
.btn-edit-mode:hover { background: var(--accent-dark); }

body.edit-mode .btn-edit-mode {
  background: #b8621b;
  border-color: #8c4a13;
}
body.edit-mode .btn-edit-mode:hover { background: #8c4a13; }

.edit-status {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 12px;
  white-space: nowrap;
  color: #555;
  background: #e3e5ea;
}
.edit-status-on {
  color: #8c4a13;
  background: #ffe0bd;
}

/* 編集中は操作バーの色を変えて、書き換えられる状態だとひと目で分かるようにする */
body.edit-mode .palette-bar {
  background: #fff3e2;
  border-bottom-color: #e0a04a;
}

/* 閲覧のみの時は、塗れる見た目・つかめる見た目を出さない */
body:not(.edit-mode) .gantt-cell { cursor: default; }
body:not(.edit-mode) .gantt-cell:hover { outline: none; }
body:not(.edit-mode) .row-drag { visibility: hidden; }
body:not(.edit-mode) .palette-label,
body:not(.edit-mode) #palette,
body:not(.edit-mode) #tool-erase,
body:not(.edit-mode) #tool-label,
body:not(.edit-mode) #custom-color {
  opacity: 0.4;
  pointer-events: none;
}

/* 書き換えを止めている間も、中身は今までどおり読めるようにしておく */
.sh-input:disabled,
.item-name-input[readonly],
.sh-input[readonly],
#progress-table input:disabled {
  color: var(--text);
  -webkit-text-fill-color: var(--text);
  opacity: 1;
  cursor: default;
}
.sh-input:disabled { background: transparent; }
#progress-table input:disabled { background: #f7f8fa; }
.row-btn:disabled { opacity: 0.25; cursor: default; }
.row-btn:disabled:hover { color: #888; }

.conn-status {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 12px;
  white-space: nowrap;
}
.conn-online {
  color: var(--ok);
  background: #e4f7ec;
}
.conn-offline {
  color: #8a6d1c;
  background: #fff4d6;
}
.conn-connecting {
  color: #555;
  background: #eee;
}

.share-status {
  margin-left: auto;
  font-size: 12px;
  color: var(--accent-dark);
  font-weight: 600;
}

/* ---------- Main / Header sheet ---------- */
main {
  flex: 1;
  padding: 12px 16px 32px;
}

.sheet-header {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 14px;
}

.sh-title-row {
  text-align: center;
  margin-bottom: 10px;
}

.sh-maintitle {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.4em;
}

.sh-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px 14px;
}

.sh-field {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  background: #fafbfc;
}

.sh-wide { grid-column: span 2; }

.sh-period { grid-column: span 2; }

.sh-label {
  font-size: 11.5px;
  color: #555;
  white-space: nowrap;
  flex: 0 0 auto;
}

.sh-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 13px;
  padding: 3px 4px;
  min-width: 0;
  color: var(--text);
}
.sh-input:focus { outline: 1px solid var(--accent); border-radius: 3px; }

.sh-date { flex: 1 1 auto; }
.sh-tilde { flex: 0 0 auto; color: #777; }

/* ---------- Gantt ---------- */
.gantt-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 18px;
  overflow: hidden;
}

.gantt-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 4px 8px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.gantt-toolbar-title {
  font-size: 13px;
  font-weight: 700;
}

.gantt-toolbar-hint {
  font-size: 11px;
  color: #777;
}

/* 表示単位の切り替え（日別／週間／月間） */
.view-switch {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  flex: 0 0 auto;
}

.view-btn {
  border: none;
  border-left: 1px solid var(--border);
  background: #fff;
  color: #444;
  font-family: inherit;
  font-size: 12px;
  padding: 4px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.view-btn:first-child { border-left: none; }
.view-btn:hover { background: #eef2fb; }
.view-btn.selected {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

/* 表示期間の絞り込み */
.gantt-toolbar-sub {
  padding-top: 0;
}

.range-select, .range-input {
  border: 1px solid var(--border);
  border-radius: 5px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
  font-size: 12px;
  padding: 3px 6px;
}
.range-select:focus, .range-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(47, 111, 237, 0.15);
}

.range-tilde { color: #777; }

.range-btn {
  padding: 4px 10px;
  font-size: 12px;
}
.range-btn:disabled {
  opacity: 0.45;
  cursor: default;
}
.range-btn:disabled:hover { background: #2b3140; }

/* 絞り込み中は、全期間を見ているわけではないことがひと目で分かるようにする */
.range-caption {
  margin: 0 4px 8px;
  padding: 5px 10px;
  border-left: 4px solid var(--accent);
  border-radius: 0 4px 4px 0;
  background: #eaf1ff;
  color: var(--accent-dark);
  font-size: 12.5px;
  font-weight: 700;
}
.range-caption[hidden] { display: none; }

.range-caption-warn {
  border-left-color: var(--danger);
  background: #fdecec;
  color: #a12b2b;
}

.gantt-wrap.range-active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(47, 111, 237, 0.18);
}

.btn-add-item {
  padding: 4px 12px;
  font-size: 12.5px;
}

.gantt-footer {
  padding: 8px 4px 2px;
  border-top: 1px solid var(--border);
  margin-top: 8px;
}

.gantt-scroll {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

#gantt-table {
  border-collapse: collapse;
  table-layout: fixed;
}

/* 週間・月間ビューは1列が複数日ぶんなので、列幅を広げて日付が読めるようにする */
#gantt-table.view-week { --cell-w: 46px; }
#gantt-table.view-month { --cell-w: 66px; }

#gantt-table th, #gantt-table td {
  border: 1px solid var(--border);
  padding: 0;
  text-align: center;
  font-size: 10.5px;
  height: var(--row-h);
}

.th-label, .td-label {
  position: sticky;
  left: 0;
  z-index: 5;
  width: var(--label-w);
  min-width: var(--label-w);
  max-width: var(--label-w);
  background: #f2f3f5;
  text-align: left;
  padding: 2px 6px !important;
}

.td-label { z-index: 4; }

.item-row-inner {
  display: flex;
  align-items: center;
  gap: 4px;
}

.item-name-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 12px;
  min-width: 0;
  padding: 2px 2px;
}
.item-name-input:focus { outline: 1px solid var(--accent); }

.item-name-input.bold { font-weight: 700; }

.row-btn {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 11px;
  color: #888;
  padding: 0 2px;
  line-height: 1;
}
.row-btn:hover { color: var(--danger); }

/* 階層の折りたたみ（閲覧のみモードでも押せる） */
.row-twisty {
  flex: 0 0 auto;
  width: 14px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 9px;
  color: #667;
  padding: 0;
  line-height: 1;
}
.row-twisty:hover { color: var(--accent); }
.row-twisty-empty { cursor: default; visibility: hidden; }

/* 折りたたみ中の親は、中に隠れた行があることが分かるようにする */
tr.row-collapsed .item-name-input { font-style: italic; }
tr.row-collapsed .td-label {
  box-shadow: inset 0 -2px 0 -1px var(--accent);
}

/* 行のドラッグ並べ替え */
.row-drag {
  flex: 0 0 auto;
  cursor: grab;
  color: #b0b4bb;
  font-size: 13px;
  line-height: 1;
  padding: 0 1px;
  user-select: none;
  touch-action: none;
}
.row-drag:hover { color: var(--accent); }
.row-drag:active { cursor: grabbing; }

tr.row-dragging {
  opacity: 0.6;
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
tr.row-dragging .td-label { background: #dbe8ff; }

body.row-drag-active {
  cursor: grabbing;
  user-select: none;
}

.th-month, .th-day, .th-weekday {
  background: #f7f8fa;
  font-weight: 600;
  color: #444;
}

.th-month { white-space: nowrap; }

.day-col {
  width: var(--cell-w);
  min-width: var(--cell-w);
  max-width: var(--cell-w);
}

.td-sat { background: var(--sat); }
.td-sun { background: var(--sun); }
.td-blank { background: var(--blank); }

.gantt-cell {
  cursor: pointer;
  user-select: none;
  position: relative;
  overflow: visible;
}
.gantt-cell:hover { outline: 1px solid var(--accent); outline-offset: -1px; }

/* ---------- 作業名ラベル（バー上に重ねて右へはみ出して表示） ---------- */
.bar-label {
  position: absolute;
  left: 1px;
  top: 50%;
  transform: translateY(-50%);
  white-space: nowrap;
  font-size: 10px;
  line-height: 1.4;
  color: #1a1c22;
  pointer-events: none;
  z-index: 3;
  /* 濃い工程バーの上に重なっても読めるように、薄い白地を敷く */
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 2px;
  padding: 0 3px;
}

.label-editor {
  position: absolute;
  z-index: 200;
  width: 260px;
  padding: 4px 6px;
  font-size: 12px;
  border: 2px solid var(--accent);
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.25);
}

/* ---------- 階層（大工種 → 中工種 → 作業） ---------- */
.td-label.lv0 { background: #e8eaee; }
.td-label.lv1 { background: #f2f3f5; }
.td-label.lv2 { background: #fafbfc; }

/* ---------- Progress / S-curve ---------- */
.progress-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
}

.section-title {
  font-size: 14px;
  margin: 0 0 10px;
}

.progress-table-scroll {
  overflow-x: auto;
  margin-bottom: 12px;
}

#progress-table {
  border-collapse: collapse;
}

#progress-table th, #progress-table td {
  border: 1px solid var(--border);
  padding: 3px 6px;
  font-size: 11.5px;
  text-align: center;
  white-space: nowrap;
}

#progress-table th { background: #f2f3f5; }

#progress-table input {
  width: 52px;
  border: 1px solid var(--border);
  border-radius: 3px;
  text-align: center;
  font-size: 11.5px;
  padding: 2px;
}

.chart-box {
  background: #fdfdfe;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  overflow-x: auto;
}

#progress-canvas { display: block; }

/* ---------- 工事案件一覧ページ ---------- */
.list-main {
  flex: 1;
  padding: 20px 16px 40px;
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
}

.list-status {
  padding: 10px 14px;
  border-radius: 6px;
  margin-bottom: 14px;
  font-size: 13px;
}
.status-info { background: #eef2f8; color: #444; }
.status-error { background: #fdecec; color: #a12b2b; border: 1px solid #f3c0c0; }

.project-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.project-card {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 18px;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.project-card:hover {
  border-color: var(--accent);
  box-shadow: 0 2px 10px rgba(47, 111, 237, 0.12);
}

.project-main {
  flex: 1;
  min-width: 0;
  text-decoration: none;
  color: inherit;
}

.project-name {
  font-size: 15px;
  font-weight: 700;
  margin-bottom: 5px;
  color: var(--accent-dark);
}

.project-info {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  font-size: 12px;
  color: #555;
  margin-bottom: 3px;
}

.project-period {
  font-size: 12.5px;
  color: #333;
}

.project-updated {
  font-size: 11px;
  color: #999;
  margin-top: 3px;
}

.project-actions {
  display: flex;
  gap: 6px;
  flex: 0 0 auto;
}
.project-actions .btn { text-decoration: none; }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #777;
}
.empty-title { font-size: 16px; font-weight: 700; margin: 0 0 8px; }
.empty-desc { font-size: 13px; margin: 0; }

/* ---------- 新規工事作成モーダル ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 22, 28, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 100;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--panel);
  border-radius: 10px;
  width: 100%;
  max-width: 760px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-title { font-size: 16px; margin: 0; }

.modal-close {
  border: none;
  background: transparent;
  font-size: 16px;
  cursor: pointer;
  color: #777;
  padding: 4px 8px;
}
.modal-close:hover { color: var(--danger); }

.modal-body {
  padding: 18px 20px 20px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px 18px;
}

.form-field { display: flex; flex-direction: column; gap: 4px; }
.form-wide { grid-column: span 2; }

.form-label {
  font-size: 12px;
  color: #444;
  font-weight: 600;
}

.req {
  font-style: normal;
  font-size: 10.5px;
  color: #fff;
  background: var(--danger);
  border-radius: 3px;
  padding: 1px 5px;
  margin-left: 4px;
}

.form-input {
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 7px 9px;
  font-size: 13px;
  font-family: inherit;
  width: 100%;
}
.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.15);
}

.form-error {
  grid-column: span 2;
  background: #fdecec;
  border: 1px solid #f3c0c0;
  color: #a12b2b;
  border-radius: 5px;
  padding: 8px 12px;
  font-size: 12.5px;
}

.modal-actions {
  grid-column: span 2;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
.modal-actions .btn { padding: 8px 18px; }

/* ---------- Footer ---------- */
.hint {
  padding: 8px 16px 14px;
  font-size: 11.5px;
  color: #666;
  text-align: center;
}

/* ---------- Print ----------
   印刷は画面の表をそのまま出すのではなく、script.js が #print-root に組み直した
   専用の紙面を出す。1枚に工種すべてが載り、工期が長いときだけ横に紙が増える。 */
#print-root { display: none; }

@media print {
  @page { size: A3 landscape; margin: 8mm; }
  html, body { background: #fff; }

  /* 印刷用DOMを組み終えた時だけ画面用UIを伏せる（組めなかった時は従来通り出す） */
  body.printing > .app { display: none !important; }
  body.printing #print-root { display: block; }
  .no-print { display: none !important; }

  .print-page {
    position: relative;
    width: 403mm;
    height: 280mm;
    overflow: hidden;
    page-break-after: always;
    break-after: page;
    page-break-inside: avoid;
    break-inside: avoid;
    color: #000;
  }
  .print-page:last-child { page-break-after: auto; break-after: auto; }

  /* 高さを固定しておく。ここが伸び縮みすると script.js 側の行数計算とズレる */
  .print-head {
    height: 26mm;
    overflow: hidden;
    box-sizing: border-box;
  }

  .print-title {
    font-size: 13pt;
    font-weight: 700;
    letter-spacing: 0.35em;
    text-align: center;
    margin: 0 0 1.5mm;
  }

  .print-meta {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.6mm 3mm;
    font-size: 7pt;
    margin-bottom: 1.2mm;
  }
  .print-meta-item {
    display: flex;
    gap: 1.5mm;
    border-bottom: 0.2mm solid #999;
    padding-bottom: 0.4mm;
    min-width: 0;
  }
  .print-meta-key { flex: 0 0 auto; color: #333; }
  .print-meta-val {
    flex: 1 1 auto;
    min-width: 0;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .print-subhead {
    display: flex;
    justify-content: space-between;
    font-size: 7pt;
    margin-bottom: 1mm;
  }

  .print-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
  }
  .print-table th, .print-table td {
    border: 0.2mm solid #9aa0a6;
    padding: 0;
    text-align: center;
    line-height: 1;
  }
  /* 見出しと項目欄ははみ出させない。日付セルだけは作業名がはみ出せるようにする
     （ここを hidden にすると、数mmしかないセル幅で作業名が切れてしまう） */
  .print-table th { overflow: hidden; }
  .print-table .p-th { background: #eceef1; font-weight: 600; }
  .print-table .p-month { white-space: nowrap; }
  .print-table .p-label {
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding-right: 1mm;
  }
  .print-table td.p-label.lv0 { background: #eef0f3; }
  .print-table td.p-label.lv1 { background: #f6f7f8; }
  .print-table .p-bold { font-weight: 700; }
  .print-table .p-cell { position: relative; }
  .print-table .td-sat { background: var(--sat); }
  .print-table .td-sun { background: var(--sun); }

  /* 作業名はセル幅（数mm）に収まらないので、はみ出して表示する。
     紙の端で切れないよう、向きは script.js 側で決めてクラスを付けている。 */
  .p-bar-label {
    position: absolute;
    left: 0.3mm;
    top: 50%;
    transform: translateY(-50%);
    white-space: nowrap;
    background: rgba(255, 255, 255, 0.88);
    padding: 0 0.4mm;
    z-index: 2;
  }
  /* 右に余白が無い時は左向きに伸ばす */
  .p-bar-label-left {
    left: auto;
    right: 0.3mm;
  }
  /* どちらにも収まらない時だけ「…」で締める */
  .p-bar-label-clip {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .print-progress-table {
    border-collapse: collapse;
    font-size: 8pt;
    margin-bottom: 4mm;
  }
  .print-progress-table th, .print-progress-table td {
    border: 0.2mm solid #9aa0a6;
    padding: 1mm 2mm;
    text-align: center;
    white-space: nowrap;
  }
  .print-progress-table th { background: #eceef1; }
  .print-chart { max-width: 100%; height: auto; }
}

@media (max-width: 700px) {
  .sh-grid { grid-template-columns: repeat(2, 1fr); }
  .sh-wide, .sh-period { grid-column: span 2; }
  .modal-body { grid-template-columns: 1fr; }
  .form-wide, .form-error, .modal-actions { grid-column: span 1; }
  .project-card { flex-direction: column; align-items: stretch; }
  .toolbar-title { max-width: 100%; }
}
