:root {
  --primary: #0f6fd3;
  --primary-dark: #0b4f9a;
  --primary-soft: #eaf4ff;
  --accent: #14a46c;
  --danger: #e53935;
  --warning: #f57c00;
  --purple: #7b4dff;
  --bg: #edf3f8;
  --panel: #ffffff;
  --panel-2: #f7fbff;
  --text: #233244;
  --muted: #667789;
  --border: #d7e4f1;
  --shadow: 0 10px 28px rgba(26, 62, 100, 0.12);
  --footer-height: 34px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", sans-serif;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
a { color: inherit; text-decoration: none; }

.app-root {
  height: calc(100vh - var(--footer-height));
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.app-header {
  flex: 0 0 auto;
  height: 78px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  box-shadow: 0 3px 12px rgba(40, 80, 120, 0.06);
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-icon {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: #fff;
  background: linear-gradient(135deg, #0f6fd3, #0b4f9a);
  box-shadow: 0 6px 14px rgba(15, 111, 211, 0.25);
  font-weight: 800;
  letter-spacing: 0.02em;
}

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  color: #0961c4;
  font-size: clamp(1.45rem, 2.2vw, 2.35rem);
  line-height: 1.05;
  white-space: nowrap;
}
.brand-text p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.btn {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  color: var(--text);
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-weight: 700;
  box-shadow: 0 3px 10px rgba(26, 62, 100, 0.08);
  transition: transform .08s ease, background-color .2s ease, border-color .2s ease;
}
.btn:hover { background: #f8fbff; border-color: #b8d4ef; }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-soft { background: var(--primary-soft); color: #0b579e; border-color: #cfe6ff; }
.btn-danger { background: #fff5f5; color: var(--danger); border-color: #ffc8c8; }
.btn-danger:hover { background: #ffecec; }
.btn-small { min-height: 32px; padding: 0 10px; border-radius: 10px; font-size: 0.84rem; }
.btn-wide { width: 100%; }

.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 300px minmax(360px, 1fr) 310px;
  gap: 0;
  overflow: hidden;
}

.sidebar, .thumbs-panel {
  background: var(--panel-2);
  border-right: 1px solid var(--border);
  min-height: 0;
  overflow: auto;
  padding: 16px;
}
.thumbs-panel {
  border-right: none;
  border-left: 1px solid var(--border);
}

.tool-section {
  margin-bottom: 18px;
}
.section-title {
  font-size: 0.88rem;
  font-weight: 800;
  color: #315a82;
  margin: 0 0 10px;
}
.tool-list { display: grid; gap: 8px; }
.tool-button {
  width: 100%;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  color: var(--text);
  font-weight: 750;
  box-shadow: 0 4px 12px rgba(26, 62, 100, 0.07);
}
.tool-button:hover { background: #fafdff; border-color: #bbd6ef; }
.tool-button .icon {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
.tool-button .chev { margin-left: auto; color: #778a9c; }

.option-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px;
  box-shadow: 0 4px 12px rgba(26, 62, 100, 0.06);
}
.option-card label {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  margin-bottom: 6px;
}
.option-row { display: flex; gap: 8px; align-items: center; }
.option-row > * { min-width: 0; }
.option-row input[type="text"], .option-row select, .field {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 34px;
  padding: 6px 10px;
  background: #fff;
}
.range-value {
  font-weight: 900;
  color: var(--primary);
  font-size: 1.1rem;
}
input[type="range"] { width: 100%; accent-color: var(--primary); }
.range-marks { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.78rem; margin-top: 3px; }
.check-row { display: flex; align-items: center; gap: 8px; color: #315a82; font-weight: 750; }
.status-box {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  color: #51687f;
  font-size: 0.83rem;
  line-height: 1.55;
  white-space: pre-wrap;
}

.preview-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, #f7fbff, #eef3f8);
  overflow: hidden;
}
.preview-head {
  flex: 0 0 auto;
  min-height: 50px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.75);
}
.preview-title { font-weight: 850; color: #284c70; }
.preview-sub { color: var(--muted); font-size: 0.84rem; }
.preview-stage-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  overflow: auto;
}
.preview-stage-wrap.is-zoomed {
  align-items: flex-start;
  justify-content: flex-start;
}
.preview-stage {
  position: relative;
  flex: 0 0 auto;
  width: min(100%, 900px);
  height: min(100%, 760px);
  min-height: 360px;
  border: 1px solid #cbdceb;
  border-radius: 18px;
  background: #dbe5ef;
  box-shadow: inset 0 0 40px rgba(49, 90, 130, 0.13);
  overflow: hidden;
}
#previewCanvas, #gridCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#gridCanvas { pointer-events: none; }
.empty-preview {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #536b82;
  text-align: center;
  padding: 24px;
}
.empty-preview strong { display: block; font-size: 1.3rem; color: #27577e; margin-bottom: 8px; }
.preview-controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px;
  border-top: 1px solid var(--border);
  background: rgba(255,255,255,0.86);
}
.page-counter {
  min-width: 92px;
  text-align: center;
  font-weight: 850;
}
.zoom-select {
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: 34px;
  padding: 4px 8px;
  background: #fff;
}

.thumbs-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.thumbs-head h2 { margin: 0; font-size: 1rem; color: #244e75; }
.thumbs-head span { font-size: 0.78rem; color: var(--muted); }
.thumbs-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 13px;
  align-items: start;
}
.thumb-item {
  border: 2px solid transparent;
  border-radius: 14px;
  background: #fff;
  padding: 7px;
  box-shadow: 0 4px 14px rgba(26, 62, 100, 0.09);
  user-select: none;
}
.thumb-item[draggable="true"] { cursor: grab; }
.thumb-item.dragging { opacity: 0.48; }
.thumb-item.selected { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,111,211,0.15); }
.thumb-item.range-selected { border-color: #4f95d9; background: #eef7ff; box-shadow: inset 0 0 0 2px rgba(15,111,211,0.10); }
.thumb-item.multi-selected { border-color: #15a36d; background: #effbf6; box-shadow: inset 0 0 0 2px rgba(21,163,109,0.18); }
.thumb-canvas-wrap {
  aspect-ratio: 0.72;
  border-radius: 10px;
  border: 1px solid #d8e4ef;
  background: #edf3f8;
  overflow: hidden;
  display: grid;
  place-items: center;
}
.thumb-canvas-wrap canvas { width: 100%; height: 100%; display: block; }
.thumb-meta {
  margin-top: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.76rem;
  color: #52687d;
}
.thumb-number {
  min-width: 25px;
  height: 25px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: #eef5ff;
  color: #0f5fa8;
  font-weight: 900;
}
.thumb-item.selected .thumb-number { background: var(--primary); color: #fff; }
.thumb-item.multi-selected .thumb-number { background: #15a36d; color: #fff; }
.thumb-source {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.app-footer {
  height: var(--footer-height);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  color: #1565c0;
  background: #f4f8ff;
  border-top: 1px solid #d7e6f8;
  font-size: 0.86rem;
  flex: 0 0 auto;
}
.app-footer a { color: #1565c0; font-weight: 800; }

.ad-panel {
  position: fixed;
  left: 50%;
  bottom: calc(var(--footer-height) + 10px);
  transform: translateX(-50%);
  width: min(720px, calc(100vw - 28px));
  z-index: 20;
  border: 1px solid #d7e6f8;
  border-radius: 12px;
  background: #ffffff;
  overflow: hidden;
  box-shadow: 0 10px 26px rgba(26, 62, 100, 0.16);
}
.ad-panel[hidden] {
  display: none !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  height: 0 !important;
  overflow: hidden !important;
}
.ad-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 8px;
  background: #f4f8ff;
  border-bottom: 1px solid #d7e6f8;
}
.ad-label {
  font-size: 0.72rem;
  color: #4f6f91;
  line-height: 1;
  font-weight: 800;
}
.ad-delay-message {
  margin-left: auto;
  margin-right: 6px;
  font-size: 0.72rem;
  color: #607d9c;
}
.ad-close-btn {
  margin: 0 !important;
  padding: 3px 8px !important;
  min-height: 0;
  height: auto;
  line-height: 1.2;
  border: none;
  border-radius: 999px;
  background: #1565c0;
  color: #ffffff;
  font-size: 0.72rem !important;
  font-weight: 800;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}
.ad-close-btn:hover { background: #0d47a1; }
.ad-body { padding: 8px; background: #ffffff; min-height: 80px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 58px;
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(680px, calc(100vw - 24px));
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(20, 42, 65, 0.92);
  color: #fff;
  font-size: 0.88rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.22);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

@media (max-width: 1080px) {
  body { overflow: auto; }
  .app-root { height: auto; min-height: calc(100vh - var(--footer-height)); }
  .workspace { grid-template-columns: 1fr; overflow: visible; }
  .sidebar, .thumbs-panel { border: none; border-bottom: 1px solid var(--border); max-height: none; }
  .preview-panel { min-height: 620px; }
  .brand-text p { display: none; }
  .ad-panel { position: static; transform: none; width: auto; margin: 10px 14px; }
}

@media (max-width: 620px) {
  .app-header { height: auto; align-items: flex-start; padding: 12px; flex-direction: column; }
  .header-actions { width: 100%; }
  .header-actions .btn { flex: 1 1 0; }
  .brand-text h1 { font-size: 1.55rem; }
  .workspace { display: block; }
  .preview-panel { min-height: 520px; }
  .preview-stage-wrap { padding: 10px; }
  .preview-stage { min-height: 310px; }
  .thumbs-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .preview-controls { flex-wrap: wrap; }
}
