:root {
  --bg-color: #f5f5f5;
  --card-bg: #ffffff;
  --axis-color: #e0e0e0;
  --primary-color: #333;
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --z-axis: 0;
  --z-connector: 1;
  --z-marker: 5;
  --z-card: 10;
  --z-header: 100;
  --z-fab: 1500;
  --z-overlay: 2000;
  --z-modal: 3000;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-color);
  margin: 0;
  padding: 0;
  color: #333;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* --- Common Utils --- */
button {
  outline: none;
}

/* --- Timeline Core Styles --- */
#timeline-wrapper {
  flex: 1;
  overflow-y: auto;
  position: relative;
  padding: 40px 0 100px 0;
  scroll-behavior: smooth;
  background-color: var(--bg-color);
  width: 100%;
}

#timeline-container {
  position: relative;
  width: 95%;
  max-width: 1200px;
  margin: 0 auto;
  min-height: 500px;
}

.axis-line {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--axis-color);
  transform: translateX(-50%);
  z-index: var(--z-axis);
}

.year-marker {
  position: absolute;
  left: 0;
  right: 0;
  height: 0;
  pointer-events: none;
  z-index: var(--z-connector);
}

.year-badge {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 0.85rem;
  font-weight: bold;
  color: white;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: var(--z-marker);
  background-color: #757575;
}

.connector-line {
  position: absolute;
  top: 0;
  height: 1px;
  z-index: var(--z-connector);
  opacity: 0.6;
}
.connector-left {
  right: 50%;
  width: 5%;
  transform-origin: right center;
  background-color: #999;
}
.connector-right {
  left: 50%;
  width: 5%;
  transform-origin: left center;
  background-color: #999;
}

.event-card {
  position: absolute;
  width: 42%;
  padding: 12px 16px;
  background: var(--card-bg);
  border-radius: 10px;
  box-shadow: var(--shadow-sm);
  font-size: 0.9rem;
  border-left: 5px solid #ccc;
  transition: transform 0.2s, z-index 0.2s, box-shadow 0.2s;
  box-sizing: border-box;
}
.event-card:hover {
  transform: scale(1.02);
  z-index: var(--z-card);
  box-shadow: var(--shadow-md);
}
.event-left {
  left: 3%;
}
.event-right {
  right: 3%;
}

/* Flex container for single-line header */
.event-header {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 6px;
  margin-bottom: 6px;
  overflow: hidden;
}

.event-source-label {
  display: inline-block;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  color: #fff;
  font-weight: bold;
  white-space: nowrap;
  flex-shrink: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.event-year-sub {
  font-weight: normal;
  font-size: 0.75rem;
  color: #999;
  white-space: nowrap;
  flex-shrink: 0;
}

h3 {
  margin: 2px 0 6px 0;
  font-size: 1.05rem;
  color: #222;
  line-height: 1.3;
}

.event-lead {
  font-weight: bold;
  font-size: 0.85rem;
  color: #444;
  margin-bottom: 6px;
  line-height: 1.4;
}

.event-desc {
  font-size: 0.85rem;
  color: #555;
  line-height: 1.6;
  margin-top: 4px;
  white-space: pre-wrap;
}

.omission-marker {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 24px;
  background: var(--bg-color);
  color: #aaa;
  font-size: 18px;
  line-height: 24px;
  text-align: center;
  z-index: 2;
  font-weight: bold;
}

/* --- PC Controls (Hidden on Mobile) --- */
.pc-controls-container {
  display: none;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 28px;
  align-items: center;
}
.history-tag {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  color: #fff;
  font-weight: bold;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  white-space: nowrap;
  cursor: default;
}
.history-tag button {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  margin-left: 6px;
  cursor: pointer;
  padding: 0;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 0.9rem;
  line-height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.history-tag button:hover {
  background: rgba(255, 255, 255, 0.4);
}

/* --- Mobile UI (Hidden on PC) --- */
.fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  background-color: #007bff;
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  z-index: var(--z-fab);
  transition: transform 0.2s;
}
.fab:active {
  transform: scale(0.95);
}
.fab::before,
.fab::after {
  content: "";
  position: absolute;
  background-color: white;
  border-radius: 2px;
}
.fab::before {
  width: 24px;
  height: 4px;
}
.fab::after {
  width: 4px;
  height: 24px;
}

.menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: var(--z-overlay);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
  display: flex;
  align-items: flex-end;
}
.menu-overlay.active {
  opacity: 1;
  visibility: visible;
}

.menu-container {
  background-color: white;
  width: 100%;
  height: 90vh;
  border-radius: 16px 16px 0 0;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.25, 1, 0.5, 1);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
}
.menu-overlay.active .menu-container {
  transform: translateY(0);
}

.menu-header {
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  border-radius: 16px 16px 0 0;
}
.menu-title {
  font-size: 18px;
  font-weight: bold;
  margin: 0;
  color: #333;
}
.header-edit-btn {
  background: transparent;
  border: 1px solid #ccc;
  color: #555;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}
.header-edit-btn::before {
  content: "✎";
  font-size: 14px;
}
.header-edit-btn:active {
  background: #f5f5f5;
  border-color: #bbb;
}

.columns-wrapper {
  flex: 1;
  display: flex;
  overflow: hidden;
  padding: 10px;
  gap: 10px;
  background-color: #f8f9fa;
}
.column {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
}
.column-header {
  background-color: #e9ecef;
  padding: 8px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  color: #495057;
  border-bottom: 1px solid #ddd;
}

.badges-container {
  padding: 8px;
  min-height: 60px;
  border-bottom: 1px dashed #ccc;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
  background-color: #fff;
}
.badge {
  color: #fff;
  border-radius: 12px;
  padding: 5px 10px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
  font-weight: bold;
}
.badge-remove {
  margin-left: 8px;
  font-weight: bold;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  width: 16px;
  height: 16px;
  justify-content: center;
}
.badge-remove:active {
  background: rgba(0, 0, 0, 0.3);
}

.options-list {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  margin: 0;
  list-style: none;
}
.option-item {
  padding: 12px;
  border-bottom: 1px solid #f1f1f1;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.2s;
}
.option-item:active {
  background-color: #e2e6ea;
}
.option-item.disabled {
  background-color: #f8f9fa;
  color: #adb5bd;
  cursor: not-allowed;
}

.menu-footer {
  padding: 12px 16px;
  border-top: 1px solid #eee;
  background-color: white;
}
.footer-close-btn {
  width: 100%;
  padding: 14px;
  background-color: #f1f3f5;
  color: #333;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.2s;
}
.footer-close-btn:active {
  background-color: #e9ecef;
}

/* --- Editor Modal --- */
#editor-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: var(--z-modal);
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(2px);
}
.modal-content {
  background: white;
  padding: 25px;
  width: 90%;
  max-width: 600px;
  height: 80%;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}
textarea {
  flex: 1;
  font-family: monospace;
  margin-bottom: 15px;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  resize: none;
}
.modal-actions {
  text-align: right;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.modal-actions button {
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid #ddd;
  background: #fff;
  cursor: pointer;
}
.modal-actions button:last-child {
  background: #333;
  color: #fff;
  border-color: #333;
}

/* --- PC Responsive Styles --- */
@media (min-width: 768px) {
  body {
    overflow-y: visible;
    height: auto;
    display: block;
  }

  .fab,
  .menu-overlay {
    display: none !important;
  }

  .pc-controls-container {
    display: block;
    position: sticky;
    top: 20px;
    z-index: var(--z-header);
    width: 100%;
    height: 0;
    overflow: visible;
    pointer-events: none;
  }

  .controls {
    position: relative;
    margin: 0 auto;
    width: fit-content;
    min-width: 600px;
    max-width: 90%;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    background: #ffffff;
    padding: 8px 24px;
    border-radius: 50px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    border: 1px solid rgba(0, 0, 0, 0.05);
    gap: 0;
    pointer-events: auto;
  }

  .side-panel {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    justify-content: flex-end;
  }
  .side-panel.right-panel {
    flex-direction: row-reverse;
  }

  .vs-divider {
    display: block;
    width: 1px;
    height: 24px;
    background-color: #e0e0e0;
    margin: 0 16px;
  }

  .add-select-wrapper {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #f0f0f0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
    position: relative;
  }
  .add-select-wrapper:hover {
    background: #e0e0e0;
  }
  .add-select-wrapper select {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
  }
  .add-icon {
    display: block;
    font-size: 1.2rem;
    color: #666;
    line-height: 1;
    pointer-events: none;
  }

  .json-btn-pc {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 30px;
    right: 30px;
    z-index: 1500;
    padding: 12px 24px;
    cursor: pointer;
    background: #333;
    color: #fff;
    border: none;
    border-radius: 50px;
    font-size: 0.9rem;
    font-weight: bold;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s, background 0.2s;
    pointer-events: auto;
  }
  .json-btn-pc:hover {
    background: #555;
    transform: translateY(-2px);
  }

  #timeline-wrapper {
    padding-top: 100px;
    height: auto;
    overflow: visible;
  }
}

@media (max-width: 767px) {
  .year-badge {
    width: 10px;
    height: 10px;
    padding: 0;
    border-radius: 50%;
    color: transparent !important;
    overflow: hidden;
    min-width: 0;
    box-shadow: none;
  }
  .event-year-sub {
    background-color: #f0f0f0;
    color: #555;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.8rem;
  }
  .event-card {
    width: 44%;
    padding: 10px;
  }
  .event-left {
    left: 2%;
  }
  .event-right {
    right: 2%;
  }
}