* {
  margin: 0;
  padding: 0;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
}

button {
  padding: revert;
}

canvas {
  image-rendering: pixelated;
}

.zoomed-in {
  zoom: 2;
}

.zoomed-out {
  zoom: 0.5;
}

.zoomed-in.zoomed-out {
  zoom: 1;
}

body {
  margin: 10px 15px;
  font-size: 10pt;
  font-family: monospace;
}

.sticky {
  position: sticky;
  top: 0;
  left: 0;
  margin-top: -10px;
  padding-top: 10px;
  width: 100%;
  display: flex;
  background-color: white;
  opacity: 1;
  z-index: 4;
}

#header-left {
  flex-direction: column;
  order: 0;
}

#preview-animations {
  padding-top: 15px;
  padding-left: 30px;
  order: 1;
}

h1 {
  margin: 10px 0px;
}

h2 {
  margin: 10px 0px;

}

h3 {
  margin: 4px 0px 0px 0px;
}

p {
  margin: 10px 0px;
}

.subtitle {
  padding-left: 50px;
}

#save {
  width: 60%;
  min-width: 832px;
  margin: 40px 0;
  float: right;
  padding-left: 15px;
}

#credits {
  width: 100%;
  clear: both;
  max-width: calc(100vw - 80px);
}

input[type=button], input[type=reset] {
  padding: 2px 1em;
}

#previewAnimationsBox > * {
  vertical-align: top;
}

#controls {
  margin-top: 10px;
  padding-top: 10px;
  order: 2;
}

#controls > * {
  background-color: white;
  opacity: 1;
  z-index: 2;
}

#chooser {
  overflow-y: auto;
  min-width: 200px;
  max-height: calc(100vh - 320px);
  margin-left: 15px;
  order: 4;
}

#preview {
  min-width: 832px;
  max-height: calc(100vh - 160px);
  overflow-y: auto;
  margin-top: 0;
  position: relative;
  order: 3;
  left: 15px;
  grid-row: span 2;
}

#customizeChar {
  display: grid;
  grid-template-columns: 40% auto;
}

#credits, #advanced {
  margin-top: 15px;
  padding-top: 15px;
  margin-left: 15px;
}

summary {
  display: none;
}

.ZPOS {
  white-space: nowrap;
}

/* smaller screens width < 1280 px */
@media screen and (max-width: 1280px) {
  body {
    max-width: calc(100vw - 30px);
  }

  #header-left {
    width: calc(100vw - 140px);
    overflow-y: auto;
    order: 0;
  }

  #customizeChar {
    display: flex;
    flex-direction: column;
    max-width: calc(100vw - 30px);
  }

  #preview {
    order: 0;
    padding-top: 50px;
    margin-bottom: -60px;
    max-height: none;
    min-width: unset;
  }

  #customizeChar > section {
    width: calc(100vw - 80px);
  }

  #chooser {
    order: 0;
    height: auto;
    max-height: none;
    margin-top: -70px;
  }

  #controls {
    z-index: 3;
    order: 0;
    margin-top: -10px;
    padding-top: 20px;
    padding-left: 20px;
  }

  #controls>details[open] {
    margin-left: -10px;
    margin-bottom: -40px;
    padding-left: 10px;
    padding-bottom: 60px;
  }

  /* override #customizeChar > section */
  #customizeChar > #controls {
    width: calc(100vw - 80px);
  }

  #preview-animations {
    order: 0;
    margin-left: 15px;
    padding-left: 0;
    padding-top: 40px;
    padding-bottom: 40px;
    width: calc(100vw - 80px);
  }

  summary {
    display: list-item;
  }

  details {
    position: relative;
    top: -32px;
  }

  #preview:has(>details:not(open)) {
    padding-bottom: 120px;
  }
  
  #preview:has(>details:not(open))+#chooser {
    padding-top: 50px;
  }
}

/* high dpi (retina) screens */
@media screen and (max-width: 1280px) and (min-resolution:1.5x) {
  #preview,
  #credits, 
  #advanced,
  #chooser
  {
    font-size: 1.25rem;
  }

  #creditsText,
  #chooser h3,
  #chooser span,
  #chooser label {
    font-size: 1.5rem;
  }
}

/* portrait mode 800 - 1280 px width, > 1200 px height */
@media screen and (max-width: 1280px) and (min-height: 1200px) {
  .sticky {
    position: fixed;
    left: 15px;
  }

  #controls {
    top: 170px;
    padding-left: 0;
    margin-top: -50px;
    padding-top: 50px;
  }

  #preview-animations {
    top: 300px;
    padding-left: 0;
  }

  #preview {
    margin-top: 470px;
  }
}

/* tablet view 800 - 1280 px, <= 1200 px height */
@media screen and (max-width: 1280px) and (max-height: 1200px) {
  .sticky {
    position: static;
  }

  #controls {
    z-index: 2;
    padding-left: 0;
    margin-top: 20px;
  }

  #preview-animations.controls-open {
    top: 300px;
  }

  #preview {
    min-width: unset;
  }
}

#chooser p.instr {
  padding: 10px 0;
  margin: 10px 0;
}

#preview p.instr {
  padding: 10px;
  margin: 12px 6px;
}

ul {
  list-style-type: none;
}

#chooser>details>ul {
  border-left: solid 1px silver;
  padding-left: 0.5em;
  margin-left: 0.5em;
}

#chooser ul>li>span {
  cursor: pointer;
}

#chooser ul>li>.condensed:after {
  padding-left: 10px;
  content: '\25B6';
}

#chooser ul>li>.expanded:after, #chooser .condensed:hover:after {
  padding-left: 10px;
  content:  '\25BC';
}

.condensed:hover, .expanded:hover {
  text-decoration: underline;
}

.search-result {
  background-color: yellow;
}

#chooser>details>ul ul {
  display: none;
  margin-left: 0.5em;
  border-left:  solid 1px silver;
  padding-left: 0.5em;
}

#chooser ul.hasPreview {
  text-indent: 0;
}

#chooser li.hasPreview {
  display: inline-block;
  padding-left: 5px;
  padding-right: 5px;
  padding-bottom: 3px;
  margin-left: 0;
  margin-right: 0;
  text-indent: 0;
  border: solid 1px transparent;
  border-radius: 3px;
}

#chooser li.hasPreview:hover {
  border-color: silver;
}

li.hasPreview canvas {
  display: block;
}

#chooser.compact canvas {
  display:  inline-block;
  width: 32px;
  vertical-align:  middle;
}

#customizeChar .buttons {
  display: block;
  margin: 50px 0;
}

.control-label {
  font-weight: bold;
}

label[for="displayMode-compact"] {
  display: inline-block;
}

#creditsText {
  width: calc(100vw - 80px);
}

a {
  border: 0;
}

/* Mobile-First Roblox-Style Avatar Editor */
@media (max-width: 1024px), .force-mobile {
  /* Hide desktop interface on mobile */
  #header-left,
  #controls,
  #preview-animations,
  #chooser,
  #preview {
    display: none;
  }

  /* Show mobile interface */
  #mobile-topbar {
    display: flex;
  }

  #avatar-editor {
    display: block;
  }

  #character-preview-section {
    display: block;
  }

  /* Mobile Top Navigation Bar */
  #mobile-topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    background: #fff;
    border-bottom: 1px solid #e1e5e9;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    z-index: 1000;
  }

  #mobile-topbar h1 {
    font-size: 20px;
    font-weight: 700;
    color: #2c3e50;
    margin: 0;
  }

  .topbar-actions {
    display: flex;
    gap: 8px;
    align-items: center;
  }

  .topbar-actions button,
  .topbar-actions a {
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    background: #f8f9fa;
    color: #495057;
    text-decoration: none;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .topbar-actions button:hover,
  .topbar-actions a:hover {
    background: #e9ecef;
    border-color: #adb5bd;
  }

  /* Export Menu */
  #mobile-export-menu {
    position: fixed;
    top: 60px;
    right: 16px;
    width: 280px;
    background: #fff;
    border: 1px solid #e1e5e9;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 1100;
    padding: 8px 0;
  }

  #mobile-export-menu button {
    display: block;
    width: 100%;
    padding: 12px 16px;
    border: none;
    background: none;
    text-align: left;
    font-size: 14px;
    color: #495057;
    cursor: pointer;
    transition: background 0.2s ease;
  }

  #mobile-export-menu button:hover {
    background: #f8f9fa;
  }

  #mobile-export-menu hr {
    border: none;
    border-top: 1px solid #e1e5e9;
    margin: 4px 0;
  }

  /* Avatar Editor - Top 2/3 of screen */
  .avatar-editor {
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    height: calc(67vh - 20px);
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    z-index: 100;
  }

  /* Category Tabs */
  .category-tabs {
    background: #fff;
    border-bottom: 1px solid #e1e5e9;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    flex-shrink: 0;
  }

  .category-tabs-container {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 16px;
  }

  .category-tabs::-webkit-scrollbar {
    height: 0;
  }

  .category-tab {
    flex: 0 0 auto;
    padding: 12px 16px;
    border: none;
    background: none;
    color: #6c757d;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.2s ease;
    white-space: nowrap;
  }

  .category-tab:hover {
    color: #007bff;
    background: #f8f9fa;
  }

  .category-tab.active {
    color: #007bff;
    border-bottom-color: #007bff;
    background: #f8f9fa;
  }

  /* Subcategory Tabs */
  .subcategory-tabs {
    background: #f8f9fa;
    border-bottom: 1px solid #e1e5e9;
    flex-shrink: 0;
  }

  .subcategory-tabs-container {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 16px;
  }

  .subcategory-tab {
    flex: 0 0 auto;
    padding: 8px 12px;
    border: none;
    background: #fff;
    color: #495057;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    border-radius: 16px;
    margin: 8px 4px;
    transition: all 0.2s ease;
    white-space: nowrap;
  }

  .subcategory-tab:hover {
    background: #e9ecef;
  }

  .subcategory-tab.active {
    background: #007bff;
    color: #fff;
  }

  /* Items Section */
  .items-section {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
  }

  .items-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 12px;
    max-width: 100%;
  }

  .item-thumbnail {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 8px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    position: relative;
  }

  .item-thumbnail:hover {
    border-color: #007bff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,123,255,0.15);
  }

  .item-thumbnail.selected {
    border-color: #007bff;
    background: #e3f2fd;
  }

  .item-thumbnail.selected::after {
    content: '✓';
    position: absolute;
    top: 4px;
    right: 4px;
    width: 16px;
    height: 16px;
    background: #007bff;
    color: white;
    border-radius: 50%;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .item-image {
    width: 48px;
    height: 48px;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    background: #f8f9fa;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 6px;
    overflow: hidden;
    position: relative;
  }

  .item-image img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
  }

  .item-image .placeholder {
    font-size: 20px;
    color: #adb5bd;
  }

  .item-name {
    font-size: 11px;
    font-weight: 500;
    color: #495057;
    line-height: 1.2;
    word-break: break-word;
    max-width: 100%;
  }

  .item-thumbnail.selected .item-name {
    color: #007bff;
    font-weight: 600;
  }

  /* Character Preview Section - Bottom Third */
  .character-preview-section {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(33vh + 20px);
    background: #fff;
    border-top: 1px solid #e1e5e9;
    box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
    z-index: 1000;
    display: flex;
    flex-direction: column;
  }

  .preview-container {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 8px 16px;
    background: #f8f9fa;
    flex: 1;
    min-height: 120px;
    overflow: visible;
  }

  #previewAnimationsBox {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    width: 100%;
    max-width: 300px;
  }

  #previewAnimations {
    display: block !important;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    border: 2px solid #e1e5e9;
    border-radius: 8px;
    background: #fff;
    width: 100%;
    height: auto;
    max-height: 200px;
    min-height: 100px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    cursor: grab;
    user-select: none;
    transition: transform 0.1s ease;
  }

  #previewAnimations:active {
    cursor: grabbing;
  }

  #previewAnimations.zoomed {
    transform: scale(2);
    cursor: grab;
  }

  .animation-controls {
    padding: 8px 16px;
    background: #fff;
    flex-shrink: 0;
    border-top: 1px solid #e9ecef;
    min-height: 50px;
  }

  .animation-buttons {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  .animation-buttons::-webkit-scrollbar {
    height: 4px;
  }

  .animation-buttons::-webkit-scrollbar-track {
    background: #f1f3f4;
    border-radius: 2px;
  }

  .animation-buttons::-webkit-scrollbar-thumb {
    background: #c1c8cd;
    border-radius: 2px;
  }

  .animation-buttons button {
    flex: 0 0 auto;
    padding: 8px 16px;
    border: 1px solid #e1e5e9;
    border-radius: 20px;
    background: #f8f9fa;
    color: #495057;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
  }

  .animation-buttons button:hover {
    background: #e9ecef;
    border-color: #adb5bd;
  }

  .animation-buttons button.active {
    background: #007bff;
    border-color: #007bff;
    color: #fff;
  }

  /* Desktop view adjustments */
  @media (min-width: 1025px) {
    .character-preview-section {
      position: relative;
      height: auto;
      margin-top: 20px;
    }
    
    .preview-container {
      padding: 20px;
      min-height: 200px;
    }
    
    #previewAnimationsBox {
      max-width: 300px;
    }
    
    #previewAnimations {
      max-height: 200px;
    }
  }

  .animation-buttons {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  .animation-buttons::-webkit-scrollbar {
    height: 4px;
  }

  .animation-buttons::-webkit-scrollbar-track {
    background: #f1f3f4;
    border-radius: 2px;
  }

  .animation-buttons::-webkit-scrollbar-thumb {
    background: #c1c8cd;
    border-radius: 2px;
  }

  .animation-buttons button {
    flex: 0 0 auto;
    padding: 8px 16px;
    border: 1px solid #e1e5e9;
    border-radius: 20px;
    background: #f8f9fa;
    color: #495057;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
  }

  .animation-buttons button:hover {
    background: #e9ecef;
    border-color: #adb5bd;
  }

  .animation-buttons button.active {
    background: #007bff;
    border-color: #007bff;
    color: #fff;
  }

  /* Mobile Character Preview */
  body {
    padding-top: 60px;
    padding-bottom: 80px;
  }

  #previewAnimationsBox {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: calc(100vh - 140px);
    padding: 20px;
  }

  #previewAnimations {
    display: block;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    border: 2px solid #e1e5e9;
    border-radius: 8px;
    background: #f8f9fa;
    max-width: 100%;
    height: auto;
  }

  /* Mobile form styling */
  .side-menu-content details {
    border: 1px solid #e1e5e9;
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 16px;
  }

  .side-menu-content details summary {
    padding: 16px;
    background: #f8f9fa;
    border-bottom: 1px solid #e1e5e9;
    cursor: pointer;
    font-weight: 600;
    font-size: 16px;
    color: #2c3e50;
    user-select: none;
  }

  .side-menu-content details summary:hover {
    background: #e9ecef;
  }

  .side-menu-content details[open] summary {
    border-bottom: 1px solid #e1e5e9;
  }

  .side-menu-content details > div {
    padding: 16px;
    background: #fff;
  }

  .side-menu-content ul {
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .side-menu-content li {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px;
    border-radius: 6px;
    transition: background 0.2s ease;
    margin-bottom: 8px;
  }

  .side-menu-content li:hover {
    background: #f8f9fa;
  }

  .side-menu-content input[type="radio"],
  .side-menu-content input[type="checkbox"] {
    margin: 0;
  }

  .side-menu-content label {
    flex: 1;
    cursor: pointer;
    font-size: 14px;
    color: #495057;
  }

  .side-menu-content .condensed {
    font-weight: 600;
    color: #2c3e50;
  }

  .side-menu-content .expanded {
    font-weight: 600;
    color: #007bff;
  }
}

/* Desktop styles - show original interface */
@media (min-width: 1025px) {
  #mobile-topbar,
  #mobile-side-menu,
  #mobile-menu-overlay,
  #mobile-animation-strip {
    display: none;
  }

  #header-left,
  #controls,
  #preview-animations,
  #chooser,
  #preview {
    display: block;
  }

  body {
    padding-top: 0;
    padding-bottom: 0;
  }
  
  /* Desktop preview adjustments */
  .character-preview-section {
    position: relative;
    height: auto;
    margin-top: 20px;
  }
  
  .preview-container {
    padding: 20px;
    min-height: 250px;
  }
  
  #previewAnimationsBox {
    max-width: 400px;
  }
  
  #previewAnimations {
    max-height: 300px;
    min-height: 150px;
  }
}
