* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: "Segoe UI", system-ui, Arial, sans-serif; color: #222; background: #fff; overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }

.app { height: 100vh; display: grid; grid-template-rows: 52px 1fr 30px; grid-template-columns: 64px 1fr; }

/* top bar */
.topbar { grid-column: 1 / 3; display: flex; align-items: center; padding: 0 14px; background: #f7f8fa; border-bottom: 1px solid #d5d9de; gap: 0; }
.title { font-size: 17px; font-weight: 600; color: #1d3557; margin-right: 24px; white-space: nowrap; }
.group { display: flex; gap: 6px; align-items: center; }
.sep { width: 1px; height: 28px; background: #d5d9de; margin: 0 10px; }
.spacer { flex: 1; }
.btn { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px; border: 1px solid #c8ced4; border-radius: 6px; background: #fff; font-size: 13.5px; white-space: nowrap; }
.btn:hover { background: #f0f3f7; }
.btn:disabled { opacity: .45; cursor: default; }
.btn:disabled:hover { background: #fff; }
.btn.icon { width: 36px; padding: 0; justify-content: center; font-size: 18px; }
.btn.primary { background: #2c6bed; border-color: #2c6bed; color: #fff; }
.btn.primary:hover { background: #245bd0; }

/* tool rail */
.rail { grid-row: 2; grid-column: 1; background: #f7f8fa; border-right: 1px solid #d5d9de; display: flex; flex-direction: column; align-items: center; padding-top: 8px; gap: 4px; position: relative; overflow: visible; }
.tool { width: 46px; height: 44px; border-radius: 8px; display: flex; align-items: center; justify-content: center; border: 1px solid transparent; background: transparent; position: relative; color: #222; padding: 0; }
.tool:hover { background: #e9edf3; }
.tool.active { background: #dbe7ff; border-color: #2c6bed; color: #1a3fa0; }
.tool svg { width: 30px; height: 30px; }
.tool .key { position: absolute; right: 3px; bottom: 1px; font-size: 9px; color: #777; }
.rail-sep { width: 34px; height: 1px; background: #d5d9de; margin: 2px 0; }

.popover { position: absolute; left: 62px; top: 112px; background: #fff; border: 1px solid #b9c0c8; border-radius: 8px; box-shadow: 0 6px 18px rgba(0,0,0,.15); padding: 8px; width: 216px; z-index: 5; }
.popover .hdr { font-size: 11.5px; color: #666; margin: 2px 0 6px 2px; }
.elements { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.el { height: 34px; display: flex; align-items: center; justify-content: center; border: 1px solid #c8ced4; border-radius: 6px; background: #fff; font-family: Arial, Helvetica, sans-serif; font-size: 16px; padding: 0; }
.el:hover { background: #f0f3f7; }
.el.sel { background: #dbe7ff; border-color: #2c6bed; }
.more { width: 100%; margin-top: 6px; height: 30px; border: 1px dashed #b9c0c8; border-radius: 6px; background: #fff; font-size: 12.5px; color: #444; }
.more:hover { background: #f0f3f7; }
.popover .hint { font-size: 10.5px; color: #777; margin-top: 6px; line-height: 1.3; }

/* canvas */
.canvas { grid-row: 2; grid-column: 2; position: relative; background: #fff; overflow: hidden; }
#sheet { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; user-select: none; }
#text-editor { position: absolute; z-index: 3; padding: 1px 3px; border: 1px solid #2c6bed; border-radius: 3px; background: #fff; outline: none; box-shadow: 0 2px 8px rgba(0,0,0,.15); }

/* status bar */
.status { grid-column: 1 / 3; display: flex; align-items: center; padding: 0 14px; background: #f7f8fa; border-top: 1px solid #d5d9de; font-size: 12.5px; color: #555; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.flash { color: #1a3fa0; font-weight: 600; }

/* dialogs */
dialog { border: 1px solid #b9c0c8; border-radius: 10px; padding: 20px 24px; min-width: 340px; max-width: 92vw; box-shadow: 0 12px 40px rgba(0,0,0,.25); font-family: inherit; }
dialog::backdrop { background: rgba(20, 30, 50, .35); }
dialog h2 { margin: 0 0 14px; font-size: 17px; color: #1d3557; }
dialog .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
dialog .muted { color: #666; font-size: 12.5px; margin: 10px 0 0; }
.an-row { display: flex; justify-content: space-between; align-items: baseline; gap: 24px; padding: 8px 0; border-bottom: 1px solid #eef0f3; font-size: 14px; }
.an-row strong { font-size: 20px; }
.an-row .chem { font-family: Arial, Helvetica, sans-serif; }
.an-row sub { font-size: 70%; }
fieldset { border: 1px solid #d5d9de; border-radius: 8px; padding: 8px 14px 10px; margin: 0 0 12px; }
legend { font-size: 12.5px; color: #555; padding: 0 4px; }
fieldset label { display: block; padding: 4px 0; font-size: 14px; }
fieldset input { margin-right: 8px; }

/* periodic table */
.ptable { display: grid; grid-template-columns: repeat(18, 38px); grid-auto-rows: 40px; gap: 3px; }
.ptable .pt { position: relative; border: 1px solid #d5d9de; border-radius: 4px; background: #fafbfc; font-family: Arial, Helvetica, sans-serif; font-size: 14px; padding: 8px 0 0; }
.ptable .pt:hover { background: #dbe7ff; border-color: #2c6bed; }
.ptable .pt.organic { background: #fff; border-color: #b9c0c8; font-weight: 600; }
.ptable .pt .z { position: absolute; top: 1px; left: 3px; font-size: 8px; color: #999; font-weight: normal; }
.ptable .pt[style*="grid-row: 9"], .ptable .pt[style*="grid-row: 10"] { margin-top: 0; }
#dlg-ptable { min-width: auto; }
