:root {
  --sidebar-w: 320px;
  --drawer-h: 60dvh;
}

html, body {
  margin: 0;
  padding: 0;
  display: block;
  border: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  display: grid;
  grid-template-columns: auto var(--sidebar-w);
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  -webkit-text-size-adjust: 100%;
  transition: grid-template-columns 0.25s ease;
}
body.sidebar-collapsed {
  grid-template-columns: 100% 0;
}
body.resizing,
body.resizing * {
  transition: none !important;
  user-select: none;
}

body.light { color: #333; }
body.dark  { color: #DDD; }

#rightbar {
  padding: 8px;
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

body.light #rightbar {
  background-color: #f0f0f0;
  border-left: 1px solid lightgray;
  box-shadow: -1px 0 2px #0002;
}
body.dark #rightbar {
  background-color: #333;
  border-left: 1px solid darkgray;
  box-shadow: -1px 0 2px #FFF2;
}

#rightbar label { font-weight: bold; }
#rightbar .input-caption { font-size: 0.8em; }
#web_color_link { font-size: 0.7em; }

#rightbar .inputs {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 100%;
  overflow: hidden;
}

.canvas-container {
  min-width: 320px;
  min-height: 200px;
  position: relative;
  height: 100vh;
  height: 100dvh;
  width: 100%;
}

.canvas-container > * {
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
  display: block;
  height: 100%;
  width: 100%;
}

#board {
  touch-action: none;
}

.canvas-overlay { pointer-events: none; }

.overlay-footer {
  position: absolute;
  bottom: 0;
  padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  vertical-align: bottom;
  box-sizing: border-box;
  width: 100%;
  pointer-events: none;
}

#trig_input {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  display: block;
  font-size: 0.85em;
  resize: none;
  overflow-y: auto;
  font-family: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
}

body.dark #trig_input {
  background-color: #404040;
  color: antiquewhite;
}

body.light #trig_input {
  background-color: white;
  color: #404040;
}

.pos {
  padding: 0.25em 0.6em;
  border-radius: 1em;
  display: inline-block;
}
body.light .pos {
  background-color: floralwhite;
  box-shadow: 0 1px 2px #0003;
  color: #795548;
}
body.dark .pos {
  background-color: #404040;
  box-shadow: 0 1px 2px #fff3;
  color: antiquewhite;
}

.pos label {
  text-align: center;
  width: 100%;
  display: inline-block;
  font-size: 0.7em;
}
body.light .pos label { border-bottom: 1px solid #0002; }
body.dark  .pos label { border-bottom: 1px solid #FFF2; }
.pos .coords { font-size: 0.9em; }

.draw-scale { float: right; }

.button {
  font-size: 0.9em;
  padding: 0.5em 1em;
  min-height: 44px;
  border-radius: 0.5em;
  cursor: pointer;
  margin: 0.2em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  border: none;
}

#reset_view, #swap_theme { float: right; }

body.light .button {
  background: linear-gradient(#d8d8d8, #bfbfbf 42%, #d8d8d8 75%);
  color: #fefefe;
  text-shadow: 0 1px 2px black;
  box-shadow: inset 0 2px 2px #999;
}
body.dark .button {
  background: linear-gradient(#3e3e3e, #3a3a3a 42%, #3c3c3c 75%);
  color: #b3b3b3;
  text-shadow: 0 1px 2px black;
  box-shadow: inset 0 2px 2px #999;
  margin-top: 5px;
}
body.light .button:hover { box-shadow: inset 0 2px 2px white,  inset 0 -2.5px 2px white; }
body.dark  .button:hover { box-shadow: inset 0 2px 2px black,  inset 0 -2.5px 2px black; }

body.dark a { color: #c0dfba; }

/* ---------- Sidebar thumb tab (toggle) ---------- */
#sidebar_toggle {
  position: absolute;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  top: 50%;
  left: -28px;
  width: 28px;
  height: 72px;
  margin-top: -36px;
  border: none;
  cursor: pointer;
  border-radius: 8px 0 0 8px;
  font-size: 18px;
  font-weight: bold;
  z-index: 11;
  -webkit-tap-highlight-color: transparent;
}
body.light #sidebar_toggle {
  background: #f0f0f0;
  color: #333;
  box-shadow: -1px 0 2px #0002;
}
body.dark #sidebar_toggle {
  background: #333;
  color: #DDD;
  box-shadow: -1px 0 2px #FFF2;
}

/* Collapsed state: the rightbar shrinks to 0 width and the toggle (anchored
   at left:-28px of #rightbar) ends up just inside the viewport edge. */

/* ---------- Sidebar drag-resize handle ---------- */
#sidebar_resize {
  position: absolute;
  top: 0;
  left: 0;
  width: 6px;
  height: 100%;
  cursor: ew-resize;
  touch-action: none;
  z-index: 10;
}
#sidebar_resize::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 36px;
  margin: -18px 0 0 -1px;
  border-radius: 1px;
  background: currentColor;
  opacity: 0.25;
}
body.sidebar-collapsed #sidebar_resize {
  display: none;
}

/* ---------- Mobile / narrow viewports ---------- */
@media (max-width: 720px) {
  body {
    grid-template-columns: 100%;
    transition: none;  /* drawer is fixed-position; grid columns don't change */
  }
  body.sidebar-collapsed {
    grid-template-columns: 100%;
  }

  .canvas-container {
    min-height: 100vh;
    min-height: 100dvh;
  }

  #rightbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: var(--drawer-h);
    transition: bottom 0.25s ease;
    z-index: 10;
    border-left: none;
    border-top: 1px solid;
    border-radius: 12px 12px 0 0;
  }
  body.light #rightbar { border-top-color: lightgray; }
  body.dark  #rightbar { border-top-color: darkgray; }

  /* Slide off below the viewport without using transform — transform on the
     parent would create a containing block that traps the position:fixed
     toggle button. */
  body.sidebar-collapsed #rightbar {
    bottom: calc(-1 * var(--drawer-h) - 20px);
  }

  /* Toggle becomes a horizontal handle on top of the drawer. */
  #sidebar_toggle {
    top: -32px;
    left: 50%;
    margin-top: 0;
    transform: translateX(-50%);
    border-radius: 8px 8px 0 0;
    width: 72px;
    height: 32px;
  }
  body.sidebar-collapsed #sidebar_toggle {
    position: fixed;
    top: auto;
    bottom: max(12px, env(safe-area-inset-bottom, 0));
    left: 50%;
    right: auto;
    margin: 0;
    transform: translateX(-50%);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  }

  /* Resize handle becomes a top edge for vertical resize. */
  #sidebar_resize {
    top: 0;
    left: 0;
    right: 0;
    width: auto;
    height: 10px;
    cursor: ns-resize;
  }
  #sidebar_resize::after {
    top: 50%;
    left: 50%;
    width: 36px;
    height: 2px;
    margin: -1px 0 0 -18px;
  }

  .overlay-footer {
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    transition: bottom 0.25s ease, padding-bottom 0.25s ease;
  }

  /* When the bottom drawer is open, ride the top of it so the coord
     bubbles stay visible above the inputs panel. */
  body:not(.sidebar-collapsed) .overlay-footer {
    bottom: var(--drawer-h);
    padding-bottom: 12px;
  }

  /* Hide the zoom bubble and float the nether bubble to the right so the
     sidebar_toggle button doesn't overlap the coords. */
  .overlay-footer .draw-scale { display: none; }
  .overlay-footer .pos.nether { float: right; }
}
