:root {
  --bg: #f3f6f8;
  --surface: #fff;
  --text: #1f2d3d;
  --muted: #6a7b8c;
  --line: #dfe7ec;
  --blue: #2fc6f6;
  --blue-dark: #0b92c7;
  --green: #49c98a;
  --violet: #8d72e8;
  --orange: #ff9d5a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
}

button, input, select { font: inherit; }
button { cursor: pointer; }

.app-shell {
  max-width: 1200px;
  min-height: 100vh;
  margin: 0 auto;
  padding: 26px;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--blue-dark);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1 { margin: 0; font-size: 30px; }
h2 { margin: 0 0 10px; font-size: 22px; }

.muted { margin: 5px 0 0; color: var(--muted); font-size: 13px; }

.header-actions {
  display: flex;
  gap: 10px;
}

.button {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  padding: 0 18px;
  font-weight: 600;
  background: #e7f7fc;
  color: #267898;
}

.button-primary {
  background: var(--blue-dark);
  color: #fff;
}

.button-secondary {
  background: #e7f7fc;
  color: #267898;
}

.button-danger { background: #fff0f0; color: #a72b2b; }
.back-button { border: 0; padding: 0; margin: 0 0 8px; background: none; color: var(--blue-dark); font-weight: 600; }

.chains-grid { display: grid; gap: 12px; }
.chain-card { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 20px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.chain-card h2 { font-size: 18px; }
.chain-actions { display: flex; gap: 8px; }
.empty-state { padding: 70px 20px; text-align: center; background: var(--surface); border: 1px dashed #bccbd4; border-radius: 12px; }
.empty-state p { color: var(--muted); margin: 8px 0 20px; }

.template-panel { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; padding: 16px; background: #eefaff; border: 1px solid #ccecf7; border-radius: 12px; }
.template-panel .template-id { min-width: 150px; }
.template-info { flex-basis: 100%; margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.task-defaults { flex-basis: 100%; display: grid; grid-template-columns: minmax(220px, 2fr) minmax(150px, 1fr) minmax(150px, 1fr); gap: 12px; padding-top: 10px; border-top: 1px solid #ccecf7; }
.task-defaults[hidden] { display: none; }
.task-defaults .task-description { grid-column: 1 / -1; }
.task-defaults textarea, .modal textarea { width: 100%; resize: vertical; }

.button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* Control Panel */
.control-panel {
  display: flex;
  gap: 20px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 20px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-panel .control-group:first-child { flex: 1; }

.control-group label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.control-group input,
.control-group select {
  height: 40px;
  padding: 0 11px;
  border: 1px solid #cdd8df;
  border-radius: 8px;
  background: #fff;
}

.control-group input:focus,
.control-group select:focus {
  outline: 2px solid rgba(47,198,246,0.22);
  border-color: var(--blue);
}

/* Add Buttons */
.add-buttons {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

/* Gantt Chart */
.gantt-container {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 20px;
}

.gantt-header {
  display: flex;
  background: #f9fcfd;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.gantt-task-col {
  width: 300px;
  padding: 12px;
  border-right: 1px solid var(--line);
}

.gantt-timeline {
  flex: 1;
  padding: 12px;
}

.gantt-body {
  min-height: 200px;
  max-height: 600px;
  overflow: auto;
}

.gantt-grid { --days: 1; width: calc(280px + var(--days) * 100px); min-width: 100%; }
.gantt-dates { position: sticky; top: 0; z-index: 5; display: grid; grid-template-columns: 280px repeat(var(--days), 100px); height: 44px; background: #f8fbfc; border-bottom: 1px solid var(--line); }
.gantt-corner, .gantt-day { display: flex; align-items: center; justify-content: center; border-right: 1px solid var(--line); color: var(--muted); font-size: 12px; font-weight: 600; }
.gantt-corner { position: sticky; left: 0; z-index: 6; justify-content: flex-start; padding: 0 12px; background: #f8fbfc; }
.gantt-grid-row { display: grid; grid-template-columns: 280px calc(var(--days) * 100px); min-height: 54px; border-bottom: 1px solid var(--line); }
.gantt-grid-name { position: sticky; left: 0; z-index: 3; border: 0; border-right: 1px solid var(--line); padding: 0 12px; text-align: left; font-weight: 600; color: var(--text); background: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gantt-grid-name:hover { background: #eefaff; }
.gantt-grid-timeline { --days: 1; position: relative; width: calc(var(--days) * 100px); background-image: repeating-linear-gradient(to right, transparent 0, transparent 99px, var(--line) 99px, var(--line) 100px); }
.gantt-grid-bar { position: absolute; top: 9px; height: 36px; overflow: hidden; border: 0; border-radius: 6px; padding: 0 10px; color: #fff; font-size: 12px; font-weight: 700; text-align: left; white-space: nowrap; background: linear-gradient(90deg, var(--blue-dark), var(--violet)); }
.gantt-grid-bar:hover { filter: brightness(1.06); box-shadow: 0 3px 10px rgba(11,146,199,.3); }

.gantt-row {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #edf1f3;
}

.gantt-row:last-child {
  border-bottom: 0;
}

.gantt-task-name {
  width: 300px;
  padding: 0 12px;
  font-weight: 600;
  border-right: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gantt-bar-container {
  flex: 1;
  position: relative;
  height: 40px;
  padding: 0 10px;
}

.gantt-bar {
  position: absolute;
  height: 28px;
  top: 6px;
  background: linear-gradient(90deg, var(--blue), var(--violet));
  border-radius: 6px;
  cursor: pointer;
  transition: transform 0.2s;
}

.gantt-bar:hover {
  transform: scaleY(1.1);
}

.gantt-delete {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.9);
  color: #a72b2b;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: none;
}

.gantt-bar:hover .gantt-delete {
  display: block;
}

.gantt-empty {
  padding: 60px 20px;
  text-align: center;
  color: var(--muted);
}

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: var(--surface);
  border-radius: 12px;
  padding: 24px;
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
}

.form-group input,
.form-group select {
  width: 100%;
  height: 40px;
  padding: 0 11px;
  border: 1px solid #cdd8df;
  border-radius: 8px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.parent-list {
  display: grid;
  gap: 10px;
  margin: 20px 0;
}

.parent-button {
  text-align: left;
  padding: 12px;
}

/* Create Section */
.create-section {
  text-align: center;
  margin: 30px 0;
}

.button-large {
  padding: 0 32px;
  min-height: 50px;
  font-size: 16px;
}

/* Notice */
.notice {
  padding: 13px 15px;
  margin-bottom: 16px;
  border: 1px solid #f0c36d;
  border-radius: 9px;
  color: #70551e;
  background: #fff6df;
  font-size: 13px;
}

.notice.error {
  border-color: #ef9d9d;
  color: #8b2929;
  background: #fff0f0;
}

/* Loader */
.loader {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 45px;
  color: var(--muted);
}

.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid #d9e7ed;
  border-top-color: var(--blue-dark);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Responsive */
@media (max-width: 700px) {
  .app-shell { padding: 16px; }
  
  .page-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .header-actions {
    width: 100%;
    flex-wrap: wrap;
  }
  
  .control-panel {
    flex-direction: column;
  }
  
  .gantt-task-col,
  .gantt-task-name {
    width: 150px;
    font-size: 12px;
  }
  
  .add-buttons {
    flex-direction: column;
  }
  
  .add-buttons .button {
    width: 100%;
  }

  .chain-card { align-items: flex-start; flex-direction: column; }
  .template-panel .button, .template-panel .control-group { width: 100%; }
}

.selector-button { width: 100%; min-height: 42px; border: 1px solid #cdd8df; border-radius: 8px; padding: 8px 11px; text-align: left; color: var(--text); background: #fff; }
.clear-selection { border: 0; margin-top: 5px; padding: 0; color: var(--blue-dark); background: none; }
.modal-wide { max-width: 760px; }
.status-legend { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; margin: 12px 0 18px; color: var(--muted); font-size: 12px; }
.status-dot { display: inline-block; flex: 0 0 10px; width: 10px; height: 10px; margin-left: 7px; border-radius: 50%; }
.status-dot.completed { background: #49c98a; }
.status-dot.active { background: #2fc6f6; }
.status-dot.overdue { background: #e85d68; }
.status-dot.waiting { background: #ffb454; }
.run-card { margin: 12px 0; padding: 14px; border: 1px solid var(--line); border-radius: 10px; }
.run-card h3 { margin: 0 0 10px; font-size: 14px; }
.run-task { display: grid; grid-template-columns: 18px 1fr auto; align-items: center; gap: 8px; width: 100%; min-height: 40px; border: 0; border-top: 1px solid #edf1f3; padding: 6px; text-align: left; background: #fff; }
.run-card-header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.run-card-header h3 { margin: 0; flex: 1 1 240px; }
.run-task-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run-task.completed { color: #27865b; }
.run-task.active { color: #147fa7; }
.run-task.overdue { color: #b5323e; background: #fff7f7; }
.run-task.waiting { color: #9b6118; }

.run-timeline { --run-days: 1; width: calc(230px + var(--run-days) * 64px); min-width: 100%; overflow: hidden; border: 1px solid var(--line); border-radius: 8px; }
.run-card { overflow-x: auto; }
.run-timeline-header { position: sticky; top: 0; z-index: 4; display: grid; grid-template-columns: 230px repeat(var(--run-days), 64px); height: 38px; background: #f8fbfc; }
.run-timeline-corner, .run-timeline-day { display: flex; align-items: center; justify-content: center; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); font-size: 11px; color: var(--muted); }
.run-timeline-corner { position: sticky; left: 0; z-index: 5; justify-content: flex-start; padding: 0 9px; background: #f8fbfc; }
.run-timeline-day.today { color: #b5323e; font-weight: 800; background: #fff0f1; }
.run-timeline-row { display: grid; grid-template-columns: 230px calc(var(--run-days) * 64px); min-height: 46px; border-bottom: 1px solid var(--line); }
.run-timeline-name { position: sticky; left: 0; z-index: 3; display: flex; align-items: center; gap: 7px; border: 0; border-right: 1px solid var(--line); padding: 0 9px; text-align: left; background: #fff; }
.run-timeline-track { --run-days: 1; position: relative; width: calc(var(--run-days) * 64px); background-image: repeating-linear-gradient(to right, transparent 0, transparent 63px, var(--line) 63px, var(--line) 64px); }
.run-today-marker { position: absolute; z-index: 1; top: 0; bottom: 0; width: 64px; background: rgba(232,93,104,.09); border-left: 2px solid rgba(232,93,104,.55); pointer-events: none; }
.run-timeline-bar { position: absolute; z-index: 2; top: 8px; height: 30px; overflow: hidden; border: 0; border-radius: 5px; padding: 0 8px; color: #fff; font-size: 11px; font-weight: 700; text-align: left; white-space: nowrap; }
.run-timeline-bar.completed { background: #49c98a; }
.run-timeline-bar.active { background: #2aaed8; }
.run-timeline-bar.overdue { background: #e85d68; }
.run-timeline-bar.waiting { background: #e6a044; }
