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

body {
  font-family: "Meiryo", "Yu Gothic", sans-serif;
  font-size: 13px;
  background: #f5f5f5;
  color: #222;
}

/* ===== Header ===== */
header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: #2c3e50;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  height: 44px;
  gap: 8px;
}
header button, header a.btn-admin {
  background: #455a6a;
  color: #fff;
  border: 1px solid #607d8b;
  border-radius: 4px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 12px;
  text-decoration: none;
}
header button:hover, header a.btn-admin:hover { background: #546e7a; }
header button.active { background: #1565c0; border-color: #1976d2; }
#period-label { font-size: 15px; font-weight: bold; margin: 0 8px; }
.header-left, .header-right { display: flex; align-items: center; gap: 6px; }

/* ===== Grid ===== */
#grid-wrapper {
  overflow: auto;
  max-height: calc(100vh - 44px);
}

#grid-table {
  border-collapse: collapse;
  white-space: nowrap;
  background: #fff;
}

#grid-table th, #grid-table td {
  border: 1px solid #ddd;
  vertical-align: top;
}

/* Fixed left columns */
#grid-table th.col-fixed,
#grid-table td.col-fixed {
  position: sticky;
  background: #fff;
  z-index: 2;
}
#grid-table thead th.col-fixed { z-index: 5; }
.col-fixed:nth-of-type(1) { left: 0; min-width: 160px; max-width: 200px; }
.col-fixed:nth-of-type(2) { left: 160px; min-width: 52px; }
.col-fixed:nth-of-type(3) { left: 212px; min-width: 52px; }
.col-fixed:nth-of-type(4) { left: 264px; min-width: 52px; }

/* Header rows */
thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #37474f;
  color: #fff;
  text-align: center;
  padding: 3px 4px;
  font-size: 11px;
}
thead th.col-fixed { background: #37474f; }
th.sun            { background: #b71c1c !important; }
th.holiday        { background: #b71c1c !important; }
th.holiday-yellow { background: #f57f17 !important; }
th.holiday-green  { background: #2e7d32 !important; }
th.holiday-blue   { background: #1565c0 !important; }

/* Date cells */
td.date-cell {
  min-width: 42px;
  max-width: 60px;
  padding: 2px;
  vertical-align: top;
  cursor: default;
}
body[data-mode="edit"] td.date-cell { cursor: pointer; }
body[data-mode="edit"] td.date-cell:hover { background: #e3f2fd; }
td.sun, td.holiday { background: #fff0f0; }
td.holiday-yellow  { background: #fffde7; }
td.holiday-green   { background: #f1f8e9; }
td.holiday-blue    { background: #e3f2fd; }

td.col-fixed {
  padding: 4px 6px;
  white-space: normal;
  word-break: break-all;
}

/* Group header row */
tr.group-row td {
  background: #455a6a;
  color: #fff;
  font-weight: bold;
  padding: 4px 8px;
}

/* Cell content */
.cell-line {
  display: block;
  font-size: 11px;
  line-height: 1.3;
  white-space: normal;
  word-break: break-all;
}
.cell-line.red { color: #c62828; }
.cell-more { color: #888; font-size: 10px; cursor: pointer; }

/* Spanning cell notes */
.cell-note-span {
  display: block;
  white-space: nowrap;
  font-size: 11px;
  line-height: 1.3;
  color: #222;
}
.cell-note-span.red { color: #c62828; }

/* Gantt bars */
.gantt-bar {
  display: block;
  height: 16px;
  border-radius: 2px;
  margin: 1px 0;
  font-size: 11px;
  line-height: 16px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  padding: 0 2px;
  color: #fff;
}
.gantt-bar.blue   { background: #1565c0; }
.gantt-bar.green  { background: #2e7d32; }
.gantt-bar.red    { background: #c62828; }
.gantt-bar.yellow { background: #f9a825; color: #333; }
.gantt-bar.orange { background: #e65100; }
.gantt-bar.purple { background: #6a1b9a; }

/* 休日・有給奨励日の白抜きバー */
.gantt-bar.muted {
  background: #fff !important;
  border: 1px solid currentColor;
  color: inherit;
}
.gantt-bar.muted.blue   { color: #1565c0; }
.gantt-bar.muted.green  { color: #2e7d32; }
.gantt-bar.muted.red    { color: #c62828; }
.gantt-bar.muted.yellow { color: #b07d00; }
.gantt-bar.muted.purple { color: #6a1b9a; }
.gantt-bar.end-date-marker {
  background: repeating-linear-gradient(
    45deg, #e65100, #e65100 3px, #fff3e0 3px, #fff3e0 6px
  );
  color: #bf360c;
  font-weight: bold;
  border: 1px solid #e65100;
}

/* ===== Popover ===== */
#overlay, #resource-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.3);
  z-index: 200;
}
.popover {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 201;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0,0,0,.25);
  width: 420px;
  max-width: 96vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.popover.hidden { display: none; }
#overlay.hidden { display: none; }
#resource-overlay.hidden { display: none; }

.popover-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid #ddd;
  font-weight: bold;
}
.popover-header button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #666;
}
.popover-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
}
.popover-footer {
  padding: 10px 14px;
  border-top: 1px solid #ddd;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.popover .error {
  color: #c62828;
  padding: 6px 14px;
  font-size: 12px;
}

/* Line editor */
.line-editor {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}
.line-editor input[type="text"] {
  flex: 1;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 13px;
}
.line-editor select {
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px;
  font-size: 12px;
}
.line-editor button { color: #999; background: none; border: none; cursor: pointer; font-size: 14px; }
.line-editor button:hover { color: #c62828; }

/* Gantt editor */
.gantt-editor {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 8px;
  margin-bottom: 8px;
  display: grid;
  gap: 4px;
}
.gantt-editor label { font-size: 11px; color: #666; }
.gantt-editor input, .gantt-editor select {
  width: 100%;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 12px;
}
.gantt-editor .gantt-del {
  color: #c62828;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 12px;
  text-align: right;
}

/* Buttons */
.btn-primary {
  background: #1565c0;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 6px 16px;
  cursor: pointer;
  font-size: 13px;
}
.btn-primary:hover { background: #1976d2; }
.btn-secondary {
  background: #f5f5f5;
  color: #333;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 12px;
}
.btn-secondary:hover { background: #e0e0e0; }

hr { border: none; border-top: 1px solid #eee; margin: 10px 0; }

/* ===== Mobile ===== */
@media (max-width: 767px) {
  body { font-size: 11px; }

  /* ヘッダーをコンパクトに */
  header { height: 38px; padding: 4px 8px; }
  #period-label { font-size: 13px; }
  header button, header a.btn-admin { font-size: 11px; padding: 3px 7px; }

  /* 現場名列を縮小 */
  .col-fixed:nth-of-type(1) { min-width: 80px !important; max-width: 80px !important; font-size: 10px; }

  /* 地域・元請・KY列を非表示 */
  #grid-table .col-fixed:nth-of-type(2),
  #grid-table .col-fixed:nth-of-type(3),
  #grid-table .col-fixed:nth-of-type(4) { display: none; }

  /* グリッドの高さを再計算 */
  #grid-wrapper { max-height: calc(100vh - 38px); }

  /* 日付セルを少し狭く */
  td.date-cell { min-width: 28px; max-width: 36px; }
  thead th { font-size: 10px; padding: 2px 2px; }

  /* セル内テキスト */
  .cell-line { font-size: 10px; }
  .gantt-bar { font-size: 10px; height: 14px; line-height: 14px; }
  .cell-rest { font-size: 10px; }
  .resource-site-label { font-size: 10px; }
}
.gantt-section strong { display: block; margin-bottom: 6px; font-size: 12px; }

/* Admin */
.admin-body { padding: 16px; max-width: 700px; margin: 0 auto; }
.admin-body section { margin-bottom: 28px; }
.admin-body h2 { margin-bottom: 6px; font-size: 15px; border-bottom: 2px solid #37474f; padding-bottom: 4px; }
.admin-note { font-size: 12px; color: #666; margin-bottom: 10px; }
.admin-body ul { list-style: none; margin-bottom: 8px; }
.admin-body li { padding: 4px 8px; border: 1px solid #ddd; border-radius: 4px; margin-bottom: 4px; display: flex; justify-content: space-between; align-items: center; }
.admin-body li button { background: none; border: 1px solid #ccc; border-radius: 3px; padding: 2px 8px; cursor: pointer; color: #c62828; font-size: 11px; }
.admin-body form { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.admin-body form input, .admin-body form select { border: 1px solid #ccc; border-radius: 4px; padding: 5px 8px; font-size: 13px; }
.admin-body form button { background: #1565c0; color: #fff; border: none; border-radius: 4px; padding: 6px 14px; cursor: pointer; }

/* 2-level group tree */
.sg-section { margin-bottom: 12px; }
.sg-l1 { background: #455a6a; color: #fff; padding: 4px 10px; border-radius: 4px; font-weight: bold; font-size: 13px; }
.sg-l2-row { display: flex; justify-content: space-between; align-items: center; padding: 3px 10px; border-left: 3px solid #607d8b; margin: 2px 0 2px 8px; }
.sg-l2-row button { background: none; border: 1px solid #ccc; border-radius: 3px; padding: 2px 8px; cursor: pointer; color: #c62828; font-size: 11px; }
.sg-empty { padding: 2px 10px; color: #aaa; font-size: 12px; margin-left: 8px; }

/* Site end-date UI */
.site-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.end-date-input { border: 1px solid #ccc; border-radius: 3px; padding: 2px 4px; font-size: 11px; }
.set-end { background: #e8f5e9; border: 1px solid #a5d6a7; border-radius: 3px; padding: 2px 8px; cursor: pointer; color: #2e7d32; font-size: 11px; }
.set-end:hover { background: #c8e6c9; }
.end-date-label { font-size: 11px; color: #2e7d32; margin-left: 6px; font-weight: bold; }

/* Grid group rows */
tr.group-row.level1 td { background: #37474f; color: #fff; font-weight: bold; padding: 4px 8px; font-size: 13px; }
tr.group-row.level2 td { background: #607d8b; color: #fff; padding: 3px 8px; font-size: 12px; }

/* Resource section header */
tr.section-header-row td { background: #1b5e20; color: #fff; font-weight: bold; padding: 4px 8px; font-size: 12px; letter-spacing: 0.05em; }
tr.section-header-row { cursor: pointer; user-select: none; }
tr.group-row { user-select: none; }

/* Resource rows */
tr.resource-row td.col-fixed { background: #f1f8e9; }
tr.resource-row:hover td { filter: brightness(0.93); }
tr.resource-row td.date-cell                 { background: #fafff6; }
tr.resource-row td.date-cell.sun             { background: #f9fbe7; }
tr.resource-row td.date-cell.holiday         { background: #f9fbe7; }
tr.resource-row td.date-cell.holiday-yellow  { background: #fffde7; }
tr.resource-row td.date-cell.holiday-green   { background: #f1f8e9; }
tr.resource-row td.date-cell.holiday-blue    { background: #e3f2fd; }
.cell-rest { display: block; font-size: 11px; color: #c62828; text-align: center; font-weight: bold; }

/* Resource cell site label */
.resource-site-label {
  display: block;
  font-size: 11px;
  line-height: 1.4;
  white-space: normal;
  word-break: break-all;
  border-left: 3px solid #333;
  color: #333;
  padding-left: 3px;
  margin: 1px 0;
}
.resource-site-label.rc-black  { color: #333;    border-left-color: #333; }
.resource-site-label.rc-blue   { color: #1565c0; border-left-color: #1565c0; }
.resource-site-label.rc-pink   { color: #c2185b; border-left-color: #c2185b; }
.resource-site-label.rc-green  { color: #2e7d32; border-left-color: #2e7d32; }
.resource-site-label.rc-red    { color: #c62828; border-left-color: #c62828; font-weight: bold; }
.resource-site-label.holiday-work {
  color: #e65100;
  border-left-color: #e65100;
}
