.bie-app {
  --bie-accent: #B42318;
  --bie-ink: #14110F;
  --bie-paper: #FBF8F3;
  --bie-line: #D9D2C5;
  position: relative;
  display: flex;
  flex-direction: column;
  height: clamp(560px, 90vh, 1100px);
  margin: 16px 0;
  background: var(--bie-paper);
  color: var(--bie-ink);
  border: 1px solid var(--bie-ink);
  font-family: "Noto Sans Devanagari", "Nirmala UI", Mangal, system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  box-sizing: border-box;
}
.bie-app *, .bie-app *::before, .bie-app *::after { box-sizing: border-box; }
.bie-app [hidden] { display: none !important; }
.bie-app:fullscreen { height: 100vh; margin: 0; border: 0; }
.bie-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- Top bar ---- */
.bie-top { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: flex-end; padding: 10px 12px; background: #fff; border-bottom: 1px solid var(--bie-line); }
.bie-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.bie-field { display: flex; flex-direction: column; gap: 2px; font-size: 12px; font-weight: 600; color: #5B554D; }
.bie-field input, .bie-field select, .bie-pgin {
  min-height: 40px; padding: 0 10px; border: 1px solid var(--bie-ink); background: #fff; color: var(--bie-ink);
  font: inherit; font-size: 15px; border-radius: 2px;
}
.bie-ed { max-width: 260px; }
.bie-pg { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.bie-pgin { width: 64px; text-align: center; }
.bie-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 0 12px;
  border: 1px solid var(--bie-ink); background: #fff; color: var(--bie-ink); font: inherit; font-weight: 600; font-size: 14px;
  cursor: pointer; border-radius: 2px; text-decoration: none; line-height: 1.2;
}
.bie-btn:hover { background: #F3E9DA; }
.bie-btn:focus-visible, .bie-pgin:focus-visible, .bie-field input:focus-visible, .bie-field select:focus-visible, .bie-th:focus-visible { outline: 3px solid var(--bie-accent); outline-offset: 1px; }
.bie-btn.bie-ico { min-width: 40px; padding: 0 8px; }
.bie-btn.is-primary { background: var(--bie-accent); border-color: var(--bie-accent); color: #fff; }
.bie-btn.is-primary:hover { filter: brightness(.92); }
.bie-btn.is-on { background: var(--bie-ink); border-color: var(--bie-ink); color: #fff; }
.bie-btn[disabled] { opacity: .45; cursor: default; }
.bie-zv { min-width: 46px; text-align: center; font-weight: 700; font-size: 14px; }
.bie-tools { margin-left: auto; align-items: center; }

/* ---- Body ---- */
.bie-body { flex: 1; min-height: 0; display: flex; }
.bie-rail { width: 118px; flex: none; overflow-y: auto; padding: 8px; background: #F3EFE6; border-right: 1px solid var(--bie-line); }
.bie-th { position: relative; display: block; width: 100%; margin: 0 0 8px; padding: 0; border: 2px solid transparent; background: #fff; cursor: pointer; }
.bie-th img { display: block; width: 100%; height: auto; }
.bie-th b { position: absolute; left: 0; bottom: 0; padding: 0 7px; background: rgba(20, 17, 15, .82); color: #fff; font-size: 12px; font-weight: 700; }
.bie-th.is-on { border-color: var(--bie-accent); }

.bie-stage { position: relative; flex: 1; min-width: 0; overflow: auto; padding: 16px; background: #E4DDCE; -webkit-overflow-scrolling: touch; outline: none; }
.bie-sheet { position: relative; margin: 0 auto; background: #fff; box-shadow: 0 2px 14px rgba(20, 17, 15, .28); }
.bie-img { display: block; width: 100%; height: auto; user-select: none; -webkit-user-select: none; -webkit-user-drag: none; }
.bie-load { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, .78); font-weight: 700; }
.bie-empty { margin: 40px auto; max-width: 420px; text-align: center; font-weight: 600; }

/* ---- Clip overlay ---- */
.bie-ovl { position: absolute; inset: 0; overflow: hidden; cursor: crosshair; touch-action: pan-x pan-y pinch-zoom; }
.bie-sel { position: absolute; outline: 2px solid #fff; box-shadow: 0 0 0 2px var(--bie-accent), 0 0 0 9999px rgba(20, 17, 15, .5); cursor: move; touch-action: none; }
.bie-h { position: absolute; width: 28px; height: 28px; margin: -14px 0 0 -14px; touch-action: none; }
.bie-h::after { content: ""; position: absolute; left: 8px; top: 8px; width: 12px; height: 12px; background: #fff; border: 2px solid var(--bie-accent); }
.bie-h[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.bie-h[data-h="n"] { left: 50%; top: 0; cursor: ns-resize; }
.bie-h[data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.bie-h[data-h="e"] { left: 100%; top: 50%; cursor: ew-resize; }
.bie-h[data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.bie-h[data-h="s"] { left: 50%; top: 100%; cursor: ns-resize; }
.bie-h[data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.bie-h[data-h="w"] { left: 0; top: 50%; cursor: ew-resize; }
.bie-dim { font-size: 13px; font-weight: 600; white-space: nowrap; }

.bie-clipbar {
  position: absolute; left: 50%; bottom: 16px; z-index: 5; transform: translateX(-50%);
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center;
  max-width: calc(100% - 24px); padding: 8px 10px; background: var(--bie-ink); color: #fff; border-radius: 4px; box-shadow: 0 4px 18px rgba(0, 0, 0, .35);
}
.bie-clipbar .bie-btn { border-color: #fff; }

/* ---- Result dialog ---- */
.bie-modal { position: absolute; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center; padding: 12px; background: rgba(20, 17, 15, .45); }
.bie-card { display: flex; flex-direction: column; width: 100%; max-width: 920px; max-height: 100%; background: #fff; border: 1px solid var(--bie-ink); }
.bie-card-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--bie-line); font-weight: 800; font-size: 17px; }
.bie-prevwrap { flex: 1; min-height: 0; overflow: auto; padding: 12px; background: #E4DDCE; text-align: center; }
.bie-prev { display: inline-block; max-width: 100%; height: auto; background: #fff; box-shadow: 0 2px 14px rgba(20, 17, 15, .28); }
.bie-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--bie-line); }
.bie-busy { position: absolute; inset: 0; z-index: 12; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, .8); font-weight: 700; }

.bie-toast {
  position: absolute; left: 50%; bottom: 72px; z-index: 20; max-width: calc(100% - 24px); padding: 8px 14px; transform: translateX(-50%);
  background: var(--bie-ink); color: #fff; border-radius: 4px; font-weight: 600; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.bie-toast.is-on { opacity: 1; }

/* ---- Phones ---- */
@media (max-width: 720px) {
  .bie-app { height: calc(100vh - 24px); height: calc(100dvh - 24px); min-height: 520px; }
  .bie-top { gap: 8px 10px; padding: 8px; }
  .bie-pick { width: 100%; }
  .bie-pick .bie-grow { flex: 1; }
  .bie-ed { max-width: none; width: 100%; }
  .bie-tools { margin-left: 0; }
  .bie-btn span.bie-lbl { display: none; }
  .bie-btn.is-primary span.bie-lbl, .bie-btn.is-on span.bie-lbl { display: inline; }
  .bie-body { flex-direction: column-reverse; }
  .bie-rail { display: flex; gap: 8px; width: auto; height: 88px; padding: 6px; overflow-x: auto; overflow-y: hidden; border-right: 0; border-top: 1px solid var(--bie-line); }
  .bie-th { flex: none; width: 52px; margin: 0; }
  .bie-stage { padding: 8px; }
  .bie-clipbar { bottom: 8px; }
}
