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

:root {
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-bg: #f1f5f9;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #1e293b;
  --color-text-muted: #64748b;
  --color-header: #0f172a;
  --radius: 10px;
  --shadow: 0 1px 6px rgba(0,0,0,0.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans JP", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--color-text);
  background: var(--color-bg);
}

/* ========== HEADER ========== */
.site-header {
  background: var(--color-header);
  color: #fff;
  padding: 1rem;
}

.back-link {
  display: inline-block;
  color: #94a3b8;
  text-decoration: none;
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
  min-height: 44px;
  line-height: 44px;
  padding: 0 0.25rem;
}

.back-link:hover {
  color: #fff;
}

.header-content h1 {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1.35;
}

.subtitle {
  font-size: 0.85rem;
  color: #94a3b8;
  margin-top: 0.25rem;
}

/* ========== MAIN ========== */
main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 1rem;
}

/* ========== AD UNITS ========== */
.ad-unit {
  margin: 1.25rem 0;
}

/* ========== TOOL SECTION (フォーム) ========== */
.tool-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.tool-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.tool-header h2 {
  font-size: 1rem;
  color: var(--color-header);
}

.btn-sample {
  background: #f8fafc;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 0 0.75rem;
  height: 44px;
  font-size: 0.85rem;
  color: var(--color-text-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

.btn-sample:hover {
  background: #e2e8f0;
  color: var(--color-text);
}

/* ========== FORM ========== */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-text);
}

.required {
  color: #ef4444;
}

input[type="text"],
input[type="date"],
textarea {
  width: 100%;
  padding: 0.55rem 0.75rem;
  font-size: 0.95rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: #f8fafc;
  color: var(--color-text);
  min-height: 44px;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input[type="text"]:focus,
input[type="date"]:focus,
textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
  background: #fff;
}

textarea {
  resize: vertical;
  min-height: 68px;
}

/* カラー行 */
.color-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

input[type="color"] {
  width: 44px;
  height: 44px;
  padding: 2px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: #f8fafc;
  cursor: pointer;
  flex-shrink: 0;
}

.preset-colors {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.color-preset {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.1s;
  padding: 0;
  min-height: unset;
}

.color-preset:hover,
.color-preset.selected {
  transform: scale(1.2);
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--color-primary);
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.btn-primary {
  flex: 1;
  min-height: 56px;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-weight: 700;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary:hover {
  background: var(--color-primary-hover);
}

.btn-secondary {
  min-height: 56px;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  background: #f1f5f9;
  color: var(--color-text-muted);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: #e2e8f0;
}

.form-error {
  margin-top: 0.5rem;
  font-size: 0.85rem;
  color: #dc2626;
  min-height: 1.2em;
}

/* ========== TIMELINE SECTION ========== */
.timeline-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.timeline-controls {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.timeline-controls h2 {
  font-size: 1rem;
  color: var(--color-header);
  padding-top: 0.5rem;
}

.control-buttons {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
  align-items: center;
}

.layout-toggle {
  display: flex;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  overflow: hidden;
}

.btn-layout {
  padding: 0 0.75rem;
  height: 44px;
  font-size: 0.85rem;
  background: #f8fafc;
  color: var(--color-text-muted);
  border: none;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.btn-layout.active,
.btn-layout[aria-pressed="true"] {
  background: var(--color-primary);
  color: #fff;
}

.btn-layout:first-child {
  border-right: 1px solid var(--color-border);
}

.btn-export-png,
.btn-export-json,
.btn-import-json,
.btn-clear {
  padding: 0 0.75rem;
  height: 44px;
  font-size: 0.82rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  border: none;
  transition: background 0.15s;
  white-space: nowrap;
}

.btn-export-png {
  background: #10b981;
  color: #fff;
}

.btn-export-png:hover {
  background: #059669;
}

.btn-export-json {
  background: #f59e0b;
  color: #fff;
}

.btn-export-json:hover {
  background: #d97706;
}

.btn-import-json {
  display: inline-flex;
  align-items: center;
  background: #6366f1;
  color: #fff;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
}

.btn-import-json:hover {
  background: #4f46e5;
}

.btn-clear {
  background: #fef2f2;
  color: #dc2626;
  border: 1px solid #fecaca;
}

.btn-clear:hover {
  background: #fee2e2;
}

/* ========== TIMELINE EMPTY ========== */
.timeline-empty {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  border: 2px dashed var(--color-border);
  border-radius: 8px;
}

/* ========== TIMELINE CANVAS ========== */
.timeline-canvas {
  overflow-x: auto;
  overflow-y: auto;
  padding-bottom: 0.5rem;
}

/* ===== HORIZONTAL LAYOUT ===== */
.timeline-inner.horizontal {
  position: relative;
  min-width: max-content;
  padding: 2.5rem 1rem 1rem;
}

.timeline-inner.horizontal .tl-axis {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 4px;
  background: linear-gradient(to right, var(--color-border), #94a3b8, var(--color-border));
  border-radius: 2px;
  transform: translateY(-50%);
}

.timeline-inner.horizontal .tl-events {
  display: flex;
  gap: 0;
  align-items: flex-start;
  position: relative;
}

/* 水平: イベントラッパー */
.timeline-inner.horizontal .tl-event-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 140px;
  max-width: 180px;
  position: relative;
}

/* 水平: 上下交互配置 */
.timeline-inner.horizontal .tl-event-wrap.above {
  flex-direction: column-reverse;
  margin-bottom: 0;
}

.timeline-inner.horizontal .tl-event-wrap.below {
  flex-direction: column;
}

/* 水平: カード */
.timeline-inner.horizontal .tl-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  font-size: 0.8rem;
  width: 130px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: box-shadow 0.15s, transform 0.15s;
  position: relative;
}

.timeline-inner.horizontal .tl-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  transform: translateY(-2px);
}

/* 水平: ステム（カードとドットを繋ぐ線） */
.timeline-inner.horizontal .tl-stem {
  width: 2px;
  background: var(--color-border);
  flex-shrink: 0;
}

.timeline-inner.horizontal .tl-event-wrap.above .tl-stem {
  height: 40px;
}

.timeline-inner.horizontal .tl-event-wrap.below .tl-stem {
  height: 40px;
}

/* 水平: ドット */
.timeline-inner.horizontal .tl-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px var(--color-border);
  flex-shrink: 0;
  z-index: 1;
}

/* ===== VERTICAL LAYOUT ===== */
.timeline-inner.vertical {
  position: relative;
  padding: 0.5rem 0;
}

.timeline-inner.vertical .tl-axis {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 24px;
  width: 4px;
  background: linear-gradient(to bottom, var(--color-border), #94a3b8, var(--color-border));
  border-radius: 2px;
}

.timeline-inner.vertical .tl-events {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding-left: 60px;
  position: relative;
}

/* 垂直: イベントラッパー */
.timeline-inner.vertical .tl-event-wrap {
  display: flex;
  align-items: flex-start;
  position: relative;
  margin-bottom: 0.75rem;
}

/* 垂直: ドット */
.timeline-inner.vertical .tl-dot {
  position: absolute;
  left: -42px;
  top: 14px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px var(--color-border);
  flex-shrink: 0;
  z-index: 1;
}

/* 垂直: ステム（ドットとカードを繋ぐ） */
.timeline-inner.vertical .tl-stem {
  position: absolute;
  left: -34px;
  top: 20px;
  height: 2px;
  width: 30px;
  background: var(--color-border);
}

/* 垂直: カード */
.timeline-inner.vertical .tl-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  font-size: 0.88rem;
  width: 100%;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: box-shadow 0.15s;
}

.timeline-inner.vertical .tl-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}

/* ===== CARD CONTENTS (共通) ===== */
.tl-card-date {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--color-text-muted);
  margin-bottom: 0.2rem;
  letter-spacing: 0.03em;
}

.tl-card-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.3;
  margin-bottom: 0.2rem;
  word-break: break-all;
}

.tl-card-category {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.1rem 0.45rem;
  border-radius: 20px;
  color: #fff;
  margin-top: 0.2rem;
}

.tl-card-desc {
  font-size: 0.78rem;
  color: var(--color-text-muted);
  margin-top: 0.35rem;
  line-height: 1.4;
  word-break: break-all;
}

/* カード左ボーダー（カラー） */
.tl-card {
  border-left-width: 4px !important;
  border-left-style: solid !important;
}

/* ========== EVENT LIST ========== */
.event-list-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.event-list-section h2 {
  font-size: 1rem;
  color: var(--color-header);
  margin-bottom: 0.75rem;
}

.event-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.event-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem;
  background: #f8fafc;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  border-left-width: 4px;
  border-left-style: solid;
}

.event-item-info {
  flex: 1;
  min-width: 0;
}

.event-item-title {
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.event-item-meta {
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.event-item-actions {
  display: flex;
  gap: 0.3rem;
  flex-shrink: 0;
}

.btn-edit,
.btn-delete {
  height: 44px;
  padding: 0 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-edit {
  background: #eff6ff;
  color: var(--color-primary);
}

.btn-edit:hover {
  background: #dbeafe;
}

.btn-delete {
  background: #fef2f2;
  color: #dc2626;
}

.btn-delete:hover {
  background: #fee2e2;
}

/* ========== HOWTO SECTION ========== */
.howto-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.howto-section h2 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--color-header);
  margin-bottom: 0.75rem;
  margin-top: 1.25rem;
}

.howto-section h2:first-child {
  margin-top: 0;
}

.howto-section ol {
  padding-left: 1.4rem;
  color: #475569;
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.howto-section p {
  color: #475569;
  font-size: 0.9rem;
  line-height: 1.7;
}

/* ========== FOOTER ========== */
.site-footer {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.85rem;
  color: #94a3b8;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.site-footer a {
  color: #64748b;
  text-decoration: underline;
}

/* ========== TOAST ========== */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: #1e293b;
  color: #fff;
  padding: 0.65rem 1.25rem;
  border-radius: 8px;
  font-size: 0.88rem;
  z-index: 9999;
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ========== RESPONSIVE ========== */
@media (min-width: 640px) {
  .site-header {
    padding: 1.25rem 2rem;
  }

  .header-content h1 {
    font-size: 1.4rem;
  }

  main {
    padding: 1.5rem 2rem;
  }

  .tool-section,
  .timeline-section,
  .event-list-section,
  .howto-section {
    padding: 1.5rem;
  }

  .color-preset {
    width: 32px;
    height: 32px;
  }
}

@media (max-width: 480px) {
  .form-grid {
    grid-template-columns: 1fr;
  }

  .form-group.full-width {
    grid-column: unset;
  }

  .control-buttons {
    gap: 0.3rem;
  }

  .btn-export-png,
  .btn-export-json,
  .btn-import-json,
  .btn-clear,
  .btn-layout {
    font-size: 0.75rem;
    padding: 0 0.55rem;
    height: 40px;
  }
}

/* ========== PNG出力用オーバーライド ========== */
.exporting .timeline-canvas {
  overflow: visible;
}
