:root {
  /* カラーパレット */
  --primary-color: #2563eb;
  --primary-dark: #1e40af;
  --secondary-color: #8b5cf6;
  /* Main Header Gradient Start */
  --header-grad-end: #6366f1;
  /* Main Header Gradient End */

  --success-color: #10b981;
  --danger-color: #ef4444;
  --warning-color: #f59e0b;
  --cell-filled-default: #3b82f6;

  /* グレースケール */
  --bg-body: #f3f4f6;
  --bg-white: #ffffff;
  --bg-gray-50: #f9fafb;
  --bg-gray-100: #f3f4f6;
  --bg-gray-200: #e5e7eb;
  --text-primary: #111827;
  --text-secondary: #4b5563;
  --border-color: #d1d5db;

  /* 曜日カラー */
  --saturday-color: #2563eb;
  --sunday-color: #dc2626;

  /* スペーシング */
  --spacing-xs: 4px;
  --spacing-sm: 8px;
  --spacing-md: 16px;

  /* フォント */
  --font-family: 'Inter', 'Noto Sans JP', sans-serif;
  --font-size-sm: 13px;
  --font-size-base: 14px;

  /* 行の高さ設定 */
  --header-height: 50px;
  --row-height: 50px;
  --col-label-width: 200px;
  --col-date-width: 60px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: var(--font-family);
  background: var(--bg-body);
  color: var(--text-primary);
  min-height: 100vh;
  padding: 24px;
  font-size: var(--font-size-base);
}

.app-container {
  background: var(--bg-white);
  border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 48px);
}

/* ヘッダー */
.app-header {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  padding: 12px;
  text-align: center;
  color: white;
}

.app-header h1 {
  font-size: 20px;
  font-weight: 700;
}

/* ツールバー */
.toolbar {
  background: var(--bg-gray-50);
  padding: 8px 16px;
  display: flex;
  gap: 12px;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
}

.month-navigation {
  display: flex;
  align-items: center;
  gap: 12px;
  background: white;
  padding: 4px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
}

.current-month {
  font-weight: 700;
  color: var(--primary-color);
  min-width: 100px;
  text-align: center;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  background: var(--border-color);
}

.btn {
  padding: 6px 12px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.2s;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: var(--primary-color);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-success {
  background: var(--success-color);
  color: white;
}

.btn-success:hover {
  background: #059669;
}

.btn-secondary {
  background: white;
  border-color: var(--border-color);
  color: var(--text-secondary);
}

.btn-secondary:hover {
  background: var(--bg-gray-50);
}

/* スケジュールコンテナ */
.schedule-container {
  flex: 1;
  overflow: auto;
  position: relative;
}

/* グリッドレイアウト */
.schedule-grid {
  display: grid;
  /* 左列固定幅 + 日付列可変繰り返し */
  grid-template-columns: var(--col-label-width) repeat(var(--days-count, 31), var(--col-date-width));
  /* 行の高さ */
  auto-rows: minmax(var(--row-height), auto);
  user-select: none;
  /* ドラッグ時にテキストが選択されないようにする */
}

/* Row Wrappers needed to be display: contents for Grid */
.header-row,
.summary-row {
  display: contents;
}

/* =========================================
   Z-INDEX & STICKY CONFIGURATION
   ========================================= */

/* 1. Header Row (Dates) */
.header-cell {
  background: var(--bg-gray-50);
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  padding: 4px;
  text-align: center;
  font-weight: 600;
  font-size: 12px;

  /* Sticky Top */
  position: sticky;
  top: 0;
  z-index: 30;
  /* Higher than content, lower than corner */
}

/* 2. Corner Label (Top Left) */
.header-cell.label {
  grid-column: 1;
  background: var(--bg-gray-100);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  /* Highest priority */
  left: 0;
  /* Sticky Left */
  top: 0;
  /* Sticky Top */
}

/* 3. Row Labels (Left Column) */
/* 3. Row Labels (Left Column) */
.row-label {
  display: flex;
  flex-direction: row;
  /* Horizontal: Controls | Inputs */
  align-items: center;
  gap: 8px;
  padding: 4px;
  background-color: #f8fafc;
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);

  /* Sticky Left */
  position: sticky;
  left: 0;
  z-index: 20;
  grid-column: 1;
}

.row-controls {
  display: grid !important;
  grid-template-columns: 20px 20px !important;
  grid-template-rows: 20px 20px !important;
  gap: 2px !important;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 42px;
  /* Ensure container has width */
}

/* Grid Positioning */
.row-controls .btn-up {
  grid-column: 1;
  grid-row: 1;
  /* Top Left */
}

.row-controls .btn-down {
  grid-column: 1;
  grid-row: 2;
  /* Bottom Left */
}

.row-controls .color-trigger {
  grid-column: 2;
  grid-row: 1;
  /* Top Right */
  width: 20px !important;
  height: 20px !important;
  margin: 0;
}

.row-controls .delete-row {
  grid-column: 2;
  grid-row: 2;
  /* Bottom Right */
}

/* =========================================
   Cell Styling
   ========================================= */

.schedule-cell {
  background: white;
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  position: relative;
  /* Crucial for absolute positioned children like .cell-count */
  cursor: pointer;
  z-index: 10;
  user-select: none;
}

.date-header {
  display: flex;
  flex-direction: column;
}

.date-header .date {
  font-size: 14px;
  font-weight: 700;
}

.date-header .day {
  font-size: 11px;
  color: var(--text-secondary);
}

.date-header.saturday {
  color: var(--saturday-color);
}

.date-header.sunday {
  color: var(--sunday-color);
}

.row-label-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.label-input {
  width: 100%;
  padding: 2px 4px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 12px;
  user-select: auto;
  /* 入力フィールド内ではテキスト選択を可能にする */
}

.color-picker {
  width: 20px;
  height: 20px;
  border: none;
  padding: 0;
  cursor: pointer;
}

.btn-icon {
  width: 20px;
  height: 20px;
  padding: 0;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Custom Color Palette */
.color-trigger {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  /* Optional: make it round or square */
  border: 1px solid rgba(0, 0, 0, 0.2);
  cursor: pointer;
  position: relative;
}

.color-palette-popup {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 100;
  background: white;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  width: 140px;
}

.color-swatch {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  cursor: pointer;
}

.color-swatch:hover {
  transform: scale(1.1);
  border-color: rgba(0, 0, 0, 0.3);
}

.color-add-btn {
  grid-column: span 5;
  margin-top: 4px;
  font-size: 10px;
  padding: 4px;
  width: 100%;
}


/* Filled Cells */
.schedule-cell.filled {
  background: var(--cell-color, var(--cell-filled-default));
  opacity: 0.9;
}

.schedule-cell:not(.filled):hover {
  background: var(--bg-gray-50);
}

.schedule-cell.filled:hover {
  opacity: 1;
}

.cell-count {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  /* Let clicks pass through to the cell */
}

.cell-count input {
  pointer-events: auto;
  /* Input needs interaction */
  width: 100%;
  padding: 0;
  margin: 0;
  text-align: center;
  background: transparent;
  border: none;
  color: black;
  font-weight: 700;
  font-size: 14px;
  line-height: 1;
  height: auto;
  user-select: auto;
  text-shadow:
    -1px -1px 0 #fff,
    1px -1px 0 #fff,
    -1px 1px 0 #fff,
    1px 1px 0 #fff;
}

.cell-count-display {
  pointer-events: none;
  /* Let clicks pass through to the cell contextmenu */
  width: 100%;
  padding: 0;
  margin: 0;
  text-align: center;
  color: black;
  font-weight: 700;
  font-size: 14px;
  line-height: 1;
  text-shadow:
    -1px -1px 0 #fff,
    1px -1px 0 #fff,
    -1px 1px 0 #fff,
    1px 1px 0 #fff;
}

/* Summary Cells */
.summary-cell {
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  background: white;
  /* Default background */
}

/* Specific Summary Row Styles */
/* 必要人員数 (Blue Header-ish) */
#totalRequiredRow .summary-cell {
  background: #eff6ff;
  color: var(--primary-dark);
}

#totalRequiredRow .summary-cell.label {
  background: #dbeafe;
}

/* 隊員数 & 休み人数 (Input fields) */
.summary-cell.input input {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  text-align: center;
  border: none;
  background: transparent;
  font-weight: 700;
  font-size: 14px;
  user-select: auto;
  /* 入力フィールド内ではテキスト選択を可能にする */
}

/* 現状人員 (Result) */
#currentStaffRow .summary-cell {
  background: #ecfdf5;
  /* Green tint */
  color: var(--success-color);
  font-weight: 700;
  font-size: 14px;
}

#currentStaffRow .summary-cell.negative {
  background: #fef2f2;
  color: var(--danger-color);
}

/* Hide spin buttons for number inputs */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type=number] {
  -moz-appearance: textfield;
  /* Firefox */
  appearance: textfield;
  /* Standard property */
}

/* =========================================
   タッチデバイス対応
   ========================================= */

/* タッチハイライトの調整 */
* {
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

/* タッチデバイスでのホバー効果を無効化 */
@media (hover: none) and (pointer: coarse) {
  .btn:hover {
    transform: none;
  }

  .schedule-cell:not(.filled):hover {
    background: white;
  }

  .color-swatch:hover {
    transform: none;
  }
}

/* =========================================
   タブレット向け (768px - 1024px)
   ========================================= */

@media screen and (max-width: 1024px) {
  body {
    padding: 12px;
  }

  .app-container {
    max-height: calc(100vh - 24px);
  }

  /* ツールバーの調整 */
  .toolbar {
    padding: 8px;
    gap: 8px;
  }

  .btn {
    padding: 8px 14px;
    font-size: 14px;
    min-height: 44px;
    /* タッチしやすいサイズ */
  }

  /* グリッドの調整 */
  :root {
    --col-date-width: 55px;
    --col-label-width: 180px;
  }

  /* ヘッダーのフォントサイズ調整 */
  .app-header h1 {
    font-size: 18px;
  }

  /* 入力フィールドのサイズ調整 */
  .label-input {
    font-size: 13px;
    padding: 4px 6px;
  }
}

/* =========================================
   スマートフォン向け (〜767px)
   ========================================= */

@media screen and (max-width: 767px) {
  body {
    padding: 8px;
    font-size: 13px;
  }

  .app-container {
    max-height: calc(100vh - 16px);
    border-radius: 8px;
    overflow-y: auto;
    /* Enable scrolling on mobile */
  }

  /* ヘッダーの調整 */
  .app-header {
    padding: 8px;
  }

  .app-header h1 {
    font-size: 16px;
  }

  .app-header>div {
    flex-direction: column !important;
    gap: 8px;
  }

  .app-header .btn {
    width: 100%;
    justify-content: center;
    padding: 6px 10px;
    font-size: 12px;
    min-height: 36px;
  }

  /* ツールバーの調整 */
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 8px;
    gap: 8px;
  }

  .toolbar-divider {
    display: none;
  }

  .month-navigation {
    width: 100%;
    justify-content: space-between;
  }

  .current-month {
    min-width: auto;
    flex: 1;
    font-size: 14px;
  }

  .btn {
    width: 100%;
    justify-content: center;
    padding: 6px 10px;
    font-size: 12px;
    min-height: 36px;
  }

  /* グリッドの調整 */
  :root {
    --col-date-width: 50px;
    --col-label-width: 140px;
    --row-height: 45px;
    --header-height: 45px;
  }

  /* ヘッダーセルのフォントサイズ */
  .header-cell {
    font-size: 11px;
    padding: 2px;
  }

  .date-header .date {
    font-size: 12px;
  }

  .date-header .day {
    font-size: 10px;
  }

  /* 行ラベルの調整 */
  .row-label {
    padding: 2px;
  }

  .label-input {
    font-size: 11px;
    padding: 3px 4px;
  }

  .row-controls {
    width: 20px;
  }

  .color-trigger {
    width: 18px;
    height: 18px;
  }

  .btn-icon {
    width: 18px;
    height: 18px;
    font-size: 9px;
  }

  /* セル内の数字 */
  .cell-count input,
  .cell-count-display {
    font-size: 12px;
  }

  /* サマリー行 */
  .summary-cell {
    font-size: 11px;
  }

  .summary-cell.input input {
    font-size: 12px;
  }

  /* カラーパレット */
  .color-palette-popup {
    width: 120px;
    grid-template-columns: repeat(4, 1fr);
  }

  .color-swatch {
    width: 18px;
    height: 18px;
  }

  .color-add-btn {
    grid-column: span 4;
    font-size: 9px;
    padding: 6px;
  }

  /* スケジュールコンテナのスクロール最適化 */
  .schedule-container {
    -webkit-overflow-scrolling: touch;
  }
}

/* =========================================
   極小スマートフォン向け (〜375px)
   ========================================= */

@media screen and (max-width: 375px) {
  :root {
    --col-date-width: 45px;
    --col-label-width: 120px;
    --row-height: 40px;
  }

  .app-header h1 {
    font-size: 14px;
  }

  .btn {
    font-size: 13px;
    padding: 8px 10px;
  }

  .header-cell {
    font-size: 10px;
  }

  .date-header .date {
    font-size: 11px;
  }

  .date-header .day {
    font-size: 9px;
  }

  .label-input {
    font-size: 10px;
  }
}

/* =========================================
   ハンバーガーメニュー
   ========================================= */

/* ハンバーガーメニューアイコン */
.hamburger-menu {
  display: none;
  /* デフォルトは非表示 */
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 1000;
  width: 44px;
  height: 44px;
  background: var(--primary-color);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.hamburger-menu span {
  display: block;
  width: 24px;
  height: 3px;
  background: white;
  border-radius: 2px;
  transition: all 0.3s ease;
}

.hamburger-menu:hover {
  background: var(--primary-dark);
  transform: scale(1.05);
}

.hamburger-menu:active {
  transform: scale(0.95);
}

/* サイドメニュー */
.side-menu {
  position: fixed;
  top: 0;
  right: -100%;
  width: 80%;
  max-width: 320px;
  height: 100vh;
  background: white;
  z-index: 1001;
  transition: right 0.3s ease;
  overflow-y: auto;
  box-shadow: -4px 0 12px rgba(0, 0, 0, 0.15);
}

.side-menu.active {
  right: 0;
}

.side-menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: var(--primary-color);
  color: white;
}

.side-menu-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.close-menu {
  background: none;
  border: none;
  color: white;
  font-size: 28px;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background 0.2s;
}

.close-menu:hover {
  background: rgba(255, 255, 255, 0.2);
}

.side-menu-content {
  padding: 16px;
}

.menu-section {
  margin-bottom: 24px;
}

.menu-section h3 {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-secondary);
  margin: 0 0 12px 0;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.menu-btn {
  width: 100%;
  margin-bottom: 8px;
  justify-content: flex-start;
  text-align: left;
}

.menu-month-nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.menu-current-month {
  text-align: center;
  font-weight: 700;
  color: var(--primary-color);
  padding: 8px;
  background: var(--bg-gray-50);
  border-radius: 6px;
}

/* オーバーレイ */
.menu-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.menu-overlay.active {
  display: block;
  opacity: 1;
}

/* スマホ画面でのみ表示 */
@media screen and (max-width: 767px) {
  .hamburger-menu {
    display: flex;
  }

  /* 既存のツールバーを非表示 */
  .toolbar {
    display: none !important;
  }
}