/* Remove generic button/input styling to avoid breaking global theme */
.tree button,
.tree input,
.tree .btn {
  line-height: 2em;
  height: 2em;
  vertical-align: middle;
  /* Revert to middle */
}

/* Helper for hidden elements */
.d-none {
  display: none;
}

/* Scope .btn to .tree to avoid conflict with style.css .btn */
.tree .btn {
  display: inline-block;
  width: 2.2em;
  /* Fixed width for icon only */
  min-width: 0;
  padding: 0;
  margin-left: 24px;
  /* Spacing 10px */
  border: 1px solid #ccc;
  /* Softer border */
  border-radius: 4px;
  /* Rounded corners */
  color: #333;
  background: #f8f9fa;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  /* Subtle shadow */
  text-shadow: none;
  font-weight: 500;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;

  /* Font size 0 to hide original text (+, -) so we can replace with icon+text */
  font-size: 10px;
  height: 2em;
  /* Fixed height matching width approximate */
  vertical-align: middle;
  /* Align with input */
  box-sizing: border-box;
  /* Ensure border is included in height */
}

/* Base style for pseudo-element text */
.tree .btn::before {
  font-size: 16px;
  /* Restore visible font size */
  display: flex;
  align-items: center;
  justify-content: center;
  /* Center the icon */
  width: 100%;
  height: 100%;
}

/* --- Button Variants (Icon + Text) --- */

/* Add (新增) */
.tree .btn.add {
  background-color: #e8f5e9;
  border-color: #a5d6a7;
  color: #2e7d32;
}

.tree .btn.add:hover {
  background-color: #c8e6c9;
}

.tree .btn.add::before {
  content: "✚";
}

/* Delete (刪除) */
.tree .btn.delete {
  background-color: #ffebee;
  border-color: #ef9a9a;
  color: #c62828;
}

.tree .btn.delete:hover {
  background-color: #ffcdd2;
}

.tree .btn.delete::before {
  content: "🗑️";
}

/* Copy (複製) - Assuming class is 'copy' */
.tree .btn.copy {
  background-color: #e3f2fd;
  border-color: #90caf9;
  color: #1565c0;
}

.tree .btn.copy:hover {
  background-color: #bbdefb;
}

.tree .btn.copy::before {
  content: "📋";
}

.tree .btn.copied {
  background-color: #4CAF50 !important;
  color: white !important;
  border-color: #2E7D32 !important;
  transition: background-color 0.2s;
}

.tree .btn.copied::before {
  content: "✅" !important;
}

/* Password (密碼) - Assuming class is 'pwd' or 'password' */
.tree .btn.pwd,
.tree .btn.password {
  background-color: #fff3e0;
  border-color: #ffcc80;
  color: #ef6c00;
}

.tree .btn.pwd:hover,
.tree .btn.password:hover {
  background-color: #ffe0b2;
}

.tree .btn.pwd::before,
.tree .btn.password::before {
  content: "🔑";
}

/* Style inputs within the tree for dark mode */
.tree input {
  background-color: rgba(255, 255, 255, 0.1);
  color: #e0e0e0;
  border: 1px solid #555;
  border-radius: 4px;
  padding: 0 0.5em;
  margin-right: 0.5em;
  /* Spacing between input and button */
  max-width: 150px;
  /* Prevent too wide on mobile */
  vertical-align: middle;
  /* Align with button */
  height: 2em;
  box-sizing: border-box;
  /* Match sizing */
}

/* Ensure hover state is also reset or styled appropriately */
.tree .btn:hover {
  transform: none;
  box-shadow: none;
  background: #ccd;
}

.node {
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 1em 0 0 1em;
  min-height: 1em;
  background-color: rgba(202, 201, 201, 0.1);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 8px;
}

#tree1 {
  margin-left: 20px;
  padding-left: 0;
}

.tree {
  --spacing: 1.5rem;
  --radius: 10px;
}

.tree li {
  display: block;
  position: relative;
  padding-left: calc(2 * var(--spacing) - var(--radius) - 2px);
  padding-bottom: calc(var(--spacing)/4);
}

.tree ul {
  margin-left: calc(var(--radius) - var(--spacing));
  padding-left: 0;
}

.tree ul li {
  border-left: 2px solid #ddd;
}

.tree ul li:last-child {
  border-color: transparent;
}

.tree ul li::before {
  content: '';
  display: block;
  position: absolute;
  top: calc(var(--spacing) / -2);
  left: -2px;
  width: calc(var(--spacing) + 2px);
  height: calc(var(--spacing) + 1px);
  border: solid #ddd;
  border-width: 0 0 2px 2px;
}

/* Fix overlapping borders: Only the last child needs the ::before left border to form the corner. 
   Others use the main li border for the vertical line. */
.tree ul li:not(:last-child)::before {
  border-left: none;
}

.tree summary {
  display: block;
  cursor: pointer;
  padding-bottom: calc(var(--spacing)/4);
  /* Allow summary to have layout */
  display: flex;
  align-items: center;
}

.tree summary::marker,
.tree summary::-webkit-details-marker {
  display: none;
}

.tree summary:focus {
  outline: none;
}

.tree summary:focus-visible {
  outline: 1px dotted #000;
}

.tree li::after,
.tree summary::before {
  content: '';
  display: block;
  position: absolute;
  top: calc(var(--spacing) / 2 - var(--radius));
  left: calc(var(--spacing) - var(--radius) - 1px);
  width: calc(2 * var(--radius));
  height: calc(2 * var(--radius));
  border-radius: 50%;
  background: #ddd;
}

.tree summary::before {
  content: "+";
  z-index: 1;
  background: #CCF;
  text-align: center;
  font-weight: bold;
  color: white;
  /* Flex centering for +/- icon */
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  line-height: 1;
}

.tree details[open]>summary::before {
  content: "-";
  /* background-position removed since we use text content centered */
  background: #CCF;
  /* Keep consistent background */
  text-align: center;
  font-weight: bold;
  color: white;
}

/* =========================================
   Controls & Layout Styles (Added for RWD)
   ========================================= */

/* Main container for controls */
.controls-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px;
  margin-bottom: 20px;
  background-color: rgba(255, 255, 255, 0.05);
  /* Slight separation */
  border-radius: 8px;
}

.control-group {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* File Upload Button Styling */
.file-upload-btn {
  position: relative;
  overflow: hidden;
  display: inline-block;
  cursor: pointer;
  margin-bottom: 0 !important;
  /* Fix potential global styles */
  line-height: normal;
  /* Reset */
  padding: 5px 10px;
  /* Add some padding */
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #f0f0f0;
  color: #333;
}

.file-upload-btn input[type=file] {
  position: absolute;
  top: 0;
  right: 0;
  min-width: 100%;
  min-height: 100%;
  font-size: 100px;
  text-align: right;
  filter: alpha(opacity=0);
  opacity: 0;
  outline: none;
  background: white;
  cursor: inherit;
  display: block;
}

/* RWD for Mobile */
@media (max-width: 600px) {
  .controls-container {
    flex-direction: column;
    align-items: stretch;
  }

  .control-group {
    justify-content: space-between;
  }

  .control-group .btn {
    flex: 1;
    text-align: center;
  }

  /* Compact tree view */
  .tree {
    --spacing: 1rem;
    /* tighter spacing */
    --radius: 8px;
  }

  /* Increase touch targets */
  .tree summary {
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .tree input {
    max-width: 120px;
    /* smaller inputs on mobile */
    font-size: 16px;
    /* prevent zoom on iOS */
  }

  /* Adjust root indentation */
  #tree1 {
    margin-left: 10px;
    padding-left: 0;
    padding-right: 10px;
    /* ensure content doesn't hit right edge */
  }
}