:root {
  color-scheme: dark;
  --bg: #171412;
  --panel: #3f2b20;
  --panel-soft: rgba(91, 59, 39, .68);
  --line: rgba(218, 160, 66, .52);
  --gold: #f4c347;
  --text: #f7ead5;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body { min-height: 100vh; color: var(--text); font-family: Arial, sans-serif; overflow: hidden; background: #566b2d; }
.castle-app { position: fixed; inset: 0; overflow: hidden; }
button, input, select { font: inherit; }

.board-shell { position: absolute; inset: 0; overflow: hidden; background: #566b2d; }
.castle-buildings-modal,
.castle-saves-modal,
.castle-settings-modal {
  --gf-modal-width: min(760px, calc(100% - 20px));
}
.castle-settings-modal { --gf-modal-width: min(460px, calc(100% - 20px)); }

.custom-deco-form {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 0;
  padding: 0;
}
.custom-deco-form[hidden],
.castle-buildings-modal-body[hidden] { display: none; }
.custom-deco-id-grid {
  display: grid;
  min-height: 0;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  align-content: start;
  gap: 8px;
  flex: 1 1 auto;
  margin: 10px;
  padding: 10px;
  overflow: auto;
  background: rgba(34, 23, 17, .22);
  border: 1px solid rgba(215, 155, 66, .3);
  border-radius: 8px;
  scrollbar-color: #98672e rgba(20, 14, 11, .38);
  scrollbar-width: thin;
}
.custom-deco-id-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  gap: 0;
  align-items: stretch;
  overflow: hidden;
  background: #e2c19c;
  border: 2px solid #a97c50;
  border-radius: 9px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .26), 0 2px 5px rgba(0, 0, 0, .14);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.custom-deco-id-row:focus-within {
  border-color: #f3c24a;
  box-shadow: 0 0 0 .2rem rgba(243, 194, 74, .25), 0 2px 5px rgba(0, 0, 0, .14);
}
.custom-deco-id-input {
  min-width: 0;
  height: 34px;
  color: #433120 !important;
  background-color: #f2d9bb !important;
  border: 0 !important;
  border-radius: 0 !important;
  font-weight: 700;
  font-size: 14px;
  text-align: center;
}
.custom-deco-id-input::placeholder { color: #8d7155; opacity: 1; }
.custom-deco-id-input:focus {
  box-shadow: none !important;
}
.custom-deco-id-input::-webkit-inner-spin-button,
.custom-deco-id-input::-webkit-outer-spin-button { margin: 0; appearance: none; }
.custom-deco-remove-row {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #6e4c31;
  background: #dfbc94;
  border: 0;
  border-left: 1px solid #a97c50;
  border-radius: 0;
  cursor: pointer;
}
.custom-deco-remove-row:hover,
.custom-deco-remove-row:focus-visible {
  color: #4b321f;
  background: #d4a978;
  outline: none;
}
.custom-deco-action,
.custom-deco-add {
  width: 100%;
  min-height: 34px;
  padding: 4px 10px;
  overflow: hidden;
  color: #433120;
  background: #f2d9bb;
  border: 2px solid #a97c50;
  border-radius: 8px;
  font-weight: 800;
  font-size: clamp(11px, 2.5vw, 14px);
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.custom-deco-apply-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 6px;
  padding: 6px 8px;
  background: linear-gradient(100deg, rgba(50, 35, 25, .96), rgba(82, 54, 35, .92));
  border-bottom: 1px solid rgba(215, 155, 66, .48);
  box-shadow: 0 5px 14px rgba(0, 0, 0, .22);
}
.custom-deco-action:hover,
.custom-deco-action:focus-visible,
.custom-deco-add:hover,
.custom-deco-add:focus-visible {
  background: #f7e4cb;
  border-color: #f3c24a;
  outline: none;
}
.custom-deco-floating {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 3;
  display: inline-flex;
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0;
  overflow: hidden;
  color: #f3c24a;
  background: rgba(96, 65, 44, .92);
  border: 1px solid rgba(243, 194, 74, .78);
  border-radius: 8px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08), 0 3px 10px rgba(0, 0, 0, .38);
  font-weight: 800;
  font-size: 23px;
  line-height: 1;
  cursor: pointer;
}
.custom-deco-floating i { -webkit-text-stroke: .35px currentColor; }
.custom-deco-floating:hover,
.custom-deco-floating:focus-visible {
  background: rgba(113, 76, 49, .96);
  border-color: rgba(243, 194, 74, .92);
  outline: none;
}
.custom-deco-floating[hidden] { display: none; }
.custom-deco-id-grid::-webkit-scrollbar { width: 7px; height: 7px; }
.custom-deco-id-grid::-webkit-scrollbar-track { background: rgba(20, 14, 11, .38); }
.custom-deco-id-grid::-webkit-scrollbar-thumb { background: #98672e; border-radius: 4px; }
.custom-deco-id-grid::-webkit-scrollbar-thumb:hover { background: #c18b3c; }

.castle-buildings-modal-card,
.castle-saves-modal-card {
  display: flex;
  height: min(680px, calc(100vh - 48px));
  height: min(680px, calc(100dvh - 48px));
  min-height: 0;
  max-height: none;
  flex-direction: column;
}

.castle-buildings-modal-body {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
}
.building-filter-bar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: 34px;
  align-items: start;
  gap: 6px;
  padding: 6px 8px;
  background: linear-gradient(100deg, rgba(50, 35, 25, .96), rgba(82, 54, 35, .92));
  border-bottom: 1px solid rgba(215, 155, 66, .48);
  box-shadow: 0 5px 14px rgba(0, 0, 0, .22);
}
.building-filter-bar .custom-input,
.building-filter-bar .custom-select,
.building-filter-bar .btn-custom,
.layout-save-bar .slot-name {
  min-width: 0;
  width: 100%;
  height: 34px;
  min-height: 34px;
  padding-block: 4px;
  color: #433120 !important;
  background-color: #f2d9bb !important;
  border: 2px solid #a97c50 !important;
  border-radius: 8px !important;
  font-weight: 700;
  font-size: 14px;
  outline: none;
}
.building-filter-bar .custom-input,
.layout-save-bar .slot-name { padding-inline: 12px; }
.building-filter-bar .custom-input::placeholder,
.layout-save-bar .slot-name::placeholder { color: #8d7155; opacity: 1; }
.building-filter-bar .custom-input:focus,
.building-filter-bar .btn-custom:focus,
.layout-save-bar .slot-name:focus {
  border-color: #f3c24a !important;
  box-shadow: 0 0 0 .2rem rgba(243, 194, 74, .25) !important;
}
.building-filter-bar .custom-select {
  padding-inline: 12px 34px;
  border-radius: 8px !important;
  text-align: center;
  text-align-last: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.building-filter-bar .custom-select option {
  color: #433120;
  background-color: #f2d9bb;
  font-weight: 700;
}
.building-filter-bar .custom-select:focus {
  border-color: #f3c24a !important;
  box-shadow: 0 0 0 .2rem rgba(243, 194, 74, .5) !important;
}
.size-filter-dropdown {
  position: relative;
  align-self: start;
  width: 100%;
  min-width: 0;
  height: 34px;
}
.size-filter-dropdown .btn-custom {
  display: flex;
  width: 100% !important;
  height: 34px;
  align-items: center;
  justify-content: center;
}
.size-filter-dropdown .dropdown-toggle::after { display: none; }
.size-filter-dropdown .dropdown-menu-custom {
  display: none;
  position: absolute !important;
  top: 100%;
  right: 0;
  left: 0;
  z-index: 1060;
  max-height: 350px;
  overflow-y: auto;
  margin: 0 !important;
  padding: 5px;
  background-color: #f3e0c2;
  border: 2px solid #a97c50;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
  scrollbar-color: #9a6a45 #332318;
}
.size-filter-dropdown .dropdown-menu-custom::-webkit-scrollbar { width: 10px; height: 10px; }
.size-filter-dropdown .dropdown-menu-custom::-webkit-scrollbar-track {
  background: #332318;
  border-left: 1px solid rgba(215, 155, 66, .16);
}
.size-filter-dropdown .dropdown-menu-custom::-webkit-scrollbar-thumb {
  background-color: #9a6a45;
  border: 2px solid #332318;
  border-radius: 999px;
}
.size-filter-dropdown .dropdown-menu-custom::-webkit-scrollbar-thumb:hover { background-color: #b47b4f; }
.size-filter-dropdown .dropdown-menu-custom::-webkit-scrollbar-thumb:active { background-color: #c28a56; }
.size-filter-dropdown .dropdown-menu-custom.show:not([hidden]) { display: block; }
.size-filter-dropdown .dropdown-menu-custom li { list-style: none; }
.size-filter-dropdown .form-check {
  margin: 0;
  color: #433120;
  cursor: pointer;
}
.size-filter-dropdown .form-check:hover { background-color: rgba(243, 194, 74, .25); }
.size-filter-dropdown .form-check-input {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin-top: 3px;
  background-color: #fff;
  border: 2px solid #a97c50;
  border-radius: 5px;
  box-shadow: none;
}
.size-filter-dropdown .form-check-input:checked {
  background-color: #f3c24a !important;
  border-color: #f3c24a !important;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m3 8 3 3 7-7'/%3e%3c/svg%3e") !important;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 12px 12px;
}
.size-filter-dropdown .form-check-label { color: #433120; font-weight: 700; cursor: pointer; }
.building-list { display: grid; min-height: 0; grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); align-content: start; gap: 8px; flex: 1; overflow: auto; padding: 10px 10px 62px; scrollbar-color: #98672e rgba(20, 14, 11, .38); scrollbar-width: thin; }
.building-entry {
  display: grid;
  width: 100%;
  min-width: 0;
  aspect-ratio: 1;
  grid-template-areas: "label" "image";
  grid-template-rows: auto minmax(0, 1fr);
  gap: 4px;
  place-items: center;
  padding: 8px;
  border: 1px solid rgba(218, 160, 66, .32);
  border-radius: 7px;
  color: var(--text);
  background: rgba(70, 45, 31, .38);
  text-align: center;
  cursor: grab;
}
.building-entry:hover, .building-entry:focus-visible { background: rgba(218,160,66,.12); border-color: rgba(218, 160, 66, .62); outline: none; }
.building-entry img { grid-area: image; width: 100%; height: 100%; min-height: 0; object-fit: contain; }
.building-entry img.is-default-flipped { transform: scaleX(-1); }
.building-entry-label { grid-area: label; min-width: 0; line-height: 1.2; }
.building-entry-name { color: var(--text); font-size: 13px; font-weight: 700; }
.building-entry-meta { color: #d8bb93; font-size: 11px; font-weight: 600; white-space: nowrap; }
.building-list::-webkit-scrollbar { width: 7px; }
.building-list::-webkit-scrollbar-track { background: rgba(20, 14, 11, .38); }
.building-list::-webkit-scrollbar-thumb { background: #98672e; border-radius: 4px; }
.building-list::-webkit-scrollbar-thumb:hover { background: #c18b3c; }

.castle-saves-modal-body {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  overflow: hidden;
}
.layout-save-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  padding: 6px 8px;
  background: linear-gradient(100deg, rgba(50, 35, 25, .96), rgba(82, 54, 35, .92));
  border-bottom: 1px solid rgba(215, 155, 66, .48);
  box-shadow: 0 5px 14px rgba(0, 0, 0, .22);
}
.save-button {
  min-width: 76px;
  min-height: 34px;
  padding: 4px 10px;
  color: #433120;
  background-color: #f2d9bb;
  border: 2px solid #a97c50;
  border-radius: 8px;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
}
.save-button:hover,
.save-button:focus-visible {
  color: #433120;
  background-color: #f7e4cb;
  border-color: #f3c24a;
  outline: none;
  box-shadow: 0 0 0 .2rem rgba(243, 194, 74, .25);
}
.saved-layouts-head,
.saved-layout-row {
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) repeat(3, minmax(66px, .7fr));
  align-items: center;
}
.saved-layouts-head {
  padding: 8px 10px;
  color: #e8c88f;
  background: rgba(50, 35, 25, .78);
  border-bottom: 1px solid rgba(215, 155, 66, .32);
  font-weight: 800;
  text-align: center;
}
.saved-layouts-head span:first-child { text-align: left; }
.saved-layouts-list {
  min-height: 0;
  max-height: none;
  flex: 1 1 auto;
  overflow: auto;
  background: rgba(34, 23, 17, .16);
  scrollbar-color: #98672e rgba(20, 14, 11, .38);
}
.saved-layout-row {
  min-height: 52px;
  padding: 7px 10px;
  color: #f7ead5;
  background: rgba(70, 45, 31, .38);
  border-bottom: 1px solid rgba(218, 160, 66, .22);
  font-weight: 800;
}
.saved-layout-row:hover { background: rgba(218, 160, 66, .08); }
.saved-layout-row.is-active {
  background: rgba(218, 160, 66, .14);
  box-shadow: inset 3px 0 #f3c24a;
}
.saved-layout-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved-layout-cell { display: flex; justify-content: center; }
.saved-layout-action {
  padding: 2px 5px;
  color: #e0b044;
  background: none;
  border: 0;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}
.saved-layout-action:hover { color: #f3c24a; }
.saved-layout-active {
  width: 18px;
  height: 18px;
  accent-color: #f3c24a;
  cursor: pointer;
}
.saved-layout-empty { padding: 24px 12px; color: #d8bb93; text-align: center; font-weight: 700; }
.saved-layouts-list::-webkit-scrollbar { width: 7px; height: 7px; }
.saved-layouts-list::-webkit-scrollbar-track { background: rgba(20, 14, 11, .38); }
.saved-layouts-list::-webkit-scrollbar-thumb { background: #98672e; border-radius: 4px; }
.saved-layouts-list::-webkit-scrollbar-thumb:hover { background: #c18b3c; }
#castleCanvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#castleCanvas.is-dragging { cursor: grabbing; }
.bottom-buttons-container {
  width: min(460px, calc(100% - 24px));
  height: 72px;
  padding: 7px;
  background: rgba(77, 55, 39, .96);
  border: 1px solid rgba(243, 194, 74, .38);
  display: flex;
  gap: 6px;
  position: fixed;
  bottom: 8px !important;
  z-index: 1000;
  left: 50% !important;
  top: auto !important;
  transform: translateX(-50%) !important;
  border-radius: 20px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .42), inset 0 1px rgba(255, 255, 255, .08);
  backdrop-filter: blur(10px);
  overflow: hidden;
}
.castle-settings-modal-body {
  display: grid;
  gap: 10px;
  padding: 12px;
}
.castle-setting-row {
  display: grid;
  gap: 2px;
  color: #f7ead5;
  font-size: 13px;
  font-weight: 800;
}
.castle-setting-row .custom-select {
  min-width: 0;
  width: 100%;
  height: 34px;
  min-height: 34px;
  padding: 4px 34px 4px 12px;
  color: #433120 !important;
  background-color: #f2d9bb !important;
  border: 2px solid #a97c50 !important;
  border-radius: 8px !important;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  text-align-last: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
  outline: none;
}
.castle-setting-row .custom-select option {
  color: #433120;
  background-color: #f2d9bb;
  font-weight: 700;
}
.castle-setting-row .custom-select:focus {
  border-color: #f3c24a !important;
  box-shadow: 0 0 0 .2rem rgba(243, 194, 74, .35) !important;
}

.bottom-buttons-container .btn {
  display: flex;
  flex: 1;
  min-width: 0;
  height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0;
  color: #ead0b2;
  background: rgba(96, 65, 44, .72);
  border: 1px solid rgba(243, 194, 74, .1);
  border-radius: 13px;
  font-weight: 700;
  transition: color .16s ease, background-color .16s ease, border-color .16s ease, transform .12s ease;
}

.bottom-buttons-container .btn:focus-visible,
.bottom-buttons-container .btn.active {
  color: #f3c24a;
  background: rgba(243, 194, 74, .14);
  border-color: rgba(243, 194, 74, .28);
  outline: none;
}

.bottom-buttons-container .btn:active {
  background: rgba(243, 194, 74, .22);
  transform: scale(.97);
}

.bottom-buttons-container .btn i {
  font-size: 26px !important;
  line-height: 1;
}

.nav-action-label {
  display: block;
  max-width: 100%;
  overflow: hidden;
  padding-inline: 3px;
  font-size: 12px;
  line-height: 1;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-action-label[hidden] { display: none; }

@media (hover: hover) and (pointer: fine) {
  .bottom-buttons-container .btn:hover {
    color: #f3c24a;
    background: rgba(243, 194, 74, .11);
    border-color: rgba(243, 194, 74, .22);
  }
}

.vr {
  display: none;
}

@media (max-width: 760px) {
  body { overflow: hidden; }
  .custom-deco-form {
    padding: 0;
  }
  .custom-deco-id-grid {
    grid-template-columns: repeat(auto-fill, minmax(125px, 1fr));
    gap: 6px;
    margin: 8px;
    padding: 8px;
  }
  .custom-deco-id-row { grid-template-columns: minmax(0, 1fr) 36px; }
  .castle-buildings-modal-card,
  .castle-saves-modal-card {
    height: min(540px, calc(100vh - 96px));
    height: min(540px, calc(100dvh - 96px));
  }
  .building-list { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); }
  .saved-layouts-head { display: none; }
  .saved-layout-row {
    grid-template-columns: minmax(0, 1fr) repeat(3, auto);
    gap: 4px;
    padding-inline: 8px;
  }
  .saved-layout-action { font-size: 11px; }
}

@media (max-width: 980px) {
  .bottom-buttons-container {
    width: min(440px, calc(100% - 20px));
    height: 66px;
    justify-content: center;
    gap: 6px;
    padding: 6px;
    position: fixed;
    bottom: max(8px, env(safe-area-inset-bottom)) !important;
    left: 50% !important;
    right: auto;
    z-index: 1000;
    background: rgba(77, 55, 39, .96);
    border: 1px solid rgba(243, 194, 74, .38);
    border-radius: 18px;
    box-shadow: 0 8px 28px rgba(0, 0, 0, .42), inset 0 1px rgba(255, 255, 255, .08);
    backdrop-filter: blur(10px);
    transform: translateX(-50%) !important;
  }

  .bottom-buttons-container .btn {
    flex: 1;
    display: flex;
    min-width: 0;
    height: 100%;
    align-items: center;
    justify-content: center;
    color: #ead0b2;
    background: rgba(96, 65, 44, .72);
    border: 1px solid rgba(243, 194, 74, .1);
    border-radius: 12px;
    text-align: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: color .16s ease, background-color .16s ease, border-color .16s ease, transform .12s ease;
  }

  .bottom-buttons-container .btn:focus-visible,
  .bottom-buttons-container .btn.active {
    color: #f3c24a;
    background: rgba(243, 194, 74, .14);
    border-color: rgba(243, 194, 74, .28);
    outline: none;
  }

  .bottom-buttons-container .btn:active {
    background: rgba(243, 194, 74, .22);
    transform: scale(.96);
  }

  .bottom-buttons-container .btn i {
    font-size: 30px !important;
  }

  .bottom-buttons-container .vr { display: none; }
}

@media (max-width: 980px) and (hover: hover) and (pointer: fine) {
  .bottom-buttons-container .btn:hover {
    color: #f3c24a;
    background: rgba(243, 194, 74, .14);
    border-color: rgba(243, 194, 74, .28);
  }
}

@media (max-width: 980px) and (hover: none) {
  .bottom-buttons-container .btn:hover:not(.active) {
    color: #ead0b2;
    background: rgba(96, 65, 44, .72);
    border-color: rgba(243, 194, 74, .1);
  }
}

@media (max-width: 760px) {
  .bottom-buttons-container {
    width: calc(100% - 16px);
    height: 74px;
    padding: 7px;
    border-radius: 20px;
  }

  .bottom-buttons-container .btn { border-radius: 14px; }
  .bottom-buttons-container .btn { gap: 3px; }
  .bottom-buttons-container .btn i { font-size: 24px !important; }
  .bottom-buttons-container .nav-action-label { font-size: clamp(7px, 2.2vw, 10px); }
}
