:root {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #111;
  color: #f5f5f5;
  color-scheme: dark;
  --topbar-h: 58px;
}
* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-height: 100%; background: #111; touch-action: manipulation; }
body { min-height: 100vh; min-height: 100dvh; overflow: hidden; }
button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

.topbar {
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid #2b2b2b;
  background: #171717;
  position: relative;
  z-index: 40;
}
.brand-wrap { min-width: 0; display: flex; align-items: center; gap: 7px; }
.brand { font-size: 16px; font-weight: 760; white-space: nowrap; letter-spacing: -.2px; }
.mode-badge {
  border: 1px solid #3b3b3b;
  border-radius: 999px;
  color: #bcbcbc;
  font-size: 10px;
  padding: 3px 7px;
  white-space: nowrap;
}
.actions { display: flex; align-items: center; gap: 5px; flex-wrap: wrap; justify-content: flex-end; }
.button, .icon-button {
  appearance: none;
  border: 1px solid #444;
  background: #262626;
  color: #fff;
  cursor: pointer;
}
.button { border-radius: 8px; padding: 8px 10px; line-height: 1; white-space: nowrap; }
.button.primary { background: #f1f1f1; color: #111; border-color: #f1f1f1; }
.button.ghost { background: transparent; min-width: 38px; }
.button:disabled { opacity: .38; cursor: default; }
.icon-button { width: 34px; height: 34px; border-radius: 8px; font-size: 20px; line-height: 1; }

.app {
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
  min-height: 0;
}
.viewer-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(45deg, #222 25%, transparent 25%),
    linear-gradient(-45deg, #222 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #222 75%),
    linear-gradient(-45deg, transparent 75%, #222 75%), #2b2b2b;
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}
.canvas-host {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  overflow: auto; padding: 10px;
  -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
}
.canvas-host canvas {
  display: block;
  max-width: 100%; max-height: 100%;
  width: auto; height: auto;
  box-shadow: 0 3px 16px rgba(0,0,0,.34);
}
.empty-state { text-align: center; padding: 24px; color: #ddd; }
.empty-title { font-size: 18px; font-weight: 720; }
.empty-sub { margin-top: 8px; font-size: 12px; color: #9d9d9d; }
.viewer-notice {
  position: absolute; left: 10px; right: 10px; bottom: 10px;
  margin: auto; max-width: 720px;
  padding: 8px 10px;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 8px;
  background: rgba(20,20,20,.86);
  color: #d6d6d6;
  font-size: 11px;
  line-height: 1.35;
  pointer-events: none;
}
.layers-panel {
  min-width: 0; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column;
  border-left: 1px solid #2b2b2b;
  background: #171717;
}
.layers-head {
  padding: 10px 11px;
  border-bottom: 1px solid #2b2b2b;
  display: flex; justify-content: space-between; gap: 8px;
}
.layers-head-main { min-width: 0; }
.layers-head-side { display: flex; align-items: flex-start; gap: 6px; }
.layers-title { font-weight: 700; }
.file-info, .status { margin-top: 3px; font-size: 10px; color: #999; line-height: 1.35; }
.file-info { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 205px; }
.status { text-align: right; max-width: 120px; }

.layer-bulk-bar {
  display: flex; gap: 6px; padding: 6px 10px;
  border-bottom: 1px solid #2b2b2b;
  background: #151515;
}
.bulk-button {
  appearance: none; border: 1px solid #303030; background: #1d1d1d; color: #bdbdbd;
  min-height: 30px; padding: 5px 9px; border-radius: 7px; font-size: 10px; cursor: pointer;
}
.bulk-button:active { background: #292929; }
.bulk-button:disabled { opacity: .35; }

.layers-list { overflow: auto; min-height: 0; -webkit-overflow-scrolling: touch; }
.layers-placeholder { padding: 16px 12px; font-size: 12px; color: #888; }
.layer-row {
  width: 100%; min-height: 43px;
  display: flex; align-items: center; gap: 5px;
  border: 0; border-bottom: 1px solid #252525;
  background: transparent; color: #eee; text-align: left;
}
.layer-row:active { background: #232323; }
.eye-button, .detail-button, .move-button {
  appearance: none; border: 0; background: transparent; color: inherit;
  min-width: 34px; height: 38px; padding: 0 7px; cursor: pointer;
}
.eye-button { font-size: 16px; }
.eye-button.is-inherited-hidden { color: #666; }
.layer-row.is-parent-hidden .layer-kind { color: #5f5f5f; }
.layer-main {
  flex: 1; min-width: 0; height: 42px;
  display: flex; align-items: center; gap: 5px;
  border: 0; background: transparent; color: inherit; text-align: left; padding: 0 4px;
}
.layer-name { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 12px; }
.layer-kind { flex: 0 0 auto; color: #777; font-size: 9px; }
.layer-row.is-hidden .layer-name { color: #6e6e6e; }
.detail-button { color: #9d9d9d; font-size: 15px; }
.move-controls { display: flex; flex: 0 0 auto; }
.move-button { min-width: 29px; width: 29px; color: #9c9c9c; font-size: 13px; }
.move-button:disabled { opacity: .22; }

.close-fullscreen {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 10002;
  border: 1px solid #555; border-radius: 999px;
  background: rgba(20,20,20,.82); color: white; padding: 9px 13px;
}
.viewer-panel.is-pseudo-fullscreen {
  position: fixed; inset: 0; z-index: 10000;
  width: 100vw; height: 100vh; height: 100dvh;
}
.viewer-panel.is-pseudo-fullscreen .canvas-host {
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

.dialog-backdrop {
  position: fixed; inset: 0; z-index: 20000;
  display: flex; align-items: center; justify-content: center;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: rgba(0,0,0,.72);
}
.dialog {
  width: min(620px, 100%);
  max-height: 88vh; max-height: 88dvh;
  overflow: auto;
  border: 1px solid #383838;
  border-radius: 14px;
  background: #181818;
  box-shadow: 0 18px 60px rgba(0,0,0,.45);
}
.mode-dialog { padding: 18px; }
.dialog-title { font-size: 17px; font-weight: 760; }
.dialog-file { margin-top: 5px; color: #999; font-size: 11px; overflow-wrap: anywhere; }
.mode-grid { display: grid; grid-template-columns: 1fr; gap: 8px; margin-top: 14px; }
.mode-card {
  appearance: none;
  display: flex; flex-direction: column; align-items: flex-start; gap: 5px;
  width: 100%; padding: 13px;
  border: 1px solid #393939; border-radius: 10px;
  background: #202020; color: #eee; text-align: left; cursor: pointer;
}
.mode-card:active { background: #292929; }
.mode-card.recommended { border-color: #666; }
.mode-name { font-size: 14px; font-weight: 730; }
.mode-desc { font-size: 11px; color: #aaa; line-height: 1.45; }
.dialog-note { margin-top: 11px; font-size: 10px; color: #858585; line-height: 1.45; }
.dialog-cancel { margin-top: 13px; }
.detail-dialog { width: min(680px, 100%); }
.detail-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 13px; border-bottom: 1px solid #2e2e2e; }
.detail-body { padding: 12px 13px; font-size: 12px; }
.detail-grid { display: grid; grid-template-columns: minmax(110px, .6fr) minmax(0, 1.4fr); gap: 7px 10px; }
.detail-key { color: #888; }
.detail-value { min-width: 0; overflow-wrap: anywhere; }
.detail-section { margin-top: 16px; padding-top: 12px; border-top: 1px solid #2d2d2d; }
.detail-section-title { font-weight: 700; margin-bottom: 8px; }
.detail-json {
  margin: 0; padding: 9px; max-height: 220px; overflow: auto;
  border-radius: 8px; background: #111; color: #bbb;
  font: 10px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.linked-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 8px; }
.small-button { padding: 7px 9px; font-size: 11px; }
.embedded-dialog { width: min(900px, 100%); height: min(82vh, 760px); display: grid; grid-template-rows: auto auto minmax(180px, 1fr) minmax(90px, 180px); overflow: hidden; }
.embedded-status { padding: 7px 12px; color: #999; font-size: 10px; border-bottom: 1px solid #2b2b2b; }
.embedded-canvas-host {
  min-height: 0; overflow: auto; display: flex; align-items: center; justify-content: center; padding: 8px;
  background: #252525;
}
.embedded-canvas-host canvas, .embedded-canvas-host img { max-width: 100%; max-height: 100%; object-fit: contain; }
.embedded-object { width: 100%; height: 100%; min-height: 280px; border: 0; background: #fff; }
.mask-preview { display: flex; justify-content: flex-start; margin-bottom: 8px; overflow: auto; }
.mask-preview canvas { max-width: min(240px, 100%); max-height: 180px; width: auto; height: auto; border: 1px solid #333; background: #222; }
.linked-file-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 0; border-bottom: 1px solid #262626; }
.linked-file-label { min-width: 0; overflow-wrap: anywhere; color: #bbb; font-size: 11px; }
.embedded-layers { min-height: 0; overflow: auto; border-top: 1px solid #2b2b2b; padding: 7px 10px; font-size: 10px; color: #aaa; }
.embedded-layer { padding: 4px 0; border-bottom: 1px solid #242424; }
.mobile-only { display: none; }

@media (max-width: 760px) {
  :root { --topbar-h: 56px; }
  .brand { font-size: 14px; }
  .mode-badge { display: none; }
  .button { padding: 8px 8px; font-size: 12px; }
  .mobile-only { display: inline-flex; align-items: center; justify-content: center; }
  .app { grid-template-columns: 1fr; }
  .layers-panel {
    position: fixed;
    z-index: 500;
    left: 0; right: 0; bottom: 0;
    height: min(62vh, 520px); height: min(62dvh, 520px);
    border-left: 0; border-top: 1px solid #3a3a3a;
    border-radius: 14px 14px 0 0;
    transform: translateY(102%);
    transition: transform .14s ease-out;
    box-shadow: 0 -10px 30px rgba(0,0,0,.38);
  }
  .layers-panel.is-open { transform: translateY(0); }
  .file-info { max-width: 240px; }
  .viewer-notice { bottom: 8px; font-size: 10px; }
  .mode-dialog { padding: 14px; }
  .embedded-dialog { height: 88dvh; grid-template-rows: auto auto minmax(160px, 1fr) minmax(90px, 160px); }
}
@media (max-width: 430px) {
  .topbar { gap: 5px; }
  .brand-wrap { flex: 0 0 auto; }
  .actions { gap: 4px; }
  #modeButton { display: none !important; }
  .button { font-size: 11px; padding: 8px 7px; }
  .brand { font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) {
  .layers-panel { transition: none; }
}

/* Layer previews: omitted entirely in Minimum mode. In Light / Show all,
   only rows near the visible scroll area are decoded and cached. */
.layer-thumb {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px solid #343434;
  border-radius: 5px;
  background:
    linear-gradient(45deg, #252525 25%, transparent 25%),
    linear-gradient(-45deg, #252525 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #252525 75%),
    linear-gradient(-45deg, transparent 75%, #252525 75%), #1d1d1d;
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0;
  color: #666;
  font-size: 11px;
}
.layer-thumb canvas { display: block; width: 100%; height: 100%; object-fit: contain; }
.layer-thumb.is-group { background: #1c1c1c; color: #777; font-size: 15px; }
.layer-thumb.is-unavailable { background: #191919; color: #555; }
.layer-row.is-hidden .layer-thumb { opacity: .42; }
@media (max-width: 430px) {
  .layer-thumb { flex-basis: 34px; width: 34px; height: 34px; }
}
