:root {
  --color-bg: #f4f6f8;
  --color-surface: #ffffff;
  --color-border: #d7dce1;
  --color-text: #1f2933;
  --color-text-muted: #6b7684;
  --color-primary: #2f6fed;
  --color-primary-dark: #1e50b8;
  --color-danger: #d9534f;
  --color-danger-dark: #b3413d;
  --color-saturday: #eaf2fc;
  --color-saturday-text: #2f6fed;
  --color-sunday: #fdecec;
  --color-sunday-text: #d9534f;
  --color-weekend-cell: #f8f9fb;
  --color-gridline: #edf0f2;
  --color-toast-bg: #1f2933;
  --color-toast-text: #fff;
  --label-col-width: 280px;
  --cell-width: 28px;
  --row-height: 34px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #14181d;
    --color-surface: #1c2128;
    --color-border: #333b45;
    --color-text: #e6e9ed;
    --color-text-muted: #8b95a1;
    --color-primary: #5b8def;
    --color-primary-dark: #7ea5f5;
    --color-danger: #e2726e;
    --color-danger-dark: #ea8f8b;
    --color-saturday: #16233d;
    --color-saturday-text: #7ea5f5;
    --color-sunday: #3a1e1e;
    --color-sunday-text: #ea8f8b;
    --color-weekend-cell: #20262e;
    --color-gridline: #262c34;
    --color-toast-bg: #e6e9ed;
    --color-toast-text: #14181d;
  }
}

:root[data-theme="dark"] {
  --color-bg: #14181d;
  --color-surface: #1c2128;
  --color-border: #333b45;
  --color-text: #e6e9ed;
  --color-text-muted: #8b95a1;
  --color-primary: #5b8def;
  --color-primary-dark: #7ea5f5;
  --color-danger: #e2726e;
  --color-danger-dark: #ea8f8b;
  --color-saturday: #16233d;
  --color-saturday-text: #7ea5f5;
  --color-sunday: #3a1e1e;
  --color-sunday-text: #ea8f8b;
  --color-weekend-cell: #20262e;
  --color-gridline: #262c34;
  --color-toast-bg: #e6e9ed;
  --color-toast-text: #14181d;
}

html {
  color-scheme: light dark;
}
:root[data-theme="light"] {
  color-scheme: light;
}
:root[data-theme="dark"] {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

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

button {
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: 6px;
  padding: 6px 12px;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

button:hover {
  background: color-mix(in srgb, var(--color-surface) 85%, var(--color-text) 8%);
}

button.primary {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
button.primary:hover {
  background: var(--color-primary-dark);
}

button.danger {
  color: var(--color-danger);
  border-color: var(--color-danger);
}
button.danger:hover {
  background: var(--color-danger);
  color: #fff;
}

button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

input[type="text"],
select,
textarea {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-surface);
  color: var(--color-text);
}

input[type="text"]:focus,
select:focus,
textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--color-primary) 45%, transparent);
  outline-offset: 1px;
  border-color: var(--color-primary);
}

.date-range-field {
  flex: 1 1 380px;
}
.date-range-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.date-picker {
  display: flex;
  align-items: center;
  gap: 4px;
}
.date-picker select {
  min-width: 0;
}
.date-picker span,
.date-range-sep {
  color: var(--color-text-muted);
  font-size: 12px;
}
.date-range-sep {
  font-size: 14px;
}

/* ---------- Toolbar ---------- */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

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

.app-logo {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--color-primary);
  color: #fff;
}

.toolbar-title-group {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
  min-width: 0;
}

.toolbar-title {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.01em;
}

.toolbar-subtitle {
  font-size: 10px;
  color: var(--color-text-muted);
  letter-spacing: 0.03em;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

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

.toolbar-btn-group button {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.btn-icon {
  display: inline-block;
  font-size: 12px;
  line-height: 1;
}

/* ---------- Main layout ---------- */
#app {
  max-width: 1600px;
  margin: 20px auto;
  padding: 0 20px 48px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  padding: 16px 18px;
}

.panel-title {
  margin: 0 0 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  text-transform: uppercase;
}

.gantt-panel {
  padding: 16px 18px 18px;
}

.project-info {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.project-info .field {
  flex: 1 1 200px;
}

.field label {
  font-size: 11px;
  color: var(--color-text-muted);
}

.field input,
.field textarea {
  width: 100%;
}

/* ---------- Gantt chart ---------- */
.gantt-wrapper {
  border: 1px solid var(--color-border);
  border-radius: 8px;
  overflow: auto;
}

.chart-grid {
  display: inline-block;
  min-width: 100%;
}

.header-row {
  display: flex;
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--color-surface);
  border-bottom: 2px solid var(--color-border);
}

.label-header {
  flex: 0 0 var(--label-col-width);
  width: var(--label-col-width);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  font-weight: 600;
  border-right: 1px solid var(--color-border);
  background: var(--color-surface);
  position: sticky;
  left: 0;
  z-index: 11;
}

.label-header button {
  font-size: 11px;
  padding: 4px 8px;
}

.days-header-wrap {
  display: flex;
  flex-direction: column;
}

.month-group-header {
  display: flex;
}

.month-group-cell {
  flex: 0 0 auto;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--color-text-muted);
  padding: 3px 0;
  border-right: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
}

.days-header {
  display: flex;
}

.day-header-cell {
  flex: 0 0 var(--cell-width);
  border-right: 1px solid var(--color-border);
  text-align: center;
  padding: 4px 0;
}

.day-header-cell.saturday {
  background: var(--color-saturday);
  color: var(--color-saturday-text);
}
.day-header-cell.sunday {
  background: var(--color-sunday);
  color: var(--color-sunday-text);
}

.day-num {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.3;
}
.day-wk {
  font-size: 10px;
  line-height: 1.3;
}

.body-rows {
  position: relative;
  display: flex;
  flex-direction: column;
}

.dependency-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 6;
  overflow: visible;
}
.dependency-line {
  stroke: var(--color-text-muted);
  stroke-width: 1.5;
  fill: none;
}
.dependency-arrowhead {
  fill: var(--color-text-muted);
}

.body-row {
  display: flex;
  border-bottom: 1px solid var(--color-border);
}

.body-row.dragging {
  opacity: 0.4;
}

.row-label {
  flex: 0 0 var(--label-col-width);
  width: var(--label-col-width);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-right: 1px solid var(--color-border);
  background: var(--color-surface);
  position: sticky;
  left: 0;
  z-index: 5;
  height: var(--row-height);
}

.row-label-main {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.row-toggle {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 10px;
  line-height: 1;
}
.row-toggle:hover {
  background: transparent;
  color: var(--color-text);
}
.row-toggle-spacer {
  flex: 0 0 auto;
  display: inline-block;
  width: 16px;
}

.drag-handle {
  flex: 0 0 auto;
  cursor: grab;
  color: var(--color-text-muted);
  user-select: none;
}

.row-label-input {
  flex: 1;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  padding: 4px 6px;
}
.row-label-input:hover,
.row-label-input:focus {
  border-color: var(--color-border);
  background: var(--color-surface);
}

.row-controls {
  flex: 0 0 auto;
  display: flex;
  gap: 2px;
}
.row-controls button {
  padding: 2px 6px;
  font-size: 11px;
  line-height: 1;
}

.row-menu {
  position: fixed;
  z-index: 300;
  display: flex;
  flex-direction: column;
  min-width: 150px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-surface);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  padding: 4px 0;
}
.row-menu[hidden] {
  display: none;
}
.row-menu button {
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--color-text);
  text-align: left;
  padding: 8px 14px;
  font-size: 12px;
}
.row-menu button:hover:not(:disabled) {
  background: color-mix(in srgb, var(--color-surface) 80%, var(--color-text) 8%);
}

.row-track {
  position: relative;
  height: var(--row-height);
  display: flex;
  user-select: none;
}

.day-cell {
  flex: 0 0 var(--cell-width);
  border-right: 1px solid var(--color-gridline);
  height: 100%;
  cursor: context-menu;
}
.day-cell.weekend {
  background: var(--color-weekend-cell);
}

.gantt-bar {
  position: absolute;
  top: 4px;
  height: calc(var(--row-height) - 8px);
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  overflow: hidden;
  cursor: grab;
}
.gantt-bar:active {
  cursor: grabbing;
}

.gantt-bar-label {
  color: #fff;
  font-size: 11px;
  padding: 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.bar-handle {
  position: absolute;
  top: 0;
  width: 8px;
  height: 100%;
  cursor: ew-resize;
}
.bar-handle-left {
  left: 0;
}
.bar-handle-right {
  right: 0;
}

/* ---------- Remarks ---------- */
.remarks-area {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.remarks-area textarea {
  resize: vertical;
  width: 100%;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-surface);
  border-radius: 8px;
  padding: 20px 22px;
  width: 380px;
  max-height: 85vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}

.modal h3 {
  margin: 0;
  font-size: 15px;
}

.color-palette {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.color-swatch {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

.dep-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 140px;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 6px 8px;
}
.dep-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 3px 0;
  cursor: pointer;
}
.dep-item input {
  flex: 0 0 auto;
}
.dep-empty {
  font-size: 12px;
  color: var(--color-text-muted);
  padding: 4px 0;
}

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
}
.modal-actions-right {
  display: flex;
  gap: 8px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-toast-bg);
  color: var(--color-toast-text);
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  z-index: 200;
}

/* ---------- Theme switch ---------- */
.theme-switch {
  display: flex;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  overflow: hidden;
}
.theme-option {
  border: none;
  border-radius: 0;
  padding: 6px 10px;
  background: var(--color-surface);
  color: var(--color-text-muted);
}
.theme-option:not(:last-child) {
  border-right: 1px solid var(--color-border);
}
.theme-option.active {
  background: var(--color-primary);
  color: #fff;
}

/* ---------- Print ---------- */
@media print {
  @page {
    size: A4 landscape;
    margin: 8mm;
  }

  body {
    background: #fff;
  }

  .no-print {
    display: none !important;
  }

  #app {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  .panel {
    border: 1px solid #999;
    box-shadow: none;
    border-radius: 0;
    padding: 8px 10px;
  }

  .panel-title {
    display: none;
  }

  .row-label,
  .label-header,
  .header-row {
    position: static;
  }

  .gantt-wrapper {
    overflow: visible;
  }
}
