/* Reset and base styles */
.tree * {
  margin: 0;
  padding: 0;
}

.tree {
  width: 100%;
  overflow-x: auto;
  padding: 20px;
  box-sizing: border-box;
}

.tree ul {
  padding-top: 20px;
  position: relative;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

.tree li {
  float: left;
  text-align: center;
  list-style-type: none;
  position: relative;
  padding: 20px 5px 0 5px;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
}

/* We will use ::before and ::after to draw the connectors */
.tree li::before,
.tree li::after {
  content: "";
  position: absolute;
  top: 0;
  right: 50%;
  border-top: 1px solid #ccc;
  width: 50%;
  height: 20px;
}

.tree li::after {
  right: auto;
  left: 50%;
  border-left: 1px solid #ccc;
}

/* We need to remove left-right connectors from elements without any siblings */
.tree li:only-child::after,
.tree li:only-child::before {
  display: none;
}

/* Remove space from the top of single children */
.tree li:only-child {
  padding-top: 0;
}

/* Time to add downward connectors from parents */
.tree ul ul::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  border-left: 1px solid #ccc;
  width: 0;
  height: 20px;
}

.tree li a {
  border: 1px solid #ccc;
  padding: 5px 10px;
  text-decoration: none;
  color: #666;
  font-family: arial, verdana, tahoma;
  font-size: 11px;
  display: inline-block;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  background: #fff;
  white-space: nowrap;
}

/* Time for some hover effects */
.tree li a:hover,
.tree li a:hover + ul li a {
  background: var(--color-liner, #c8e4f8);
  color: #000;
  border: 1px solid var(--color-accent, #94a0b4);
}

/* Connector styles on hover */
.tree li a:hover + ul li::after,
.tree li a:hover + ul li::before,
.tree li a:hover + ul::before,
.tree li a:hover + ul ul::before {
  border-color: var(--color-accent, #94a0b4);
}

/* Remove left connector from first child and right connector from last child */
.tree li:first-child::before,
.tree li:last-child::after {
  border: 0 none;
}

/* Adding back the vertical connector to the last nodes */
.tree li:last-child::before {
  border-right: 1px solid #ccc;
  border-radius: 0 5px 0 0;
  -webkit-border-radius: 0 5px 0 0;
  -moz-border-radius: 0 5px 0 0;
}

.tree li:first-child::after {
  border-radius: 5px 0 0 0;
  -webkit-border-radius: 5px 0 0 0;
  -moz-border-radius: 5px 0 0 0;
}

/* Clearfix */
.tree ul::after {
  content: "";
  display: table;
  clear: both;
}

/* Large screens optimization */
@media (min-width: 1200px) {
  .tree {
    padding: 30px;
  }

  .tree li a {
    padding: 8px 15px;
    font-size: 12px;
  }
}

/* Tablet styles */
@media (max-width: 1024px) {
  .tree {
    padding: 15px;
  }

  .tree li {
    padding: 15px 4px 0 4px;
  }

  .tree li a {
    font-size: 10px;
    padding: 6px 8px;
  }
}

/* Small tablet / large mobile */
@media (max-width: 768px) {
  .tree {
    padding: 10px;
    font-size: 14px;
  }

  .tree li {
    padding: 12px 3px 0 3px;
  }

  .tree li a {
    font-size: 9px;
    padding: 5px 6px;
    min-width: 50px;
  }

  .tree ul {
    padding-top: 15px;
  }

  .tree li::before,
  .tree li::after {
    height: 15px;
  }

  .tree ul ul::before {
    height: 15px;
  }
}

/* Mobile styles */
@media (max-width: 600px) {
  .tree {
    padding: 5px;
    overflow-x: scroll;
    -webkit-overflow-scrolling: touch;
  }

  .tree li {
    padding: 10px 2px 0 2px;
    min-width: 60px;
  }

  .tree li a {
    font-size: 8px;
    padding: 4px 6px;
    min-width: 45px;
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
  }

  .tree ul {
    padding-top: 12px;
  }

  .tree li::before,
  .tree li::after {
    height: 12px;
  }

  .tree ul ul::before {
    height: 12px;
  }

  /* Mobile touch effects - using :active and :focus */
  .tree li a:active,
  .tree li a:focus {
    background: var(--color-liner, #c8e4f8) !important;
    color: #000 !important;
    border: 1px solid var(--color-accent, #94a0b4) !important;
    transform: scale(1.1) !important;
    z-index: 100 !important;
    position: relative !important;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3) !important;
    outline: none !important;
  }

  /* Touch device specific styles */
  .tree li a {
    -webkit-tap-highlight-color: transparent;
    tap-highlight-color: transparent;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
  }

  /* Force active state on touch */
  .tree li a:active {
    background: var(--color-liner, #c8e4f8) !important;
    color: #000 !important;
    border-color: var(--color-accent, #94a0b4) !important;
    transform: scale(1.05) !important;
  }

  /* Alternative hover effect for touch devices */
  @media (hover: none) and (pointer: coarse) {
    .tree li a {
      transition: all 0.1s ease !important;
    }

    .tree li a:active {
      background: var(--color-liner, #c8e4f8) !important;
      color: #000 !important;
      border: 2px solid var(--color-accent, #94a0b4) !important;
      transform: scale(1.1) !important;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2) !important;
    }

    /* Make tap area larger for better touch experience */
    .tree li a {
      min-height: 32px;
      display: flex !important;
      align-items: center !important;
      justify-content: center !important;
    }
  }
}

/* Very small screens */
@media (max-width: 480px) {
  .tree {
    padding: 3px;
  }

  .tree li {
    padding: 8px 1px 0 1px;
    min-width: 50px;
  }

  .tree li a {
    font-size: 7px;
    padding: 3px 4px;
    min-width: 40px;
    max-width: 70px;
    word-break: break-word;
    white-space: normal;
  }

  .tree ul {
    padding-top: 10px;
  }

  .tree li::before,
  .tree li::after {
    height: 10px;
  }

  .tree ul ul::before {
    height: 10px;
  }
}

/* Additional mobile optimizations */
@media (max-width: 600px) {
  /* Ensure tree doesn't break on very wide structures */
  .tree {
    min-width: 320px;
  }

  /* Better spacing for deep structures */
  .tree ul ul ul {
    margin-top: 5px;
  }

  /* Smooth transitions for mobile interactions */
  .tree li a {
    transition: all 0.2s ease;
  }

  /* Better visual hierarchy on mobile */
  .tree > ul > li > a {
    font-weight: bold;
    background: #f8f9fa;
  }

  .tree > ul > li > ul > li > a {
    background: #fff;
  }

  /* Add pressed/touched state class */
  .tree li a.touched {
    background: var(--color-liner, #c8e4f8) !important;
    color: #000 !important;
    border: 2px solid var(--color-accent, #94a0b4) !important;
    transform: scale(1.1) !important;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important;
    z-index: 100 !important;
    position: relative !important;
  }
}
