:root {
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #ef4444;
  --success: #10b981;
  --shadow: 0 18px 45px rgba(15, 23, 42, .08);
  --shadow-sm: 0 8px 22px rgba(15, 23, 42, .06);
  --radius: 18px;
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-soft: #111827;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #334155;
  --primary: #60a5fa;
  --primary-dark: #3b82f6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  font-family: "Noto Sans JP", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top left, rgba(37,99,235,.12), transparent 30%), var(--bg);
}

button, input, select {
  font-family: inherit;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: 72px 1fr;
}

.header {
  height: 72px;
  padding: 0 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255,255,255,.78);
  backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--border);
}

[data-theme="dark"] .header {
  background: rgba(30,41,59,.78);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon,
.login-logo {
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: white;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-sm);
}

.brand-icon {
  width: 42px;
  height: 42px;
  border-radius: 14px;
}

.brand-title {
  font-size: 22px;
  font-weight: 900;
}

.brand-sub {
  font-size: 12px;
  color: var(--muted);
}

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

.user-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 12px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.user-pill .material-icons {
  font-size: 18px;
}

.role-badge {
  padding: 3px 8px;
  background: rgba(37,99,235,.1);
  color: var(--primary);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 900;
}

.main {
  min-height: 0;
  display: grid;
  grid-template-columns: 310px minmax(420px, 1fr) 330px;
  gap: 18px;
  padding: 18px;
}

.panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  font-weight: 900;
}

.panel-body {
  padding: 16px;
  overflow: auto;
  flex: 1;
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.toolbar-separator {
  width: 1px;
  height: 28px;
  background: var(--border);
}

.zoom-group {
  flex: 1 1 220px;
}

#zoomSlider {
  width: min(220px, 100%);
  flex: 1;
}

.zoom-icon {
  color: var(--muted);
}

.zoom-label {
  min-width: 48px;
  text-align: right;
  font-size: 12px;
  font-weight: 900;
  color: var(--muted);
}

.page-label {
  min-width: 70px;
  text-align: center;
  font-size: 13px;
  font-weight: 900;
  color: var(--muted);
}

.canvas-area {
  position: relative;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background-color: var(--surface-soft);
  background-image: radial-gradient(var(--border) 1px, transparent 1px);
  background-size: 22px 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-empty {
  position: absolute;
  pointer-events: none;
  display: grid;
  gap: 8px;
  justify-items: center;
  color: var(--muted);
  text-align: center;
}

.canvas-empty .material-icons {
  font-size: 64px;
  opacity: .55;
}

.canvas-empty strong {
  font-size: 15px;
}

.canvas-empty small {
  font-size: 13px;
}

.canvas-container {
  box-shadow: var(--shadow);
}

.floating-delete {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 20;
  display: none;
}

.drop-zone {
  border: 1.5px dashed var(--border);
  border-radius: 16px;
  background: var(--surface-soft);
  padding: 24px 14px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: .18s;
  display: grid;
  gap: 6px;
  justify-items: center;
}

.drop-zone:hover,
.drop-zone.dragover {
  border-color: var(--primary);
  color: var(--primary);
  background: rgba(37,99,235,.06);
}

.drop-zone .material-icons {
  font-size: 40px;
}

.drop-zone small {
  font-size: 12px;
}

.file-list {
  margin-top: 14px;
  display: grid;
  gap: 8px;
}

.file-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface);
  cursor: pointer;
  font-size: 13px;
  transition: .18s;
}

.file-item:hover {
  border-color: var(--primary);
}

.file-item.active {
  border-color: var(--primary);
  background: rgba(37,99,235,.08);
  color: var(--primary);
  font-weight: 900;
}

.file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn,
.icon-btn {
  cursor: pointer;
  transition: .18s;
}

.btn {
  border: 1px solid transparent;
  border-radius: 13px;
  padding: 10px 14px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  white-space: nowrap;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}

.btn:disabled {
  opacity: .55;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-outline {
  border-color: var(--border);
  background: transparent;
}

.btn-dark {
  background: var(--text);
  color: var(--surface);
}

.btn-danger {
  background: var(--danger);
  color: white;
}

.btn-wide {
  width: 100%;
  margin-bottom: 10px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: transparent;
  color: var(--muted);
}

.icon-btn:hover {
  background: var(--surface-soft);
  color: var(--text);
}

.section-title {
  margin: 18px 0 10px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 900;
}

.template-list {
  display: grid;
  gap: 10px;
}

.template-card {
  position: relative;
  overflow: hidden;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 14px;
  cursor: pointer;
  transition: .18s;
}

.template-card:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.template-card img {
  display: block;
  width: 100%;
  height: auto;
}

.template-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted);
}

.template-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.template-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: rgba(239,68,68,.95);
  color: white;
  cursor: pointer;
  opacity: 0;
  transition: .18s;
}

.template-card:hover .template-delete {
  opacity: 1;
}

.empty {
  padding: 14px;
  border: 1px dashed var(--border);
  border-radius: 14px;
  color: var(--muted);
  background: var(--surface-soft);
  text-align: center;
  font-size: 13px;
}

.admin-box {
  display: none;
  margin-top: 18px;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid rgba(37,99,235,.18);
  background: rgba(37,99,235,.07);
}

.admin-box.show {
  display: block;
}

.admin-box strong {
  display: block;
  margin-bottom: 8px;
  color: var(--primary);
  font-size: 12px;
}

#allUserTemplateSection {
  display: none;
}

.login-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  place-items: center;
  background: rgba(15,23,42,.65);
  backdrop-filter: blur(12px);
  padding: 24px;
}

.login-modal.show {
  display: grid;
}

.login-card {
  width: min(460px, 100%);
  padding: 30px;
  background: var(--surface);
  border-radius: 24px;
  border: 1px solid var(--border);
  box-shadow: 0 28px 80px rgba(0,0,0,.22);
  text-align: center;
}

.login-logo {
  width: 60px;
  height: 60px;
  border-radius: 20px;
  margin: 0 auto 14px;
}

.login-logo .material-icons {
  font-size: 34px;
}

.login-card h1 {
  margin: 0;
  font-size: 28px;
}

.login-card p {
  margin: 8px 0 20px;
  color: var(--muted);
  font-size: 14px;
}

.input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  font-size: 15px;
  outline: none;
}

.input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(37,99,235,.12);
}

.loading {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: center;
  background: var(--bg);
  color: var(--muted);
  font-weight: 900;
}

.loading.hidden {
  display: none;
}

.spinner {
  width: 46px;
  height: 46px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 999px;
  animation: spin .8s linear infinite;
  margin: 0 auto 14px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.toast-container {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 3000;
  display: grid;
  gap: 10px;
}

.toast {
  min-width: 260px;
  padding: 12px 14px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 5px solid var(--primary);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
}

.toast.success {
  border-left-color: var(--success);
}

.toast.error {
  border-left-color: var(--danger);
}

@media (max-width: 1200px) {
  .main {
    grid-template-columns: 280px minmax(360px, 1fr) 300px;
  }

  .fit-text {
    display: none;
  }
}

@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  .app {
    height: auto;
    min-height: 100vh;
  }

  .main {
    grid-template-columns: 1fr;
  }

  .left-panel,
  .right-panel {
    min-height: 360px;
  }

  .canvas-area {
    height: 70vh;
  }

  .toolbar {
    align-items: stretch;
  }

  .toolbar-group,
  .btn-fit {
    flex: 1 1 auto;
  }

  .zoom-group {
    flex-basis: 100%;
  }

  .header {
    height: auto;
    padding: 12px 16px;
    gap: 12px;
    flex-wrap: wrap;
  }

  .header-actions {
    flex-wrap: wrap;
  }
}